~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Andy Lester
  • Date: 2008-08-10 02:15:48 UTC
  • mto: (266.1.31 use-replace-funcs)
  • mto: This revision was merged to the branch mainline in revision 295.
  • Revision ID: andy@petdance.com-20080810021548-0zx8nhzva6al10k3
Added a proper const qualifer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
#include <drizzled/server_includes.h>
18
18
#include <drizzled/sql_select.h>
19
 
#include <drizzled/error.h>
20
 
#include CMATH_H
21
 
 
22
 
#if defined(CMATH_NAMESPACE)
23
 
using namespace CMATH_NAMESPACE;
24
 
#endif
 
19
#include <drizzled/drizzled_error_messages.h>
25
20
 
26
21
const String my_null_string("NULL", 4, default_charset_info);
27
22
 
42
37
  return &real_traits_instance;
43
38
}
44
39
 
45
 
int64_t Hybrid_type_traits::val_int(Hybrid_type *val,
46
 
                                    bool) const
47
 
{
48
 
  return (int64_t) rint(val->real);
49
 
}
50
40
 
51
41
my_decimal *
52
 
Hybrid_type_traits::val_decimal(Hybrid_type *val, my_decimal *) const
 
42
Hybrid_type_traits::val_decimal(Hybrid_type *val,
 
43
                                my_decimal *to __attribute__((unused))) const
53
44
{
54
45
  double2my_decimal(E_DEC_FATAL_ERROR, val->real, val->dec_buf);
55
46
  return val->dec_buf;
76
67
Hybrid_type_traits_decimal::fix_length_and_dec(Item *item, Item *arg) const
77
68
{
78
69
  item->decimals= arg->decimals;
79
 
  item->max_length= cmin(arg->max_length + DECIMAL_LONGLONG_DIGITS,
80
 
                        (unsigned int)DECIMAL_MAX_STR_LENGTH);
 
70
  item->max_length= min(arg->max_length + DECIMAL_LONGLONG_DIGITS,
 
71
                        DECIMAL_MAX_STR_LENGTH);
81
72
}
82
73
 
83
74
 
152
143
}
153
144
 
154
145
void
155
 
Hybrid_type_traits_integer::fix_length_and_dec(Item *item, Item *) const
 
146
Hybrid_type_traits_integer::fix_length_and_dec(Item *item,
 
147
                                               Item *arg __attribute__((unused))) const
156
148
{
157
149
  item->decimals= 0;
158
150
  item->max_length= MY_INT64_NUM_DECIMAL_DIGITS;
264
256
                     res->ptr(), res->length(), res->charset(),
265
257
                     decimal_value) & E_DEC_BAD_NUM)
266
258
  {
267
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
259
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
268
260
                        ER_TRUNCATED_WRONG_VALUE,
269
261
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
270
262
                        str_value.c_ptr());
390
382
  cmp_context= (Item_result)-1;
391
383
 
392
384
  /* Put item in free list so that we can free all items at end */
393
 
  Session *session= current_session;
394
 
  next= session->free_list;
395
 
  session->free_list= this;
 
385
  THD *thd= current_thd;
 
386
  next= thd->free_list;
 
387
  thd->free_list= this;
396
388
  /*
397
389
    Item constructor can be called during execution other then SQL_COM
398
 
    command => we should check session->lex->current_select on zero (session->lex
 
390
    command => we should check thd->lex->current_select on zero (thd->lex
399
391
    can be uninitialised)
400
392
  */
401
 
  if (session->lex->current_select)
 
393
  if (thd->lex->current_select)
402
394
  {
403
395
    enum_parsing_place place= 
404
 
      session->lex->current_select->parsing_place;
 
396
      thd->lex->current_select->parsing_place;
405
397
    if (place == SELECT_LIST ||
406
398
        place == IN_HAVING)
407
 
      session->lex->current_select->select_n_having_items++;
 
399
      thd->lex->current_select->select_n_having_items++;
408
400
  }
409
401
}
410
402
 
415
407
  Used for duplicating lists in processing queries with temporary
416
408
  tables.
417
409
*/
418
 
Item::Item(Session *session, Item *item):
 
410
Item::Item(THD *thd, Item *item):
419
411
  is_expensive_cache(-1),
420
412
  rsize(0),
421
413
  str_value(item->str_value),
432
424
  collation(item->collation),
433
425
  cmp_context(item->cmp_context)
434
426
{
435
 
  next= session->free_list;                             // Put in free list
436
 
  session->free_list= this;
 
427
  next= thd->free_list;                         // Put in free list
 
428
  thd->free_list= this;
437
429
}
438
430
 
439
431
 
440
 
uint32_t Item::decimal_precision() const
 
432
uint Item::decimal_precision() const
441
433
{
442
434
  Item_result restype= result_type();
443
435
 
444
436
  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
445
 
    return cmin(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
446
 
               (unsigned int)DECIMAL_MAX_PRECISION);
447
 
  return cmin(max_length, (uint32_t)DECIMAL_MAX_PRECISION);
 
437
    return min(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
 
438
               DECIMAL_MAX_PRECISION);
 
439
  return min(max_length, DECIMAL_MAX_PRECISION);
448
440
}
449
441
 
450
442
 
454
446
 
455
447
  if (name)
456
448
  {
457
 
    Session *session= current_session;
 
449
    THD *thd= current_thd;
458
450
    str->append(STRING_WITH_LEN(" AS "));
459
 
    append_identifier(session, str, name, (uint) strlen(name));
 
451
    append_identifier(thd, str, name, (uint) strlen(name));
460
452
  }
461
453
}
462
454
 
477
469
  @param arg   a dummy parameter, is not used here
478
470
*/
479
471
 
480
 
bool Item::cleanup_processor(unsigned char *)
 
472
bool Item::cleanup_processor(uchar *arg __attribute__((unused)))
481
473
{
482
474
  if (fixed)
483
475
    cleanup();
508
500
 
509
501
  This function is designed to ease transformation of Item trees.
510
502
  Re-execution note: every such transformation is registered for
511
 
  rollback by Session::change_item_tree() and is rolled back at the end
512
 
  of execution by Session::rollback_item_tree_changes().
 
503
  rollback by THD::change_item_tree() and is rolled back at the end
 
504
  of execution by THD::rollback_item_tree_changes().
513
505
 
514
506
  Therefore:
515
507
  - this function can not be used at prepared statement prepare
516
508
  (in particular, in fix_fields!), as only permanent
517
509
  transformation of Item trees are allowed at prepare.
518
510
  - the transformer function shall allocate new Items in execution
519
 
  memory root (session->mem_root) and not anywhere else: allocated
 
511
  memory root (thd->mem_root) and not anywhere else: allocated
520
512
  items will be gone in the end of execution.
521
513
 
522
514
  If you don't need to transform an item tree, but only traverse
527
519
  @param arg            opaque argument passed to the functor
528
520
 
529
521
  @return
530
 
    Returns pointer to the new subtree root.  Session::change_item_tree()
 
522
    Returns pointer to the new subtree root.  THD::change_item_tree()
531
523
    should be called for it if transformation took place, i.e. if a
532
524
    pointer to newly allocated item is returned.
533
525
*/
534
526
 
535
 
Item* Item::transform(Item_transformer transformer, unsigned char *arg)
 
527
Item* Item::transform(Item_transformer transformer, uchar *arg)
536
528
{
537
529
  return (this->*transformer)(arg);
538
530
}
556
548
  Constructor used by Item_field & Item_*_ref (see Item comment)
557
549
*/
558
550
 
559
 
Item_ident::Item_ident(Session *session, Item_ident *item)
560
 
  :Item(session, item),
 
551
Item_ident::Item_ident(THD *thd, Item_ident *item)
 
552
  :Item(thd, item),
561
553
   orig_db_name(item->orig_db_name),
562
554
   orig_table_name(item->orig_table_name), 
563
555
   orig_field_name(item->orig_field_name),
589
581
  return;
590
582
}
591
583
 
592
 
bool Item_ident::remove_dependence_processor(unsigned char * arg)
 
584
bool Item_ident::remove_dependence_processor(uchar * arg)
593
585
{
594
586
  if (depended_from == (st_select_lex *) arg)
595
587
    depended_from= 0;
615
607
    for the subsequent items.
616
608
*/
617
609
 
618
 
bool Item_field::collect_item_field_processor(unsigned char *arg)
 
610
bool Item_field::collect_item_field_processor(uchar *arg)
619
611
{
620
612
  List<Item_field> *item_list= (List<Item_field>*) arg;
621
613
  List_iterator<Item_field> item_list_it(*item_list);
646
638
    false otherwise
647
639
*/
648
640
 
649
 
bool Item_field::find_item_in_field_list_processor(unsigned char *arg)
 
641
bool Item_field::find_item_in_field_list_processor(uchar *arg)
650
642
{
651
643
  KEY_PART_INFO *first_non_group_part= *((KEY_PART_INFO **) arg);
652
644
  KEY_PART_INFO *last_part= *(((KEY_PART_INFO **) arg) + 1);
669
661
    column read set or to register used fields in a view
670
662
*/
671
663
 
672
 
bool Item_field::register_field_in_read_map(unsigned char *arg)
 
664
bool Item_field::register_field_in_read_map(uchar *arg)
673
665
{
674
 
  Table *table= (Table *) arg;
 
666
  TABLE *table= (TABLE *) arg;
675
667
  if (field->table == table || !table)
676
668
    bitmap_set_bit(field->table->read_set, field->field_index);
677
 
  if (field->vcol_info && field->vcol_info->expr_item)
678
 
    return field->vcol_info->expr_item->walk(&Item::register_field_in_read_map, 
679
 
                                             1, arg);
680
669
  return 0;
681
670
}
682
671
 
683
 
/*
684
 
  Mark field in bitmap supplied as *arg
685
 
 
686
 
*/
687
 
 
688
 
bool Item_field::register_field_in_bitmap(unsigned char *arg)
689
 
{
690
 
  MY_BITMAP *bitmap= (MY_BITMAP *) arg;
691
 
  assert(bitmap);
692
 
  bitmap_set_bit(bitmap, field->field_index);
693
 
  return false;
694
 
}
695
 
 
696
 
 
697
 
bool Item::check_cols(uint32_t c)
 
672
 
 
673
bool Item::check_cols(uint c)
698
674
{
699
675
  if (c != 1)
700
676
  {
705
681
}
706
682
 
707
683
 
708
 
void Item::set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs)
 
684
void Item::set_name(const char *str, uint length, const CHARSET_INFO * const cs)
709
685
{
710
686
  if (!length)
711
687
  {
716
692
  }
717
693
  if (cs->ctype)
718
694
  {
719
 
    uint32_t orig_len= length;
 
695
    uint orig_len= length;
720
696
    /*
721
697
      This will probably need a better implementation in the future:
722
698
      a function in CHARSET_INFO structure.
729
705
    if (orig_len != length && !is_autogenerated_name)
730
706
    {
731
707
      if (length == 0)
732
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
708
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
733
709
                            ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY),
734
710
                            str + length - orig_len);
735
711
      else
736
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
712
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
737
713
                            ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES),
738
714
                            str + length - orig_len);
739
715
    }
746
722
                                   &res_length);
747
723
  }
748
724
  else
749
 
    name= sql_strmake(str, (name_length= cmin(length,(unsigned int)MAX_ALIAS_NAME)));
 
725
    name= sql_strmake(str, (name_length= min(length,MAX_ALIAS_NAME)));
750
726
}
751
727
 
752
728
 
754
730
  @details
755
731
  This function is called when:
756
732
  - Comparing items in the WHERE clause (when doing where optimization)
757
 
  - When trying to find an order_st BY/GROUP BY item in the SELECT part
 
733
  - When trying to find an ORDER BY/GROUP BY item in the SELECT part
758
734
*/
759
735
 
760
 
bool Item::eq(const Item *item, bool) const
 
736
bool Item::eq(const Item *item, bool binary_cmp __attribute__((unused))) const
761
737
{
762
738
  /*
763
739
    Note, that this is never true if item is a Item_param:
787
763
  the latter returns a non-fixed Item, so val_str() crashes afterwards.
788
764
  Override Item_num method, to return a fixed item.
789
765
*/
790
 
Item *Item_num::safe_charset_converter(const CHARSET_INFO * const)
 
766
Item *Item_num::safe_charset_converter(const CHARSET_INFO * const tocs __attribute__((unused)))
791
767
{
792
768
  Item_string *conv;
793
769
  char buf[64];
802
778
}
803
779
 
804
780
 
805
 
Item *Item_static_float_func::safe_charset_converter(const CHARSET_INFO * const)
 
781
Item *Item_static_float_func::safe_charset_converter(const CHARSET_INFO * const tocs __attribute__((unused)))
806
782
{
807
783
  Item_string *conv;
808
784
  char buf[64];
821
797
Item *Item_string::safe_charset_converter(const CHARSET_INFO * const tocs)
822
798
{
823
799
  Item_string *conv;
824
 
  uint32_t conv_errors;
 
800
  uint conv_errors;
825
801
  char *ptr;
826
802
  String tmp, cstr, *ostr= val_str(&tmp);
827
803
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
837
813
    */
838
814
    return NULL;
839
815
  }
840
 
  if (!(ptr= current_session->strmake(cstr.ptr(), cstr.length())))
 
816
  if (!(ptr= current_thd->strmake(cstr.ptr(), cstr.length())))
841
817
    return NULL;
842
818
  conv->str_value.set(ptr, cstr.length(), cstr.charset());
843
819
  /* Ensure that no one is going to change the result string */
850
826
{
851
827
  if (const_item())
852
828
  {
853
 
    uint32_t cnv_errors;
 
829
    uint cnv_errors;
854
830
    String *ostr= val_str(&cnvstr);
855
831
    cnvitem->str_value.copy(ostr->ptr(), ostr->length(),
856
832
                            ostr->charset(), tocs, &cnv_errors);
867
843
Item *Item_static_string_func::safe_charset_converter(const CHARSET_INFO * const tocs)
868
844
{
869
845
  Item_string *conv;
870
 
  uint32_t conv_errors;
 
846
  uint conv_errors;
871
847
  String tmp, cstr, *ostr= val_str(&tmp);
872
848
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
873
849
  if (conv_errors ||
909
885
  As a extra convenience the time structure is reset on error!
910
886
*/
911
887
 
912
 
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
888
bool Item::get_date(DRIZZLE_TIME *ltime,uint fuzzydate)
913
889
{
914
890
  if (result_type() == STRING_RESULT)
915
891
  {
924
900
  {
925
901
    int64_t value= val_int();
926
902
    int was_cut;
927
 
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
 
903
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1LL)
928
904
    {
929
905
      char buff[22], *end;
930
906
      end= int64_t10_to_str(value, buff, -10);
931
 
      make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
907
      make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
932
908
                                   buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
933
 
                                   NULL);
 
909
                                   NullS);
934
910
      goto err;
935
911
    }
936
912
  }
962
938
 
963
939
const CHARSET_INFO *Item::default_charset()
964
940
{
965
 
  return current_session->variables.collation_connection;
 
941
  return current_thd->variables.collation_connection;
966
942
}
967
943
 
968
944
 
977
953
int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
978
954
{
979
955
  int res;
980
 
  Table *table= field->table;
981
 
  Session *session= table->in_use;
982
 
  enum_check_fields tmp= session->count_cuted_fields;
983
 
  ulong sql_mode= session->variables.sql_mode;
984
 
  session->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
985
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
956
  TABLE *table= field->table;
 
957
  THD *thd= table->in_use;
 
958
  enum_check_fields tmp= thd->count_cuted_fields;
 
959
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
 
960
  ulong sql_mode= thd->variables.sql_mode;
 
961
  thd->variables.sql_mode&= ~(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE);
 
962
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
986
963
  res= save_in_field(field, no_conversions);
987
 
  session->count_cuted_fields= tmp;
988
 
  session->variables.sql_mode= sql_mode;
 
964
  thd->count_cuted_fields= tmp;
 
965
  dbug_tmp_restore_column_map(table->write_set, old_map);
 
966
  thd->variables.sql_mode= sql_mode;
989
967
  return res;
990
968
}
991
969
 
1015
993
/**
1016
994
  Move SUM items out from item tree and replace with reference.
1017
995
 
1018
 
  @param session                        Thread handler
 
996
  @param thd                    Thread handler
1019
997
  @param ref_pointer_array      Pointer to array of reference fields
1020
998
  @param fields         All fields in select
1021
999
  @param ref                    Pointer to item
1028
1006
    All found SUM items are added FIRST in the fields list and
1029
1007
    we replace the item with a reference.
1030
1008
 
1031
 
    session->fatal_error() may be called if we are out of memory
 
1009
    thd->fatal_error() may be called if we are out of memory
1032
1010
*/
1033
1011
 
1034
 
void Item::split_sum_func2(Session *session, Item **ref_pointer_array,
 
1012
void Item::split_sum_func2(THD *thd, Item **ref_pointer_array,
1035
1013
                           List<Item> &fields, Item **ref, 
1036
1014
                           bool skip_registered)
1037
1015
{
1045
1023
        ((Item_func *) this)->functype() == Item_func::TRIG_COND_FUNC)))
1046
1024
  {
1047
1025
    /* Will split complicated items and ignore simple ones */
1048
 
    split_sum_func(session, ref_pointer_array, fields);
 
1026
    split_sum_func(thd, ref_pointer_array, fields);
1049
1027
  }
1050
1028
  else if ((type() == SUM_FUNC_ITEM || (used_tables() & ~PARAM_TABLE_BIT)) &&
1051
1029
           type() != SUBSELECT_ITEM &&
1063
1041
      Item_ref to allow fields from view being stored in tmp table.
1064
1042
    */
1065
1043
    Item_aggregate_ref *item_ref;
1066
 
    uint32_t el= fields.elements;
 
1044
    uint el= fields.elements;
1067
1045
    Item *real_itm= real_item();
1068
1046
 
1069
1047
    ref_pointer_array[el]= real_itm;
1070
 
    if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
 
1048
    if (!(item_ref= new Item_aggregate_ref(&thd->lex->current_select->context,
1071
1049
                                           ref_pointer_array + el, 0, name)))
1072
1050
      return;                                   // fatal_error is set
1073
1051
    if (type() == SUM_FUNC_ITEM)
1074
1052
      item_ref->depended_from= ((Item_sum *) this)->depended_from(); 
1075
1053
    fields.push_front(real_itm);
1076
 
    session->change_item_tree(ref, item_ref);
 
1054
    thd->change_item_tree(ref, item_ref);
1077
1055
  }
1078
1056
}
1079
1057
 
1133
1111
  @endcode
1134
1112
*/
1135
1113
 
1136
 
bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
 
1114
bool DTCollation::aggregate(DTCollation &dt, uint flags)
1137
1115
{
1138
1116
  if (!my_charset_same(collation, dt.collation))
1139
1117
  {
1253
1231
 
1254
1232
 
1255
1233
static
1256
 
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
 
1234
void my_coll_agg_error(Item** args, uint count, const char *fname,
1257
1235
                       int item_sep)
1258
1236
{
1259
1237
  if (count == 2)
1267
1245
 
1268
1246
 
1269
1247
bool agg_item_collations(DTCollation &c, const char *fname,
1270
 
                         Item **av, uint32_t count, uint32_t flags, int item_sep)
 
1248
                         Item **av, uint count, uint flags, int item_sep)
1271
1249
{
1272
 
  uint32_t i;
 
1250
  uint i;
1273
1251
  Item **arg;
1274
1252
  c.set(av[0]->collation);
1275
1253
  for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
1291
1269
 
1292
1270
 
1293
1271
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
1294
 
                                        Item **av, uint32_t count, uint32_t flags)
 
1272
                                        Item **av, uint count, uint flags)
1295
1273
{
1296
1274
  return (agg_item_collations(c, fname, av, count,
1297
1275
                              flags | MY_COLL_DISALLOW_NONE, 1));
1319
1297
  @code
1320
1298
    collect(A,B,C) ::= collect(collect(A,B),C)
1321
1299
  @endcode
1322
 
  Since this function calls Session::change_item_tree() on the passed Item **
 
1300
  Since this function calls THD::change_item_tree() on the passed Item **
1323
1301
  pointers, it is necessary to pass the original Item **'s, not copies.
1324
1302
  Otherwise their values will not be properly restored (see BUG#20769).
1325
1303
  If the items are not consecutive (eg. args[2] and args[5]), use the
1330
1308
*/
1331
1309
 
1332
1310
bool agg_item_charsets(DTCollation &coll, const char *fname,
1333
 
                       Item **args, uint32_t nargs, uint32_t flags, int item_sep)
 
1311
                       Item **args, uint nargs, uint flags, int item_sep)
1334
1312
{
1335
1313
  Item **arg, *safe_args[2];
1336
1314
 
1352
1330
    safe_args[1]= args[item_sep];
1353
1331
  }
1354
1332
 
1355
 
  Session *session= current_session;
 
1333
  THD *thd= current_thd;
 
1334
  Query_arena *arena, backup;
1356
1335
  bool res= false;
1357
 
  uint32_t i;
 
1336
  uint i;
 
1337
  /*
 
1338
    In case we're in statement prepare, create conversion item
 
1339
    in its memory: it will be reused on each execute.
 
1340
  */
 
1341
  arena= NULL;
1358
1342
 
1359
1343
  for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
1360
1344
  {
1393
1377
      been created in prepare. In this case register the change for
1394
1378
      rollback.
1395
1379
    */
1396
 
    session->change_item_tree(arg, conv);
 
1380
    thd->change_item_tree(arg, conv);
1397
1381
    /*
1398
1382
      We do not check conv->fixed, because Item_func_conv_charset which can
1399
1383
      be return by safe_charset_converter can't be fixed at creation
1400
1384
    */
1401
 
    conv->fix_fields(session, arg);
 
1385
    conv->fix_fields(thd, arg);
1402
1386
  }
1403
 
 
 
1387
  if (arena)
 
1388
    thd->restore_active_arena(arena, &backup);
1404
1389
  return res;
1405
1390
}
1406
1391
 
1421
1406
/**********************************************/
1422
1407
 
1423
1408
Item_field::Item_field(Field *f)
1424
 
  :Item_ident(0, NULL, *f->table_name, f->field_name),
 
1409
  :Item_ident(0, NullS, *f->table_name, f->field_name),
1425
1410
   item_equal(0), no_const_subst(0),
1426
1411
   have_privileges(0), any_privileges(0)
1427
1412
{
1441
1426
  Item_field (this is important in prepared statements).
1442
1427
*/
1443
1428
 
1444
 
Item_field::Item_field(Session *, Name_resolution_context *context_arg,
 
1429
Item_field::Item_field(THD *thd __attribute__((unused)),
 
1430
                       Name_resolution_context *context_arg,
1445
1431
                       Field *f)
1446
1432
  :Item_ident(context_arg, f->table->s->db.str, *f->table_name, f->field_name),
1447
1433
   item_equal(0), no_const_subst(0),
1458
1444
   field(0), result_field(0), item_equal(0), no_const_subst(0),
1459
1445
   have_privileges(0), any_privileges(0)
1460
1446
{
1461
 
  SELECT_LEX *select= current_session->lex->current_select;
 
1447
  SELECT_LEX *select= current_thd->lex->current_select;
1462
1448
  collation.set(DERIVATION_IMPLICIT);
1463
1449
  if (select && select->parsing_place != IN_HAVING)
1464
1450
      select->select_n_where_fields++;
1468
1454
  Constructor need to process subselect with temporary tables (see Item)
1469
1455
*/
1470
1456
 
1471
 
Item_field::Item_field(Session *session, Item_field *item)
1472
 
  :Item_ident(session, item),
 
1457
Item_field::Item_field(THD *thd, Item_field *item)
 
1458
  :Item_ident(thd, item),
1473
1459
   field(item->field),
1474
1460
   result_field(item->result_field),
1475
1461
   item_equal(item->item_equal),
1520
1506
  {
1521
1507
    tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
1522
1508
                          (uint) strlen(field_name)+3);
1523
 
    strxmov(tmp,db_name,".",table_name,".",field_name,NULL);
 
1509
    strxmov(tmp,db_name,".",table_name,".",field_name,NullS);
1524
1510
  }
1525
1511
  else
1526
1512
  {
1528
1514
    {
1529
1515
      tmp= (char*) sql_alloc((uint) strlen(table_name) +
1530
1516
                             (uint) strlen(field_name) + 2);
1531
 
      strxmov(tmp, table_name, ".", field_name, NULL);
 
1517
      strxmov(tmp, table_name, ".", field_name, NullS);
1532
1518
    }
1533
1519
    else
1534
1520
      tmp= (char*) field_name;
1537
1523
}
1538
1524
 
1539
1525
void Item_ident::print(String *str,
1540
 
                       enum_query_type)
 
1526
                       enum_query_type query_type __attribute__((unused)))
1541
1527
{
1542
 
  Session *session= current_session;
 
1528
  THD *thd= current_thd;
1543
1529
  char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
1544
1530
  const char *d_name= db_name, *t_name= table_name;
1545
1531
  if (lower_case_table_names== 1 ||
1547
1533
  {
1548
1534
    if (table_name && table_name[0])
1549
1535
    {
1550
 
      my_stpcpy(t_name_buff, table_name);
 
1536
      stpcpy(t_name_buff, table_name);
1551
1537
      my_casedn_str(files_charset_info, t_name_buff);
1552
1538
      t_name= t_name_buff;
1553
1539
    }
1554
1540
    if (db_name && db_name[0])
1555
1541
    {
1556
 
      my_stpcpy(d_name_buff, db_name);
 
1542
      stpcpy(d_name_buff, db_name);
1557
1543
      my_casedn_str(files_charset_info, d_name_buff);
1558
1544
      d_name= d_name_buff;
1559
1545
    }
1563
1549
  {
1564
1550
    const char *nm= (field_name && field_name[0]) ?
1565
1551
                      field_name : name ? name : "tmp_field";
1566
 
    append_identifier(session, str, nm, (uint) strlen(nm));
 
1552
    append_identifier(thd, str, nm, (uint) strlen(nm));
1567
1553
    return;
1568
1554
  }
1569
1555
  if (db_name && db_name[0] && !alias_name_used)
1570
1556
  {
1571
1557
    {
1572
 
      append_identifier(session, str, d_name, (uint)strlen(d_name));
 
1558
      append_identifier(thd, str, d_name, (uint)strlen(d_name));
1573
1559
      str->append('.');
1574
1560
    }
1575
 
    append_identifier(session, str, t_name, (uint)strlen(t_name));
 
1561
    append_identifier(thd, str, t_name, (uint)strlen(t_name));
1576
1562
    str->append('.');
1577
 
    append_identifier(session, str, field_name, (uint)strlen(field_name));
 
1563
    append_identifier(thd, str, field_name, (uint)strlen(field_name));
1578
1564
  }
1579
1565
  else
1580
1566
  {
1581
1567
    if (table_name[0])
1582
1568
    {
1583
 
      append_identifier(session, str, t_name, (uint) strlen(t_name));
 
1569
      append_identifier(thd, str, t_name, (uint) strlen(t_name));
1584
1570
      str->append('.');
1585
 
      append_identifier(session, str, field_name, (uint) strlen(field_name));
 
1571
      append_identifier(thd, str, field_name, (uint) strlen(field_name));
1586
1572
    }
1587
1573
    else
1588
 
      append_identifier(session, str, field_name, (uint) strlen(field_name));
 
1574
      append_identifier(thd, str, field_name, (uint) strlen(field_name));
1589
1575
  }
1590
1576
}
1591
1577
 
1634
1620
  return result_field->val_str(str,&str_value);
1635
1621
}
1636
1622
 
1637
 
bool Item_field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
1623
bool Item_field::get_date(DRIZZLE_TIME *ltime,uint fuzzydate)
1638
1624
{
1639
1625
  if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
1640
1626
  {
1644
1630
  return 0;
1645
1631
}
1646
1632
 
1647
 
bool Item_field::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
1633
bool Item_field::get_date_result(DRIZZLE_TIME *ltime,uint fuzzydate)
1648
1634
{
1649
1635
  if ((null_value=result_field->is_null()) ||
1650
1636
      result_field->get_date(ltime,fuzzydate))
1714
1700
}
1715
1701
 
1716
1702
 
1717
 
bool Item_field::eq(const Item *item, bool) const
 
1703
bool Item_field::eq(const Item *item,
 
1704
                    bool binary_cmp __attribute__((unused))) const
1718
1705
{
1719
1706
  Item *real_item= ((Item *) item)->real_item();
1720
1707
  if (real_item->type() != FIELD_ITEM)
1752
1739
}
1753
1740
 
1754
1741
 
1755
 
void Item_field::fix_after_pullout(st_select_lex *new_parent, Item **)
 
1742
void Item_field::fix_after_pullout(st_select_lex *new_parent,
 
1743
                                   Item **ref __attribute__((unused)))
1756
1744
{
1757
1745
  if (new_parent == depended_from)
1758
1746
    depended_from= NULL;
1765
1753
}
1766
1754
 
1767
1755
 
1768
 
Item *Item_field::get_tmp_table_item(Session *session)
 
1756
Item *Item_field::get_tmp_table_item(THD *thd)
1769
1757
{
1770
 
  Item_field *new_item= new Item_field(session, this);
 
1758
  Item_field *new_item= new Item_field(thd, this);
1771
1759
  if (new_item)
1772
1760
    new_item->field= new_item->result_field;
1773
1761
  return new_item;
1774
1762
}
1775
1763
 
1776
 
int64_t Item_field::val_int_endpoint(bool, bool *)
 
1764
int64_t Item_field::val_int_endpoint(bool left_endp __attribute__((unused)),
 
1765
                                      bool *incl_endp __attribute__((unused)))
1777
1766
{
1778
1767
  int64_t res= val_int();
1779
1768
  return null_value? INT64_MIN : res;
1785
1774
  This is always 'signed'. Unsigned values are created with Item_uint()
1786
1775
*/
1787
1776
 
1788
 
Item_int::Item_int(const char *str_arg, uint32_t length)
 
1777
Item_int::Item_int(const char *str_arg, uint length)
1789
1778
{
1790
1779
  char *end_ptr= (char*) str_arg + length;
1791
1780
  int error;
1810
1799
  return str;
1811
1800
}
1812
1801
 
1813
 
void Item_int::print(String *str, enum_query_type)
 
1802
void Item_int::print(String *str,
 
1803
                     enum_query_type query_type __attribute__((unused)))
1814
1804
{
1815
1805
  // my_charset_bin is good enough for numbers
1816
1806
  str_value.set(value, &my_charset_bin);
1818
1808
}
1819
1809
 
1820
1810
 
1821
 
Item_uint::Item_uint(const char *str_arg, uint32_t length):
 
1811
Item_uint::Item_uint(const char *str_arg, uint length):
1822
1812
  Item_int(str_arg, length)
1823
1813
{
1824
1814
  unsigned_flag= 1;
1825
1815
}
1826
1816
 
1827
1817
 
1828
 
Item_uint::Item_uint(const char *str_arg, int64_t i, uint32_t length):
 
1818
Item_uint::Item_uint(const char *str_arg, int64_t i, uint length):
1829
1819
  Item_int(str_arg, i, length)
1830
1820
{
1831
1821
  unsigned_flag= 1;
1841
1831
}
1842
1832
 
1843
1833
 
1844
 
void Item_uint::print(String *str, enum_query_type)
 
1834
void Item_uint::print(String *str,
 
1835
                      enum_query_type query_type __attribute__((unused)))
1845
1836
{
1846
1837
  // latin1 is good enough for numbers
1847
1838
  str_value.set((uint64_t) value, default_charset());
1849
1840
}
1850
1841
 
1851
1842
 
1852
 
Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
 
1843
Item_decimal::Item_decimal(const char *str_arg, uint length,
1853
1844
                           const CHARSET_INFO * const charset)
1854
1845
{
1855
1846
  str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
1870
1861
}
1871
1862
 
1872
1863
 
1873
 
Item_decimal::Item_decimal(double val, int, int)
 
1864
Item_decimal::Item_decimal(double val,
 
1865
                           int precision __attribute__((unused)),
 
1866
                           int scale __attribute__((unused)))
1874
1867
{
1875
1868
  double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
1876
1869
  decimals= (uint8_t) decimal_value.frac;
1881
1874
 
1882
1875
 
1883
1876
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
1884
 
                           uint32_t decimal_par, uint32_t length)
 
1877
                           uint decimal_par, uint length)
1885
1878
{
1886
1879
  my_decimal2decimal(val_arg, &decimal_value);
1887
1880
  name= (char*) str;
1901
1894
}
1902
1895
 
1903
1896
 
1904
 
Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
 
1897
Item_decimal::Item_decimal(const uchar *bin, int precision, int scale)
1905
1898
{
1906
1899
  binary2my_decimal(E_DEC_FATAL_ERROR, bin,
1907
1900
                    &decimal_value, precision, scale);
1933
1926
  return result;
1934
1927
}
1935
1928
 
1936
 
void Item_decimal::print(String *str, enum_query_type)
 
1929
void Item_decimal::print(String *str,
 
1930
                         enum_query_type query_type __attribute__((unused)))
1937
1931
{
1938
1932
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
1939
1933
  str->append(str_value);
1940
1934
}
1941
1935
 
1942
1936
 
1943
 
bool Item_decimal::eq(const Item *item, bool) const
 
1937
bool Item_decimal::eq(const Item *item,
 
1938
                      bool binary_cmp __attribute__((unused))) const
1944
1939
{
1945
1940
  if (type() == item->type() && item->basic_const_item())
1946
1941
  {
1977
1972
}
1978
1973
 
1979
1974
 
1980
 
int64_t Item_float::val_int()
1981
 
{
1982
 
  assert(fixed == 1);
1983
 
  if (value <= (double) INT64_MIN)
1984
 
  {
1985
 
     return INT64_MIN;
1986
 
  }
1987
 
  else if (value >= (double) (uint64_t) INT64_MAX)
1988
 
  {
1989
 
    return INT64_MAX;
1990
 
  }
1991
 
  return (int64_t) rint(value);
1992
 
}
1993
 
 
1994
1975
my_decimal *Item_float::val_decimal(my_decimal *decimal_value)
1995
1976
{
1996
1977
  // following assert is redundant, because fixed=1 assigned in constructor
2017
1998
  }
2018
1999
  else
2019
2000
  {
2020
 
    Session *session= current_session;
 
2001
    THD *thd= current_thd;
2021
2002
    LEX_STRING utf8_lex_str;
2022
2003
 
2023
 
    session->convert_string(&utf8_lex_str,
 
2004
    thd->convert_string(&utf8_lex_str,
2024
2005
                        system_charset_info,
2025
2006
                        str_value.c_ptr_safe(),
2026
2007
                        str_value.length(),
2054
2035
      We can use str_value.ptr() here as Item_string is gurantee to put an
2055
2036
      end \0 here.
2056
2037
    */
2057
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2038
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2058
2039
                        ER_TRUNCATED_WRONG_VALUE,
2059
2040
                        ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
2060
2041
                        str_value.ptr());
2084
2065
  if (err > 0 ||
2085
2066
      (end != org_end && !check_if_only_end_space(cs, end, org_end)))
2086
2067
  {
2087
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2068
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2088
2069
                        ER_TRUNCATED_WRONG_VALUE,
2089
2070
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
2090
2071
                        str_value.ptr());
2099
2080
}
2100
2081
 
2101
2082
 
2102
 
bool Item_null::eq(const Item *item, bool) const
 
2083
bool Item_null::eq(const Item *item,
 
2084
                   bool binary_cmp __attribute__((unused))) const
2103
2085
{ return item->type() == type(); }
2104
2086
 
2105
2087
 
2118
2100
  return 0;
2119
2101
}
2120
2102
/* ARGSUSED */
2121
 
String *Item_null::val_str(String *)
 
2103
String *Item_null::val_str(String *str __attribute__((unused)))
2122
2104
{
2123
2105
  // following assert is redundant, because fixed=1 assigned in constructor
2124
2106
  assert(fixed == 1);
2126
2108
  return 0;
2127
2109
}
2128
2110
 
2129
 
my_decimal *Item_null::val_decimal(my_decimal *)
 
2111
my_decimal *Item_null::val_decimal(my_decimal *decimal_value __attribute__((unused)))
2130
2112
{
2131
2113
  return 0;
2132
2114
}
2146
2128
*/
2147
2129
 
2148
2130
static void
2149
 
default_set_param_func(Item_param *param, unsigned char **, ulong)
 
2131
default_set_param_func(Item_param *param,
 
2132
                       uchar **pos __attribute__((unused)),
 
2133
                       ulong len __attribute__((unused)))
2150
2134
{
2151
2135
  param->set_null();
2152
2136
}
2153
2137
 
2154
2138
 
2155
 
Item_param::Item_param(uint32_t pos_in_query_arg) :
 
2139
Item_param::Item_param(uint pos_in_query_arg) :
2156
2140
  state(NO_VALUE),
2157
2141
  item_result_type(STRING_RESULT),
2158
2142
  /* Don't pretend to be a literal unless value for this item is set. */
2223
2207
    internal decimal value.
2224
2208
*/
2225
2209
 
2226
 
void Item_param::set_decimal(char *str, ulong length)
 
2210
void Item_param::set_decimal(const char *str, ulong length)
2227
2211
{
2228
2212
  char *end;
2229
2213
 
2230
 
  end= str+length;
2231
 
  str2my_decimal((uint)E_DEC_FATAL_ERROR, str, &decimal_value, &end);
 
2214
  end= (char*) str+length;
 
2215
  str2my_decimal(E_DEC_FATAL_ERROR, str, &decimal_value, &end);
2232
2216
  state= DECIMAL_VALUE;
2233
2217
  decimals= decimal_value.frac;
2234
2218
  max_length= my_decimal_precision_to_length(decimal_value.precision(),
2251
2235
    the fact that even wrong value sent over binary protocol fits into
2252
2236
    MAX_DATE_STRING_REP_LENGTH buffer.
2253
2237
*/
2254
 
void Item_param::set_time(DRIZZLE_TIME *tm,
2255
 
                          enum enum_drizzle_timestamp_type time_type,
 
2238
void Item_param::set_time(DRIZZLE_TIME *tm, timestamp_type time_type,
2256
2239
                          uint32_t max_length_arg)
2257
2240
2258
2241
  value.time= *tm;
2264
2247
      value.time.minute > 59 || value.time.second > 59)
2265
2248
  {
2266
2249
    char buff[MAX_DATE_STRING_REP_LENGTH];
2267
 
    uint32_t length= my_TIME_to_str(&value.time, buff);
2268
 
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2250
    uint length= my_TIME_to_str(&value.time, buff);
 
2251
    make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2269
2252
                                 buff, length, time_type, 0);
2270
2253
    set_zero_time(&value.time, DRIZZLE_TIMESTAMP_ERROR);
2271
2254
  }
2284
2267
    Assign string with no conversion: data is converted only after it's
2285
2268
    been written to the binary log.
2286
2269
  */
2287
 
  uint32_t dummy_errors;
 
2270
  uint dummy_errors;
2288
2271
  if (str_value.copy(str, length, &my_charset_bin, &my_charset_bin,
2289
2272
                     &dummy_errors))
2290
2273
    return(true);
2320
2303
/**
2321
2304
  Set parameter value from user variable value.
2322
2305
 
2323
 
  @param session   Current thread
 
2306
  @param thd   Current thread
2324
2307
  @param entry User variable structure (NULL means use NULL value)
2325
2308
 
2326
2309
  @retval
2329
2312
    1 Out of memory
2330
2313
*/
2331
2314
 
2332
 
bool Item_param::set_from_user_var(Session *session, const user_var_entry *entry)
 
2315
bool Item_param::set_from_user_var(THD *thd, const user_var_entry *entry)
2333
2316
{
2334
2317
  if (entry && entry->value)
2335
2318
  {
2354
2337
    case STRING_RESULT:
2355
2338
    {
2356
2339
      const CHARSET_INFO * const fromcs= entry->collation.collation;
2357
 
      const CHARSET_INFO * const tocs= session->variables.collation_connection;
 
2340
      const CHARSET_INFO * const tocs= thd->variables.collation_connection;
2358
2341
      uint32_t dummy_offset;
2359
2342
 
2360
2343
      value.cs_info.character_set_of_placeholder= 
2480
2463
}
2481
2464
 
2482
2465
 
2483
 
bool Item_param::get_date(DRIZZLE_TIME *res, uint32_t fuzzydate)
 
2466
bool Item_param::get_date(DRIZZLE_TIME *res, uint fuzzydate)
2484
2467
{
2485
2468
  if (state == TIME_VALUE)
2486
2469
  {
2687
2670
  connection.
2688
2671
*/
2689
2672
 
2690
 
bool Item_param::convert_str_value(Session *session)
 
2673
bool Item_param::convert_str_value(THD *thd)
2691
2674
{
2692
2675
  bool rc= false;
2693
2676
  if (state == STRING_VALUE || state == LONG_DATA_VALUE)
2701
2684
    if (value.cs_info.final_character_set_of_str_value !=
2702
2685
        value.cs_info.character_set_of_placeholder)
2703
2686
    {
2704
 
      rc= session->convert_string(&str_value,
 
2687
      rc= thd->convert_string(&str_value,
2705
2688
                              value.cs_info.character_set_of_placeholder,
2706
2689
                              value.cs_info.final_character_set_of_str_value);
2707
2690
    }
2792
2775
 
2793
2776
/* End of Item_param related */
2794
2777
 
2795
 
void Item_param::print(String *str, enum_query_type)
 
2778
void Item_param::print(String *str,
 
2779
                       enum_query_type query_type __attribute__((unused)))
2796
2780
{
2797
2781
  if (state == NO_VALUE)
2798
2782
  {
2822
2806
}
2823
2807
 
2824
2808
/* ARGSUSED */
2825
 
String *Item_copy_string::val_str(String *)
 
2809
String *Item_copy_string::val_str(String *str __attribute__((unused)))
2826
2810
{
2827
2811
  // Item_copy_string is used without fix_fields call
2828
2812
  if (null_value)
2846
2830
*/
2847
2831
 
2848
2832
/* ARGSUSED */
2849
 
bool Item::fix_fields(Session *, Item **)
 
2833
bool Item::fix_fields(THD *thd __attribute__((unused)),
 
2834
                      Item **ref __attribute__((unused)))
2850
2835
{
2851
2836
 
2852
2837
  // We do not check fields which are fixed during construction
2900
2885
}
2901
2886
 
2902
2887
 
2903
 
bool Item_ref_null_helper::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
 
2888
bool Item_ref_null_helper::get_date(DRIZZLE_TIME *ltime, uint fuzzydate)
2904
2889
{  
2905
2890
  return (owner->was_null|= null_value= (*ref)->get_date(ltime, fuzzydate));
2906
2891
}
2910
2895
  Mark item and SELECT_LEXs as dependent if item was resolved in
2911
2896
  outer SELECT.
2912
2897
 
2913
 
  @param session             thread handler
 
2898
  @param thd             thread handler
2914
2899
  @param last            select from which current item depend
2915
2900
  @param current         current select
2916
2901
  @param resolved_item   item which was resolved in outer SELECT(for warning)
2918
2903
                         substitution)
2919
2904
*/
2920
2905
 
2921
 
static void mark_as_dependent(Session *session, SELECT_LEX *last, SELECT_LEX *current,
 
2906
static void mark_as_dependent(THD *thd, SELECT_LEX *last, SELECT_LEX *current,
2922
2907
                              Item_ident *resolved_item,
2923
2908
                              Item_ident *mark_item)
2924
2909
{
2930
2915
  if (mark_item)
2931
2916
    mark_item->depended_from= last;
2932
2917
  current->mark_as_dependent(last);
2933
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
2918
  if (thd->lex->describe & DESCRIBE_EXTENDED)
2934
2919
  {
2935
2920
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
2936
2921
    sprintf(warn_buff, ER(ER_WARN_FIELD_RESOLVED),
2938
2923
            table_name, (table_name [0] ? "." : ""),
2939
2924
            resolved_item->field_name,
2940
2925
            current->select_number, last->select_number);
2941
 
    push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
2926
    push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2942
2927
                 ER_WARN_FIELD_RESOLVED, warn_buff);
2943
2928
  }
2944
2929
}
2948
2933
  Mark range of selects and resolved identifier (field/reference)
2949
2934
  item as dependent.
2950
2935
 
2951
 
  @param session             thread handler
 
2936
  @param thd             thread handler
2952
2937
  @param last_select     select where resolved_item was resolved
2953
2938
  @param current_sel     current select (select where resolved_item was placed)
2954
2939
  @param found_field     field which was found during resolving
2964
2949
    resolved identifier.
2965
2950
*/
2966
2951
 
2967
 
void mark_select_range_as_dependent(Session *session,
 
2952
void mark_select_range_as_dependent(THD *thd,
2968
2953
                                    SELECT_LEX *last_select,
2969
2954
                                    SELECT_LEX *current_sel,
2970
2955
                                    Field *found_field, Item *found_item,
3002
2987
      prev_subselect_item->used_tables_cache|=
3003
2988
        found_field->table->map;
3004
2989
    prev_subselect_item->const_item_cache= 0;
3005
 
    mark_as_dependent(session, last_select, current_sel, resolved_item,
 
2990
    mark_as_dependent(thd, last_select, current_sel, resolved_item,
3006
2991
                      dependent);
3007
2992
  }
3008
2993
}
3023
3008
    - NULL if find_item is not in group_list
3024
3009
*/
3025
3010
 
3026
 
static Item** find_field_in_group_list(Item *find_item, order_st *group_list)
 
3011
static Item** find_field_in_group_list(Item *find_item, ORDER *group_list)
3027
3012
{
3028
3013
  const char *db_name;
3029
3014
  const char *table_name;
3030
3015
  const char *field_name;
3031
 
  order_st      *found_group= NULL;
 
3016
  ORDER      *found_group= NULL;
3032
3017
  int         found_match_degree= 0;
3033
3018
  Item_ident *cur_field;
3034
3019
  int         cur_match_degree= 0;
3054
3039
 
3055
3040
  assert(field_name != 0);
3056
3041
 
3057
 
  for (order_st *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
 
3042
  for (ORDER *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
3058
3043
  {
3059
3044
    if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
3060
3045
    {
3101
3086
          is ambiguous.
3102
3087
        */
3103
3088
        my_error(ER_NON_UNIQ_ERROR, MYF(0),
3104
 
                 find_item->full_name(), current_session->where);
 
3089
                 find_item->full_name(), current_thd->where);
3105
3090
        return NULL;
3106
3091
      }
3107
3092
    }
3127
3112
  derived SELECT column. This extension is allowed only if the
3128
3113
  MODE_ONLY_FULL_GROUP_BY sql mode isn't enabled.
3129
3114
 
3130
 
  @param session     current thread
 
3115
  @param thd     current thread
3131
3116
  @param ref     column reference being resolved
3132
3117
  @param select  the select that ref is resolved against
3133
3118
 
3151
3136
*/
3152
3137
 
3153
3138
static Item**
3154
 
resolve_ref_in_select_and_group(Session *session, Item_ident *ref, SELECT_LEX *select)
 
3139
resolve_ref_in_select_and_group(THD *thd, Item_ident *ref, SELECT_LEX *select)
3155
3140
{
3156
3141
  Item **group_by_ref= NULL;
3157
3142
  Item **select_ref= NULL;
3158
 
  order_st *group_list= (order_st*) select->group_list.first;
 
3143
  ORDER *group_list= (ORDER*) select->group_list.first;
3159
3144
  bool ambiguous_fields= false;
3160
 
  uint32_t counter;
 
3145
  uint counter;
3161
3146
  enum_resolution_type resolution;
3162
3147
 
3163
3148
  /*
3181
3166
        !((*group_by_ref)->eq(*select_ref, 0)))
3182
3167
    {
3183
3168
      ambiguous_fields= true;
3184
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
3169
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
3185
3170
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
3186
 
                          current_session->where);
 
3171
                          current_thd->where);
3187
3172
 
3188
3173
    }
3189
3174
  }
3224
3209
  current select as dependent. The found reference of field should be
3225
3210
  provided in 'from_field'.
3226
3211
 
3227
 
  @param[in] session             current thread
 
3212
  @param[in] thd             current thread
3228
3213
  @param[in,out] from_field  found field reference or (Field*)not_found_field
3229
3214
  @param[in,out] reference   view column if this item was resolved to a
3230
3215
    view column
3252
3237
*/
3253
3238
 
3254
3239
int
3255
 
Item_field::fix_outer_field(Session *session, Field **from_field, Item **reference)
 
3240
Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
3256
3241
{
3257
3242
  enum_parsing_place place= NO_MATTER;
3258
3243
  bool field_found= (*from_field != not_found_field);
3269
3254
  */
3270
3255
  Name_resolution_context *last_checked_context= context;
3271
3256
  Item **ref= (Item **) not_found_item;
3272
 
  SELECT_LEX *current_sel= (SELECT_LEX *) session->lex->current_select;
 
3257
  SELECT_LEX *current_sel= (SELECT_LEX *) thd->lex->current_select;
3273
3258
  Name_resolution_context *outer_context= 0;
3274
3259
  SELECT_LEX *select= 0;
3275
3260
  /* Currently derived tables cannot be correlated */
3299
3284
      the found view field into '*reference', in other words, it
3300
3285
      substitutes this Item_field with the found expression.
3301
3286
    */
3302
 
    if (field_found || (*from_field= find_field_in_tables(session, this,
 
3287
    if (field_found || (*from_field= find_field_in_tables(thd, this,
3303
3288
                                          outer_context->
3304
3289
                                            first_name_resolution_table,
3305
3290
                                          outer_context->
3333
3318
            ;
3334
3319
            if (!(rf= new Item_outer_ref(context, this)))
3335
3320
              return -1;
3336
 
            session->change_item_tree(reference, rf);
 
3321
            thd->change_item_tree(reference, rf);
3337
3322
            select->inner_refs_list.push_back(rf);
3338
 
            rf->in_sum_func= session->lex->in_sum_func;
 
3323
            rf->in_sum_func= thd->lex->in_sum_func;
3339
3324
          }
3340
3325
          /*
3341
3326
            A reference is resolved to a nest level that's outer or the same as
3342
3327
            the nest level of the enclosing set function : adjust the value of
3343
3328
            max_arg_level for the function if it's needed.
3344
3329
          */
3345
 
          if (session->lex->in_sum_func &&
3346
 
              session->lex->in_sum_func->nest_level >= select->nest_level)
 
3330
          if (thd->lex->in_sum_func &&
 
3331
              thd->lex->in_sum_func->nest_level >= select->nest_level)
3347
3332
          {
3348
3333
            Item::Type ref_type= (*reference)->type();
3349
 
            set_if_bigger(session->lex->in_sum_func->max_arg_level,
 
3334
            set_if_bigger(thd->lex->in_sum_func->max_arg_level,
3350
3335
                          select->nest_level);
3351
3336
            set_field(*from_field);
3352
3337
            fixed= 1;
3353
 
            mark_as_dependent(session, last_checked_context->select_lex,
 
3338
            mark_as_dependent(thd, last_checked_context->select_lex,
3354
3339
                              context->select_lex, this,
3355
3340
                              ((ref_type == REF_ITEM ||
3356
3341
                                ref_type == FIELD_ITEM) ?
3365
3350
            (*reference)->used_tables();
3366
3351
          prev_subselect_item->const_item_cache&=
3367
3352
            (*reference)->const_item();
3368
 
          mark_as_dependent(session, last_checked_context->select_lex,
 
3353
          mark_as_dependent(thd, last_checked_context->select_lex,
3369
3354
                            context->select_lex, this,
3370
3355
                            ((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ?
3371
3356
                             (Item_ident*) (*reference) :
3385
3370
    /* Search in SELECT and GROUP lists of the outer select. */
3386
3371
    if (place != IN_WHERE && place != IN_ON)
3387
3372
    {
3388
 
      if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
 
3373
      if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
3389
3374
        return -1; /* Some error occurred (e.g. ambiguous names). */
3390
3375
      if (ref != not_found_item)
3391
3376
      {
3413
3398
    if (upward_lookup)
3414
3399
    {
3415
3400
      // We can't say exactly what absent table or field
3416
 
      my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), session->where);
 
3401
      my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), thd->where);
3417
3402
    }
3418
3403
    else
3419
3404
    {
3420
3405
      /* Call find_field_in_tables only to report the error */
3421
 
      find_field_in_tables(session, this,
 
3406
      find_field_in_tables(thd, this,
3422
3407
                           context->first_name_resolution_table,
3423
3408
                           context->last_name_resolution_table,
3424
3409
                           reference, REPORT_ALL_ERRORS,
3457
3442
    if (place != IN_HAVING && select->group_list.elements)
3458
3443
    {
3459
3444
      outer_context->select_lex->inner_refs_list.push_back((Item_outer_ref*)rf);
3460
 
      ((Item_outer_ref*)rf)->in_sum_func= session->lex->in_sum_func;
 
3445
      ((Item_outer_ref*)rf)->in_sum_func= thd->lex->in_sum_func;
3461
3446
    }
3462
 
    session->change_item_tree(reference, rf);
 
3447
    thd->change_item_tree(reference, rf);
3463
3448
    /*
3464
3449
      rf is Item_ref => never substitute other items (in this case)
3465
3450
      during fix_fields() => we can use rf after fix_fields()
3466
3451
    */
3467
3452
    assert(!rf->fixed);                // Assured by Item_ref()
3468
 
    if (rf->fix_fields(session, reference) || rf->check_cols(1))
 
3453
    if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3469
3454
      return -1;
3470
3455
 
3471
 
    mark_as_dependent(session, last_checked_context->select_lex,
 
3456
    mark_as_dependent(thd, last_checked_context->select_lex,
3472
3457
                      context->select_lex, this,
3473
3458
                      rf);
3474
3459
    return 0;
3475
3460
  }
3476
3461
  else
3477
3462
  {
3478
 
    mark_as_dependent(session, last_checked_context->select_lex,
 
3463
    mark_as_dependent(thd, last_checked_context->select_lex,
3479
3464
                      context->select_lex,
3480
3465
                      this, (Item_ident*)*reference);
3481
3466
    if (last_checked_context->select_lex->having_fix_field)
3486
3471
                       (char*) cached_table->alias, (char*) field_name);
3487
3472
      if (!rf)
3488
3473
        return -1;
3489
 
      session->change_item_tree(reference, rf);
 
3474
      thd->change_item_tree(reference, rf);
3490
3475
      /*
3491
3476
        rf is Item_ref => never substitute other items (in this case)
3492
3477
        during fix_fields() => we can use rf after fix_fields()
3493
3478
      */
3494
3479
      assert(!rf->fixed);                // Assured by Item_ref()
3495
 
      if (rf->fix_fields(session, reference) || rf->check_cols(1))
 
3480
      if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3496
3481
        return -1;
3497
3482
      return 0;
3498
3483
    }
3536
3521
    Notice that compared to Item_ref::fix_fields, here we first search the FROM
3537
3522
    clause, and then we search the SELECT and GROUP BY clauses.
3538
3523
 
3539
 
  @param[in]     session        current thread
 
3524
  @param[in]     thd        current thread
3540
3525
  @param[in,out] reference  view column if this item was resolved to a
3541
3526
    view column
3542
3527
 
3546
3531
    false on success
3547
3532
*/
3548
3533
 
3549
 
bool Item_field::fix_fields(Session *session, Item **reference)
 
3534
bool Item_field::fix_fields(THD *thd, Item **reference)
3550
3535
{
3551
3536
  assert(fixed == 0);
3552
3537
  Field *from_field= (Field *)not_found_field;
3559
3544
      expression to 'reference', i.e. it substitute that expression instead
3560
3545
      of this Item_field
3561
3546
    */
3562
 
    if ((from_field= find_field_in_tables(session, this,
 
3547
    if ((from_field= find_field_in_tables(thd, this,
3563
3548
                                          context->first_name_resolution_table,
3564
3549
                                          context->last_name_resolution_table,
3565
3550
                                          reference,
3566
 
                                          session->lex->use_only_table_context ?
 
3551
                                          thd->lex->use_only_table_context ?
3567
3552
                                            REPORT_ALL_ERRORS : 
3568
3553
                                            IGNORE_EXCEPT_NON_UNIQUE,
3569
3554
                                          !any_privileges,
3572
3557
    {
3573
3558
      int ret;
3574
3559
      /* Look up in current select's item_list to find aliased fields */
3575
 
      if (session->lex->current_select->is_item_list_lookup)
 
3560
      if (thd->lex->current_select->is_item_list_lookup)
3576
3561
      {
3577
 
        uint32_t counter;
 
3562
        uint counter;
3578
3563
        enum_resolution_type resolution;
3579
 
        Item** res= find_item_in_list(this, session->lex->current_select->item_list,
 
3564
        Item** res= find_item_in_list(this, thd->lex->current_select->item_list,
3580
3565
                                      &counter, REPORT_EXCEPT_NOT_FOUND,
3581
3566
                                      &resolution);
3582
3567
        if (!res)
3600
3585
            {
3601
3586
              /* The column to which we link isn't valid. */
3602
3587
              my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name, 
3603
 
                       current_session->where);
 
3588
                       current_thd->where);
3604
3589
              return(1);
3605
3590
            }
3606
3591
 
3617
3602
            Item_ref *rf= new Item_ref(context, db_name,table_name,field_name);
3618
3603
            if (!rf)
3619
3604
              return 1;
3620
 
            session->change_item_tree(reference, rf);
 
3605
            thd->change_item_tree(reference, rf);
3621
3606
            /*
3622
3607
              Because Item_ref never substitutes itself with other items 
3623
3608
              in Item_ref::fix_fields(), we can safely use the original 
3624
3609
              pointer to it even after fix_fields()
3625
3610
             */
3626
 
            return rf->fix_fields(session, reference) ||  rf->check_cols(1);
 
3611
            return rf->fix_fields(thd, reference) ||  rf->check_cols(1);
3627
3612
          }
3628
3613
        }
3629
3614
      }
3630
 
      if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
 
3615
      if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3631
3616
        goto error;
3632
3617
      outer_fixed= true;
3633
3618
      if (!ret)
3641
3626
        cached_table->select_lex != context->select_lex)
3642
3627
    {
3643
3628
      int ret;
3644
 
      if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
 
3629
      if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3645
3630
        goto error;
3646
3631
      outer_fixed= 1;
3647
3632
      if (!ret)
3664
3649
      return false;
3665
3650
 
3666
3651
    set_field(from_field);
3667
 
    if (session->lex->in_sum_func &&
3668
 
        session->lex->in_sum_func->nest_level == 
3669
 
        session->lex->current_select->nest_level)
3670
 
      set_if_bigger(session->lex->in_sum_func->max_arg_level,
3671
 
                    session->lex->current_select->nest_level);
 
3652
    if (thd->lex->in_sum_func &&
 
3653
        thd->lex->in_sum_func->nest_level == 
 
3654
        thd->lex->current_select->nest_level)
 
3655
      set_if_bigger(thd->lex->in_sum_func->max_arg_level,
 
3656
                    thd->lex->current_select->nest_level);
3672
3657
  }
3673
 
  else if (session->mark_used_columns != MARK_COLUMNS_NONE)
 
3658
  else if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3674
3659
  {
3675
 
    Table *table= field->table;
 
3660
    TABLE *table= field->table;
3676
3661
    MY_BITMAP *current_bitmap, *other_bitmap;
3677
 
    if (session->mark_used_columns == MARK_COLUMNS_READ)
 
3662
    if (thd->mark_used_columns == MARK_COLUMNS_READ)
3678
3663
    {
3679
3664
      current_bitmap= table->read_set;
3680
3665
      other_bitmap=   table->write_set;
3701
3686
  return false;
3702
3687
 
3703
3688
error:
3704
 
  context->process_error(session);
 
3689
  context->process_error(thd);
3705
3690
  return true;
3706
3691
}
3707
3692
 
3794
3779
    false  otherwise
3795
3780
*/
3796
3781
 
3797
 
bool Item_field::subst_argument_checker(unsigned char **arg)
 
3782
bool Item_field::subst_argument_checker(uchar **arg)
3798
3783
{
3799
3784
  return (result_type() != STRING_RESULT) || (*arg);
3800
3785
}
3824
3809
    - pointer to the field item, otherwise.
3825
3810
*/
3826
3811
 
3827
 
Item *Item_field::equal_fields_propagator(unsigned char *arg)
 
3812
Item *Item_field::equal_fields_propagator(uchar *arg)
3828
3813
{
3829
3814
  if (no_const_subst)
3830
3815
    return this;
3857
3842
  See comments in Arg_comparator::set_compare_func() for details.
3858
3843
*/
3859
3844
 
3860
 
bool Item_field::set_no_const_sub(unsigned char *)
 
3845
bool Item_field::set_no_const_sub(uchar *arg __attribute__((unused)))
3861
3846
{
3862
3847
  if (field->charset() != &my_charset_bin)
3863
3848
    no_const_subst=1;
3890
3875
    - this - otherwise.
3891
3876
*/
3892
3877
 
3893
 
Item *Item_field::replace_equal_field(unsigned char *)
 
3878
Item *Item_field::replace_equal_field(uchar *arg __attribute__((unused)))
3894
3879
{
3895
3880
  if (item_equal)
3896
3881
  {
3926
3911
  tmp_field->type=              field_type_arg;
3927
3912
  tmp_field->length=max_length;
3928
3913
  tmp_field->decimals=decimals;
 
3914
  if (unsigned_flag)
 
3915
    tmp_field->flags |= UNSIGNED_FLAG;
3929
3916
}
3930
3917
 
3931
3918
void Item::make_field(Send_field *tmp_field)
3984
3971
  /* Check whether we got a well-formed string */
3985
3972
  const CHARSET_INFO * const cs= str->charset();
3986
3973
  int well_formed_error;
3987
 
  uint32_t wlen= cs->cset->well_formed_len(cs,
 
3974
  uint wlen= cs->cset->well_formed_len(cs,
3988
3975
                                       str->ptr(), str->ptr() + str->length(),
3989
3976
                                       str->length(), &well_formed_error);
3990
3977
  if (wlen < str->length())
3991
3978
  {
3992
 
    Session *session= current_session;
 
3979
    THD *thd= current_thd;
3993
3980
    char hexbuf[7];
3994
3981
    enum DRIZZLE_ERROR::enum_warning_level level;
3995
 
    uint32_t diff= str->length() - wlen;
 
3982
    uint diff= str->length() - wlen;
3996
3983
    set_if_smaller(diff, 3);
3997
3984
    octet2hex(hexbuf, str->ptr() + wlen, diff);
3998
3985
    if (send_error)
4006
3993
      null_value= 1;
4007
3994
      str= 0;
4008
3995
    }
4009
 
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
 
3996
    push_warning_printf(thd, level, ER_INVALID_CHARACTER_STRING,
4010
3997
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
4011
3998
  }
4012
3999
  return str;
4066
4053
  @param table          Table for which the field is created
4067
4054
*/
4068
4055
 
4069
 
Field *Item::make_string_field(Table *table)
 
4056
Field *Item::make_string_field(TABLE *table)
4070
4057
{
4071
4058
  Field *field;
4072
4059
  assert(collation.collation);
4095
4082
    \#    Created field
4096
4083
*/
4097
4084
 
4098
 
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
 
4085
Field *Item::tmp_table_field_from_field_type(TABLE *table, bool fixed_length __attribute__((unused)))
4099
4086
{
4100
4087
  /*
4101
4088
    The field functions defines a field to be not null if null_ptr is not 0
4102
4089
  */
4103
 
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
 
4090
  uchar *null_ptr= maybe_null ? (uchar*) "" : 0;
4104
4091
  Field *field;
4105
4092
 
4106
4093
  switch (field_type()) {
4107
4094
  case DRIZZLE_TYPE_NEWDECIMAL:
4108
 
    field= new Field_new_decimal((unsigned char*) 0, max_length, null_ptr, 0,
 
4095
    field= new Field_new_decimal((uchar*) 0, max_length, null_ptr, 0,
4109
4096
                                 Field::NONE, name, decimals, 0,
4110
4097
                                 unsigned_flag);
4111
4098
    break;
 
4099
  case DRIZZLE_TYPE_TINY:
 
4100
    field= new Field_tiny((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
 
4101
                          name, 0, unsigned_flag);
 
4102
    break;
 
4103
  case DRIZZLE_TYPE_SHORT:
 
4104
    field= new Field_short((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
 
4105
                           name, 0, unsigned_flag);
 
4106
    break;
4112
4107
  case DRIZZLE_TYPE_LONG:
4113
 
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
4108
    field= new Field_long((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4114
4109
                          name, 0, unsigned_flag);
4115
4110
    break;
4116
4111
  case DRIZZLE_TYPE_LONGLONG:
4117
 
    field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
4112
    field= new Field_int64_t((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4118
4113
                              name, 0, unsigned_flag);
4119
4114
    break;
4120
4115
  case DRIZZLE_TYPE_DOUBLE:
4121
 
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
4116
    field= new Field_double((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4122
4117
                            name, decimals, 0, unsigned_flag);
4123
4118
    break;
4124
4119
  case DRIZZLE_TYPE_NULL:
4125
 
    field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
 
4120
    field= new Field_null((uchar*) 0, max_length, Field::NONE,
4126
4121
                          name, &my_charset_bin);
4127
4122
    break;
4128
4123
  case DRIZZLE_TYPE_NEWDATE:
4142
4137
    assert(0);
4143
4138
    /* Fall through to make_string_field() */
4144
4139
  case DRIZZLE_TYPE_ENUM:
 
4140
  case DRIZZLE_TYPE_SET:
4145
4141
  case DRIZZLE_TYPE_VARCHAR:
4146
4142
    return make_string_field(table);
4147
4143
  case DRIZZLE_TYPE_BLOB:
4312
4308
}
4313
4309
 
4314
4310
 
4315
 
int Item_string::save_in_field(Field *field, bool)
 
4311
int Item_string::save_in_field(Field *field,
 
4312
                               bool no_conversions __attribute__((unused)))
4316
4313
{
4317
4314
  String *result;
4318
4315
  result=val_str(&str_value);
4327
4324
}
4328
4325
 
4329
4326
 
4330
 
int Item_int::save_in_field(Field *field, bool)
 
4327
int Item_int::save_in_field(Field *field,
 
4328
                            bool no_conversions __attribute__((unused)))
4331
4329
{
4332
4330
  int64_t nr=val_int();
4333
4331
  if (null_value)
4337
4335
}
4338
4336
 
4339
4337
 
4340
 
int Item_decimal::save_in_field(Field *field, bool)
 
4338
int Item_decimal::save_in_field(Field *field,
 
4339
                                bool no_conversions __attribute__((unused)))
4341
4340
{
4342
4341
  field->set_notnull();
4343
4342
  return field->store_decimal(&decimal_value);
4344
4343
}
4345
4344
 
4346
4345
 
4347
 
bool Item_int::eq(const Item *arg, bool) const
 
4346
bool Item_int::eq(const Item *arg,
 
4347
                  bool binary_cmp __attribute__((unused))) const
4348
4348
{
4349
4349
  /* No need to check for null value as basic constant can't be NULL */
4350
4350
  if (arg->basic_const_item() && arg->type() == type())
4380
4380
}
4381
4381
 
4382
4382
 
4383
 
static uint32_t nr_of_decimals(const char *str, const char *end)
 
4383
static uint nr_of_decimals(const char *str, const char *end)
4384
4384
{
4385
4385
  const char *decimal_point;
4386
4386
 
4408
4408
  value is not a true double value (overflow)
4409
4409
*/
4410
4410
 
4411
 
Item_float::Item_float(const char *str_arg, uint32_t length)
 
4411
Item_float::Item_float(const char *str_arg, uint length)
4412
4412
{
4413
4413
  int error;
4414
4414
  char *end_not_used;
4430
4430
}
4431
4431
 
4432
4432
 
4433
 
int Item_float::save_in_field(Field *field, bool)
 
4433
int Item_float::save_in_field(Field *field,
 
4434
                              bool no_conversions __attribute__((unused)))
4434
4435
{
4435
4436
  double nr= val_real();
4436
4437
  if (null_value)
4440
4441
}
4441
4442
 
4442
4443
 
4443
 
void Item_float::print(String *str, enum_query_type)
 
4444
void Item_float::print(String *str,
 
4445
                       enum_query_type query_type __attribute__((unused)))
4444
4446
{
4445
4447
  if (presentation)
4446
4448
  {
4460
4462
  In number context this is a int64_t value.
4461
4463
*/
4462
4464
 
4463
 
bool Item_float::eq(const Item *arg, bool) const
 
4465
bool Item_float::eq(const Item *arg,
 
4466
                    bool binary_cmp __attribute__((unused))) const
4464
4467
{
4465
4468
  if (arg->basic_const_item() && arg->type() == type())
4466
4469
  {
4475
4478
}
4476
4479
 
4477
4480
 
4478
 
inline uint32_t char_val(char X)
 
4481
inline uint char_val(char X)
4479
4482
{
4480
4483
  return (uint) (X >= '0' && X <= '9' ? X-'0' :
4481
4484
                 X >= 'A' && X <= 'Z' ? X-'A'+10 :
4483
4486
}
4484
4487
 
4485
4488
 
4486
 
Item_hex_string::Item_hex_string(const char *str, uint32_t str_length)
 
4489
Item_hex_string::Item_hex_string(const char *str, uint str_length)
4487
4490
{
4488
4491
  max_length=(str_length+1)/2;
4489
4492
  char *ptr=(char*) sql_alloc(max_length+1);
4509
4512
  // following assert is redundant, because fixed=1 assigned in constructor
4510
4513
  assert(fixed == 1);
4511
4514
  char *end=(char*) str_value.ptr()+str_value.length(),
4512
 
       *ptr=end-cmin(str_value.length(),(uint32_t)sizeof(int64_t));
 
4515
       *ptr=end-min(str_value.length(),sizeof(int64_t));
4513
4516
 
4514
4517
  uint64_t value=0;
4515
4518
  for (; ptr != end ; ptr++)
4516
 
    value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
 
4519
    value=(value << 8)+ (uint64_t) (uchar) *ptr;
4517
4520
  return (int64_t) value;
4518
4521
}
4519
4522
 
4528
4531
}
4529
4532
 
4530
4533
 
4531
 
int Item_hex_string::save_in_field(Field *field, bool)
 
4534
int Item_hex_string::save_in_field(Field *field,
 
4535
                                   bool no_conversions __attribute__((unused)))
4532
4536
{
4533
4537
  field->set_notnull();
4534
4538
  if (field->result_type() == STRING_RESULT)
4535
 
    return field->store(str_value.ptr(), str_value.length(),
 
4539
    return field->store(str_value.ptr(), str_value.length(), 
4536
4540
                        collation.collation);
4537
4541
 
4538
4542
  uint64_t nr;
4558
4562
}
4559
4563
 
4560
4564
 
4561
 
void Item_hex_string::print(String *str, enum_query_type)
 
4565
void Item_hex_string::print(String *str,
 
4566
                            enum_query_type query_type __attribute__((unused)))
4562
4567
{
4563
4568
  char *end= (char*) str_value.ptr() + str_value.length(),
4564
 
       *ptr= end - cmin(str_value.length(), (uint32_t)sizeof(int64_t));
 
4569
       *ptr= end - min(str_value.length(), sizeof(int64_t));
4565
4570
  str->append("0x");
4566
4571
  for (; ptr != end ; ptr++)
4567
4572
  {
4568
 
    str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
4569
 
    str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
 
4573
    str->append(_dig_vec_lower[((uchar) *ptr) >> 4]);
 
4574
    str->append(_dig_vec_lower[((uchar) *ptr) & 0x0F]);
4570
4575
  }
4571
4576
}
4572
4577
 
4602
4607
  In number context this is a int64_t value.
4603
4608
*/
4604
4609
  
4605
 
Item_bin_string::Item_bin_string(const char *str, uint32_t str_length)
 
4610
Item_bin_string::Item_bin_string(const char *str, uint str_length)
4606
4611
{
4607
4612
  const char *end= str + str_length - 1;
4608
 
  unsigned char bits= 0;
4609
 
  uint32_t power= 1;
 
4613
  uchar bits= 0;
 
4614
  uint power= 1;
4610
4615
 
4611
4616
  max_length= (str_length + 7) >> 3;
4612
4617
  char *ptr= (char*) sql_alloc(max_length + 1);
4638
4643
*/
4639
4644
 
4640
4645
bool Item_null::send(Protocol *protocol,
4641
 
                     String *)
 
4646
                     String *packet __attribute__((unused)))
4642
4647
{
4643
4648
  return protocol->store_null();
4644
4649
}
4656
4661
  default:
4657
4662
  case DRIZZLE_TYPE_NULL:
4658
4663
  case DRIZZLE_TYPE_ENUM:
 
4664
  case DRIZZLE_TYPE_SET:
4659
4665
  case DRIZZLE_TYPE_BLOB:
4660
4666
  case DRIZZLE_TYPE_VARCHAR:
4661
4667
  case DRIZZLE_TYPE_NEWDECIMAL:
4665
4671
      result= protocol->store(res->ptr(),res->length(),res->charset());
4666
4672
    break;
4667
4673
  }
 
4674
  case DRIZZLE_TYPE_TINY:
 
4675
  {
 
4676
    int64_t nr;
 
4677
    nr= val_int();
 
4678
    if (!null_value)
 
4679
      result= protocol->store_tiny(nr);
 
4680
    break;
 
4681
  }
 
4682
  case DRIZZLE_TYPE_SHORT:
 
4683
  {
 
4684
    int64_t nr;
 
4685
    nr= val_int();
 
4686
    if (!null_value)
 
4687
      result= protocol->store_short(nr);
 
4688
    break;
 
4689
  }
4668
4690
  case DRIZZLE_TYPE_LONG:
4669
4691
  {
4670
4692
    int64_t nr;
4717
4739
}
4718
4740
 
4719
4741
 
4720
 
bool Item_field::send(Protocol *protocol, String *)
 
4742
bool Item_field::send(Protocol *protocol,
 
4743
                      String *buffer __attribute__((unused)))
4721
4744
{
4722
4745
  return protocol->store(result_field);
4723
4746
}
4729
4752
    need to set no_errors to prevent warnings about type conversion
4730
4753
    popping up.
4731
4754
  */
4732
 
  Session *session= field->table->in_use;
 
4755
  THD *thd= field->table->in_use;
4733
4756
  int no_errors;
4734
4757
 
4735
 
  no_errors= session->no_errors;
4736
 
  session->no_errors= 1;
 
4758
  no_errors= thd->no_errors;
 
4759
  thd->no_errors= 1;
4737
4760
  Item::update_null_value();
4738
 
  session->no_errors= no_errors;
 
4761
  thd->no_errors= no_errors;
4739
4762
}
4740
4763
 
4741
4764
 
4761
4784
    this field    otherwise
4762
4785
*/
4763
4786
 
4764
 
Item *Item_field::update_value_transformer(unsigned char *select_arg)
 
4787
Item *Item_field::update_value_transformer(uchar *select_arg)
4765
4788
{
4766
4789
  SELECT_LEX *select= (SELECT_LEX*)select_arg;
4767
4790
  assert(fixed);
4768
4791
 
4769
 
  if (field->table != select->context.table_list->table)
 
4792
  if (field->table != select->context.table_list->table &&
 
4793
      type() != Item::TRIGGER_FIELD_ITEM)
4770
4794
  {
4771
4795
    List<Item> *all_fields= &select->join->all_fields;
4772
4796
    Item **ref_pointer_array= select->ref_pointer_array;
4803
4827
                   Item **item, const char *table_name_arg,
4804
4828
                   const char *field_name_arg,
4805
4829
                   bool alias_name_used_arg)
4806
 
  :Item_ident(context_arg, NULL, table_name_arg, field_name_arg),
 
4830
  :Item_ident(context_arg, NullS, table_name_arg, field_name_arg),
4807
4831
   result_field(0), ref(item)
4808
4832
{
4809
4833
  alias_name_used= alias_name_used_arg;
4863
4887
    Item_field::fix_fields, here we first search the SELECT and GROUP BY
4864
4888
    clauses, and then we search the FROM clause.
4865
4889
 
4866
 
  @param[in]     session        current thread
 
4890
  @param[in]     thd        current thread
4867
4891
  @param[in,out] reference  view column if this item was resolved to a
4868
4892
    view column
4869
4893
 
4879
4903
    false on success
4880
4904
*/
4881
4905
 
4882
 
bool Item_ref::fix_fields(Session *session, Item **reference)
 
4906
bool Item_ref::fix_fields(THD *thd, Item **reference)
4883
4907
{
4884
4908
  enum_parsing_place place= NO_MATTER;
4885
4909
  assert(fixed == 0);
4886
 
  SELECT_LEX *current_sel= session->lex->current_select;
 
4910
  SELECT_LEX *current_sel= thd->lex->current_select;
4887
4911
 
4888
4912
  if (!ref || ref == not_found_item)
4889
4913
  {
4890
 
    if (!(ref= resolve_ref_in_select_and_group(session, this,
 
4914
    if (!(ref= resolve_ref_in_select_and_group(thd, this,
4891
4915
                                               context->select_lex)))
4892
4916
      goto error;             /* Some error occurred (e.g. ambiguous names). */
4893
4917
 
4902
4926
      {
4903
4927
        /* The current reference cannot be resolved in this query. */
4904
4928
        my_error(ER_BAD_FIELD_ERROR,MYF(0),
4905
 
                 this->full_name(), current_session->where);
 
4929
                 this->full_name(), current_thd->where);
4906
4930
        goto error;
4907
4931
      }
4908
4932
 
4927
4951
        /* Search in the SELECT and GROUP lists of the outer select. */
4928
4952
        if (outer_context->resolve_in_select_list)
4929
4953
        {
4930
 
          if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
 
4954
          if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
4931
4955
            goto error; /* Some error occurred (e.g. ambiguous names). */
4932
4956
          if (ref != not_found_item)
4933
4957
          {
4964
4988
            field expression to 'reference', i.e. it substitute that
4965
4989
            expression instead of this Item_ref
4966
4990
          */
4967
 
          from_field= find_field_in_tables(session, this,
 
4991
          from_field= find_field_in_tables(thd, this,
4968
4992
                                           outer_context->
4969
4993
                                             first_name_resolution_table,
4970
4994
                                           outer_context->
4982
5006
            prev_subselect_item->const_item_cache&=
4983
5007
              (*reference)->const_item();
4984
5008
            assert((*reference)->type() == REF_ITEM);
4985
 
            mark_as_dependent(session, last_checked_context->select_lex,
 
5009
            mark_as_dependent(thd, last_checked_context->select_lex,
4986
5010
                              context->select_lex, this,
4987
5011
                              ((refer_type == REF_ITEM ||
4988
5012
                                refer_type == FIELD_ITEM) ?
5035
5059
        Item_field* fld;
5036
5060
        if (!(fld= new Item_field(from_field)))
5037
5061
          goto error;
5038
 
        session->change_item_tree(reference, fld);
5039
 
        mark_as_dependent(session, last_checked_context->select_lex,
5040
 
                          session->lex->current_select, this, fld);
 
5062
        thd->change_item_tree(reference, fld);
 
5063
        mark_as_dependent(thd, last_checked_context->select_lex,
 
5064
                          thd->lex->current_select, this, fld);
5041
5065
        /*
5042
5066
          A reference is resolved to a nest level that's outer or the same as
5043
5067
          the nest level of the enclosing set function : adjust the value of
5044
5068
          max_arg_level for the function if it's needed.
5045
5069
        */
5046
 
        if (session->lex->in_sum_func &&
5047
 
            session->lex->in_sum_func->nest_level >= 
 
5070
        if (thd->lex->in_sum_func &&
 
5071
            thd->lex->in_sum_func->nest_level >= 
5048
5072
            last_checked_context->select_lex->nest_level)
5049
 
          set_if_bigger(session->lex->in_sum_func->max_arg_level,
 
5073
          set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5050
5074
                        last_checked_context->select_lex->nest_level);
5051
5075
        return false;
5052
5076
      }
5054
5078
      {
5055
5079
        /* The item was not a table field and not a reference */
5056
5080
        my_error(ER_BAD_FIELD_ERROR, MYF(0),
5057
 
                 this->full_name(), current_session->where);
 
5081
                 this->full_name(), current_thd->where);
5058
5082
        goto error;
5059
5083
      }
5060
5084
      /* Should be checked in resolve_ref_in_select_and_group(). */
5061
5085
      assert(*ref && (*ref)->fixed);
5062
 
      mark_as_dependent(session, last_checked_context->select_lex,
 
5086
      mark_as_dependent(thd, last_checked_context->select_lex,
5063
5087
                        context->select_lex, this, this);
5064
5088
      /*
5065
5089
        A reference is resolved to a nest level that's outer or the same as
5066
5090
        the nest level of the enclosing set function : adjust the value of
5067
5091
        max_arg_level for the function if it's needed.
5068
5092
      */
5069
 
      if (session->lex->in_sum_func &&
5070
 
          session->lex->in_sum_func->nest_level >= 
 
5093
      if (thd->lex->in_sum_func &&
 
5094
          thd->lex->in_sum_func->nest_level >= 
5071
5095
          last_checked_context->select_lex->nest_level)
5072
 
        set_if_bigger(session->lex->in_sum_func->max_arg_level,
 
5096
        set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5073
5097
                      last_checked_context->select_lex->nest_level);
5074
5098
    }
5075
5099
  }
5102
5126
  return false;
5103
5127
 
5104
5128
error:
5105
 
  context->process_error(session);
 
5129
  context->process_error(thd);
5106
5130
  return true;
5107
5131
}
5108
5132
 
5144
5168
    if ((*ref)->type() != Item::CACHE_ITEM && ref_type() != VIEW_REF &&
5145
5169
        !table_name && name && alias_name_used)
5146
5170
    {
5147
 
      Session *session= current_session;
5148
 
      append_identifier(session, str, name, (uint) strlen(name));
 
5171
      THD *thd= current_thd;
 
5172
      append_identifier(thd, str, name, (uint) strlen(name));
5149
5173
    }
5150
5174
    else
5151
5175
      (*ref)->print(str, query_type);
5284
5308
}
5285
5309
 
5286
5310
 
5287
 
bool Item_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
5311
bool Item_ref::get_date(DRIZZLE_TIME *ltime,uint fuzzydate)
5288
5312
{
5289
5313
  return (null_value=(*ref)->get_date_result(ltime,fuzzydate));
5290
5314
}
5326
5350
}
5327
5351
 
5328
5352
 
5329
 
Item *Item_ref::get_tmp_table_item(Session *session)
 
5353
Item *Item_ref::get_tmp_table_item(THD *thd)
5330
5354
{
5331
5355
  if (!result_field)
5332
 
    return (*ref)->get_tmp_table_item(session);
 
5356
    return (*ref)->get_tmp_table_item(thd);
5333
5357
 
5334
5358
  Item_field *item= new Item_field(result_field);
5335
5359
  if (item)
5398
5422
}
5399
5423
 
5400
5424
 
5401
 
bool Item_direct_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
5425
bool Item_direct_ref::get_date(DRIZZLE_TIME *ltime,uint fuzzydate)
5402
5426
{
5403
5427
  return (null_value=(*ref)->get_date(ltime,fuzzydate));
5404
5428
}
5407
5431
/**
5408
5432
  Prepare referenced field then call usual Item_direct_ref::fix_fields .
5409
5433
 
5410
 
  @param session         thread handler
 
5434
  @param thd         thread handler
5411
5435
  @param reference   reference on reference where this item stored
5412
5436
 
5413
5437
  @retval
5416
5440
    true    Error
5417
5441
*/
5418
5442
 
5419
 
bool Item_direct_view_ref::fix_fields(Session *session, Item **reference)
 
5443
bool Item_direct_view_ref::fix_fields(THD *thd, Item **reference)
5420
5444
{
5421
5445
  /* view fild reference must be defined */
5422
5446
  assert(*ref);
5423
5447
  /* (*ref)->check_cols() will be made in Item_direct_ref::fix_fields */
5424
5448
  if (!(*ref)->fixed &&
5425
 
      ((*ref)->fix_fields(session, ref)))
 
5449
      ((*ref)->fix_fields(thd, ref)))
5426
5450
    return true;
5427
 
  return Item_direct_ref::fix_fields(session, reference);
 
5451
  return Item_direct_ref::fix_fields(thd, reference);
5428
5452
}
5429
5453
 
5430
5454
/*
5432
5456
 
5433
5457
  SYNOPSIS
5434
5458
    Item_outer_ref::fix_fields()
5435
 
    session         thread handler
 
5459
    thd         thread handler
5436
5460
    reference   reference on reference where this item stored
5437
5461
 
5438
5462
  RETURN
5440
5464
    true    Error
5441
5465
*/
5442
5466
 
5443
 
bool Item_outer_ref::fix_fields(Session *session, Item **reference)
 
5467
bool Item_outer_ref::fix_fields(THD *thd, Item **reference)
5444
5468
{
5445
5469
  bool err;
5446
5470
  /* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
5447
 
  if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(session, reference)))
 
5471
  if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(thd, reference)))
5448
5472
    return true;
5449
 
  err= Item_direct_ref::fix_fields(session, reference);
 
5473
  err= Item_direct_ref::fix_fields(thd, reference);
5450
5474
  if (!outer_ref)
5451
5475
    outer_ref= *ref;
5452
5476
  if ((*ref)->type() == Item::FIELD_ITEM)
5463
5487
  }
5464
5488
}
5465
5489
 
5466
 
void Item_ref::fix_after_pullout(st_select_lex *new_parent, Item **)
 
5490
void Item_ref::fix_after_pullout(st_select_lex *new_parent,
 
5491
                                 Item **refptr __attribute__((unused)))
5467
5492
{
5468
5493
  if (depended_from == new_parent)
5469
5494
  {
5489
5514
    false   otherwise
5490
5515
*/
5491
5516
 
5492
 
bool Item_direct_view_ref::eq(const Item *item, bool) const
 
5517
bool Item_direct_view_ref::eq(const Item *item,
 
5518
                              bool binary_cmp __attribute__((unused))) const
5493
5519
{
5494
5520
  if (item->type() == REF_ITEM)
5495
5521
  {
5510
5536
}
5511
5537
 
5512
5538
 
5513
 
bool Item_default_value::fix_fields(Session *session, Item **)
 
5539
bool Item_default_value::fix_fields(THD *thd,
 
5540
                                    Item **items __attribute__((unused)))
5514
5541
{
5515
5542
  Item *real_arg;
5516
5543
  Item_field *field_arg;
5522
5549
    fixed= 1;
5523
5550
    return false;
5524
5551
  }
5525
 
  if (!arg->fixed && arg->fix_fields(session, &arg))
 
5552
  if (!arg->fixed && arg->fix_fields(thd, &arg))
5526
5553
    goto error;
5527
5554
 
5528
5555
 
5549
5576
  return false;
5550
5577
 
5551
5578
error:
5552
 
  context->process_error(session);
 
5579
  context->process_error(thd);
5553
5580
  return true;
5554
5581
}
5555
5582
 
5601
5628
  same time it can replace some nodes in the tree.
5602
5629
*/ 
5603
5630
 
5604
 
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
 
5631
Item *Item_default_value::transform(Item_transformer transformer, uchar *args)
5605
5632
{
5606
5633
  Item *new_item= arg->transform(transformer, args);
5607
5634
  if (!new_item)
5608
5635
    return 0;
5609
5636
 
5610
5637
  /*
5611
 
    Session::change_item_tree() should be called only if the tree was
 
5638
    THD::change_item_tree() should be called only if the tree was
5612
5639
    really transformed, i.e. when a new item has been created.
5613
5640
    Otherwise we'll be allocating a lot of unnecessary memory for
5614
5641
    change records at each execution.
5615
5642
  */
5616
5643
  if (arg != new_item)
5617
 
    current_session->change_item_tree(&arg, new_item);
 
5644
    current_thd->change_item_tree(&arg, new_item);
5618
5645
  return (this->*transformer)(args);
5619
5646
}
5620
5647
 
5626
5653
}
5627
5654
 
5628
5655
 
5629
 
bool Item_insert_value::fix_fields(Session *session, Item **)
 
5656
bool Item_insert_value::fix_fields(THD *thd,
 
5657
                                   Item **items __attribute__((unused)))
5630
5658
{
5631
5659
  assert(fixed == 0);
5632
5660
  /* We should only check that arg is in first table */
5633
5661
  if (!arg->fixed)
5634
5662
  {
5635
5663
    bool res;
5636
 
    TableList *orig_next_table= context->last_name_resolution_table;
 
5664
    TABLE_LIST *orig_next_table= context->last_name_resolution_table;
5637
5665
    context->last_name_resolution_table= context->first_name_resolution_table;
5638
 
    res= arg->fix_fields(session, &arg);
 
5666
    res= arg->fix_fields(thd, &arg);
5639
5667
    context->last_name_resolution_table= orig_next_table;
5640
5668
    if (res)
5641
5669
      return true;
5708
5736
}
5709
5737
 
5710
5738
 
5711
 
void resolve_const_item(Session *session, Item **ref, Item *comp_item)
 
5739
void resolve_const_item(THD *thd, Item **ref, Item *comp_item)
5712
5740
{
5713
5741
  Item *item= *ref;
5714
5742
  Item *new_item= NULL;
5728
5756
      new_item= new Item_null(name);
5729
5757
    else
5730
5758
    {
5731
 
      uint32_t length= result->length();
 
5759
      uint length= result->length();
5732
5760
      char *tmp_str= sql_strmake(result->ptr(), length);
5733
5761
      new_item= new Item_string(name, tmp_str, length, result->charset());
5734
5762
    }
5737
5765
  case INT_RESULT:
5738
5766
  {
5739
5767
    int64_t result=item->val_int();
5740
 
    uint32_t length=item->max_length;
 
5768
    uint length=item->max_length;
5741
5769
    bool null_value=item->null_value;
5742
5770
    new_item= (null_value ? (Item*) new Item_null(name) :
5743
5771
               (Item*) new Item_int(name, result, length));
5756
5784
    */
5757
5785
    Item_row *item_row= (Item_row*) item;
5758
5786
    Item_row *comp_item_row= (Item_row*) comp_item;
5759
 
    uint32_t col;
 
5787
    uint col;
5760
5788
    new_item= 0;
5761
5789
    /*
5762
5790
      If item and comp_item are both Item_rows and have same number of cols
5768
5796
    assert(item_row->cols() == comp_item_row->cols());
5769
5797
    col= item_row->cols();
5770
5798
    while (col-- > 0)
5771
 
      resolve_const_item(session, item_row->addr(col),
 
5799
      resolve_const_item(thd, item_row->addr(col),
5772
5800
                         comp_item_row->element_index(col));
5773
5801
    break;
5774
5802
  }
5776
5804
  case REAL_RESULT:
5777
5805
  {                                             // It must REAL_RESULT
5778
5806
    double result= item->val_real();
5779
 
    uint32_t length=item->max_length,decimals=item->decimals;
 
5807
    uint length=item->max_length,decimals=item->decimals;
5780
5808
    bool null_value=item->null_value;
5781
5809
    new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
5782
5810
               new Item_float(name, result, decimals, length));
5786
5814
  {
5787
5815
    my_decimal decimal_value;
5788
5816
    my_decimal *result= item->val_decimal(&decimal_value);
5789
 
    uint32_t length= item->max_length, decimals= item->decimals;
 
5817
    uint length= item->max_length, decimals= item->decimals;
5790
5818
    bool null_value= item->null_value;
5791
5819
    new_item= (null_value ?
5792
5820
               (Item*) new Item_null(name) :
5797
5825
    assert(0);
5798
5826
  }
5799
5827
  if (new_item)
5800
 
    session->change_item_tree(ref, new_item);
 
5828
    thd->change_item_tree(ref, new_item);
5801
5829
}
5802
5830
 
5803
5831
/**
5969
5997
  return str;
5970
5998
}
5971
5999
 
5972
 
my_decimal *Item_cache_decimal::val_decimal(my_decimal *)
 
6000
my_decimal *Item_cache_decimal::val_decimal(my_decimal *val __attribute__((unused)))
5973
6001
{
5974
6002
  assert(fixed);
5975
6003
  return &decimal_value;
6039
6067
}
6040
6068
 
6041
6069
 
6042
 
bool Item_cache_row::allocate(uint32_t num)
 
6070
bool Item_cache_row::allocate(uint num)
6043
6071
{
6044
6072
  item_count= num;
6045
 
  Session *session= current_session;
 
6073
  THD *thd= current_thd;
6046
6074
  return (!(values= 
6047
 
            (Item_cache **) session->calloc(sizeof(Item_cache *)*item_count)));
 
6075
            (Item_cache **) thd->calloc(sizeof(Item_cache *)*item_count)));
6048
6076
}
6049
6077
 
6050
6078
 
6053
6081
  example= item;
6054
6082
  if (!values && allocate(item->cols()))
6055
6083
    return 1;
6056
 
  for (uint32_t i= 0; i < item_count; i++)
 
6084
  for (uint i= 0; i < item_count; i++)
6057
6085
  {
6058
6086
    Item *el= item->element_index(i);
6059
6087
    Item_cache *tmp;
6069
6097
{
6070
6098
  null_value= 0;
6071
6099
  item->bring_value();
6072
 
  for (uint32_t i= 0; i < item_count; i++)
 
6100
  for (uint i= 0; i < item_count; i++)
6073
6101
  {
6074
6102
    values[i]->store(item->element_index(i));
6075
6103
    null_value|= values[i]->null_value;
6077
6105
}
6078
6106
 
6079
6107
 
6080
 
void Item_cache_row::illegal_method_call(const char *)
 
6108
void Item_cache_row::illegal_method_call(const char *method __attribute__((unused)))
6081
6109
{
6082
6110
  assert(0);
6083
6111
  my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
6085
6113
}
6086
6114
 
6087
6115
 
6088
 
bool Item_cache_row::check_cols(uint32_t c)
 
6116
bool Item_cache_row::check_cols(uint c)
6089
6117
{
6090
6118
  if (c != item_count)
6091
6119
  {
6098
6126
 
6099
6127
bool Item_cache_row::null_inside()
6100
6128
{
6101
 
  for (uint32_t i= 0; i < item_count; i++)
 
6129
  for (uint i= 0; i < item_count; i++)
6102
6130
  {
6103
6131
    if (values[i]->cols() > 1)
6104
6132
    {
6118
6146
 
6119
6147
void Item_cache_row::bring_value()
6120
6148
{
6121
 
  for (uint32_t i= 0; i < item_count; i++)
 
6149
  for (uint i= 0; i < item_count; i++)
6122
6150
    values[i]->bring_value();
6123
6151
  return;
6124
6152
}
6125
6153
 
6126
6154
 
6127
 
Item_type_holder::Item_type_holder(Session *session, Item *item)
6128
 
  :Item(session, item), enum_set_typelib(0), fld_type(get_real_type(item))
 
6155
Item_type_holder::Item_type_holder(THD *thd, Item *item)
 
6156
  :Item(thd, item), enum_set_typelib(0), fld_type(get_real_type(item))
6129
6157
{
6130
6158
  assert(item->fixed);
6131
6159
  maybe_null= item->maybe_null;
6220
6248
  Find field type which can carry current Item_type_holder type and
6221
6249
  type of given Item.
6222
6250
 
6223
 
  @param session     thread handler
 
6251
  @param thd     thread handler
6224
6252
  @param item    given item to join its parameters with this item ones
6225
6253
 
6226
6254
  @retval
6229
6257
    false  OK
6230
6258
*/
6231
6259
 
6232
 
bool Item_type_holder::join_types(Session *, Item *item)
 
6260
bool Item_type_holder::join_types(THD *thd __attribute__((unused)),
 
6261
                                  Item *item)
6233
6262
{
6234
 
  uint32_t max_length_orig= max_length;
6235
 
  uint32_t decimals_orig= decimals;
 
6263
  uint max_length_orig= max_length;
 
6264
  uint decimals_orig= decimals;
6236
6265
  fld_type= Field::field_type_merge(fld_type, get_real_type(item));
6237
6266
  {
6238
6267
    int item_decimals= item->decimals;
6239
6268
    /* fix variable decimals which always is NOT_FIXED_DEC */
6240
6269
    if (Field::result_merge_type(fld_type) == INT_RESULT)
6241
6270
      item_decimals= 0;
6242
 
    decimals= cmax((int)decimals, item_decimals);
 
6271
    decimals= max(decimals, item_decimals);
6243
6272
  }
6244
6273
  if (Field::result_merge_type(fld_type) == DECIMAL_RESULT)
6245
6274
  {
6246
 
    decimals= cmin((int)cmax(decimals, item->decimals), DECIMAL_MAX_SCALE);
6247
 
    int precision= cmin(cmax(prev_decimal_int_part, item->decimal_int_part())
 
6275
    decimals= min(max(decimals, item->decimals), DECIMAL_MAX_SCALE);
 
6276
    int precision= min(max(prev_decimal_int_part, item->decimal_int_part())
6248
6277
                       + decimals, DECIMAL_MAX_PRECISION);
6249
6278
    unsigned_flag&= item->unsigned_flag;
6250
6279
    max_length= my_decimal_precision_to_length(precision, decimals,
6275
6304
     */
6276
6305
    if (collation.collation != &my_charset_bin)
6277
6306
    {
6278
 
      max_length= cmax(old_max_chars * collation.collation->mbmaxlen,
 
6307
      max_length= max(old_max_chars * collation.collation->mbmaxlen,
6279
6308
                      display_length(item) /
6280
6309
                      item->collation.collation->mbmaxlen *
6281
6310
                      collation.collation->mbmaxlen);
6290
6319
    {
6291
6320
      int delta1= max_length_orig - decimals_orig;
6292
6321
      int delta2= item->max_length - item->decimals;
6293
 
      max_length= cmax(delta1, delta2) + decimals;
 
6322
      max_length= max(delta1, delta2) + decimals;
6294
6323
      if (fld_type == DRIZZLE_TYPE_DOUBLE && max_length > DBL_DIG + 2) 
6295
6324
      {
6296
6325
        max_length= DBL_DIG + 7;
6302
6331
    break;
6303
6332
  }
6304
6333
  default:
6305
 
    max_length= cmax(max_length, display_length(item));
 
6334
    max_length= max(max_length, display_length(item));
6306
6335
  };
6307
6336
  maybe_null|= item->maybe_null;
6308
6337
  get_full_info(item);
6335
6364
  case DRIZZLE_TYPE_VARCHAR:
6336
6365
  case DRIZZLE_TYPE_NEWDECIMAL:
6337
6366
  case DRIZZLE_TYPE_ENUM:
 
6367
  case DRIZZLE_TYPE_SET:
6338
6368
  case DRIZZLE_TYPE_BLOB:
 
6369
  case DRIZZLE_TYPE_TINY:
6339
6370
    return 4;
 
6371
  case DRIZZLE_TYPE_SHORT:
 
6372
    return 6;
6340
6373
  case DRIZZLE_TYPE_LONG:
6341
6374
    return MY_INT32_NUM_DECIMAL_DIGITS;
6342
6375
  case DRIZZLE_TYPE_DOUBLE:
6362
6395
    created field
6363
6396
*/
6364
6397
 
6365
 
Field *Item_type_holder::make_field_by_type(Table *table)
 
6398
Field *Item_type_holder::make_field_by_type(TABLE *table)
6366
6399
{
6367
6400
  /*
6368
6401
    The field functions defines a field to be not null if null_ptr is not 0
6369
6402
  */
6370
 
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
 
6403
  uchar *null_ptr= maybe_null ? (uchar*) "" : 0;
6371
6404
  Field *field;
6372
6405
 
6373
6406
  switch (fld_type) {
6374
6407
  case DRIZZLE_TYPE_ENUM:
6375
6408
    assert(enum_set_typelib);
6376
 
    field= new Field_enum((unsigned char *) 0, max_length, null_ptr, 0,
 
6409
    field= new Field_enum((uchar *) 0, max_length, null_ptr, 0,
6377
6410
                          Field::NONE, name,
6378
6411
                          get_enum_pack_length(enum_set_typelib->count),
6379
6412
                          enum_set_typelib, collation.collation);
6380
6413
    if (field)
6381
6414
      field->init(table);
6382
6415
    return field;
 
6416
  case DRIZZLE_TYPE_SET:
 
6417
    assert(enum_set_typelib);
 
6418
    field= new Field_set((uchar *) 0, max_length, null_ptr, 0,
 
6419
                         Field::NONE, name,
 
6420
                         get_set_pack_length(enum_set_typelib->count),
 
6421
                         enum_set_typelib, collation.collation);
 
6422
    if (field)
 
6423
      field->init(table);
 
6424
    return field;
6383
6425
  case DRIZZLE_TYPE_NULL:
6384
6426
    return make_string_field(table);
6385
6427
  default:
6397
6439
*/
6398
6440
void Item_type_holder::get_full_info(Item *item)
6399
6441
{
6400
 
  if (fld_type == DRIZZLE_TYPE_ENUM)
 
6442
  if (fld_type == DRIZZLE_TYPE_ENUM ||
 
6443
      fld_type == DRIZZLE_TYPE_SET)
6401
6444
  {
6402
6445
    if (item->type() == Item::SUM_FUNC_ITEM &&
6403
6446
        (((Item_sum*)item)->sum_func() == Item_sum::MAX_FUNC ||
6411
6454
                 get_real_type(item) == DRIZZLE_TYPE_NULL) ||
6412
6455
                (!enum_set_typelib &&
6413
6456
                 item->type() == Item::FIELD_ITEM &&
6414
 
                 (get_real_type(item) == DRIZZLE_TYPE_ENUM) &&
 
6457
                 (get_real_type(item) == DRIZZLE_TYPE_ENUM ||
 
6458
                  get_real_type(item) == DRIZZLE_TYPE_SET) &&
6415
6459
                 ((Field_enum*)((Item_field *) item)->field)->typelib));
6416
6460
    if (!enum_set_typelib)
6417
6461
    {
6460
6504
    do nothing
6461
6505
*/
6462
6506
 
6463
 
void dummy_error_processor(Session *, void *)
 
6507
void dummy_error_processor(THD *thd __attribute__((unused)),
 
6508
                           void *data __attribute__((unused)))
6464
6509
{}
6465
6510
 
6466
6511
/**