~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_cmpfunc.cc

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
  This file defines all compare functions
22
22
*/
23
23
 
24
 
#ifdef USE_PRAGMA_IMPLEMENTATION
25
 
#pragma implementation                          // gcc: Class implementation
26
 
#endif
27
 
 
28
 
#include "mysql_priv.h"
29
 
#include <m_ctype.h>
30
 
#include "sql_select.h"
 
24
#include <drizzled/server_includes.h>
 
25
#include <drizzled/sql_select.h>
31
26
 
32
27
static bool convert_constant_item(THD *, Item_field *, Item **);
33
28
 
34
29
static Item_result item_store_type(Item_result a, Item *item,
35
 
                                   my_bool unsigned_flag)
 
30
                                   bool unsigned_flag)
36
31
{
37
32
  Item_result b= item->result_type();
38
33
 
47
42
    return INT_RESULT;
48
43
}
49
44
 
50
 
static void agg_result_type(Item_result *type, Item **items, uint nitems)
 
45
static void agg_result_type(Item_result *type, Item **items, uint32_t nitems)
51
46
{
52
47
  Item **item, **item_end;
53
 
  my_bool unsigned_flag= 0;
 
48
  bool unsigned_flag= 0;
54
49
 
55
50
  *type= STRING_RESULT;
56
51
  /* Skip beginning NULL items */
95
90
 
96
91
static int cmp_row_type(Item* item1, Item* item2)
97
92
{
98
 
  uint n= item1->cols();
 
93
  uint32_t n= item1->cols();
99
94
  if (item2->check_cols(n))
100
95
    return 1;
101
 
  for (uint i=0; i<n; i++)
 
96
  for (uint32_t i=0; i<n; i++)
102
97
  {
103
98
    if (item2->element_index(i)->check_cols(item1->element_index(i)->cols()) ||
104
99
        (item1->element_index(i)->result_type() == ROW_RESULT &&
132
127
    0  otherwise
133
128
*/
134
129
 
135
 
static int agg_cmp_type(Item_result *type, Item **items, uint nitems)
 
130
static int agg_cmp_type(Item_result *type, Item **items, uint32_t nitems)
136
131
{
137
 
  uint i;
 
132
  uint32_t i;
138
133
  type[0]= items[0]->result_type();
139
134
  for (i= 1 ; i < nitems ; i++)
140
135
  {
171
166
  @return aggregated field type.
172
167
*/
173
168
 
174
 
enum_field_types agg_field_type(Item **items, uint nitems)
 
169
enum_field_types agg_field_type(Item **items, uint32_t nitems)
175
170
{
176
 
  uint i;
 
171
  uint32_t i;
177
172
  if (!nitems || items[0]->result_type() == ROW_RESULT )
178
173
    return (enum_field_types)-1;
179
174
  enum_field_types res= items[0]->field_type();
199
194
    Bitmap of collected types - otherwise
200
195
*/
201
196
 
202
 
static uint collect_cmp_types(Item **items, uint nitems)
 
197
static uint32_t collect_cmp_types(Item **items, uint32_t nitems)
203
198
{
204
 
  uint i;
205
 
  uint found_types;
 
199
  uint32_t i;
 
200
  uint32_t found_types;
206
201
  Item_result left_result= items[0]->result_type();
207
 
  DBUG_ASSERT(nitems > 1);
 
202
  assert(nitems > 1);
208
203
  found_types= 0;
209
204
  for (i= 1; i < nitems ; i++)
210
205
  {
269
264
  NULL if some arg is NULL.
270
265
*/
271
266
 
272
 
longlong Item_func_not::val_int()
 
267
int64_t Item_func_not::val_int()
273
268
{
274
 
  DBUG_ASSERT(fixed == 1);
 
269
  assert(fixed == 1);
275
270
  bool value= args[0]->val_bool();
276
271
  null_value=args[0]->null_value;
277
272
  return ((!null_value && value == 0) ? 1 : 0);
298
293
*/
299
294
 
300
295
 
301
 
longlong Item_func_not_all::val_int()
 
296
int64_t Item_func_not_all::val_int()
302
297
{
303
 
  DBUG_ASSERT(fixed == 1);
 
298
  assert(fixed == 1);
304
299
  bool value= args[0]->val_bool();
305
300
 
306
301
  /*
339
334
    returns some rows it return same value as argument (true/false).
340
335
*/
341
336
 
342
 
longlong Item_func_nop_all::val_int()
 
337
int64_t Item_func_nop_all::val_int()
343
338
{
344
 
  DBUG_ASSERT(fixed == 1);
345
 
  longlong value= args[0]->val_int();
 
339
  assert(fixed == 1);
 
340
  int64_t value= args[0]->val_int();
346
341
 
347
342
  /*
348
343
    return false if there was records in underlying select in max/min
391
386
 
392
387
  if (!(*item)->with_subselect && (*item)->const_item())
393
388
  {
394
 
    TABLE *table= field->table;
395
389
    ulong orig_sql_mode= thd->variables.sql_mode;
396
390
    enum_check_fields orig_count_cuted_fields= thd->count_cuted_fields;
397
 
    my_bitmap_map *old_write_map;
398
 
    my_bitmap_map *old_read_map;
399
 
    ulonglong orig_field_val= 0; /* original field value if valid */
 
391
    uint64_t orig_field_val= 0; /* original field value if valid */
400
392
 
401
 
    if (table)
402
 
    {
403
 
      old_write_map= dbug_tmp_use_all_columns(table, table->write_set);
404
 
      old_read_map= dbug_tmp_use_all_columns(table, table->read_set);
405
 
    }
406
393
    /* For comparison purposes allow invalid dates like 2000-01-32 */
407
394
    thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) | 
408
395
                             MODE_INVALID_DATES;
427
414
    {
428
415
      result= field->store(orig_field_val, true);
429
416
      /* orig_field_val must be a valid value that can be restored back. */
430
 
      DBUG_ASSERT(!result);
 
417
      assert(!result);
431
418
    }
432
419
    thd->variables.sql_mode= orig_sql_mode;
433
420
    thd->count_cuted_fields= orig_count_cuted_fields;
434
 
    if (table)
435
 
    {
436
 
      dbug_tmp_restore_column_map(table->write_set, old_write_map);
437
 
      dbug_tmp_restore_column_map(table->read_set, old_read_map);
438
 
    }
439
421
  }
440
422
  return result;
441
423
}
483
465
  }
484
466
 
485
467
  thd= current_thd;
486
 
  if (!thd->is_context_analysis_only())
 
468
 
 
469
  if (args[0]->real_item()->type() == FIELD_ITEM)
487
470
  {
488
 
    if (args[0]->real_item()->type() == FIELD_ITEM)
 
471
    Item_field *field_item= (Item_field*) (args[0]->real_item());
 
472
    if (field_item->field->can_be_compared_as_int64_t() &&
 
473
        !(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
489
474
    {
490
 
      Item_field *field_item= (Item_field*) (args[0]->real_item());
491
 
      if (field_item->field->can_be_compared_as_longlong() &&
492
 
          !(field_item->is_datetime() &&
493
 
            args[1]->result_type() == STRING_RESULT))
 
475
      if (convert_constant_item(thd, field_item, &args[1]))
494
476
      {
495
 
        if (convert_constant_item(thd, field_item, &args[1]))
496
 
        {
497
 
          cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
498
 
                           INT_RESULT);         // Works for all types.
499
 
          args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
500
 
          return;
501
 
        }
 
477
        cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
 
478
                         INT_RESULT);           // Works for all types.
 
479
        args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
 
480
        return;
502
481
      }
503
482
    }
 
483
 
504
484
    if (args[1]->real_item()->type() == FIELD_ITEM)
505
485
    {
506
486
      Item_field *field_item= (Item_field*) (args[1]->real_item());
507
 
      if (field_item->field->can_be_compared_as_longlong() &&
 
487
      if (field_item->field->can_be_compared_as_int64_t() &&
508
488
          !(field_item->is_datetime() &&
509
489
            args[0]->result_type() == STRING_RESULT))
510
490
      {
530
510
  switch (type) {
531
511
  case ROW_RESULT:
532
512
  {
533
 
    uint n= (*a)->cols();
 
513
    uint32_t n= (*a)->cols();
534
514
    if (n != (*b)->cols())
535
515
    {
536
516
      my_error(ER_OPERAND_COLUMNS, MYF(0), n);
539
519
    }
540
520
    if (!(comparators= new Arg_comparator[n]))
541
521
      return 1;
542
 
    for (uint i=0; i < n; i++)
 
522
    for (uint32_t i=0; i < n; i++)
543
523
    {
544
524
      if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
545
525
      {
581
561
        which would be transformed to:
582
562
        WHERE col= 'j'
583
563
      */
584
 
      (*a)->walk(&Item::set_no_const_sub, false, (uchar*) 0);
585
 
      (*b)->walk(&Item::set_no_const_sub, false, (uchar*) 0);
 
564
      (*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
 
565
      (*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
586
566
    }
587
567
    break;
588
568
  }
610
590
  {
611
591
    if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
612
592
    {
613
 
      precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
 
593
      precision= 5 / log_10[cmax((*a)->decimals, (*b)->decimals) + 1];
614
594
      if (func == &Arg_comparator::compare_real)
615
595
        func= &Arg_comparator::compare_real_fixed;
616
596
      else if (func == &Arg_comparator::compare_e_real)
619
599
    break;
620
600
  }
621
601
  default:
622
 
    DBUG_ASSERT(0);
 
602
    assert(0);
623
603
  }
624
604
  return 0;
625
605
}
647
627
    converted value. 0 on error and on zero-dates -- check 'failure'
648
628
*/
649
629
 
650
 
static ulonglong
651
 
get_date_from_str(THD *thd, String *str, timestamp_type warn_type,
 
630
static uint64_t
 
631
get_date_from_str(THD *thd, String *str, enum enum_drizzle_timestamp_type warn_type,
652
632
                  char *warn_name, bool *error_arg)
653
633
{
654
 
  ulonglong value= 0;
 
634
  uint64_t value= 0;
655
635
  int error;
656
 
  MYSQL_TIME l_time;
657
 
  enum_mysql_timestamp_type ret;
 
636
  DRIZZLE_TIME l_time;
 
637
  enum enum_drizzle_timestamp_type ret;
658
638
 
659
639
  ret= str_to_datetime(str->ptr(), str->length(), &l_time,
660
640
                       (TIME_FUZZY_DATE | MODE_INVALID_DATES |
661
 
                        (thd->variables.sql_mode &
662
 
                         (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE))),
 
641
                        (thd->variables.sql_mode & MODE_NO_ZERO_DATE)),
663
642
                       &error);
664
643
 
665
 
  if (ret == MYSQL_TIMESTAMP_DATETIME || ret == MYSQL_TIMESTAMP_DATE)
 
644
  if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
666
645
  {
667
646
    /*
668
647
      Do not return yet, we may still want to throw a "trailing garbage"
669
648
      warning.
670
649
    */
671
650
    *error_arg= false;
672
 
    value= TIME_to_ulonglong_datetime(&l_time);
 
651
    value= TIME_to_uint64_t_datetime(&l_time);
673
652
  }
674
653
  else
675
654
  {
678
657
  }
679
658
 
680
659
  if (error > 0)
681
 
    make_truncated_value_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
660
    make_truncated_value_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
682
661
                                 str->ptr(), str->length(),
683
662
                                 warn_type, warn_name);
684
663
 
719
698
*/
720
699
 
721
700
enum Arg_comparator::enum_date_cmp_type
722
 
Arg_comparator::can_compare_as_dates(Item *a, Item *b, ulonglong *const_value)
 
701
Arg_comparator::can_compare_as_dates(Item *a, Item *b, uint64_t *const_value)
723
702
{
724
703
  enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
725
704
  Item *str_arg= 0, *date_arg= 0;
756
735
        ((Item_func*)str_arg)->functype() != Item_func::GUSERVAR_FUNC))
757
736
    {
758
737
      THD *thd= current_thd;
759
 
      ulonglong value;
 
738
      uint64_t value;
760
739
      bool error;
761
740
      String tmp, *str_val= 0;
762
 
      timestamp_type t_type= (date_arg->field_type() == MYSQL_TYPE_DATE ?
763
 
                              MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME);
 
741
      enum enum_drizzle_timestamp_type t_type= (date_arg->field_type() == DRIZZLE_TYPE_NEWDATE ?
 
742
                              DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME);
764
743
 
765
744
      str_val= str_arg->val_str(&tmp);
766
745
      if (str_arg->null_value)
790
769
  DESCRIPTION
791
770
    Retrieves the correct TIME value from given item for comparison by the
792
771
    compare_datetime() function.
793
 
    If item's result can be compared as longlong then its int value is used
 
772
    If item's result can be compared as int64_t then its int value is used
794
773
    and a value returned by get_time function is used otherwise.
795
774
    If an item is a constant one then its value is cached and it isn't
796
775
    get parsed again. An Item_cache_int object is used for for cached values.
801
780
    obtained value
802
781
*/
803
782
 
804
 
ulonglong
805
 
get_time_value(THD *thd, Item ***item_arg, Item **cache_arg,
806
 
               Item *warn_item, bool *is_null)
 
783
uint64_t
 
784
get_time_value(THD *thd __attribute__((unused)),
 
785
               Item ***item_arg, Item **cache_arg,
 
786
               Item *warn_item __attribute__((unused)),
 
787
               bool *is_null)
807
788
{
808
 
  ulonglong value;
 
789
  uint64_t value;
809
790
  Item *item= **item_arg;
810
 
  MYSQL_TIME ltime;
 
791
  DRIZZLE_TIME ltime;
811
792
 
812
 
  if (item->result_as_longlong())
 
793
  if (item->result_as_int64_t())
813
794
  {
814
795
    value= item->val_int();
815
796
    *is_null= item->null_value;
817
798
  else
818
799
  {
819
800
    *is_null= item->get_time(&ltime);
820
 
    value= !*is_null ? TIME_to_ulonglong_datetime(&ltime) : 0;
 
801
    value= !*is_null ? TIME_to_uint64_t_datetime(&ltime) : 0;
821
802
  }
822
803
  /*
823
804
    Do not cache GET_USER_VAR() function as its const_item() may return true
842
823
                                        Item_result type)
843
824
{
844
825
  enum enum_date_cmp_type cmp_type;
845
 
  ulonglong const_value= (ulonglong)-1;
 
826
  uint64_t const_value= (uint64_t)-1;
846
827
  a= a1;
847
828
  b= a2;
848
829
 
855
836
    a_cache= 0;
856
837
    b_cache= 0;
857
838
 
858
 
    if (const_value != (ulonglong)-1)
 
839
    if (const_value != (uint64_t)-1)
859
840
    {
860
841
      Item_cache_int *cache= new Item_cache_int();
861
842
      /* Mark the cache as non-const to prevent re-caching. */
878
859
    get_value_func= &get_datetime_value;
879
860
    return 0;
880
861
  }
881
 
  else if (type == STRING_RESULT && (*a)->field_type() == MYSQL_TYPE_TIME &&
882
 
           (*b)->field_type() == MYSQL_TYPE_TIME)
 
862
  else if (type == STRING_RESULT && (*a)->field_type() == DRIZZLE_TYPE_TIME &&
 
863
           (*b)->field_type() == DRIZZLE_TYPE_TIME)
883
864
  {
884
865
    /* Compare TIME values as integers. */
885
866
    thd= current_thd;
927
908
  DESCRIPTION
928
909
    Retrieves the correct DATETIME value from given item for comparison by the
929
910
    compare_datetime() function.
930
 
    If item's result can be compared as longlong then its int value is used
 
911
    If item's result can be compared as int64_t then its int value is used
931
912
    and its string value is used otherwise. Strings are always parsed and
932
913
    converted to int values by the get_date_from_str() function.
933
914
    This allows us to compare correctly string dates with missed insignificant
942
923
    obtained value
943
924
*/
944
925
 
945
 
ulonglong
 
926
uint64_t
946
927
get_datetime_value(THD *thd, Item ***item_arg, Item **cache_arg,
947
928
                   Item *warn_item, bool *is_null)
948
929
{
949
 
  ulonglong value= 0;
 
930
  uint64_t value= 0;
950
931
  String buf, *str= 0;
951
932
  Item *item= **item_arg;
952
933
 
953
 
  if (item->result_as_longlong())
 
934
  if (item->result_as_int64_t())
954
935
  {
955
936
    value= item->val_int();
956
937
    *is_null= item->null_value;
957
938
    enum_field_types f_type= item->field_type();
958
939
    /*
959
 
      Item_date_add_interval may return MYSQL_TYPE_STRING as the result
 
940
      Item_date_add_interval may return DRIZZLE_TYPE_STRING as the result
960
941
      field type. To detect that the DATE value has been returned we
961
942
      compare it with 100000000L - any DATE value should be less than it.
962
943
      Don't shift cached DATETIME values up for the second time.
963
944
    */
964
 
    if (f_type == MYSQL_TYPE_DATE ||
965
 
        (f_type != MYSQL_TYPE_DATETIME && value < 100000000L))
 
945
    if (f_type == DRIZZLE_TYPE_NEWDATE ||
 
946
        (f_type != DRIZZLE_TYPE_DATETIME && value < 100000000L))
966
947
      value*= 1000000L;
967
948
  }
968
949
  else
971
952
    *is_null= item->null_value;
972
953
  }
973
954
  if (*is_null)
974
 
    return ~(ulonglong) 0;
 
955
    return ~(uint64_t) 0;
975
956
  /*
976
957
    Convert strings to the integer DATE/DATETIME representation.
977
958
    Even if both dates provided in strings we can't compare them directly as
982
963
  {
983
964
    bool error;
984
965
    enum_field_types f_type= warn_item->field_type();
985
 
    timestamp_type t_type= f_type ==
986
 
      MYSQL_TYPE_DATE ? MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME;
 
966
    enum enum_drizzle_timestamp_type t_type= f_type ==
 
967
      DRIZZLE_TYPE_NEWDATE ? DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME;
987
968
    value= get_date_from_str(thd, str, t_type, warn_item->name, &error);
988
969
    /*
989
970
      If str did not contain a valid date according to the current
999
980
  if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
1000
981
      ((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
1001
982
  {
1002
 
    Item_cache_int *cache= new Item_cache_int(MYSQL_TYPE_DATETIME);
 
983
    Item_cache_int *cache= new Item_cache_int(DRIZZLE_TYPE_DATETIME);
1003
984
    /* Mark the cache as non-const to prevent re-caching. */
1004
985
    cache->set_used_tables(1);
1005
986
    cache->store(item, value);
1033
1014
int Arg_comparator::compare_datetime()
1034
1015
{
1035
1016
  bool is_null= false;
1036
 
  ulonglong a_value, b_value;
 
1017
  uint64_t a_value, b_value;
1037
1018
 
1038
1019
  /* Get DATE/DATETIME/TIME value of the 'a' item. */
1039
1020
  a_value= (*get_value_func)(thd, &a, &a_cache, *b, &is_null);
1098
1079
    if ((res2= (*b)->val_str(&owner->tmp_value2)))
1099
1080
    {
1100
1081
      owner->null_value= 0;
1101
 
      uint res1_length= res1->length();
1102
 
      uint res2_length= res2->length();
1103
 
      int cmp= memcmp(res1->ptr(), res2->ptr(), min(res1_length,res2_length));
 
1082
      uint32_t res1_length= res1->length();
 
1083
      uint32_t res2_length= res2->length();
 
1084
      int cmp= memcmp(res1->ptr(), res2->ptr(), cmin(res1_length,res2_length));
1104
1085
      return cmp ? cmp : (int) (res1_length - res2_length);
1105
1086
    }
1106
1087
  }
1237
1218
 
1238
1219
int Arg_comparator::compare_int_signed()
1239
1220
{
1240
 
  longlong val1= (*a)->val_int();
 
1221
  int64_t val1= (*a)->val_int();
1241
1222
  if (!(*a)->null_value)
1242
1223
  {
1243
 
    longlong val2= (*b)->val_int();
 
1224
    int64_t val2= (*b)->val_int();
1244
1225
    if (!(*b)->null_value)
1245
1226
    {
1246
1227
      owner->null_value= 0;
1260
1241
 
1261
1242
int Arg_comparator::compare_int_unsigned()
1262
1243
{
1263
 
  ulonglong val1= (*a)->val_int();
 
1244
  uint64_t val1= (*a)->val_int();
1264
1245
  if (!(*a)->null_value)
1265
1246
  {
1266
 
    ulonglong val2= (*b)->val_int();
 
1247
    uint64_t val2= (*b)->val_int();
1267
1248
    if (!(*b)->null_value)
1268
1249
    {
1269
1250
      owner->null_value= 0;
1283
1264
 
1284
1265
int Arg_comparator::compare_int_signed_unsigned()
1285
1266
{
1286
 
  longlong sval1= (*a)->val_int();
 
1267
  int64_t sval1= (*a)->val_int();
1287
1268
  if (!(*a)->null_value)
1288
1269
  {
1289
 
    ulonglong uval2= (ulonglong)(*b)->val_int();
 
1270
    uint64_t uval2= (uint64_t)(*b)->val_int();
1290
1271
    if (!(*b)->null_value)
1291
1272
    {
1292
1273
      owner->null_value= 0;
1293
 
      if (sval1 < 0 || (ulonglong)sval1 < uval2)
 
1274
      if (sval1 < 0 || (uint64_t)sval1 < uval2)
1294
1275
        return -1;
1295
 
      if ((ulonglong)sval1 == uval2)
 
1276
      if ((uint64_t)sval1 == uval2)
1296
1277
        return 0;
1297
1278
      return 1;
1298
1279
    }
1308
1289
 
1309
1290
int Arg_comparator::compare_int_unsigned_signed()
1310
1291
{
1311
 
  ulonglong uval1= (ulonglong)(*a)->val_int();
 
1292
  uint64_t uval1= (uint64_t)(*a)->val_int();
1312
1293
  if (!(*a)->null_value)
1313
1294
  {
1314
 
    longlong sval2= (*b)->val_int();
 
1295
    int64_t sval2= (*b)->val_int();
1315
1296
    if (!(*b)->null_value)
1316
1297
    {
1317
1298
      owner->null_value= 0;
1318
1299
      if (sval2 < 0)
1319
1300
        return 1;
1320
 
      if (uval1 < (ulonglong)sval2)
 
1301
      if (uval1 < (uint64_t)sval2)
1321
1302
        return -1;
1322
 
      if (uval1 == (ulonglong)sval2)
 
1303
      if (uval1 == (uint64_t)sval2)
1323
1304
        return 0;
1324
1305
      return 1;
1325
1306
    }
1331
1312
 
1332
1313
int Arg_comparator::compare_e_int()
1333
1314
{
1334
 
  longlong val1= (*a)->val_int();
1335
 
  longlong val2= (*b)->val_int();
 
1315
  int64_t val1= (*a)->val_int();
 
1316
  int64_t val2= (*b)->val_int();
1336
1317
  if ((*a)->null_value || (*b)->null_value)
1337
1318
    return test((*a)->null_value && (*b)->null_value);
1338
1319
  return test(val1 == val2);
1343
1324
*/
1344
1325
int Arg_comparator::compare_e_int_diff_signedness()
1345
1326
{
1346
 
  longlong val1= (*a)->val_int();
1347
 
  longlong val2= (*b)->val_int();
 
1327
  int64_t val1= (*a)->val_int();
 
1328
  int64_t val2= (*b)->val_int();
1348
1329
  if ((*a)->null_value || (*b)->null_value)
1349
1330
    return test((*a)->null_value && (*b)->null_value);
1350
1331
  return (val1 >= 0) && test(val1 == val2);
1356
1337
  bool was_null= 0;
1357
1338
  (*a)->bring_value();
1358
1339
  (*b)->bring_value();
1359
 
  uint n= (*a)->cols();
1360
 
  for (uint i= 0; i<n; i++)
 
1340
  uint32_t n= (*a)->cols();
 
1341
  for (uint32_t i= 0; i<n; i++)
1361
1342
  {
1362
1343
    res= comparators[i].compare();
1363
1344
    if (owner->null_value)
1399
1380
{
1400
1381
  (*a)->bring_value();
1401
1382
  (*b)->bring_value();
1402
 
  uint n= (*a)->cols();
1403
 
  for (uint i= 0; i<n; i++)
 
1383
  uint32_t n= (*a)->cols();
 
1384
  for (uint32_t i= 0; i<n; i++)
1404
1385
  {
1405
1386
    if (!comparators[i].compare())
1406
1387
      return 0;
1456
1437
}
1457
1438
 
1458
1439
 
1459
 
longlong Item_func_truth::val_int()
 
1440
int64_t Item_func_truth::val_int()
1460
1441
{
1461
1442
  return (val_bool() ? 1 : 0);
1462
1443
}
1463
1444
 
1464
1445
 
1465
 
bool Item_in_optimizer::fix_left(THD *thd, Item **ref)
 
1446
bool Item_in_optimizer::fix_left(THD *thd, Item **ref __attribute__((unused)))
1466
1447
{
1467
1448
  if ((!args[0]->fixed && args[0]->fix_fields(thd, args)) ||
1468
1449
      (!cache && !(cache= Item_cache::get_cache(args[0]))))
1478
1459
  }
1479
1460
  else
1480
1461
  {
1481
 
    uint n= cache->cols();
1482
 
    for (uint i= 0; i < n; i++)
 
1462
    uint32_t n= cache->cols();
 
1463
    for (uint32_t i= 0; i < n; i++)
1483
1464
    {
1484
1465
      if (args[0]->element_index(i)->used_tables())
1485
1466
        ((Item_cache *)cache->element_index(i))->set_used_tables(OUTER_REF_TABLE_BIT);
1498
1479
 
1499
1480
bool Item_in_optimizer::fix_fields(THD *thd, Item **ref)
1500
1481
{
1501
 
  DBUG_ASSERT(fixed == 0);
 
1482
  assert(fixed == 0);
1502
1483
  if (fix_left(thd, ref))
1503
1484
    return true;
1504
1485
  if (args[0]->maybe_null)
1523
1504
}
1524
1505
 
1525
1506
 
1526
 
longlong Item_in_optimizer::val_int()
 
1507
int64_t Item_in_optimizer::val_int()
1527
1508
{
1528
1509
  bool tmp;
1529
 
  DBUG_ASSERT(fixed == 1);
 
1510
  assert(fixed == 1);
1530
1511
  cache->store(args[0]);
1531
1512
  
1532
1513
  if (cache->null_value)
1566
1547
        }
1567
1548
        else
1568
1549
        {
1569
 
          uint i;
1570
 
          uint ncols= cache->cols();
 
1550
          uint32_t i;
 
1551
          uint32_t ncols= cache->cols();
1571
1552
          /*
1572
1553
            Turn off the predicates that are based on column compares for
1573
1554
            which the left part is currently NULL
1604
1585
 
1605
1586
void Item_in_optimizer::cleanup()
1606
1587
{
1607
 
  DBUG_ENTER("Item_in_optimizer::cleanup");
1608
1588
  Item_bool_func::cleanup();
1609
1589
  if (!save_cache)
1610
1590
    cache= 0;
1611
 
  DBUG_VOID_RETURN;
 
1591
  return;
1612
1592
}
1613
1593
 
1614
1594
 
1641
1621
    @retval NULL if an error occurred
1642
1622
*/
1643
1623
 
1644
 
Item *Item_in_optimizer::transform(Item_transformer transformer, uchar *argument)
 
1624
Item *Item_in_optimizer::transform(Item_transformer transformer, unsigned char *argument)
1645
1625
{
1646
1626
  Item *new_item;
1647
1627
 
1648
 
  DBUG_ASSERT(arg_count == 2);
 
1628
  assert(arg_count == 2);
1649
1629
 
1650
1630
  /* Transform the left IN operand. */
1651
1631
  new_item= (*args)->transform(transformer, argument);
1667
1647
    transformation, we only make both operands the same.
1668
1648
    TODO: is it the way it should be?
1669
1649
  */
1670
 
  DBUG_ASSERT((args[1])->type() == Item::SUBSELECT_ITEM &&
 
1650
  assert((args[1])->type() == Item::SUBSELECT_ITEM &&
1671
1651
              (((Item_subselect*)(args[1]))->substype() ==
1672
1652
               Item_subselect::IN_SUBS ||
1673
1653
               ((Item_subselect*)(args[1]))->substype() ==
1683
1663
 
1684
1664
 
1685
1665
 
1686
 
longlong Item_func_eq::val_int()
 
1666
int64_t Item_func_eq::val_int()
1687
1667
{
1688
 
  DBUG_ASSERT(fixed == 1);
 
1668
  assert(fixed == 1);
1689
1669
  int value= cmp.compare();
1690
1670
  return value == 0 ? 1 : 0;
1691
1671
}
1699
1679
  maybe_null=null_value=0;
1700
1680
}
1701
1681
 
1702
 
longlong Item_func_equal::val_int()
 
1682
int64_t Item_func_equal::val_int()
1703
1683
{
1704
 
  DBUG_ASSERT(fixed == 1);
 
1684
  assert(fixed == 1);
1705
1685
  return cmp.compare();
1706
1686
}
1707
1687
 
1708
 
longlong Item_func_ne::val_int()
 
1688
int64_t Item_func_ne::val_int()
1709
1689
{
1710
 
  DBUG_ASSERT(fixed == 1);
 
1690
  assert(fixed == 1);
1711
1691
  int value= cmp.compare();
1712
1692
  return value != 0 && !null_value ? 1 : 0;
1713
1693
}
1714
1694
 
1715
1695
 
1716
 
longlong Item_func_ge::val_int()
 
1696
int64_t Item_func_ge::val_int()
1717
1697
{
1718
 
  DBUG_ASSERT(fixed == 1);
 
1698
  assert(fixed == 1);
1719
1699
  int value= cmp.compare();
1720
1700
  return value >= 0 ? 1 : 0;
1721
1701
}
1722
1702
 
1723
1703
 
1724
 
longlong Item_func_gt::val_int()
 
1704
int64_t Item_func_gt::val_int()
1725
1705
{
1726
 
  DBUG_ASSERT(fixed == 1);
 
1706
  assert(fixed == 1);
1727
1707
  int value= cmp.compare();
1728
1708
  return value > 0 ? 1 : 0;
1729
1709
}
1730
1710
 
1731
 
longlong Item_func_le::val_int()
 
1711
int64_t Item_func_le::val_int()
1732
1712
{
1733
 
  DBUG_ASSERT(fixed == 1);
 
1713
  assert(fixed == 1);
1734
1714
  int value= cmp.compare();
1735
1715
  return value <= 0 && !null_value ? 1 : 0;
1736
1716
}
1737
1717
 
1738
1718
 
1739
 
longlong Item_func_lt::val_int()
 
1719
int64_t Item_func_lt::val_int()
1740
1720
{
1741
 
  DBUG_ASSERT(fixed == 1);
 
1721
  assert(fixed == 1);
1742
1722
  int value= cmp.compare();
1743
1723
  return value < 0 && !null_value ? 1 : 0;
1744
1724
}
1745
1725
 
1746
1726
 
1747
 
longlong Item_func_strcmp::val_int()
 
1727
int64_t Item_func_strcmp::val_int()
1748
1728
{
1749
 
  DBUG_ASSERT(fixed == 1);
 
1729
  assert(fixed == 1);
1750
1730
  String *a=args[0]->val_str(&tmp_value1);
1751
1731
  String *b=args[1]->val_str(&tmp_value2);
1752
1732
  if (!a || !b)
1756
1736
  }
1757
1737
  int value= sortcmp(a,b,cmp.cmp_collation.collation);
1758
1738
  null_value=0;
1759
 
  return !value ? 0 : (value < 0 ? (longlong) -1 : (longlong) 1);
 
1739
  return !value ? 0 : (value < 0 ? (int64_t) -1 : (int64_t) 1);
1760
1740
}
1761
1741
 
1762
1742
 
1773
1753
    return 0;
1774
1754
  if (negated != ((Item_func_opt_neg *) item_func)->negated)
1775
1755
    return 0;
1776
 
  for (uint i=0; i < arg_count ; i++)
 
1756
  for (uint32_t i=0; i < arg_count ; i++)
1777
1757
    if (!args[i]->eq(item_func->arguments()[i], binary_cmp))
1778
1758
      return 0;
1779
1759
  return 1;
1782
1762
 
1783
1763
void Item_func_interval::fix_length_and_dec()
1784
1764
{
1785
 
  uint rows= row->cols();
 
1765
  uint32_t rows= row->cols();
1786
1766
  
1787
1767
  use_decimal_comparison= ((row->element_index(0)->result_type() ==
1788
1768
                            DECIMAL_RESULT) ||
1792
1772
  {
1793
1773
    bool not_null_consts= true;
1794
1774
 
1795
 
    for (uint i= 1; not_null_consts && i < rows; i++)
 
1775
    for (uint32_t i= 1; not_null_consts && i < rows; i++)
1796
1776
    {
1797
1777
      Item *el= row->element_index(i);
1798
1778
      not_null_consts&= el->const_item() & !el->is_null();
1804
1784
    {
1805
1785
      if (use_decimal_comparison)
1806
1786
      {
1807
 
        for (uint i= 1; i < rows; i++)
 
1787
        for (uint32_t i= 1; i < rows; i++)
1808
1788
        {
1809
1789
          Item *el= row->element_index(i);
1810
1790
          interval_range *range= intervals + (i-1);
1829
1809
      }
1830
1810
      else
1831
1811
      {
1832
 
        for (uint i= 1; i < rows; i++)
 
1812
        for (uint32_t i= 1; i < rows; i++)
1833
1813
        {
1834
1814
          intervals[i-1].dbl= row->element_index(i)->val_real();
1835
1815
        }
1859
1839
    - arg_count if higher than biggest argument
1860
1840
*/
1861
1841
 
1862
 
longlong Item_func_interval::val_int()
 
1842
int64_t Item_func_interval::val_int()
1863
1843
{
1864
 
  DBUG_ASSERT(fixed == 1);
 
1844
  assert(fixed == 1);
1865
1845
  double value;
1866
1846
  my_decimal dec_buf, *dec= NULL;
1867
 
  uint i;
 
1847
  uint32_t i;
1868
1848
 
1869
1849
  if (use_decimal_comparison)
1870
1850
  {
1882
1862
 
1883
1863
  if (intervals)
1884
1864
  {                                     // Use binary search to find interval
1885
 
    uint start,end;
 
1865
    uint32_t start,end;
1886
1866
    start= 0;
1887
1867
    end=   row->cols()-2;
1888
1868
    while (start != end)
1889
1869
    {
1890
 
      uint mid= (start + end + 1) / 2;
 
1870
      uint32_t mid= (start + end + 1) / 2;
1891
1871
      interval_range *range= intervals + mid;
1892
 
      my_bool cmp_result;
 
1872
      bool cmp_result;
1893
1873
      /*
1894
1874
        The values in the range intervall may have different types,
1895
1875
        Only do a decimal comparision of the first argument is a decimal
2021
2001
        datetime_found= true;
2022
2002
        continue;
2023
2003
      }
2024
 
      if (args[i]->field_type() == MYSQL_TYPE_TIME &&
2025
 
          args[i]->result_as_longlong())
 
2004
      if (args[i]->field_type() == DRIZZLE_TYPE_TIME &&
 
2005
          args[i]->result_as_int64_t())
2026
2006
        time_items_found++;
2027
2007
    }
2028
2008
  }
2043
2023
           thd->lex->sql_command != SQLCOM_SHOW_CREATE)
2044
2024
  {
2045
2025
    Item_field *field_item= (Item_field*) (args[0]->real_item());
2046
 
    if (field_item->field->can_be_compared_as_longlong())
 
2026
    if (field_item->field->can_be_compared_as_int64_t())
2047
2027
    {
2048
2028
      /*
2049
2029
        The following can't be recoded with || as convert_constant_item
2058
2038
}
2059
2039
 
2060
2040
 
2061
 
longlong Item_func_between::val_int()
 
2041
int64_t Item_func_between::val_int()
2062
2042
{                                               // ANSI BETWEEN
2063
 
  DBUG_ASSERT(fixed == 1);
 
2043
  assert(fixed == 1);
2064
2044
  if (compare_as_dates)
2065
2045
  {
2066
2046
    int ge_res, le_res;
2071
2051
    le_res= le_cmp.compare();
2072
2052
 
2073
2053
    if (!args[1]->null_value && !args[2]->null_value)
2074
 
      return (longlong) ((ge_res >= 0 && le_res <=0) != negated);
 
2054
      return (int64_t) ((ge_res >= 0 && le_res <=0) != negated);
2075
2055
    else if (args[1]->null_value)
2076
2056
    {
2077
2057
      null_value= le_res > 0;                   // not null if false range.
2090
2070
    a=args[1]->val_str(&value1);
2091
2071
    b=args[2]->val_str(&value2);
2092
2072
    if (!args[1]->null_value && !args[2]->null_value)
2093
 
      return (longlong) ((sortcmp(value,a,cmp_collation.collation) >= 0 &&
 
2073
      return (int64_t) ((sortcmp(value,a,cmp_collation.collation) >= 0 &&
2094
2074
                          sortcmp(value,b,cmp_collation.collation) <= 0) !=
2095
2075
                         negated);
2096
2076
    if (args[1]->null_value && args[2]->null_value)
2108
2088
  }
2109
2089
  else if (cmp_type == INT_RESULT)
2110
2090
  {
2111
 
    longlong value=args[0]->val_int(), a, b;
 
2091
    int64_t value=args[0]->val_int(), a, b;
2112
2092
    if ((null_value=args[0]->null_value))
2113
2093
      return 0;                                 /* purecov: inspected */
2114
2094
    a=args[1]->val_int();
2115
2095
    b=args[2]->val_int();
2116
2096
    if (!args[1]->null_value && !args[2]->null_value)
2117
 
      return (longlong) ((value >= a && value <= b) != negated);
 
2097
      return (int64_t) ((value >= a && value <= b) != negated);
2118
2098
    if (args[1]->null_value && args[2]->null_value)
2119
2099
      null_value=1;
2120
2100
    else if (args[1]->null_value)
2135
2115
    a_dec= args[1]->val_decimal(&a_buf);
2136
2116
    b_dec= args[2]->val_decimal(&b_buf);
2137
2117
    if (!args[1]->null_value && !args[2]->null_value)
2138
 
      return (longlong) ((my_decimal_cmp(dec, a_dec) >= 0 &&
 
2118
      return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2139
2119
                          my_decimal_cmp(dec, b_dec) <= 0) != negated);
2140
2120
    if (args[1]->null_value && args[2]->null_value)
2141
2121
      null_value=1;
2152
2132
    a= args[1]->val_real();
2153
2133
    b= args[2]->val_real();
2154
2134
    if (!args[1]->null_value && !args[2]->null_value)
2155
 
      return (longlong) ((value >= a && value <= b) != negated);
 
2135
      return (int64_t) ((value >= a && value <= b) != negated);
2156
2136
    if (args[1]->null_value && args[2]->null_value)
2157
2137
      null_value=1;
2158
2138
    else if (args[1]->null_value)
2164
2144
      null_value= value >= a;
2165
2145
    }
2166
2146
  }
2167
 
  return (longlong) (!null_value && negated);
 
2147
  return (int64_t) (!null_value && negated);
2168
2148
}
2169
2149
 
2170
2150
 
2186
2166
{
2187
2167
  agg_result_type(&hybrid_type, args, 2);
2188
2168
  maybe_null=args[1]->maybe_null;
2189
 
  decimals= max(args[0]->decimals, args[1]->decimals);
 
2169
  decimals= cmax(args[0]->decimals, args[1]->decimals);
2190
2170
  unsigned_flag= args[0]->unsigned_flag && args[1]->unsigned_flag;
2191
2171
 
2192
2172
  if (hybrid_type == DECIMAL_RESULT || hybrid_type == INT_RESULT) 
2197
2177
    int len1= args[1]->max_length - args[1]->decimals
2198
2178
      - (args[1]->unsigned_flag ? 0 : 1);
2199
2179
 
2200
 
    max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
 
2180
    max_length= cmax(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2201
2181
  }
2202
2182
  else
2203
 
    max_length= max(args[0]->max_length, args[1]->max_length);
 
2183
    max_length= cmax(args[0]->max_length, args[1]->max_length);
2204
2184
 
2205
2185
  switch (hybrid_type) {
2206
2186
  case STRING_RESULT:
2214
2194
    break;
2215
2195
  case ROW_RESULT:
2216
2196
  default:
2217
 
    DBUG_ASSERT(0);
 
2197
    assert(0);
2218
2198
  }
2219
2199
  cached_field_type= agg_field_type(args, 2);
2220
2200
}
2221
2201
 
2222
2202
 
2223
 
uint Item_func_ifnull::decimal_precision() const
 
2203
uint32_t Item_func_ifnull::decimal_precision() const
2224
2204
{
2225
 
  int max_int_part=max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2226
 
  return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
 
2205
  int max_int_part=cmax(args[0]->decimal_int_part(),args[1]->decimal_int_part());
 
2206
  return cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2227
2207
}
2228
2208
 
2229
2209
 
2232
2212
  return cached_field_type;
2233
2213
}
2234
2214
 
2235
 
Field *Item_func_ifnull::tmp_table_field(TABLE *table)
 
2215
Field *Item_func_ifnull::tmp_table_field(Table *table)
2236
2216
{
2237
2217
  return tmp_table_field_from_field_type(table, 0);
2238
2218
}
2240
2220
double
2241
2221
Item_func_ifnull::real_op()
2242
2222
{
2243
 
  DBUG_ASSERT(fixed == 1);
 
2223
  assert(fixed == 1);
2244
2224
  double value= args[0]->val_real();
2245
2225
  if (!args[0]->null_value)
2246
2226
  {
2253
2233
  return value;
2254
2234
}
2255
2235
 
2256
 
longlong
 
2236
int64_t
2257
2237
Item_func_ifnull::int_op()
2258
2238
{
2259
 
  DBUG_ASSERT(fixed == 1);
2260
 
  longlong value=args[0]->val_int();
 
2239
  assert(fixed == 1);
 
2240
  int64_t value=args[0]->val_int();
2261
2241
  if (!args[0]->null_value)
2262
2242
  {
2263
2243
    null_value=0;
2272
2252
 
2273
2253
my_decimal *Item_func_ifnull::decimal_op(my_decimal *decimal_value)
2274
2254
{
2275
 
  DBUG_ASSERT(fixed == 1);
 
2255
  assert(fixed == 1);
2276
2256
  my_decimal *value= args[0]->val_decimal(decimal_value);
2277
2257
  if (!args[0]->null_value)
2278
2258
  {
2289
2269
String *
2290
2270
Item_func_ifnull::str_op(String *str)
2291
2271
{
2292
 
  DBUG_ASSERT(fixed == 1);
 
2272
  assert(fixed == 1);
2293
2273
  String *res  =args[0]->val_str(str);
2294
2274
  if (!args[0]->null_value)
2295
2275
  {
2334
2314
bool
2335
2315
Item_func_if::fix_fields(THD *thd, Item **ref)
2336
2316
{
2337
 
  DBUG_ASSERT(fixed == 0);
 
2317
  assert(fixed == 0);
2338
2318
  args[0]->top_level_item();
2339
2319
 
2340
2320
  if (Item_func::fix_fields(thd, ref))
2351
2331
Item_func_if::fix_length_and_dec()
2352
2332
{
2353
2333
  maybe_null=args[1]->maybe_null || args[2]->maybe_null;
2354
 
  decimals= max(args[1]->decimals, args[2]->decimals);
 
2334
  decimals= cmax(args[1]->decimals, args[2]->decimals);
2355
2335
  unsigned_flag=args[1]->unsigned_flag && args[2]->unsigned_flag;
2356
2336
 
2357
2337
  enum Item_result arg1_type=args[1]->result_type();
2395
2375
    int len2= args[2]->max_length - args[2]->decimals
2396
2376
      - (args[2]->unsigned_flag ? 0 : 1);
2397
2377
 
2398
 
    max_length=max(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
 
2378
    max_length=cmax(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
2399
2379
  }
2400
2380
  else
2401
 
    max_length= max(args[1]->max_length, args[2]->max_length);
 
2381
    max_length= cmax(args[1]->max_length, args[2]->max_length);
2402
2382
}
2403
2383
 
2404
2384
 
2405
 
uint Item_func_if::decimal_precision() const
 
2385
uint32_t Item_func_if::decimal_precision() const
2406
2386
{
2407
 
  int precision=(max(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
 
2387
  int precision=(cmax(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
2408
2388
                 decimals);
2409
 
  return min(precision, DECIMAL_MAX_PRECISION);
 
2389
  return cmin(precision, DECIMAL_MAX_PRECISION);
2410
2390
}
2411
2391
 
2412
2392
 
2413
2393
double
2414
2394
Item_func_if::val_real()
2415
2395
{
2416
 
  DBUG_ASSERT(fixed == 1);
 
2396
  assert(fixed == 1);
2417
2397
  Item *arg= args[0]->val_bool() ? args[1] : args[2];
2418
2398
  double value= arg->val_real();
2419
2399
  null_value=arg->null_value;
2420
2400
  return value;
2421
2401
}
2422
2402
 
2423
 
longlong
 
2403
int64_t
2424
2404
Item_func_if::val_int()
2425
2405
{
2426
 
  DBUG_ASSERT(fixed == 1);
 
2406
  assert(fixed == 1);
2427
2407
  Item *arg= args[0]->val_bool() ? args[1] : args[2];
2428
 
  longlong value=arg->val_int();
 
2408
  int64_t value=arg->val_int();
2429
2409
  null_value=arg->null_value;
2430
2410
  return value;
2431
2411
}
2433
2413
String *
2434
2414
Item_func_if::val_str(String *str)
2435
2415
{
2436
 
  DBUG_ASSERT(fixed == 1);
 
2416
  assert(fixed == 1);
2437
2417
  Item *arg= args[0]->val_bool() ? args[1] : args[2];
2438
2418
  String *res=arg->val_str(str);
2439
2419
  if (res)
2446
2426
my_decimal *
2447
2427
Item_func_if::val_decimal(my_decimal *decimal_value)
2448
2428
{
2449
 
  DBUG_ASSERT(fixed == 1);
 
2429
  assert(fixed == 1);
2450
2430
  Item *arg= args[0]->val_bool() ? args[1] : args[2];
2451
2431
  my_decimal *value= arg->val_decimal(decimal_value);
2452
2432
  null_value= arg->null_value;
2485
2465
double
2486
2466
Item_func_nullif::val_real()
2487
2467
{
2488
 
  DBUG_ASSERT(fixed == 1);
 
2468
  assert(fixed == 1);
2489
2469
  double value;
2490
2470
  if (!cmp.compare())
2491
2471
  {
2497
2477
  return value;
2498
2478
}
2499
2479
 
2500
 
longlong
 
2480
int64_t
2501
2481
Item_func_nullif::val_int()
2502
2482
{
2503
 
  DBUG_ASSERT(fixed == 1);
2504
 
  longlong value;
 
2483
  assert(fixed == 1);
 
2484
  int64_t value;
2505
2485
  if (!cmp.compare())
2506
2486
  {
2507
2487
    null_value=1;
2515
2495
String *
2516
2496
Item_func_nullif::val_str(String *str)
2517
2497
{
2518
 
  DBUG_ASSERT(fixed == 1);
 
2498
  assert(fixed == 1);
2519
2499
  String *res;
2520
2500
  if (!cmp.compare())
2521
2501
  {
2531
2511
my_decimal *
2532
2512
Item_func_nullif::val_decimal(my_decimal * decimal_value)
2533
2513
{
2534
 
  DBUG_ASSERT(fixed == 1);
 
2514
  assert(fixed == 1);
2535
2515
  my_decimal *res;
2536
2516
  if (!cmp.compare())
2537
2517
  {
2572
2552
           failed
2573
2553
*/
2574
2554
 
2575
 
Item *Item_func_case::find_item(String *str)
 
2555
Item *Item_func_case::find_item(String *str __attribute__((unused)))
2576
2556
{
2577
 
  uint value_added_map= 0;
 
2557
  uint32_t value_added_map= 0;
2578
2558
 
2579
2559
  if (first_expr_num == -1)
2580
2560
  {
2581
 
    for (uint i=0 ; i < ncases ; i+=2)
 
2561
    for (uint32_t i=0 ; i < ncases ; i+=2)
2582
2562
    {
2583
2563
      // No expression between CASE and the first WHEN
2584
2564
      if (args[i]->val_bool())
2589
2569
  else
2590
2570
  {
2591
2571
    /* Compare every WHEN argument with it and return the first match */
2592
 
    for (uint i=0 ; i < ncases ; i+=2)
 
2572
    for (uint32_t i=0 ; i < ncases ; i+=2)
2593
2573
    {
2594
2574
      cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
2595
 
      DBUG_ASSERT(cmp_type != ROW_RESULT);
2596
 
      DBUG_ASSERT(cmp_items[(uint)cmp_type]);
 
2575
      assert(cmp_type != ROW_RESULT);
 
2576
      assert(cmp_items[(uint)cmp_type]);
2597
2577
      if (!(value_added_map & (1<<(uint)cmp_type)))
2598
2578
      {
2599
2579
        cmp_items[(uint)cmp_type]->store_value(args[first_expr_num]);
2612
2592
 
2613
2593
String *Item_func_case::val_str(String *str)
2614
2594
{
2615
 
  DBUG_ASSERT(fixed == 1);
 
2595
  assert(fixed == 1);
2616
2596
  String *res;
2617
2597
  Item *item=find_item(str);
2618
2598
 
2628
2608
}
2629
2609
 
2630
2610
 
2631
 
longlong Item_func_case::val_int()
 
2611
int64_t Item_func_case::val_int()
2632
2612
{
2633
 
  DBUG_ASSERT(fixed == 1);
 
2613
  assert(fixed == 1);
2634
2614
  char buff[MAX_FIELD_WIDTH];
2635
2615
  String dummy_str(buff,sizeof(buff),default_charset());
2636
2616
  Item *item=find_item(&dummy_str);
2637
 
  longlong res;
 
2617
  int64_t res;
2638
2618
 
2639
2619
  if (!item)
2640
2620
  {
2648
2628
 
2649
2629
double Item_func_case::val_real()
2650
2630
{
2651
 
  DBUG_ASSERT(fixed == 1);
 
2631
  assert(fixed == 1);
2652
2632
  char buff[MAX_FIELD_WIDTH];
2653
2633
  String dummy_str(buff,sizeof(buff),default_charset());
2654
2634
  Item *item=find_item(&dummy_str);
2667
2647
 
2668
2648
my_decimal *Item_func_case::val_decimal(my_decimal *decimal_value)
2669
2649
{
2670
 
  DBUG_ASSERT(fixed == 1);
 
2650
  assert(fixed == 1);
2671
2651
  char buff[MAX_FIELD_WIDTH];
2672
2652
  String dummy_str(buff, sizeof(buff), default_charset());
2673
2653
  Item *item= find_item(&dummy_str);
2691
2671
    buff should match stack usage from
2692
2672
    Item_func_case::val_int() -> Item_func_case::find_item()
2693
2673
  */
2694
 
  uchar buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(longlong)*2];
 
2674
  unsigned char buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(int64_t)*2];
2695
2675
  bool res= Item_func::fix_fields(thd, ref);
2696
2676
  /*
2697
2677
    Call check_stack_overrun after fix_fields to be sure that stack variable
2713
2693
 
2714
2694
void Item_func_case::agg_num_lengths(Item *arg)
2715
2695
{
2716
 
  uint len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
 
2696
  uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2717
2697
                                           arg->unsigned_flag) - arg->decimals;
2718
2698
  set_if_bigger(max_length, len); 
2719
2699
  set_if_bigger(decimals, arg->decimals);
2724
2704
void Item_func_case::fix_length_and_dec()
2725
2705
{
2726
2706
  Item **agg;
2727
 
  uint nagg;
2728
 
  uint found_types= 0;
 
2707
  uint32_t nagg;
 
2708
  uint32_t found_types= 0;
2729
2709
  if (!(agg= (Item**) sql_alloc(sizeof(Item*)*(ncases+1))))
2730
2710
    return;
2731
2711
  
2752
2732
  */
2753
2733
  if (first_expr_num != -1)
2754
2734
  {
2755
 
    uint i;
 
2735
    uint32_t i;
2756
2736
    agg[0]= args[first_expr_num];
2757
2737
    left_result_type= agg[0]->result_type();
2758
2738
 
2766
2746
    {
2767
2747
      if (found_types & (1 << i) && !cmp_items[i])
2768
2748
      {
2769
 
        DBUG_ASSERT((Item_result)i != ROW_RESULT);
 
2749
        assert((Item_result)i != ROW_RESULT);
2770
2750
        if ((Item_result)i == STRING_RESULT &&
2771
2751
            agg_arg_charsets(cmp_collation, agg, nagg, MY_COLL_CMP_CONV, 1))
2772
2752
          return;
2786
2766
  unsigned_flag= true;
2787
2767
  if (cached_result_type == STRING_RESULT)
2788
2768
  {
2789
 
    for (uint i= 0; i < ncases; i+= 2)
 
2769
    for (uint32_t i= 0; i < ncases; i+= 2)
2790
2770
      agg_str_lengths(args[i + 1]);
2791
2771
    if (else_expr_num != -1)
2792
2772
      agg_str_lengths(args[else_expr_num]);
2793
2773
  }
2794
2774
  else
2795
2775
  {
2796
 
    for (uint i= 0; i < ncases; i+= 2)
 
2776
    for (uint32_t i= 0; i < ncases; i+= 2)
2797
2777
      agg_num_lengths(args[i + 1]);
2798
2778
    if (else_expr_num != -1) 
2799
2779
      agg_num_lengths(args[else_expr_num]);
2803
2783
}
2804
2784
 
2805
2785
 
2806
 
uint Item_func_case::decimal_precision() const
 
2786
uint32_t Item_func_case::decimal_precision() const
2807
2787
{
2808
2788
  int max_int_part=0;
2809
 
  for (uint i=0 ; i < ncases ; i+=2)
 
2789
  for (uint32_t i=0 ; i < ncases ; i+=2)
2810
2790
    set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
2811
2791
 
2812
2792
  if (else_expr_num != -1) 
2813
2793
    set_if_bigger(max_int_part, args[else_expr_num]->decimal_int_part());
2814
 
  return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
 
2794
  return cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2815
2795
}
2816
2796
 
2817
2797
 
2828
2808
    args[first_expr_num]->print(str, query_type);
2829
2809
    str->append(' ');
2830
2810
  }
2831
 
  for (uint i=0 ; i < ncases ; i+=2)
 
2811
  for (uint32_t i=0 ; i < ncases ; i+=2)
2832
2812
  {
2833
2813
    str->append(STRING_WITH_LEN("when "));
2834
2814
    args[i]->print(str, query_type);
2848
2828
 
2849
2829
void Item_func_case::cleanup()
2850
2830
{
2851
 
  uint i;
2852
 
  DBUG_ENTER("Item_func_case::cleanup");
 
2831
  uint32_t i;
2853
2832
  Item_func::cleanup();
2854
2833
  for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
2855
2834
  {
2856
2835
    delete cmp_items[i];
2857
2836
    cmp_items[i]= 0;
2858
2837
  }
2859
 
  DBUG_VOID_RETURN;
 
2838
  return;
2860
2839
}
2861
2840
 
2862
2841
 
2866
2845
 
2867
2846
String *Item_func_coalesce::str_op(String *str)
2868
2847
{
2869
 
  DBUG_ASSERT(fixed == 1);
 
2848
  assert(fixed == 1);
2870
2849
  null_value=0;
2871
 
  for (uint i=0 ; i < arg_count ; i++)
 
2850
  for (uint32_t i=0 ; i < arg_count ; i++)
2872
2851
  {
2873
2852
    String *res;
2874
2853
    if ((res=args[i]->val_str(str)))
2878
2857
  return 0;
2879
2858
}
2880
2859
 
2881
 
longlong Item_func_coalesce::int_op()
 
2860
int64_t Item_func_coalesce::int_op()
2882
2861
{
2883
 
  DBUG_ASSERT(fixed == 1);
 
2862
  assert(fixed == 1);
2884
2863
  null_value=0;
2885
 
  for (uint i=0 ; i < arg_count ; i++)
 
2864
  for (uint32_t i=0 ; i < arg_count ; i++)
2886
2865
  {
2887
 
    longlong res=args[i]->val_int();
 
2866
    int64_t res=args[i]->val_int();
2888
2867
    if (!args[i]->null_value)
2889
2868
      return res;
2890
2869
  }
2894
2873
 
2895
2874
double Item_func_coalesce::real_op()
2896
2875
{
2897
 
  DBUG_ASSERT(fixed == 1);
 
2876
  assert(fixed == 1);
2898
2877
  null_value=0;
2899
 
  for (uint i=0 ; i < arg_count ; i++)
 
2878
  for (uint32_t i=0 ; i < arg_count ; i++)
2900
2879
  {
2901
2880
    double res= args[i]->val_real();
2902
2881
    if (!args[i]->null_value)
2909
2888
 
2910
2889
my_decimal *Item_func_coalesce::decimal_op(my_decimal *decimal_value)
2911
2890
{
2912
 
  DBUG_ASSERT(fixed == 1);
 
2891
  assert(fixed == 1);
2913
2892
  null_value= 0;
2914
 
  for (uint i= 0; i < arg_count; i++)
 
2893
  for (uint32_t i= 0; i < arg_count; i++)
2915
2894
  {
2916
2895
    my_decimal *res= args[i]->val_decimal(decimal_value);
2917
2896
    if (!args[i]->null_value)
2944
2923
    break;
2945
2924
  case ROW_RESULT:
2946
2925
  default:
2947
 
    DBUG_ASSERT(0);
 
2926
    assert(0);
2948
2927
  }
2949
2928
}
2950
2929
 
2953
2932
****************************************************************************/
2954
2933
 
2955
2934
/*
2956
 
  Determine which of the signed longlong arguments is bigger
 
2935
  Determine which of the signed int64_t arguments is bigger
2957
2936
 
2958
2937
  SYNOPSIS
2959
2938
    cmp_longs()
2961
2940
      b_val     right argument
2962
2941
 
2963
2942
  DESCRIPTION
2964
 
    This function will compare two signed longlong arguments
 
2943
    This function will compare two signed int64_t arguments
2965
2944
    and will return -1, 0, or 1 if left argument is smaller than,
2966
2945
    equal to or greater than the right argument.
2967
2946
 
2970
2949
    0           left argument is equal to the right argument.
2971
2950
    1           left argument is greater than the right argument.
2972
2951
*/
2973
 
static inline int cmp_longs (longlong a_val, longlong b_val)
 
2952
static inline int cmp_longs (int64_t a_val, int64_t b_val)
2974
2953
{
2975
2954
  return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
2976
2955
}
2977
2956
 
2978
2957
 
2979
2958
/*
2980
 
  Determine which of the unsigned longlong arguments is bigger
 
2959
  Determine which of the unsigned int64_t arguments is bigger
2981
2960
 
2982
2961
  SYNOPSIS
2983
2962
    cmp_ulongs()
2985
2964
      b_val     right argument
2986
2965
 
2987
2966
  DESCRIPTION
2988
 
    This function will compare two unsigned longlong arguments
 
2967
    This function will compare two unsigned int64_t arguments
2989
2968
    and will return -1, 0, or 1 if left argument is smaller than,
2990
2969
    equal to or greater than the right argument.
2991
2970
 
2994
2973
    0           left argument is equal to the right argument.
2995
2974
    1           left argument is greater than the right argument.
2996
2975
*/
2997
 
static inline int cmp_ulongs (ulonglong a_val, ulonglong b_val)
 
2976
static inline int cmp_ulongs (uint64_t a_val, uint64_t b_val)
2998
2977
{
2999
2978
  return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
3000
2979
}
3001
2980
 
3002
2981
 
3003
2982
/*
3004
 
  Compare two integers in IN value list format (packed_longlong) 
 
2983
  Compare two integers in IN value list format (packed_int64_t) 
3005
2984
 
3006
2985
  SYNOPSIS
3007
 
    cmp_longlong()
 
2986
    cmp_int64_t()
3008
2987
      cmp_arg   an argument passed to the calling function (my_qsort2)
3009
2988
      a         left argument
3010
2989
      b         right argument
3014
2993
    format and will return -1, 0, or 1 if left argument is smaller than,
3015
2994
    equal to or greater than the right argument.
3016
2995
    It's used in sorting the IN values list and finding an element in it.
3017
 
    Depending on the signedness of the arguments cmp_longlong() will
 
2996
    Depending on the signedness of the arguments cmp_int64_t() will
3018
2997
    compare them as either signed (using cmp_longs()) or unsigned (using
3019
2998
    cmp_ulongs()).
3020
2999
 
3023
3002
    0           left argument is equal to the right argument.
3024
3003
    1           left argument is greater than the right argument.
3025
3004
*/
3026
 
int cmp_longlong(void *cmp_arg, 
3027
 
                 in_longlong::packed_longlong *a,
3028
 
                 in_longlong::packed_longlong *b)
 
3005
int cmp_int64_t(void *cmp_arg __attribute__((unused)),
 
3006
                 in_int64_t::packed_int64_t *a,
 
3007
                 in_int64_t::packed_int64_t *b)
3029
3008
{
3030
3009
  if (a->unsigned_flag != b->unsigned_flag)
3031
3010
  { 
3033
3012
      One of the args is unsigned and is too big to fit into the 
3034
3013
      positive signed range. Report no match.
3035
3014
    */  
3036
 
    if ((a->unsigned_flag && ((ulonglong) a->val) > (ulonglong) LONGLONG_MAX) ||
3037
 
        (b->unsigned_flag && ((ulonglong) b->val) > (ulonglong) LONGLONG_MAX))
 
3015
    if ((a->unsigned_flag && ((uint64_t) a->val) > (uint64_t) INT64_MAX) ||
 
3016
        (b->unsigned_flag && ((uint64_t) b->val) > (uint64_t) INT64_MAX))
3038
3017
      return a->unsigned_flag ? 1 : -1;
3039
3018
    /*
3040
3019
      Although the signedness differs both args can fit into the signed 
3043
3022
    return cmp_longs (a->val, b->val);
3044
3023
  }
3045
3024
  if (a->unsigned_flag)
3046
 
    return cmp_ulongs ((ulonglong) a->val, (ulonglong) b->val);
 
3025
    return cmp_ulongs ((uint64_t) a->val, (uint64_t) b->val);
3047
3026
  else
3048
3027
    return cmp_longs (a->val, b->val);
3049
3028
}
3050
3029
 
3051
 
static int cmp_double(void *cmp_arg, double *a,double *b)
 
3030
static int cmp_double(void *cmp_arg __attribute__((unused)), double *a,double *b)
3052
3031
{
3053
3032
  return *a < *b ? -1 : *a == *b ? 0 : 1;
3054
3033
}
3055
3034
 
3056
 
static int cmp_row(void *cmp_arg, cmp_item_row *a, cmp_item_row *b)
 
3035
static int cmp_row(void *cmp_arg __attribute__((unused)), cmp_item_row *a, cmp_item_row *b)
3057
3036
{
3058
3037
  return a->compare(b);
3059
3038
}
3060
3039
 
3061
3040
 
3062
 
static int cmp_decimal(void *cmp_arg, my_decimal *a, my_decimal *b)
 
3041
static int cmp_decimal(void *cmp_arg __attribute__((unused)), my_decimal *a, my_decimal *b)
3063
3042
{
3064
3043
  /*
3065
3044
    We need call of fixing buffer pointer, because fast sort just copy
3073
3052
 
3074
3053
int in_vector::find(Item *item)
3075
3054
{
3076
 
  uchar *result=get_value(item);
 
3055
  unsigned char *result=get_value(item);
3077
3056
  if (!result || !used_count)
3078
3057
    return 0;                           // Null value
3079
3058
 
3080
 
  uint start,end;
 
3059
  uint32_t start,end;
3081
3060
  start=0; end=used_count-1;
3082
3061
  while (start != end)
3083
3062
  {
3084
 
    uint mid=(start+end+1)/2;
 
3063
    uint32_t mid=(start+end+1)/2;
3085
3064
    int res;
3086
3065
    if ((res=(*compare)(collation, base+mid*size, result)) == 0)
3087
3066
      return 1;
3093
3072
  return (int) ((*compare)(collation, base+start*size, result) == 0);
3094
3073
}
3095
3074
 
3096
 
in_string::in_string(uint elements,qsort2_cmp cmp_func, CHARSET_INFO *cs)
 
3075
in_string::in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs)
3097
3076
  :in_vector(elements, sizeof(String), cmp_func, cs),
3098
3077
   tmp(buff, sizeof(buff), &my_charset_bin)
3099
3078
{}
3103
3082
  if (base)
3104
3083
  {
3105
3084
    // base was allocated with help of sql_alloc => following is OK
3106
 
    for (uint i=0 ; i < count ; i++)
 
3085
    for (uint32_t i=0 ; i < count ; i++)
3107
3086
      ((String*) base)[i].free();
3108
3087
  }
3109
3088
}
3110
3089
 
3111
 
void in_string::set(uint pos,Item *item)
 
3090
void in_string::set(uint32_t pos,Item *item)
3112
3091
{
3113
3092
  String *str=((String*) base)+pos;
3114
3093
  String *res=item->val_str(str);
3123
3102
  }
3124
3103
  if (!str->charset())
3125
3104
  {
3126
 
    CHARSET_INFO *cs;
 
3105
    const CHARSET_INFO *cs;
3127
3106
    if (!(cs= item->collation.collation))
3128
3107
      cs= &my_charset_bin;              // Should never happen for STR items
3129
3108
    str->set_charset(cs);
3131
3110
}
3132
3111
 
3133
3112
 
3134
 
uchar *in_string::get_value(Item *item)
 
3113
unsigned char *in_string::get_value(Item *item)
3135
3114
{
3136
 
  return (uchar*) item->val_str(&tmp);
 
3115
  return (unsigned char*) item->val_str(&tmp);
3137
3116
}
3138
3117
 
3139
 
in_row::in_row(uint elements, Item * item)
 
3118
in_row::in_row(uint32_t elements, Item * item __attribute__((unused)))
3140
3119
{
3141
3120
  base= (char*) new cmp_item_row[count= elements];
3142
3121
  size= sizeof(cmp_item_row);
3155
3134
    delete [] (cmp_item_row*) base;
3156
3135
}
3157
3136
 
3158
 
uchar *in_row::get_value(Item *item)
 
3137
unsigned char *in_row::get_value(Item *item)
3159
3138
{
3160
3139
  tmp.store_value(item);
3161
3140
  if (item->is_null())
3162
3141
    return 0;
3163
 
  return (uchar *)&tmp;
 
3142
  return (unsigned char *)&tmp;
3164
3143
}
3165
3144
 
3166
 
void in_row::set(uint pos, Item *item)
 
3145
void in_row::set(uint32_t pos, Item *item)
3167
3146
{
3168
 
  DBUG_ENTER("in_row::set");
3169
 
  DBUG_PRINT("enter", ("pos: %u  item: 0x%lx", pos, (ulong) item));
3170
3147
  ((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3171
 
  DBUG_VOID_RETURN;
 
3148
  return;
3172
3149
}
3173
3150
 
3174
 
in_longlong::in_longlong(uint elements)
3175
 
  :in_vector(elements,sizeof(packed_longlong),(qsort2_cmp) cmp_longlong, 0)
 
3151
in_int64_t::in_int64_t(uint32_t elements)
 
3152
  :in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3176
3153
{}
3177
3154
 
3178
 
void in_longlong::set(uint pos,Item *item)
 
3155
void in_int64_t::set(uint32_t pos,Item *item)
3179
3156
{
3180
 
  struct packed_longlong *buff= &((packed_longlong*) base)[pos];
 
3157
  struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3181
3158
  
3182
3159
  buff->val= item->val_int();
3183
3160
  buff->unsigned_flag= item->unsigned_flag;
3184
3161
}
3185
3162
 
3186
 
uchar *in_longlong::get_value(Item *item)
 
3163
unsigned char *in_int64_t::get_value(Item *item)
3187
3164
{
3188
3165
  tmp.val= item->val_int();
3189
3166
  if (item->null_value)
3190
3167
    return 0;
3191
3168
  tmp.unsigned_flag= item->unsigned_flag;
3192
 
  return (uchar*) &tmp;
 
3169
  return (unsigned char*) &tmp;
3193
3170
}
3194
3171
 
3195
 
void in_datetime::set(uint pos,Item *item)
 
3172
void in_datetime::set(uint32_t pos,Item *item)
3196
3173
{
3197
3174
  Item **tmp_item= &item;
3198
3175
  bool is_null;
3199
 
  struct packed_longlong *buff= &((packed_longlong*) base)[pos];
 
3176
  struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3200
3177
 
3201
3178
  buff->val= get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
3202
3179
  buff->unsigned_flag= 1L;
3203
3180
}
3204
3181
 
3205
 
uchar *in_datetime::get_value(Item *item)
 
3182
unsigned char *in_datetime::get_value(Item *item)
3206
3183
{
3207
3184
  bool is_null;
3208
3185
  Item **tmp_item= lval_cache ? &lval_cache : &item;
3210
3187
  if (item->null_value)
3211
3188
    return 0;
3212
3189
  tmp.unsigned_flag= 1L;
3213
 
  return (uchar*) &tmp;
 
3190
  return (unsigned char*) &tmp;
3214
3191
}
3215
3192
 
3216
 
in_double::in_double(uint elements)
 
3193
in_double::in_double(uint32_t elements)
3217
3194
  :in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
3218
3195
{}
3219
3196
 
3220
 
void in_double::set(uint pos,Item *item)
 
3197
void in_double::set(uint32_t pos,Item *item)
3221
3198
{
3222
3199
  ((double*) base)[pos]= item->val_real();
3223
3200
}
3224
3201
 
3225
 
uchar *in_double::get_value(Item *item)
 
3202
unsigned char *in_double::get_value(Item *item)
3226
3203
{
3227
3204
  tmp= item->val_real();
3228
3205
  if (item->null_value)
3229
3206
    return 0;                                   /* purecov: inspected */
3230
 
  return (uchar*) &tmp;
 
3207
  return (unsigned char*) &tmp;
3231
3208
}
3232
3209
 
3233
3210
 
3234
 
in_decimal::in_decimal(uint elements)
 
3211
in_decimal::in_decimal(uint32_t elements)
3235
3212
  :in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3236
3213
{}
3237
3214
 
3238
3215
 
3239
 
void in_decimal::set(uint pos, Item *item)
 
3216
void in_decimal::set(uint32_t pos, Item *item)
3240
3217
{
3241
3218
  /* as far as 'item' is constant, we can store reference on my_decimal */
3242
3219
  my_decimal *dec= ((my_decimal *)base) + pos;
3249
3226
}
3250
3227
 
3251
3228
 
3252
 
uchar *in_decimal::get_value(Item *item)
 
3229
unsigned char *in_decimal::get_value(Item *item)
3253
3230
{
3254
3231
  my_decimal *result= item->val_decimal(&val);
3255
3232
  if (item->null_value)
3256
3233
    return 0;
3257
 
  return (uchar *)result;
 
3234
  return (unsigned char *)result;
3258
3235
}
3259
3236
 
3260
3237
 
3261
3238
cmp_item* cmp_item::get_comparator(Item_result type,
3262
 
                                   CHARSET_INFO *cs)
 
3239
                                   const CHARSET_INFO * const cs)
3263
3240
{
3264
3241
  switch (type) {
3265
3242
  case STRING_RESULT:
3273
3250
  case DECIMAL_RESULT:
3274
3251
    return new cmp_item_decimal;
3275
3252
  default:
3276
 
    DBUG_ASSERT(0);
 
3253
    assert(0);
3277
3254
    break;
3278
3255
  }
3279
3256
  return 0; // to satisfy compiler :)
3303
3280
 
3304
3281
cmp_item_row::~cmp_item_row()
3305
3282
{
3306
 
  DBUG_ENTER("~cmp_item_row");
3307
 
  DBUG_PRINT("enter",("this: 0x%lx", (long) this));
3308
3283
  if (comparators)
3309
3284
  {
3310
 
    for (uint i= 0; i < n; i++)
 
3285
    for (uint32_t i= 0; i < n; i++)
3311
3286
    {
3312
3287
      if (comparators[i])
3313
3288
        delete comparators[i];
3314
3289
    }
3315
3290
  }
3316
 
  DBUG_VOID_RETURN;
 
3291
  return;
3317
3292
}
3318
3293
 
3319
3294
 
3326
3301
 
3327
3302
void cmp_item_row::store_value(Item *item)
3328
3303
{
3329
 
  DBUG_ENTER("cmp_item_row::store_value");
3330
3304
  n= item->cols();
3331
3305
  alloc_comparators();
3332
3306
  if (comparators)
3333
3307
  {
3334
3308
    item->bring_value();
3335
3309
    item->null_value= 0;
3336
 
    for (uint i=0; i < n; i++)
 
3310
    for (uint32_t i=0; i < n; i++)
3337
3311
    {
3338
3312
      if (!comparators[i])
3339
3313
        if (!(comparators[i]=
3344
3318
      item->null_value|= item->element_index(i)->null_value;
3345
3319
    }
3346
3320
  }
3347
 
  DBUG_VOID_RETURN;
 
3321
  return;
3348
3322
}
3349
3323
 
3350
3324
 
3361
3335
  {
3362
3336
    item->bring_value();
3363
3337
    item->null_value= 0;
3364
 
    for (uint i=0; i < n; i++)
 
3338
    for (uint32_t i=0; i < n; i++)
3365
3339
    {
3366
3340
      if (!(comparators[i]= tmpl->comparators[i]->make_same()))
3367
3341
        break;                                  // new failed
3383
3357
  }
3384
3358
  bool was_null= 0;
3385
3359
  arg->bring_value();
3386
 
  for (uint i=0; i < n; i++)
 
3360
  for (uint32_t i=0; i < n; i++)
3387
3361
  {
3388
3362
    if (comparators[i]->cmp(arg->element_index(i)))
3389
3363
    {
3399
3373
int cmp_item_row::compare(cmp_item *c)
3400
3374
{
3401
3375
  cmp_item_row *l_cmp= (cmp_item_row *) c;
3402
 
  for (uint i=0; i < n; i++)
 
3376
  for (uint32_t i=0; i < n; i++)
3403
3377
  {
3404
3378
    int res;
3405
3379
    if ((res= comparators[i]->compare(l_cmp->comparators[i])))
3531
3505
}
3532
3506
 
3533
3507
 
3534
 
static int srtcmp_in(CHARSET_INFO *cs, const String *x,const String *y)
 
3508
static int srtcmp_in(const CHARSET_INFO * const cs, const String *x,const String *y)
3535
3509
{
3536
3510
  return cs->coll->strnncollsp(cs,
3537
 
                               (uchar *) x->ptr(),x->length(),
3538
 
                               (uchar *) y->ptr(),y->length(), 0);
 
3511
                               (unsigned char *) x->ptr(),x->length(),
 
3512
                               (unsigned char *) y->ptr(),y->length(), 0);
3539
3513
}
3540
3514
 
3541
3515
 
3548
3522
  /* true <=> arguments values will be compared as DATETIMEs. */
3549
3523
  bool compare_as_datetime= false;
3550
3524
  Item *date_arg= 0;
3551
 
  uint found_types= 0;
3552
 
  uint type_cnt= 0, i;
 
3525
  uint32_t found_types= 0;
 
3526
  uint32_t type_cnt= 0, i;
3553
3527
  Item_result cmp_type= STRING_RESULT;
3554
3528
  left_result_type= args[0]->result_type();
3555
3529
  if (!(found_types= collect_cmp_types(args, arg_count)))
3605
3579
    /* All DATE/DATETIME fields/functions has the STRING result type. */
3606
3580
    if (cmp_type == STRING_RESULT || cmp_type == ROW_RESULT)
3607
3581
    {
3608
 
      uint col, cols= args[0]->cols();
 
3582
      uint32_t col, cols= args[0]->cols();
3609
3583
 
3610
3584
      for (col= 0; col < cols; col++)
3611
3585
      {
3632
3606
            */
3633
3607
            if (!date_arg)
3634
3608
              date_arg= itm;
3635
 
            else if (itm->field_type() == MYSQL_TYPE_DATETIME)
 
3609
            else if (itm->field_type() == DRIZZLE_TYPE_DATETIME)
3636
3610
            {
3637
3611
              date_arg= itm;
3638
3612
              /* All arguments are already checked to have the STRING result. */
3685
3659
          cmp_type != INT_RESULT)
3686
3660
      {
3687
3661
        Item_field *field_item= (Item_field*) (args[0]->real_item());
3688
 
        if (field_item->field->can_be_compared_as_longlong())
 
3662
        if (field_item->field->can_be_compared_as_int64_t())
3689
3663
        {
3690
3664
          bool all_converted= true;
3691
3665
          for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3703
3677
                            cmp_collation.collation);
3704
3678
        break;
3705
3679
      case INT_RESULT:
3706
 
        array= new in_longlong(arg_count-1);
 
3680
        array= new in_int64_t(arg_count-1);
3707
3681
        break;
3708
3682
      case REAL_RESULT:
3709
3683
        array= new in_double(arg_count-1);
3720
3694
        array= new in_decimal(arg_count - 1);
3721
3695
        break;
3722
3696
      default:
3723
 
        DBUG_ASSERT(0);
 
3697
        assert(0);
3724
3698
        return;
3725
3699
      }
3726
3700
    }
3727
3701
    if (array && !(thd->is_fatal_error))                // If not EOM
3728
3702
    {
3729
 
      uint j=0;
3730
 
      for (uint i=1 ; i < arg_count ; i++)
 
3703
      uint32_t j=0;
 
3704
      for (uint32_t i=1 ; i < arg_count ; i++)
3731
3705
      {
3732
3706
        array->set(j,args[i]);
3733
3707
        if (!args[i]->null_value)                       // Skip NULL values
3802
3776
    Value of the function
3803
3777
*/
3804
3778
 
3805
 
longlong Item_func_in::val_int()
 
3779
int64_t Item_func_in::val_int()
3806
3780
{
3807
3781
  cmp_item *in_item;
3808
 
  DBUG_ASSERT(fixed == 1);
3809
 
  uint value_added_map= 0;
 
3782
  assert(fixed == 1);
 
3783
  uint32_t value_added_map= 0;
3810
3784
  if (array)
3811
3785
  {
3812
3786
    int tmp=array->find(args[0]);
3813
3787
    null_value=args[0]->null_value || (!tmp && have_null);
3814
 
    return (longlong) (!null_value && tmp != negated);
 
3788
    return (int64_t) (!null_value && tmp != negated);
3815
3789
  }
3816
3790
 
3817
 
  for (uint i= 1 ; i < arg_count ; i++)
 
3791
  for (uint32_t i= 1 ; i < arg_count ; i++)
3818
3792
  {
3819
3793
    Item_result cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
3820
3794
    in_item= cmp_items[(uint)cmp_type];
3821
 
    DBUG_ASSERT(in_item);
 
3795
    assert(in_item);
3822
3796
    if (!(value_added_map & (1 << (uint)cmp_type)))
3823
3797
    {
3824
3798
      in_item->store_value(args[0]);
3828
3802
      value_added_map|= 1 << (uint)cmp_type;
3829
3803
    }
3830
3804
    if (!in_item->cmp(args[i]) && !args[i]->null_value)
3831
 
      return (longlong) (!negated);
 
3805
      return (int64_t) (!negated);
3832
3806
    have_null|= args[i]->null_value;
3833
3807
  }
3834
3808
 
3835
3809
  null_value= have_null;
3836
 
  return (longlong) (!null_value && negated);
 
3810
  return (int64_t) (!null_value && negated);
3837
3811
}
3838
3812
 
3839
3813
 
3840
 
longlong Item_func_bit_or::val_int()
 
3814
int64_t Item_func_bit_or::val_int()
3841
3815
{
3842
 
  DBUG_ASSERT(fixed == 1);
3843
 
  ulonglong arg1= (ulonglong) args[0]->val_int();
 
3816
  assert(fixed == 1);
 
3817
  uint64_t arg1= (uint64_t) args[0]->val_int();
3844
3818
  if (args[0]->null_value)
3845
3819
  {
3846
3820
    null_value=1; /* purecov: inspected */
3847
3821
    return 0; /* purecov: inspected */
3848
3822
  }
3849
 
  ulonglong arg2= (ulonglong) args[1]->val_int();
 
3823
  uint64_t arg2= (uint64_t) args[1]->val_int();
3850
3824
  if (args[1]->null_value)
3851
3825
  {
3852
3826
    null_value=1;
3853
3827
    return 0;
3854
3828
  }
3855
3829
  null_value=0;
3856
 
  return (longlong) (arg1 | arg2);
 
3830
  return (int64_t) (arg1 | arg2);
3857
3831
}
3858
3832
 
3859
3833
 
3860
 
longlong Item_func_bit_and::val_int()
 
3834
int64_t Item_func_bit_and::val_int()
3861
3835
{
3862
 
  DBUG_ASSERT(fixed == 1);
3863
 
  ulonglong arg1= (ulonglong) args[0]->val_int();
 
3836
  assert(fixed == 1);
 
3837
  uint64_t arg1= (uint64_t) args[0]->val_int();
3864
3838
  if (args[0]->null_value)
3865
3839
  {
3866
3840
    null_value=1; /* purecov: inspected */
3867
3841
    return 0; /* purecov: inspected */
3868
3842
  }
3869
 
  ulonglong arg2= (ulonglong) args[1]->val_int();
 
3843
  uint64_t arg2= (uint64_t) args[1]->val_int();
3870
3844
  if (args[1]->null_value)
3871
3845
  {
3872
3846
    null_value=1; /* purecov: inspected */
3873
3847
    return 0; /* purecov: inspected */
3874
3848
  }
3875
3849
  null_value=0;
3876
 
  return (longlong) (arg1 & arg2);
 
3850
  return (int64_t) (arg1 & arg2);
3877
3851
}
3878
3852
 
3879
3853
Item_cond::Item_cond(THD *thd, Item_cond *item)
3896
3870
 
3897
3871
 
3898
3872
bool
3899
 
Item_cond::fix_fields(THD *thd, Item **ref)
 
3873
Item_cond::fix_fields(THD *thd, Item **ref __attribute__((unused)))
3900
3874
{
3901
 
  DBUG_ASSERT(fixed == 0);
 
3875
  assert(fixed == 0);
3902
3876
  List_iterator<Item> li(list);
3903
3877
  Item *item;
3904
3878
  void *orig_thd_marker= thd->thd_marker;
3905
 
  uchar buff[sizeof(char*)];                    // Max local vars in function
 
3879
  unsigned char buff[sizeof(char*)];                    // Max local vars in function
3906
3880
  not_null_tables_cache= used_tables_cache= 0;
3907
3881
  const_item_cache= 1;
3908
3882
 
3973
3947
}
3974
3948
 
3975
3949
 
3976
 
void Item_cond::fix_after_pullout(st_select_lex *new_parent, Item **ref)
 
3950
void Item_cond::fix_after_pullout(st_select_lex *new_parent, Item **ref __attribute__((unused)))
3977
3951
{
3978
3952
  List_iterator<Item> li(list);
3979
3953
  Item *item;
4005
3979
}
4006
3980
 
4007
3981
 
4008
 
bool Item_cond::walk(Item_processor processor, bool walk_subquery, uchar *arg)
 
3982
bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
4009
3983
{
4010
3984
  List_iterator_fast<Item> li(list);
4011
3985
  Item *item;
4034
4008
    Item returned as the result of transformation of the root node 
4035
4009
*/
4036
4010
 
4037
 
Item *Item_cond::transform(Item_transformer transformer, uchar *arg)
 
4011
Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
4038
4012
{
4039
4013
  List_iterator<Item> li(list);
4040
4014
  Item *item;
4081
4055
    Item returned as the result of transformation of the root node 
4082
4056
*/
4083
4057
 
4084
 
Item *Item_cond::compile(Item_analyzer analyzer, uchar **arg_p,
4085
 
                         Item_transformer transformer, uchar *arg_t)
 
4058
Item *Item_cond::compile(Item_analyzer analyzer, unsigned char **arg_p,
 
4059
                         Item_transformer transformer, unsigned char *arg_t)
4086
4060
{
4087
4061
  if (!(this->*analyzer)(arg_p))
4088
4062
    return 0;
4095
4069
      The same parameter value of arg_p must be passed
4096
4070
      to analyze any argument of the condition formula.
4097
4071
    */   
4098
 
    uchar *arg_v= *arg_p;
 
4072
    unsigned char *arg_v= *arg_p;
4099
4073
    Item *new_item= item->compile(analyzer, &arg_v, transformer, arg_t);
4100
4074
    if (new_item && new_item != item)
4101
4075
      li.replace(new_item);
4207
4181
      if (!(new_item= new Item_func_not(item)))
4208
4182
        return;                                 // Fatal OEM error
4209
4183
    }
4210
 
    VOID(li.replace(new_item));
 
4184
    li.replace(new_item);
4211
4185
  }
4212
4186
}
4213
4187
 
4232
4206
*/
4233
4207
 
4234
4208
 
4235
 
longlong Item_cond_and::val_int()
 
4209
int64_t Item_cond_and::val_int()
4236
4210
{
4237
 
  DBUG_ASSERT(fixed == 1);
 
4211
  assert(fixed == 1);
4238
4212
  List_iterator_fast<Item> li(list);
4239
4213
  Item *item;
4240
4214
  null_value= 0;
4250
4224
}
4251
4225
 
4252
4226
 
4253
 
longlong Item_cond_or::val_int()
 
4227
int64_t Item_cond_or::val_int()
4254
4228
{
4255
 
  DBUG_ASSERT(fixed == 1);
 
4229
  assert(fixed == 1);
4256
4230
  List_iterator_fast<Item> li(list);
4257
4231
  Item *item;
4258
4232
  null_value=0;
4311
4285
}
4312
4286
 
4313
4287
 
4314
 
longlong Item_func_isnull::val_int()
 
4288
int64_t Item_func_isnull::val_int()
4315
4289
{
4316
 
  DBUG_ASSERT(fixed == 1);
 
4290
  assert(fixed == 1);
4317
4291
  /*
4318
4292
    Handle optimization if the argument can't be null
4319
4293
    This has to be here because of the test in update_used_tables().
4323
4297
  return args[0]->is_null() ? 1: 0;
4324
4298
}
4325
4299
 
4326
 
longlong Item_is_not_null_test::val_int()
 
4300
int64_t Item_is_not_null_test::val_int()
4327
4301
{
4328
 
  DBUG_ASSERT(fixed == 1);
4329
 
  DBUG_ENTER("Item_is_not_null_test::val_int");
 
4302
  assert(fixed == 1);
4330
4303
  if (!used_tables_cache && !with_subselect)
4331
4304
  {
4332
4305
    owner->was_null|= (!cached_value);
4333
 
    DBUG_PRINT("info", ("cached: %ld", (long) cached_value));
4334
 
    DBUG_RETURN(cached_value);
 
4306
    return(cached_value);
4335
4307
  }
4336
4308
  if (args[0]->is_null())
4337
4309
  {
4338
 
    DBUG_PRINT("info", ("null"));
4339
4310
    owner->was_null|= 1;
4340
 
    DBUG_RETURN(0);
 
4311
    return(0);
4341
4312
  }
4342
4313
  else
4343
 
    DBUG_RETURN(1);
 
4314
    return(1);
4344
4315
}
4345
4316
 
4346
4317
/**
4351
4322
  if (!args[0]->maybe_null)
4352
4323
  {
4353
4324
    used_tables_cache= 0;                       /* is always true */
4354
 
    cached_value= (longlong) 1;
 
4325
    cached_value= (int64_t) 1;
4355
4326
  }
4356
4327
  else
4357
4328
  {
4359
4330
    if (!(used_tables_cache=args[0]->used_tables()) && !with_subselect)
4360
4331
    {
4361
4332
      /* Remember if the value is always NULL or never NULL */
4362
 
      cached_value= (longlong) !args[0]->is_null();
 
4333
      cached_value= (int64_t) !args[0]->is_null();
4363
4334
    }
4364
4335
  }
4365
4336
}
4366
4337
 
4367
4338
 
4368
 
longlong Item_func_isnotnull::val_int()
 
4339
int64_t Item_func_isnotnull::val_int()
4369
4340
{
4370
 
  DBUG_ASSERT(fixed == 1);
 
4341
  assert(fixed == 1);
4371
4342
  return args[0]->is_null() ? 0 : 1;
4372
4343
}
4373
4344
 
4380
4351
}
4381
4352
 
4382
4353
 
4383
 
longlong Item_func_like::val_int()
 
4354
int64_t Item_func_like::val_int()
4384
4355
{
4385
 
  DBUG_ASSERT(fixed == 1);
 
4356
  assert(fixed == 1);
4386
4357
  String* res = args[0]->val_str(&tmp_value1);
4387
4358
  if (args[0]->null_value)
4388
4359
  {
4430
4401
 
4431
4402
bool Item_func_like::fix_fields(THD *thd, Item **ref)
4432
4403
{
4433
 
  DBUG_ASSERT(fixed == 0);
 
4404
  assert(fixed == 0);
4434
4405
  if (Item_bool_func2::fix_fields(thd, ref) ||
4435
4406
      escape_item->fix_fields(thd, &escape_item))
4436
4407
    return true;
4447
4418
    String *escape_str= escape_item->val_str(&tmp_value1);
4448
4419
    if (escape_str)
4449
4420
    {
4450
 
      if (escape_used_in_parsing && (
4451
 
             (((thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
4452
 
                escape_str->numchars() != 1) ||
4453
 
               escape_str->numchars() > 1)))
 
4421
      if (escape_used_in_parsing && escape_str->numchars() > 1)
4454
4422
      {
4455
4423
        my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4456
4424
        return true;
4458
4426
 
4459
4427
      if (use_mb(cmp.cmp_collation.collation))
4460
4428
      {
4461
 
        CHARSET_INFO *cs= escape_str->charset();
 
4429
        const CHARSET_INFO * const cs= escape_str->charset();
4462
4430
        my_wc_t wc;
4463
4431
        int rc= cs->cset->mb_wc(cs, &wc,
4464
 
                                (const uchar*) escape_str->ptr(),
4465
 
                                (const uchar*) escape_str->ptr() +
 
4432
                                (const unsigned char*) escape_str->ptr(),
 
4433
                                (const unsigned char*) escape_str->ptr() +
4466
4434
                                               escape_str->length());
4467
4435
        escape= (int) (rc > 0 ? wc : '\\');
4468
4436
      }
4473
4441
          code instead of Unicode code as "escape" argument.
4474
4442
          Convert to "cs" if charset of escape differs.
4475
4443
        */
4476
 
        CHARSET_INFO *cs= cmp.cmp_collation.collation;
4477
 
        uint32 unused;
 
4444
        const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
 
4445
        uint32_t unused;
4478
4446
        if (escape_str->needs_conversion(escape_str->length(),
4479
4447
                                         escape_str->charset(), cs, &unused))
4480
4448
        {
4481
4449
          char ch;
4482
 
          uint errors;
4483
 
          uint32 cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
 
4450
          uint32_t errors;
 
4451
          uint32_t cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
4484
4452
                                          escape_str->length(),
4485
4453
                                          escape_str->charset(), &errors);
4486
4454
          escape= cnvlen ? ch : '\\';
4496
4464
      We could also do boyer-more for non-const items, but as we would have to
4497
4465
      recompute the tables for each row it's not worth it.
4498
4466
    */
4499
 
    if (args[1]->const_item() && !use_strnxfrm(collation.collation) &&
4500
 
       !(specialflag & SPECIAL_NO_NEW_FUNC))
 
4467
    if (args[1]->const_item() && !use_strnxfrm(collation.collation)) 
4501
4468
    {
4502
4469
      String* res2 = args[1]->val_str(&tmp_value2);
4503
4470
      if (!res2)
4523
4490
      {
4524
4491
        pattern     = first + 1;
4525
4492
        pattern_len = (int) len - 2;
4526
 
        DBUG_PRINT("info", ("Initializing pattern: '%s'", first));
4527
4493
        int *suff = (int*) thd->alloc((int) (sizeof(int)*
4528
4494
                                      ((pattern_len + 1)*2+
4529
4495
                                      alphabet_size)));
4531
4497
        bmBc      = bmGs + pattern_len + 1;
4532
4498
        turboBM_compute_good_suffix_shifts(suff);
4533
4499
        turboBM_compute_bad_character_shifts();
4534
 
        DBUG_PRINT("info",("done"));
4535
4500
      }
4536
4501
    }
4537
4502
  }
4545
4510
}
4546
4511
 
4547
4512
#ifdef LIKE_CMP_TOUPPER
4548
 
#define likeconv(cs,A) (uchar) (cs)->toupper(A)
 
4513
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
4549
4514
#else
4550
 
#define likeconv(cs,A) (uchar) (cs)->sort_order[(uchar) (A)]
 
4515
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
4551
4516
#endif
4552
4517
 
4553
4518
 
4561
4526
  int            f = 0;
4562
4527
  int            g = plm1;
4563
4528
  int *const splm1 = suff + plm1;
4564
 
  CHARSET_INFO  *cs= cmp.cmp_collation.collation;
 
4529
  const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
4565
4530
 
4566
4531
  *splm1 = pattern_len;
4567
4532
 
4576
4541
      else
4577
4542
      {
4578
4543
        if (i < g)
4579
 
          g = i; // g = min(i, g)
 
4544
          g = i; // g = cmin(i, g)
4580
4545
        f = i;
4581
4546
        while (g >= 0 && pattern[g] == pattern[g + plm1 - f])
4582
4547
          g--;
4595
4560
      else
4596
4561
      {
4597
4562
        if (i < g)
4598
 
          g = i; // g = min(i, g)
 
4563
          g = i; // g = cmin(i, g)
4599
4564
        f = i;
4600
4565
        while (g >= 0 &&
4601
4566
               likeconv(cs, pattern[g]) == likeconv(cs, pattern[g + plm1 - f]))
4661
4626
  int *end = bmBc + alphabet_size;
4662
4627
  int j;
4663
4628
  const int plm1 = pattern_len - 1;
4664
 
  CHARSET_INFO  *cs= cmp.cmp_collation.collation;
 
4629
  const CHARSET_INFO *const cs= cmp.cmp_collation.collation;
4665
4630
 
4666
4631
  for (i = bmBc; i < end; i++)
4667
4632
    *i = pattern_len;
4669
4634
  if (!cs->sort_order)
4670
4635
  {
4671
4636
    for (j = 0; j < plm1; j++)
4672
 
      bmBc[(uint) (uchar) pattern[j]] = plm1 - j;
 
4637
      bmBc[(uint) (unsigned char) pattern[j]] = plm1 - j;
4673
4638
  }
4674
4639
  else
4675
4640
  {
4693
4658
  int shift = pattern_len;
4694
4659
  int j     = 0;
4695
4660
  int u     = 0;
4696
 
  CHARSET_INFO  *cs= cmp.cmp_collation.collation;
 
4661
  const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
4697
4662
 
4698
4663
  const int plm1=  pattern_len - 1;
4699
4664
  const int tlmpl= text_len - pattern_len;
4715
4680
 
4716
4681
      register const int v = plm1 - i;
4717
4682
      turboShift = u - v;
4718
 
      bcShift    = bmBc[(uint) (uchar) text[i + j]] - plm1 + i;
4719
 
      shift      = max(turboShift, bcShift);
4720
 
      shift      = max(shift, bmGs[i]);
 
4683
      bcShift    = bmBc[(uint) (unsigned char) text[i + j]] - plm1 + i;
 
4684
      shift      = (turboShift > bcShift) ? turboShift : bcShift;
 
4685
      shift      = (shift > bmGs[i]) ? shift : bmGs[i];
4721
4686
      if (shift == bmGs[i])
4722
 
        u = min(pattern_len - shift, v);
 
4687
        u = (pattern_len - shift < v) ? pattern_len - shift : v;
4723
4688
      else
4724
4689
      {
4725
4690
        if (turboShift < bcShift)
4726
 
          shift = max(shift, u + 1);
 
4691
          shift = cmax(shift, u + 1);
4727
4692
        u = 0;
4728
4693
      }
4729
4694
      j+= shift;
4747
4712
      register const int v = plm1 - i;
4748
4713
      turboShift = u - v;
4749
4714
      bcShift    = bmBc[(uint) likeconv(cs, text[i + j])] - plm1 + i;
4750
 
      shift      = max(turboShift, bcShift);
4751
 
      shift      = max(shift, bmGs[i]);
 
4715
      shift      = (turboShift > bcShift) ? turboShift : bcShift;
 
4716
      shift      = cmax(shift, bmGs[i]);
4752
4717
      if (shift == bmGs[i])
4753
 
        u = min(pattern_len - shift, v);
 
4718
        u = (pattern_len - shift < v) ? pattern_len - shift : v;
4754
4719
      else
4755
4720
      {
4756
4721
        if (turboShift < bcShift)
4757
 
          shift = max(shift, u + 1);
 
4722
          shift = cmax(shift, u + 1);
4758
4723
        u = 0;
4759
4724
      }
4760
4725
      j+= shift;
4780
4745
    very fast to use.
4781
4746
*/
4782
4747
 
4783
 
longlong Item_cond_xor::val_int()
 
4748
int64_t Item_cond_xor::val_int()
4784
4749
{
4785
 
  DBUG_ASSERT(fixed == 1);
 
4750
  assert(fixed == 1);
4786
4751
  List_iterator<Item> li(list);
4787
4752
  Item *item;
4788
4753
  int result=0; 
4796
4761
      return 0;
4797
4762
    }
4798
4763
  }
4799
 
  return (longlong) result;
 
4764
  return (int64_t) result;
4800
4765
}
4801
4766
 
4802
4767
/**
4825
4790
    NULL if we cannot apply NOT transformation (see Item::neg_transformer()).
4826
4791
*/
4827
4792
 
4828
 
Item *Item_func_not::neg_transformer(THD *thd)  /* NOT(x)  ->  x */
 
4793
Item *Item_func_not::neg_transformer(THD *thd __attribute__((unused)))  /* NOT(x)  ->  x */
4829
4794
{
4830
4795
  return args[0];
4831
4796
}
4832
4797
 
4833
4798
 
4834
 
Item *Item_bool_rowready_func2::neg_transformer(THD *thd)
 
4799
Item *Item_bool_rowready_func2::neg_transformer(THD *thd __attribute__((unused)))
4835
4800
{
4836
4801
  Item *item= negated_item();
4837
4802
  return item;
4841
4806
/**
4842
4807
  a IS NULL  ->  a IS NOT NULL.
4843
4808
*/
4844
 
Item *Item_func_isnull::neg_transformer(THD *thd)
 
4809
Item *Item_func_isnull::neg_transformer(THD *thd __attribute__((unused)))
4845
4810
{
4846
4811
  Item *item= new Item_func_isnotnull(args[0]);
4847
4812
  return item;
4851
4816
/**
4852
4817
  a IS NOT NULL  ->  a IS NULL.
4853
4818
*/
4854
 
Item *Item_func_isnotnull::neg_transformer(THD *thd)
 
4819
Item *Item_func_isnotnull::neg_transformer(THD *thd __attribute__((unused)))
4855
4820
{
4856
4821
  Item *item= new Item_func_isnull(args[0]);
4857
4822
  return item;
4876
4841
}
4877
4842
 
4878
4843
 
4879
 
Item *Item_func_nop_all::neg_transformer(THD *thd)
 
4844
Item *Item_func_nop_all::neg_transformer(THD *thd __attribute__((unused)))
4880
4845
{
4881
4846
  /* "NOT (e $cmp$ ANY (SELECT ...)) -> e $rev_cmp$" ALL (SELECT ...) */
4882
4847
  Item_func_not_all *new_item= new Item_func_not_all(args[0]);
4887
4852
  return new_item;
4888
4853
}
4889
4854
 
4890
 
Item *Item_func_not_all::neg_transformer(THD *thd)
 
4855
Item *Item_func_not_all::neg_transformer(THD *thd __attribute__((unused)))
4891
4856
{
4892
4857
  /* "NOT (e $cmp$ ALL (SELECT ...)) -> e $rev_cmp$" ANY (SELECT ...) */
4893
4858
  Item_func_nop_all *new_item= new Item_func_nop_all(args[0]);
4938
4903
*/
4939
4904
Item *Item_bool_rowready_func2::negated_item()
4940
4905
{
4941
 
  DBUG_ASSERT(0);
 
4906
  assert(0);
4942
4907
  return 0;
4943
4908
}
4944
4909
 
4994
4959
  fields.push_back(f);
4995
4960
}
4996
4961
 
4997
 
uint Item_equal::members()
 
4962
uint32_t Item_equal::members()
4998
4963
{
4999
4964
  return fields.elements;
5000
4965
}
5127
5092
  }
5128
5093
}
5129
5094
 
5130
 
bool Item_equal::fix_fields(THD *thd, Item **ref)
 
5095
bool Item_equal::fix_fields(THD *thd __attribute__((unused)), Item **ref __attribute__((unused)))
5131
5096
{
5132
5097
  List_iterator_fast<Item_field> li(fields);
5133
5098
  Item *item;
5162
5127
  }
5163
5128
}
5164
5129
 
5165
 
longlong Item_equal::val_int()
 
5130
int64_t Item_equal::val_int()
5166
5131
{
5167
5132
  Item_field *item_field;
5168
5133
  if (cond_false)
5191
5156
                                      item->collation.collation);
5192
5157
}
5193
5158
 
5194
 
bool Item_equal::walk(Item_processor processor, bool walk_subquery, uchar *arg)
 
5159
bool Item_equal::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
5195
5160
{
5196
5161
  List_iterator_fast<Item_field> it(fields);
5197
5162
  Item *item;
5203
5168
  return Item_func::walk(processor, walk_subquery, arg);
5204
5169
}
5205
5170
 
5206
 
Item *Item_equal::transform(Item_transformer transformer, uchar *arg)
 
5171
Item *Item_equal::transform(Item_transformer transformer, unsigned char *arg)
5207
5172
{
5208
5173
  List_iterator<Item_field> it(fields);
5209
5174
  Item *item;