~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/item_cmpfunc.cc

  • Committer: Monty Taylor
  • Date: 2008-07-22 05:48:51 UTC
  • mto: (202.1.3 toru)
  • mto: This revision was merged to the branch mainline in revision 204.
  • Revision ID: monty@inaugust.com-20080722054851-airxt73370725p7x
Re-enabled optimizations for the normal build, and added back the --with-debug option to turn them off. 

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
 
#include <drizzled/server_includes.h>
25
 
#include <drizzled/sql_select.h>
 
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"
26
31
 
27
32
static bool convert_constant_item(THD *, Item_field *, Item **);
28
33
 
29
34
static Item_result item_store_type(Item_result a, Item *item,
30
 
                                   bool unsigned_flag)
 
35
                                   my_bool unsigned_flag)
31
36
{
32
37
  Item_result b= item->result_type();
33
38
 
42
47
    return INT_RESULT;
43
48
}
44
49
 
45
 
static void agg_result_type(Item_result *type, Item **items, uint32_t nitems)
 
50
static void agg_result_type(Item_result *type, Item **items, uint nitems)
46
51
{
47
52
  Item **item, **item_end;
48
 
  bool unsigned_flag= 0;
 
53
  my_bool unsigned_flag= 0;
49
54
 
50
55
  *type= STRING_RESULT;
51
56
  /* Skip beginning NULL items */
90
95
 
91
96
static int cmp_row_type(Item* item1, Item* item2)
92
97
{
93
 
  uint32_t n= item1->cols();
 
98
  uint n= item1->cols();
94
99
  if (item2->check_cols(n))
95
100
    return 1;
96
 
  for (uint32_t i=0; i<n; i++)
 
101
  for (uint i=0; i<n; i++)
97
102
  {
98
103
    if (item2->element_index(i)->check_cols(item1->element_index(i)->cols()) ||
99
104
        (item1->element_index(i)->result_type() == ROW_RESULT &&
127
132
    0  otherwise
128
133
*/
129
134
 
130
 
static int agg_cmp_type(Item_result *type, Item **items, uint32_t nitems)
 
135
static int agg_cmp_type(Item_result *type, Item **items, uint nitems)
131
136
{
132
 
  uint32_t i;
 
137
  uint i;
133
138
  type[0]= items[0]->result_type();
134
139
  for (i= 1 ; i < nitems ; i++)
135
140
  {
166
171
  @return aggregated field type.
167
172
*/
168
173
 
169
 
enum_field_types agg_field_type(Item **items, uint32_t nitems)
 
174
enum_field_types agg_field_type(Item **items, uint nitems)
170
175
{
171
 
  uint32_t i;
 
176
  uint i;
172
177
  if (!nitems || items[0]->result_type() == ROW_RESULT )
173
178
    return (enum_field_types)-1;
174
179
  enum_field_types res= items[0]->field_type();
194
199
    Bitmap of collected types - otherwise
195
200
*/
196
201
 
197
 
static uint32_t collect_cmp_types(Item **items, uint32_t nitems)
 
202
static uint collect_cmp_types(Item **items, uint nitems)
198
203
{
199
 
  uint32_t i;
200
 
  uint32_t found_types;
 
204
  uint i;
 
205
  uint found_types;
201
206
  Item_result left_result= items[0]->result_type();
202
207
  assert(nitems > 1);
203
208
  found_types= 0;
386
391
 
387
392
  if (!(*item)->with_subselect && (*item)->const_item())
388
393
  {
 
394
    TABLE *table= field->table;
389
395
    ulong orig_sql_mode= thd->variables.sql_mode;
390
396
    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;
391
399
    uint64_t orig_field_val= 0; /* original field value if valid */
392
400
 
 
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
    }
393
406
    /* For comparison purposes allow invalid dates like 2000-01-32 */
394
407
    thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) | 
395
408
                             MODE_INVALID_DATES;
418
431
    }
419
432
    thd->variables.sql_mode= orig_sql_mode;
420
433
    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
    }
421
439
  }
422
440
  return result;
423
441
}
510
528
  switch (type) {
511
529
  case ROW_RESULT:
512
530
  {
513
 
    uint32_t n= (*a)->cols();
 
531
    uint n= (*a)->cols();
514
532
    if (n != (*b)->cols())
515
533
    {
516
534
      my_error(ER_OPERAND_COLUMNS, MYF(0), n);
519
537
    }
520
538
    if (!(comparators= new Arg_comparator[n]))
521
539
      return 1;
522
 
    for (uint32_t i=0; i < n; i++)
 
540
    for (uint i=0; i < n; i++)
523
541
    {
524
542
      if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
525
543
      {
561
579
        which would be transformed to:
562
580
        WHERE col= 'j'
563
581
      */
564
 
      (*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
565
 
      (*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
 
582
      (*a)->walk(&Item::set_no_const_sub, false, (uchar*) 0);
 
583
      (*b)->walk(&Item::set_no_const_sub, false, (uchar*) 0);
566
584
    }
567
585
    break;
568
586
  }
590
608
  {
591
609
    if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
592
610
    {
593
 
      precision= 5 / log_10[cmax((*a)->decimals, (*b)->decimals) + 1];
 
611
      precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
594
612
      if (func == &Arg_comparator::compare_real)
595
613
        func= &Arg_comparator::compare_real_fixed;
596
614
      else if (func == &Arg_comparator::compare_e_real)
628
646
*/
629
647
 
630
648
static uint64_t
631
 
get_date_from_str(THD *thd, String *str, enum enum_drizzle_timestamp_type warn_type,
 
649
get_date_from_str(THD *thd, String *str, timestamp_type warn_type,
632
650
                  char *warn_name, bool *error_arg)
633
651
{
634
652
  uint64_t value= 0;
635
653
  int error;
636
 
  DRIZZLE_TIME l_time;
637
 
  enum enum_drizzle_timestamp_type ret;
 
654
  MYSQL_TIME l_time;
 
655
  enum_mysql_timestamp_type ret;
638
656
 
639
657
  ret= str_to_datetime(str->ptr(), str->length(), &l_time,
640
658
                       (TIME_FUZZY_DATE | MODE_INVALID_DATES |
641
 
                        (thd->variables.sql_mode & MODE_NO_ZERO_DATE)),
 
659
                        (thd->variables.sql_mode &
 
660
                         (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE))),
642
661
                       &error);
643
662
 
644
 
  if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
 
663
  if (ret == MYSQL_TIMESTAMP_DATETIME || ret == MYSQL_TIMESTAMP_DATE)
645
664
  {
646
665
    /*
647
666
      Do not return yet, we may still want to throw a "trailing garbage"
657
676
  }
658
677
 
659
678
  if (error > 0)
660
 
    make_truncated_value_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
679
    make_truncated_value_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
661
680
                                 str->ptr(), str->length(),
662
681
                                 warn_type, warn_name);
663
682
 
738
757
      uint64_t value;
739
758
      bool error;
740
759
      String tmp, *str_val= 0;
741
 
      enum enum_drizzle_timestamp_type t_type= (date_arg->field_type() == DRIZZLE_TYPE_NEWDATE ?
742
 
                              DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME);
 
760
      timestamp_type t_type= (date_arg->field_type() == MYSQL_TYPE_NEWDATE ?
 
761
                              MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME);
743
762
 
744
763
      str_val= str_arg->val_str(&tmp);
745
764
      if (str_arg->null_value)
781
800
*/
782
801
 
783
802
uint64_t
784
 
get_time_value(THD *thd __attribute__((unused)),
 
803
get_time_value(THD *thd __attribute__((__unused__)),
785
804
               Item ***item_arg, Item **cache_arg,
786
 
               Item *warn_item __attribute__((unused)),
 
805
               Item *warn_item __attribute__((__unused__)),
787
806
               bool *is_null)
788
807
{
789
808
  uint64_t value;
790
809
  Item *item= **item_arg;
791
 
  DRIZZLE_TIME ltime;
 
810
  MYSQL_TIME ltime;
792
811
 
793
812
  if (item->result_as_int64_t())
794
813
  {
859
878
    get_value_func= &get_datetime_value;
860
879
    return 0;
861
880
  }
862
 
  else if (type == STRING_RESULT && (*a)->field_type() == DRIZZLE_TYPE_TIME &&
863
 
           (*b)->field_type() == DRIZZLE_TYPE_TIME)
 
881
  else if (type == STRING_RESULT && (*a)->field_type() == MYSQL_TYPE_TIME &&
 
882
           (*b)->field_type() == MYSQL_TYPE_TIME)
864
883
  {
865
884
    /* Compare TIME values as integers. */
866
885
    thd= current_thd;
937
956
    *is_null= item->null_value;
938
957
    enum_field_types f_type= item->field_type();
939
958
    /*
940
 
      Item_date_add_interval may return DRIZZLE_TYPE_STRING as the result
 
959
      Item_date_add_interval may return MYSQL_TYPE_STRING as the result
941
960
      field type. To detect that the DATE value has been returned we
942
961
      compare it with 100000000L - any DATE value should be less than it.
943
962
      Don't shift cached DATETIME values up for the second time.
944
963
    */
945
 
    if (f_type == DRIZZLE_TYPE_NEWDATE ||
946
 
        (f_type != DRIZZLE_TYPE_DATETIME && value < 100000000L))
 
964
    if (f_type == MYSQL_TYPE_NEWDATE ||
 
965
        (f_type != MYSQL_TYPE_DATETIME && value < 100000000L))
947
966
      value*= 1000000L;
948
967
  }
949
968
  else
963
982
  {
964
983
    bool error;
965
984
    enum_field_types f_type= warn_item->field_type();
966
 
    enum enum_drizzle_timestamp_type t_type= f_type ==
967
 
      DRIZZLE_TYPE_NEWDATE ? DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME;
 
985
    timestamp_type t_type= f_type ==
 
986
      MYSQL_TYPE_NEWDATE ? MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME;
968
987
    value= get_date_from_str(thd, str, t_type, warn_item->name, &error);
969
988
    /*
970
989
      If str did not contain a valid date according to the current
980
999
  if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
981
1000
      ((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
982
1001
  {
983
 
    Item_cache_int *cache= new Item_cache_int(DRIZZLE_TYPE_DATETIME);
 
1002
    Item_cache_int *cache= new Item_cache_int(MYSQL_TYPE_DATETIME);
984
1003
    /* Mark the cache as non-const to prevent re-caching. */
985
1004
    cache->set_used_tables(1);
986
1005
    cache->store(item, value);
1079
1098
    if ((res2= (*b)->val_str(&owner->tmp_value2)))
1080
1099
    {
1081
1100
      owner->null_value= 0;
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));
 
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));
1085
1104
      return cmp ? cmp : (int) (res1_length - res2_length);
1086
1105
    }
1087
1106
  }
1337
1356
  bool was_null= 0;
1338
1357
  (*a)->bring_value();
1339
1358
  (*b)->bring_value();
1340
 
  uint32_t n= (*a)->cols();
1341
 
  for (uint32_t i= 0; i<n; i++)
 
1359
  uint n= (*a)->cols();
 
1360
  for (uint i= 0; i<n; i++)
1342
1361
  {
1343
1362
    res= comparators[i].compare();
1344
1363
    if (owner->null_value)
1380
1399
{
1381
1400
  (*a)->bring_value();
1382
1401
  (*b)->bring_value();
1383
 
  uint32_t n= (*a)->cols();
1384
 
  for (uint32_t i= 0; i<n; i++)
 
1402
  uint n= (*a)->cols();
 
1403
  for (uint i= 0; i<n; i++)
1385
1404
  {
1386
1405
    if (!comparators[i].compare())
1387
1406
      return 0;
1443
1462
}
1444
1463
 
1445
1464
 
1446
 
bool Item_in_optimizer::fix_left(THD *thd, Item **ref __attribute__((unused)))
 
1465
bool Item_in_optimizer::fix_left(THD *thd, Item **ref __attribute__((__unused__)))
1447
1466
{
1448
1467
  if ((!args[0]->fixed && args[0]->fix_fields(thd, args)) ||
1449
1468
      (!cache && !(cache= Item_cache::get_cache(args[0]))))
1459
1478
  }
1460
1479
  else
1461
1480
  {
1462
 
    uint32_t n= cache->cols();
1463
 
    for (uint32_t i= 0; i < n; i++)
 
1481
    uint n= cache->cols();
 
1482
    for (uint i= 0; i < n; i++)
1464
1483
    {
1465
1484
      if (args[0]->element_index(i)->used_tables())
1466
1485
        ((Item_cache *)cache->element_index(i))->set_used_tables(OUTER_REF_TABLE_BIT);
1547
1566
        }
1548
1567
        else
1549
1568
        {
1550
 
          uint32_t i;
1551
 
          uint32_t ncols= cache->cols();
 
1569
          uint i;
 
1570
          uint ncols= cache->cols();
1552
1571
          /*
1553
1572
            Turn off the predicates that are based on column compares for
1554
1573
            which the left part is currently NULL
1621
1640
    @retval NULL if an error occurred
1622
1641
*/
1623
1642
 
1624
 
Item *Item_in_optimizer::transform(Item_transformer transformer, unsigned char *argument)
 
1643
Item *Item_in_optimizer::transform(Item_transformer transformer, uchar *argument)
1625
1644
{
1626
1645
  Item *new_item;
1627
1646
 
1753
1772
    return 0;
1754
1773
  if (negated != ((Item_func_opt_neg *) item_func)->negated)
1755
1774
    return 0;
1756
 
  for (uint32_t i=0; i < arg_count ; i++)
 
1775
  for (uint i=0; i < arg_count ; i++)
1757
1776
    if (!args[i]->eq(item_func->arguments()[i], binary_cmp))
1758
1777
      return 0;
1759
1778
  return 1;
1762
1781
 
1763
1782
void Item_func_interval::fix_length_and_dec()
1764
1783
{
1765
 
  uint32_t rows= row->cols();
 
1784
  uint rows= row->cols();
1766
1785
  
1767
1786
  use_decimal_comparison= ((row->element_index(0)->result_type() ==
1768
1787
                            DECIMAL_RESULT) ||
1772
1791
  {
1773
1792
    bool not_null_consts= true;
1774
1793
 
1775
 
    for (uint32_t i= 1; not_null_consts && i < rows; i++)
 
1794
    for (uint i= 1; not_null_consts && i < rows; i++)
1776
1795
    {
1777
1796
      Item *el= row->element_index(i);
1778
1797
      not_null_consts&= el->const_item() & !el->is_null();
1784
1803
    {
1785
1804
      if (use_decimal_comparison)
1786
1805
      {
1787
 
        for (uint32_t i= 1; i < rows; i++)
 
1806
        for (uint i= 1; i < rows; i++)
1788
1807
        {
1789
1808
          Item *el= row->element_index(i);
1790
1809
          interval_range *range= intervals + (i-1);
1809
1828
      }
1810
1829
      else
1811
1830
      {
1812
 
        for (uint32_t i= 1; i < rows; i++)
 
1831
        for (uint i= 1; i < rows; i++)
1813
1832
        {
1814
1833
          intervals[i-1].dbl= row->element_index(i)->val_real();
1815
1834
        }
1844
1863
  assert(fixed == 1);
1845
1864
  double value;
1846
1865
  my_decimal dec_buf, *dec= NULL;
1847
 
  uint32_t i;
 
1866
  uint i;
1848
1867
 
1849
1868
  if (use_decimal_comparison)
1850
1869
  {
1862
1881
 
1863
1882
  if (intervals)
1864
1883
  {                                     // Use binary search to find interval
1865
 
    uint32_t start,end;
 
1884
    uint start,end;
1866
1885
    start= 0;
1867
1886
    end=   row->cols()-2;
1868
1887
    while (start != end)
1869
1888
    {
1870
 
      uint32_t mid= (start + end + 1) / 2;
 
1889
      uint mid= (start + end + 1) / 2;
1871
1890
      interval_range *range= intervals + mid;
1872
 
      bool cmp_result;
 
1891
      my_bool cmp_result;
1873
1892
      /*
1874
1893
        The values in the range intervall may have different types,
1875
1894
        Only do a decimal comparision of the first argument is a decimal
2001
2020
        datetime_found= true;
2002
2021
        continue;
2003
2022
      }
2004
 
      if (args[i]->field_type() == DRIZZLE_TYPE_TIME &&
 
2023
      if (args[i]->field_type() == MYSQL_TYPE_TIME &&
2005
2024
          args[i]->result_as_int64_t())
2006
2025
        time_items_found++;
2007
2026
    }
2166
2185
{
2167
2186
  agg_result_type(&hybrid_type, args, 2);
2168
2187
  maybe_null=args[1]->maybe_null;
2169
 
  decimals= cmax(args[0]->decimals, args[1]->decimals);
 
2188
  decimals= max(args[0]->decimals, args[1]->decimals);
2170
2189
  unsigned_flag= args[0]->unsigned_flag && args[1]->unsigned_flag;
2171
2190
 
2172
2191
  if (hybrid_type == DECIMAL_RESULT || hybrid_type == INT_RESULT) 
2177
2196
    int len1= args[1]->max_length - args[1]->decimals
2178
2197
      - (args[1]->unsigned_flag ? 0 : 1);
2179
2198
 
2180
 
    max_length= cmax(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
 
2199
    max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2181
2200
  }
2182
2201
  else
2183
 
    max_length= cmax(args[0]->max_length, args[1]->max_length);
 
2202
    max_length= max(args[0]->max_length, args[1]->max_length);
2184
2203
 
2185
2204
  switch (hybrid_type) {
2186
2205
  case STRING_RESULT:
2200
2219
}
2201
2220
 
2202
2221
 
2203
 
uint32_t Item_func_ifnull::decimal_precision() const
 
2222
uint Item_func_ifnull::decimal_precision() const
2204
2223
{
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);
 
2224
  int max_int_part=max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
 
2225
  return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2207
2226
}
2208
2227
 
2209
2228
 
2212
2231
  return cached_field_type;
2213
2232
}
2214
2233
 
2215
 
Field *Item_func_ifnull::tmp_table_field(Table *table)
 
2234
Field *Item_func_ifnull::tmp_table_field(TABLE *table)
2216
2235
{
2217
2236
  return tmp_table_field_from_field_type(table, 0);
2218
2237
}
2331
2350
Item_func_if::fix_length_and_dec()
2332
2351
{
2333
2352
  maybe_null=args[1]->maybe_null || args[2]->maybe_null;
2334
 
  decimals= cmax(args[1]->decimals, args[2]->decimals);
 
2353
  decimals= max(args[1]->decimals, args[2]->decimals);
2335
2354
  unsigned_flag=args[1]->unsigned_flag && args[2]->unsigned_flag;
2336
2355
 
2337
2356
  enum Item_result arg1_type=args[1]->result_type();
2375
2394
    int len2= args[2]->max_length - args[2]->decimals
2376
2395
      - (args[2]->unsigned_flag ? 0 : 1);
2377
2396
 
2378
 
    max_length=cmax(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
 
2397
    max_length=max(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
2379
2398
  }
2380
2399
  else
2381
 
    max_length= cmax(args[1]->max_length, args[2]->max_length);
 
2400
    max_length= max(args[1]->max_length, args[2]->max_length);
2382
2401
}
2383
2402
 
2384
2403
 
2385
 
uint32_t Item_func_if::decimal_precision() const
 
2404
uint Item_func_if::decimal_precision() const
2386
2405
{
2387
 
  int precision=(cmax(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
 
2406
  int precision=(max(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
2388
2407
                 decimals);
2389
 
  return cmin(precision, DECIMAL_MAX_PRECISION);
 
2408
  return min(precision, DECIMAL_MAX_PRECISION);
2390
2409
}
2391
2410
 
2392
2411
 
2552
2571
           failed
2553
2572
*/
2554
2573
 
2555
 
Item *Item_func_case::find_item(String *str __attribute__((unused)))
 
2574
Item *Item_func_case::find_item(String *str __attribute__((__unused__)))
2556
2575
{
2557
 
  uint32_t value_added_map= 0;
 
2576
  uint value_added_map= 0;
2558
2577
 
2559
2578
  if (first_expr_num == -1)
2560
2579
  {
2561
 
    for (uint32_t i=0 ; i < ncases ; i+=2)
 
2580
    for (uint i=0 ; i < ncases ; i+=2)
2562
2581
    {
2563
2582
      // No expression between CASE and the first WHEN
2564
2583
      if (args[i]->val_bool())
2569
2588
  else
2570
2589
  {
2571
2590
    /* Compare every WHEN argument with it and return the first match */
2572
 
    for (uint32_t i=0 ; i < ncases ; i+=2)
 
2591
    for (uint i=0 ; i < ncases ; i+=2)
2573
2592
    {
2574
2593
      cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
2575
2594
      assert(cmp_type != ROW_RESULT);
2671
2690
    buff should match stack usage from
2672
2691
    Item_func_case::val_int() -> Item_func_case::find_item()
2673
2692
  */
2674
 
  unsigned char buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(int64_t)*2];
 
2693
  uchar buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(int64_t)*2];
2675
2694
  bool res= Item_func::fix_fields(thd, ref);
2676
2695
  /*
2677
2696
    Call check_stack_overrun after fix_fields to be sure that stack variable
2693
2712
 
2694
2713
void Item_func_case::agg_num_lengths(Item *arg)
2695
2714
{
2696
 
  uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
 
2715
  uint len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2697
2716
                                           arg->unsigned_flag) - arg->decimals;
2698
2717
  set_if_bigger(max_length, len); 
2699
2718
  set_if_bigger(decimals, arg->decimals);
2704
2723
void Item_func_case::fix_length_and_dec()
2705
2724
{
2706
2725
  Item **agg;
2707
 
  uint32_t nagg;
2708
 
  uint32_t found_types= 0;
 
2726
  uint nagg;
 
2727
  uint found_types= 0;
2709
2728
  if (!(agg= (Item**) sql_alloc(sizeof(Item*)*(ncases+1))))
2710
2729
    return;
2711
2730
  
2732
2751
  */
2733
2752
  if (first_expr_num != -1)
2734
2753
  {
2735
 
    uint32_t i;
 
2754
    uint i;
2736
2755
    agg[0]= args[first_expr_num];
2737
2756
    left_result_type= agg[0]->result_type();
2738
2757
 
2766
2785
  unsigned_flag= true;
2767
2786
  if (cached_result_type == STRING_RESULT)
2768
2787
  {
2769
 
    for (uint32_t i= 0; i < ncases; i+= 2)
 
2788
    for (uint i= 0; i < ncases; i+= 2)
2770
2789
      agg_str_lengths(args[i + 1]);
2771
2790
    if (else_expr_num != -1)
2772
2791
      agg_str_lengths(args[else_expr_num]);
2773
2792
  }
2774
2793
  else
2775
2794
  {
2776
 
    for (uint32_t i= 0; i < ncases; i+= 2)
 
2795
    for (uint i= 0; i < ncases; i+= 2)
2777
2796
      agg_num_lengths(args[i + 1]);
2778
2797
    if (else_expr_num != -1) 
2779
2798
      agg_num_lengths(args[else_expr_num]);
2783
2802
}
2784
2803
 
2785
2804
 
2786
 
uint32_t Item_func_case::decimal_precision() const
 
2805
uint Item_func_case::decimal_precision() const
2787
2806
{
2788
2807
  int max_int_part=0;
2789
 
  for (uint32_t i=0 ; i < ncases ; i+=2)
 
2808
  for (uint i=0 ; i < ncases ; i+=2)
2790
2809
    set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
2791
2810
 
2792
2811
  if (else_expr_num != -1) 
2793
2812
    set_if_bigger(max_int_part, args[else_expr_num]->decimal_int_part());
2794
 
  return cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
 
2813
  return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2795
2814
}
2796
2815
 
2797
2816
 
2808
2827
    args[first_expr_num]->print(str, query_type);
2809
2828
    str->append(' ');
2810
2829
  }
2811
 
  for (uint32_t i=0 ; i < ncases ; i+=2)
 
2830
  for (uint i=0 ; i < ncases ; i+=2)
2812
2831
  {
2813
2832
    str->append(STRING_WITH_LEN("when "));
2814
2833
    args[i]->print(str, query_type);
2828
2847
 
2829
2848
void Item_func_case::cleanup()
2830
2849
{
2831
 
  uint32_t i;
 
2850
  uint i;
2832
2851
  Item_func::cleanup();
2833
2852
  for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
2834
2853
  {
2847
2866
{
2848
2867
  assert(fixed == 1);
2849
2868
  null_value=0;
2850
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
2869
  for (uint i=0 ; i < arg_count ; i++)
2851
2870
  {
2852
2871
    String *res;
2853
2872
    if ((res=args[i]->val_str(str)))
2861
2880
{
2862
2881
  assert(fixed == 1);
2863
2882
  null_value=0;
2864
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
2883
  for (uint i=0 ; i < arg_count ; i++)
2865
2884
  {
2866
2885
    int64_t res=args[i]->val_int();
2867
2886
    if (!args[i]->null_value)
2875
2894
{
2876
2895
  assert(fixed == 1);
2877
2896
  null_value=0;
2878
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
2897
  for (uint i=0 ; i < arg_count ; i++)
2879
2898
  {
2880
2899
    double res= args[i]->val_real();
2881
2900
    if (!args[i]->null_value)
2890
2909
{
2891
2910
  assert(fixed == 1);
2892
2911
  null_value= 0;
2893
 
  for (uint32_t i= 0; i < arg_count; i++)
 
2912
  for (uint i= 0; i < arg_count; i++)
2894
2913
  {
2895
2914
    my_decimal *res= args[i]->val_decimal(decimal_value);
2896
2915
    if (!args[i]->null_value)
3002
3021
    0           left argument is equal to the right argument.
3003
3022
    1           left argument is greater than the right argument.
3004
3023
*/
3005
 
int cmp_int64_t(void *cmp_arg __attribute__((unused)),
 
3024
int cmp_int64_t(void *cmp_arg __attribute__((__unused__)),
3006
3025
                 in_int64_t::packed_int64_t *a,
3007
3026
                 in_int64_t::packed_int64_t *b)
3008
3027
{
3027
3046
    return cmp_longs (a->val, b->val);
3028
3047
}
3029
3048
 
3030
 
static int cmp_double(void *cmp_arg __attribute__((unused)), double *a,double *b)
 
3049
static int cmp_double(void *cmp_arg __attribute__((__unused__)), double *a,double *b)
3031
3050
{
3032
3051
  return *a < *b ? -1 : *a == *b ? 0 : 1;
3033
3052
}
3034
3053
 
3035
 
static int cmp_row(void *cmp_arg __attribute__((unused)), cmp_item_row *a, cmp_item_row *b)
 
3054
static int cmp_row(void *cmp_arg __attribute__((__unused__)), cmp_item_row *a, cmp_item_row *b)
3036
3055
{
3037
3056
  return a->compare(b);
3038
3057
}
3039
3058
 
3040
3059
 
3041
 
static int cmp_decimal(void *cmp_arg __attribute__((unused)), my_decimal *a, my_decimal *b)
 
3060
static int cmp_decimal(void *cmp_arg __attribute__((__unused__)), my_decimal *a, my_decimal *b)
3042
3061
{
3043
3062
  /*
3044
3063
    We need call of fixing buffer pointer, because fast sort just copy
3052
3071
 
3053
3072
int in_vector::find(Item *item)
3054
3073
{
3055
 
  unsigned char *result=get_value(item);
 
3074
  uchar *result=get_value(item);
3056
3075
  if (!result || !used_count)
3057
3076
    return 0;                           // Null value
3058
3077
 
3059
 
  uint32_t start,end;
 
3078
  uint start,end;
3060
3079
  start=0; end=used_count-1;
3061
3080
  while (start != end)
3062
3081
  {
3063
 
    uint32_t mid=(start+end+1)/2;
 
3082
    uint mid=(start+end+1)/2;
3064
3083
    int res;
3065
3084
    if ((res=(*compare)(collation, base+mid*size, result)) == 0)
3066
3085
      return 1;
3072
3091
  return (int) ((*compare)(collation, base+start*size, result) == 0);
3073
3092
}
3074
3093
 
3075
 
in_string::in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs)
 
3094
in_string::in_string(uint elements,qsort2_cmp cmp_func, CHARSET_INFO *cs)
3076
3095
  :in_vector(elements, sizeof(String), cmp_func, cs),
3077
3096
   tmp(buff, sizeof(buff), &my_charset_bin)
3078
3097
{}
3082
3101
  if (base)
3083
3102
  {
3084
3103
    // base was allocated with help of sql_alloc => following is OK
3085
 
    for (uint32_t i=0 ; i < count ; i++)
 
3104
    for (uint i=0 ; i < count ; i++)
3086
3105
      ((String*) base)[i].free();
3087
3106
  }
3088
3107
}
3089
3108
 
3090
 
void in_string::set(uint32_t pos,Item *item)
 
3109
void in_string::set(uint pos,Item *item)
3091
3110
{
3092
3111
  String *str=((String*) base)+pos;
3093
3112
  String *res=item->val_str(str);
3102
3121
  }
3103
3122
  if (!str->charset())
3104
3123
  {
3105
 
    const CHARSET_INFO *cs;
 
3124
    CHARSET_INFO *cs;
3106
3125
    if (!(cs= item->collation.collation))
3107
3126
      cs= &my_charset_bin;              // Should never happen for STR items
3108
3127
    str->set_charset(cs);
3110
3129
}
3111
3130
 
3112
3131
 
3113
 
unsigned char *in_string::get_value(Item *item)
 
3132
uchar *in_string::get_value(Item *item)
3114
3133
{
3115
 
  return (unsigned char*) item->val_str(&tmp);
 
3134
  return (uchar*) item->val_str(&tmp);
3116
3135
}
3117
3136
 
3118
 
in_row::in_row(uint32_t elements, Item * item __attribute__((unused)))
 
3137
in_row::in_row(uint elements, Item * item __attribute__((__unused__)))
3119
3138
{
3120
3139
  base= (char*) new cmp_item_row[count= elements];
3121
3140
  size= sizeof(cmp_item_row);
3134
3153
    delete [] (cmp_item_row*) base;
3135
3154
}
3136
3155
 
3137
 
unsigned char *in_row::get_value(Item *item)
 
3156
uchar *in_row::get_value(Item *item)
3138
3157
{
3139
3158
  tmp.store_value(item);
3140
3159
  if (item->is_null())
3141
3160
    return 0;
3142
 
  return (unsigned char *)&tmp;
 
3161
  return (uchar *)&tmp;
3143
3162
}
3144
3163
 
3145
 
void in_row::set(uint32_t pos, Item *item)
 
3164
void in_row::set(uint pos, Item *item)
3146
3165
{
3147
3166
  ((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3148
3167
  return;
3149
3168
}
3150
3169
 
3151
 
in_int64_t::in_int64_t(uint32_t elements)
 
3170
in_int64_t::in_int64_t(uint elements)
3152
3171
  :in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3153
3172
{}
3154
3173
 
3155
 
void in_int64_t::set(uint32_t pos,Item *item)
 
3174
void in_int64_t::set(uint pos,Item *item)
3156
3175
{
3157
3176
  struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3158
3177
  
3160
3179
  buff->unsigned_flag= item->unsigned_flag;
3161
3180
}
3162
3181
 
3163
 
unsigned char *in_int64_t::get_value(Item *item)
 
3182
uchar *in_int64_t::get_value(Item *item)
3164
3183
{
3165
3184
  tmp.val= item->val_int();
3166
3185
  if (item->null_value)
3167
3186
    return 0;
3168
3187
  tmp.unsigned_flag= item->unsigned_flag;
3169
 
  return (unsigned char*) &tmp;
 
3188
  return (uchar*) &tmp;
3170
3189
}
3171
3190
 
3172
 
void in_datetime::set(uint32_t pos,Item *item)
 
3191
void in_datetime::set(uint pos,Item *item)
3173
3192
{
3174
3193
  Item **tmp_item= &item;
3175
3194
  bool is_null;
3179
3198
  buff->unsigned_flag= 1L;
3180
3199
}
3181
3200
 
3182
 
unsigned char *in_datetime::get_value(Item *item)
 
3201
uchar *in_datetime::get_value(Item *item)
3183
3202
{
3184
3203
  bool is_null;
3185
3204
  Item **tmp_item= lval_cache ? &lval_cache : &item;
3187
3206
  if (item->null_value)
3188
3207
    return 0;
3189
3208
  tmp.unsigned_flag= 1L;
3190
 
  return (unsigned char*) &tmp;
 
3209
  return (uchar*) &tmp;
3191
3210
}
3192
3211
 
3193
 
in_double::in_double(uint32_t elements)
 
3212
in_double::in_double(uint elements)
3194
3213
  :in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
3195
3214
{}
3196
3215
 
3197
 
void in_double::set(uint32_t pos,Item *item)
 
3216
void in_double::set(uint pos,Item *item)
3198
3217
{
3199
3218
  ((double*) base)[pos]= item->val_real();
3200
3219
}
3201
3220
 
3202
 
unsigned char *in_double::get_value(Item *item)
 
3221
uchar *in_double::get_value(Item *item)
3203
3222
{
3204
3223
  tmp= item->val_real();
3205
3224
  if (item->null_value)
3206
3225
    return 0;                                   /* purecov: inspected */
3207
 
  return (unsigned char*) &tmp;
 
3226
  return (uchar*) &tmp;
3208
3227
}
3209
3228
 
3210
3229
 
3211
 
in_decimal::in_decimal(uint32_t elements)
 
3230
in_decimal::in_decimal(uint elements)
3212
3231
  :in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3213
3232
{}
3214
3233
 
3215
3234
 
3216
 
void in_decimal::set(uint32_t pos, Item *item)
 
3235
void in_decimal::set(uint pos, Item *item)
3217
3236
{
3218
3237
  /* as far as 'item' is constant, we can store reference on my_decimal */
3219
3238
  my_decimal *dec= ((my_decimal *)base) + pos;
3226
3245
}
3227
3246
 
3228
3247
 
3229
 
unsigned char *in_decimal::get_value(Item *item)
 
3248
uchar *in_decimal::get_value(Item *item)
3230
3249
{
3231
3250
  my_decimal *result= item->val_decimal(&val);
3232
3251
  if (item->null_value)
3233
3252
    return 0;
3234
 
  return (unsigned char *)result;
 
3253
  return (uchar *)result;
3235
3254
}
3236
3255
 
3237
3256
 
3238
3257
cmp_item* cmp_item::get_comparator(Item_result type,
3239
 
                                   const CHARSET_INFO * const cs)
 
3258
                                   CHARSET_INFO *cs)
3240
3259
{
3241
3260
  switch (type) {
3242
3261
  case STRING_RESULT:
3282
3301
{
3283
3302
  if (comparators)
3284
3303
  {
3285
 
    for (uint32_t i= 0; i < n; i++)
 
3304
    for (uint i= 0; i < n; i++)
3286
3305
    {
3287
3306
      if (comparators[i])
3288
3307
        delete comparators[i];
3307
3326
  {
3308
3327
    item->bring_value();
3309
3328
    item->null_value= 0;
3310
 
    for (uint32_t i=0; i < n; i++)
 
3329
    for (uint i=0; i < n; i++)
3311
3330
    {
3312
3331
      if (!comparators[i])
3313
3332
        if (!(comparators[i]=
3335
3354
  {
3336
3355
    item->bring_value();
3337
3356
    item->null_value= 0;
3338
 
    for (uint32_t i=0; i < n; i++)
 
3357
    for (uint i=0; i < n; i++)
3339
3358
    {
3340
3359
      if (!(comparators[i]= tmpl->comparators[i]->make_same()))
3341
3360
        break;                                  // new failed
3357
3376
  }
3358
3377
  bool was_null= 0;
3359
3378
  arg->bring_value();
3360
 
  for (uint32_t i=0; i < n; i++)
 
3379
  for (uint i=0; i < n; i++)
3361
3380
  {
3362
3381
    if (comparators[i]->cmp(arg->element_index(i)))
3363
3382
    {
3373
3392
int cmp_item_row::compare(cmp_item *c)
3374
3393
{
3375
3394
  cmp_item_row *l_cmp= (cmp_item_row *) c;
3376
 
  for (uint32_t i=0; i < n; i++)
 
3395
  for (uint i=0; i < n; i++)
3377
3396
  {
3378
3397
    int res;
3379
3398
    if ((res= comparators[i]->compare(l_cmp->comparators[i])))
3505
3524
}
3506
3525
 
3507
3526
 
3508
 
static int srtcmp_in(const CHARSET_INFO * const cs, const String *x,const String *y)
 
3527
static int srtcmp_in(CHARSET_INFO *cs, const String *x,const String *y)
3509
3528
{
3510
3529
  return cs->coll->strnncollsp(cs,
3511
 
                               (unsigned char *) x->ptr(),x->length(),
3512
 
                               (unsigned char *) y->ptr(),y->length(), 0);
 
3530
                               (uchar *) x->ptr(),x->length(),
 
3531
                               (uchar *) y->ptr(),y->length(), 0);
3513
3532
}
3514
3533
 
3515
3534
 
3522
3541
  /* true <=> arguments values will be compared as DATETIMEs. */
3523
3542
  bool compare_as_datetime= false;
3524
3543
  Item *date_arg= 0;
3525
 
  uint32_t found_types= 0;
3526
 
  uint32_t type_cnt= 0, i;
 
3544
  uint found_types= 0;
 
3545
  uint type_cnt= 0, i;
3527
3546
  Item_result cmp_type= STRING_RESULT;
3528
3547
  left_result_type= args[0]->result_type();
3529
3548
  if (!(found_types= collect_cmp_types(args, arg_count)))
3579
3598
    /* All DATE/DATETIME fields/functions has the STRING result type. */
3580
3599
    if (cmp_type == STRING_RESULT || cmp_type == ROW_RESULT)
3581
3600
    {
3582
 
      uint32_t col, cols= args[0]->cols();
 
3601
      uint col, cols= args[0]->cols();
3583
3602
 
3584
3603
      for (col= 0; col < cols; col++)
3585
3604
      {
3606
3625
            */
3607
3626
            if (!date_arg)
3608
3627
              date_arg= itm;
3609
 
            else if (itm->field_type() == DRIZZLE_TYPE_DATETIME)
 
3628
            else if (itm->field_type() == MYSQL_TYPE_DATETIME)
3610
3629
            {
3611
3630
              date_arg= itm;
3612
3631
              /* All arguments are already checked to have the STRING result. */
3700
3719
    }
3701
3720
    if (array && !(thd->is_fatal_error))                // If not EOM
3702
3721
    {
3703
 
      uint32_t j=0;
3704
 
      for (uint32_t i=1 ; i < arg_count ; i++)
 
3722
      uint j=0;
 
3723
      for (uint i=1 ; i < arg_count ; i++)
3705
3724
      {
3706
3725
        array->set(j,args[i]);
3707
3726
        if (!args[i]->null_value)                       // Skip NULL values
3780
3799
{
3781
3800
  cmp_item *in_item;
3782
3801
  assert(fixed == 1);
3783
 
  uint32_t value_added_map= 0;
 
3802
  uint value_added_map= 0;
3784
3803
  if (array)
3785
3804
  {
3786
3805
    int tmp=array->find(args[0]);
3788
3807
    return (int64_t) (!null_value && tmp != negated);
3789
3808
  }
3790
3809
 
3791
 
  for (uint32_t i= 1 ; i < arg_count ; i++)
 
3810
  for (uint i= 1 ; i < arg_count ; i++)
3792
3811
  {
3793
3812
    Item_result cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
3794
3813
    in_item= cmp_items[(uint)cmp_type];
3870
3889
 
3871
3890
 
3872
3891
bool
3873
 
Item_cond::fix_fields(THD *thd, Item **ref __attribute__((unused)))
 
3892
Item_cond::fix_fields(THD *thd, Item **ref __attribute__((__unused__)))
3874
3893
{
3875
3894
  assert(fixed == 0);
3876
3895
  List_iterator<Item> li(list);
3877
3896
  Item *item;
3878
3897
  void *orig_thd_marker= thd->thd_marker;
3879
 
  unsigned char buff[sizeof(char*)];                    // Max local vars in function
 
3898
  uchar buff[sizeof(char*)];                    // Max local vars in function
3880
3899
  not_null_tables_cache= used_tables_cache= 0;
3881
3900
  const_item_cache= 1;
3882
3901
 
3947
3966
}
3948
3967
 
3949
3968
 
3950
 
void Item_cond::fix_after_pullout(st_select_lex *new_parent, Item **ref __attribute__((unused)))
 
3969
void Item_cond::fix_after_pullout(st_select_lex *new_parent, Item **ref __attribute__((__unused__)))
3951
3970
{
3952
3971
  List_iterator<Item> li(list);
3953
3972
  Item *item;
3979
3998
}
3980
3999
 
3981
4000
 
3982
 
bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
 
4001
bool Item_cond::walk(Item_processor processor, bool walk_subquery, uchar *arg)
3983
4002
{
3984
4003
  List_iterator_fast<Item> li(list);
3985
4004
  Item *item;
4008
4027
    Item returned as the result of transformation of the root node 
4009
4028
*/
4010
4029
 
4011
 
Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
 
4030
Item *Item_cond::transform(Item_transformer transformer, uchar *arg)
4012
4031
{
4013
4032
  List_iterator<Item> li(list);
4014
4033
  Item *item;
4055
4074
    Item returned as the result of transformation of the root node 
4056
4075
*/
4057
4076
 
4058
 
Item *Item_cond::compile(Item_analyzer analyzer, unsigned char **arg_p,
4059
 
                         Item_transformer transformer, unsigned char *arg_t)
 
4077
Item *Item_cond::compile(Item_analyzer analyzer, uchar **arg_p,
 
4078
                         Item_transformer transformer, uchar *arg_t)
4060
4079
{
4061
4080
  if (!(this->*analyzer)(arg_p))
4062
4081
    return 0;
4069
4088
      The same parameter value of arg_p must be passed
4070
4089
      to analyze any argument of the condition formula.
4071
4090
    */   
4072
 
    unsigned char *arg_v= *arg_p;
 
4091
    uchar *arg_v= *arg_p;
4073
4092
    Item *new_item= item->compile(analyzer, &arg_v, transformer, arg_t);
4074
4093
    if (new_item && new_item != item)
4075
4094
      li.replace(new_item);
4181
4200
      if (!(new_item= new Item_func_not(item)))
4182
4201
        return;                                 // Fatal OEM error
4183
4202
    }
4184
 
    li.replace(new_item);
 
4203
    VOID(li.replace(new_item));
4185
4204
  }
4186
4205
}
4187
4206
 
4418
4437
    String *escape_str= escape_item->val_str(&tmp_value1);
4419
4438
    if (escape_str)
4420
4439
    {
4421
 
      if (escape_used_in_parsing && escape_str->numchars() > 1)
 
4440
      if (escape_used_in_parsing && (
 
4441
             (((thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
 
4442
                escape_str->numchars() != 1) ||
 
4443
               escape_str->numchars() > 1)))
4422
4444
      {
4423
4445
        my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4424
4446
        return true;
4426
4448
 
4427
4449
      if (use_mb(cmp.cmp_collation.collation))
4428
4450
      {
4429
 
        const CHARSET_INFO * const cs= escape_str->charset();
 
4451
        CHARSET_INFO *cs= escape_str->charset();
4430
4452
        my_wc_t wc;
4431
4453
        int rc= cs->cset->mb_wc(cs, &wc,
4432
 
                                (const unsigned char*) escape_str->ptr(),
4433
 
                                (const unsigned char*) escape_str->ptr() +
 
4454
                                (const uchar*) escape_str->ptr(),
 
4455
                                (const uchar*) escape_str->ptr() +
4434
4456
                                               escape_str->length());
4435
4457
        escape= (int) (rc > 0 ? wc : '\\');
4436
4458
      }
4441
4463
          code instead of Unicode code as "escape" argument.
4442
4464
          Convert to "cs" if charset of escape differs.
4443
4465
        */
4444
 
        const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
4445
 
        uint32_t unused;
 
4466
        CHARSET_INFO *cs= cmp.cmp_collation.collation;
 
4467
        uint32 unused;
4446
4468
        if (escape_str->needs_conversion(escape_str->length(),
4447
4469
                                         escape_str->charset(), cs, &unused))
4448
4470
        {
4449
4471
          char ch;
4450
 
          uint32_t errors;
4451
 
          uint32_t cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
 
4472
          uint errors;
 
4473
          uint32 cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
4452
4474
                                          escape_str->length(),
4453
4475
                                          escape_str->charset(), &errors);
4454
4476
          escape= cnvlen ? ch : '\\';
4464
4486
      We could also do boyer-more for non-const items, but as we would have to
4465
4487
      recompute the tables for each row it's not worth it.
4466
4488
    */
4467
 
    if (args[1]->const_item() && !use_strnxfrm(collation.collation)) 
 
4489
    if (args[1]->const_item() && !use_strnxfrm(collation.collation) &&
 
4490
       !(specialflag & SPECIAL_NO_NEW_FUNC))
4468
4491
    {
4469
4492
      String* res2 = args[1]->val_str(&tmp_value2);
4470
4493
      if (!res2)
4510
4533
}
4511
4534
 
4512
4535
#ifdef LIKE_CMP_TOUPPER
4513
 
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
 
4536
#define likeconv(cs,A) (uchar) (cs)->toupper(A)
4514
4537
#else
4515
 
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
 
4538
#define likeconv(cs,A) (uchar) (cs)->sort_order[(uchar) (A)]
4516
4539
#endif
4517
4540
 
4518
4541
 
4526
4549
  int            f = 0;
4527
4550
  int            g = plm1;
4528
4551
  int *const splm1 = suff + plm1;
4529
 
  const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
 
4552
  CHARSET_INFO  *cs= cmp.cmp_collation.collation;
4530
4553
 
4531
4554
  *splm1 = pattern_len;
4532
4555
 
4541
4564
      else
4542
4565
      {
4543
4566
        if (i < g)
4544
 
          g = i; // g = cmin(i, g)
 
4567
          g = i; // g = min(i, g)
4545
4568
        f = i;
4546
4569
        while (g >= 0 && pattern[g] == pattern[g + plm1 - f])
4547
4570
          g--;
4560
4583
      else
4561
4584
      {
4562
4585
        if (i < g)
4563
 
          g = i; // g = cmin(i, g)
 
4586
          g = i; // g = min(i, g)
4564
4587
        f = i;
4565
4588
        while (g >= 0 &&
4566
4589
               likeconv(cs, pattern[g]) == likeconv(cs, pattern[g + plm1 - f]))
4626
4649
  int *end = bmBc + alphabet_size;
4627
4650
  int j;
4628
4651
  const int plm1 = pattern_len - 1;
4629
 
  const CHARSET_INFO *const cs= cmp.cmp_collation.collation;
 
4652
  CHARSET_INFO  *cs= cmp.cmp_collation.collation;
4630
4653
 
4631
4654
  for (i = bmBc; i < end; i++)
4632
4655
    *i = pattern_len;
4634
4657
  if (!cs->sort_order)
4635
4658
  {
4636
4659
    for (j = 0; j < plm1; j++)
4637
 
      bmBc[(uint) (unsigned char) pattern[j]] = plm1 - j;
 
4660
      bmBc[(uint) (uchar) pattern[j]] = plm1 - j;
4638
4661
  }
4639
4662
  else
4640
4663
  {
4658
4681
  int shift = pattern_len;
4659
4682
  int j     = 0;
4660
4683
  int u     = 0;
4661
 
  const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
 
4684
  CHARSET_INFO  *cs= cmp.cmp_collation.collation;
4662
4685
 
4663
4686
  const int plm1=  pattern_len - 1;
4664
4687
  const int tlmpl= text_len - pattern_len;
4680
4703
 
4681
4704
      register const int v = plm1 - i;
4682
4705
      turboShift = u - v;
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];
 
4706
      bcShift    = bmBc[(uint) (uchar) text[i + j]] - plm1 + i;
 
4707
      shift      = max(turboShift, bcShift);
 
4708
      shift      = max(shift, bmGs[i]);
4686
4709
      if (shift == bmGs[i])
4687
 
        u = (pattern_len - shift < v) ? pattern_len - shift : v;
 
4710
        u = min(pattern_len - shift, v);
4688
4711
      else
4689
4712
      {
4690
4713
        if (turboShift < bcShift)
4691
 
          shift = cmax(shift, u + 1);
 
4714
          shift = max(shift, u + 1);
4692
4715
        u = 0;
4693
4716
      }
4694
4717
      j+= shift;
4712
4735
      register const int v = plm1 - i;
4713
4736
      turboShift = u - v;
4714
4737
      bcShift    = bmBc[(uint) likeconv(cs, text[i + j])] - plm1 + i;
4715
 
      shift      = (turboShift > bcShift) ? turboShift : bcShift;
4716
 
      shift      = cmax(shift, bmGs[i]);
 
4738
      shift      = max(turboShift, bcShift);
 
4739
      shift      = max(shift, bmGs[i]);
4717
4740
      if (shift == bmGs[i])
4718
 
        u = (pattern_len - shift < v) ? pattern_len - shift : v;
 
4741
        u = min(pattern_len - shift, v);
4719
4742
      else
4720
4743
      {
4721
4744
        if (turboShift < bcShift)
4722
 
          shift = cmax(shift, u + 1);
 
4745
          shift = max(shift, u + 1);
4723
4746
        u = 0;
4724
4747
      }
4725
4748
      j+= shift;
4790
4813
    NULL if we cannot apply NOT transformation (see Item::neg_transformer()).
4791
4814
*/
4792
4815
 
4793
 
Item *Item_func_not::neg_transformer(THD *thd __attribute__((unused)))  /* NOT(x)  ->  x */
 
4816
Item *Item_func_not::neg_transformer(THD *thd __attribute__((__unused__)))      /* NOT(x)  ->  x */
4794
4817
{
4795
4818
  return args[0];
4796
4819
}
4797
4820
 
4798
4821
 
4799
 
Item *Item_bool_rowready_func2::neg_transformer(THD *thd __attribute__((unused)))
 
4822
Item *Item_bool_rowready_func2::neg_transformer(THD *thd __attribute__((__unused__)))
4800
4823
{
4801
4824
  Item *item= negated_item();
4802
4825
  return item;
4806
4829
/**
4807
4830
  a IS NULL  ->  a IS NOT NULL.
4808
4831
*/
4809
 
Item *Item_func_isnull::neg_transformer(THD *thd __attribute__((unused)))
 
4832
Item *Item_func_isnull::neg_transformer(THD *thd __attribute__((__unused__)))
4810
4833
{
4811
4834
  Item *item= new Item_func_isnotnull(args[0]);
4812
4835
  return item;
4816
4839
/**
4817
4840
  a IS NOT NULL  ->  a IS NULL.
4818
4841
*/
4819
 
Item *Item_func_isnotnull::neg_transformer(THD *thd __attribute__((unused)))
 
4842
Item *Item_func_isnotnull::neg_transformer(THD *thd __attribute__((__unused__)))
4820
4843
{
4821
4844
  Item *item= new Item_func_isnull(args[0]);
4822
4845
  return item;
4841
4864
}
4842
4865
 
4843
4866
 
4844
 
Item *Item_func_nop_all::neg_transformer(THD *thd __attribute__((unused)))
 
4867
Item *Item_func_nop_all::neg_transformer(THD *thd __attribute__((__unused__)))
4845
4868
{
4846
4869
  /* "NOT (e $cmp$ ANY (SELECT ...)) -> e $rev_cmp$" ALL (SELECT ...) */
4847
4870
  Item_func_not_all *new_item= new Item_func_not_all(args[0]);
4852
4875
  return new_item;
4853
4876
}
4854
4877
 
4855
 
Item *Item_func_not_all::neg_transformer(THD *thd __attribute__((unused)))
 
4878
Item *Item_func_not_all::neg_transformer(THD *thd __attribute__((__unused__)))
4856
4879
{
4857
4880
  /* "NOT (e $cmp$ ALL (SELECT ...)) -> e $rev_cmp$" ANY (SELECT ...) */
4858
4881
  Item_func_nop_all *new_item= new Item_func_nop_all(args[0]);
4959
4982
  fields.push_back(f);
4960
4983
}
4961
4984
 
4962
 
uint32_t Item_equal::members()
 
4985
uint Item_equal::members()
4963
4986
{
4964
4987
  return fields.elements;
4965
4988
}
5092
5115
  }
5093
5116
}
5094
5117
 
5095
 
bool Item_equal::fix_fields(THD *thd __attribute__((unused)), Item **ref __attribute__((unused)))
 
5118
bool Item_equal::fix_fields(THD *thd __attribute__((__unused__)), Item **ref __attribute__((__unused__)))
5096
5119
{
5097
5120
  List_iterator_fast<Item_field> li(fields);
5098
5121
  Item *item;
5156
5179
                                      item->collation.collation);
5157
5180
}
5158
5181
 
5159
 
bool Item_equal::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
 
5182
bool Item_equal::walk(Item_processor processor, bool walk_subquery, uchar *arg)
5160
5183
{
5161
5184
  List_iterator_fast<Item_field> it(fields);
5162
5185
  Item *item;
5168
5191
  return Item_func::walk(processor, walk_subquery, arg);
5169
5192
}
5170
5193
 
5171
 
Item *Item_equal::transform(Item_transformer transformer, unsigned char *arg)
 
5194
Item *Item_equal::transform(Item_transformer transformer, uchar *arg)
5172
5195
{
5173
5196
  List_iterator<Item_field> it(fields);
5174
5197
  Item *item;