~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_cmpfunc.cc

  • Committer: Monty Taylor
  • Date: 2008-09-05 22:55:50 UTC
  • mfrom: (373.1.9 stl-client-progs)
  • Revision ID: monty@inaugust.com-20080905225550-zco374c9s7kxwqyb
Merged removal of DYNAMIC_STRING.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <drizzled/server_includes.h>
25
25
#include <drizzled/sql_select.h>
26
26
 
27
 
static bool convert_constant_item(Session *, Item_field *, Item **);
 
27
static bool convert_constant_item(THD *, Item_field *, Item **);
28
28
 
29
29
static Item_result item_store_type(Item_result a, Item *item,
30
30
                                   bool unsigned_flag)
42
42
    return INT_RESULT;
43
43
}
44
44
 
45
 
static void agg_result_type(Item_result *type, Item **items, uint32_t nitems)
 
45
static void agg_result_type(Item_result *type, Item **items, uint nitems)
46
46
{
47
47
  Item **item, **item_end;
48
48
  bool unsigned_flag= 0;
90
90
 
91
91
static int cmp_row_type(Item* item1, Item* item2)
92
92
{
93
 
  uint32_t n= item1->cols();
 
93
  uint n= item1->cols();
94
94
  if (item2->check_cols(n))
95
95
    return 1;
96
 
  for (uint32_t i=0; i<n; i++)
 
96
  for (uint i=0; i<n; i++)
97
97
  {
98
98
    if (item2->element_index(i)->check_cols(item1->element_index(i)->cols()) ||
99
99
        (item1->element_index(i)->result_type() == ROW_RESULT &&
127
127
    0  otherwise
128
128
*/
129
129
 
130
 
static int agg_cmp_type(Item_result *type, Item **items, uint32_t nitems)
 
130
static int agg_cmp_type(Item_result *type, Item **items, uint nitems)
131
131
{
132
 
  uint32_t i;
 
132
  uint i;
133
133
  type[0]= items[0]->result_type();
134
134
  for (i= 1 ; i < nitems ; i++)
135
135
  {
166
166
  @return aggregated field type.
167
167
*/
168
168
 
169
 
enum_field_types agg_field_type(Item **items, uint32_t nitems)
 
169
enum_field_types agg_field_type(Item **items, uint nitems)
170
170
{
171
 
  uint32_t i;
 
171
  uint i;
172
172
  if (!nitems || items[0]->result_type() == ROW_RESULT )
173
173
    return (enum_field_types)-1;
174
174
  enum_field_types res= items[0]->field_type();
194
194
    Bitmap of collected types - otherwise
195
195
*/
196
196
 
197
 
static uint32_t collect_cmp_types(Item **items, uint32_t nitems)
 
197
static uint collect_cmp_types(Item **items, uint nitems)
198
198
{
199
 
  uint32_t i;
200
 
  uint32_t found_types;
 
199
  uint i;
 
200
  uint found_types;
201
201
  Item_result left_result= items[0]->result_type();
202
202
  assert(nitems > 1);
203
203
  found_types= 0;
361
361
    also when comparing bigint to strings (in which case strings
362
362
    are converted to bigints).
363
363
 
364
 
  @param  session             thread handle
 
364
  @param  thd             thread handle
365
365
  @param  field_item      item will be converted using the type of this field
366
366
  @param[in,out] item     reference to the item to convert
367
367
 
378
378
    1  Item was replaced with an integer version of the item
379
379
*/
380
380
 
381
 
static bool convert_constant_item(Session *session, Item_field *field_item,
 
381
static bool convert_constant_item(THD *thd, Item_field *field_item,
382
382
                                  Item **item)
383
383
{
384
384
  Field *field= field_item->field;
386
386
 
387
387
  if (!(*item)->with_subselect && (*item)->const_item())
388
388
  {
389
 
    ulong orig_sql_mode= session->variables.sql_mode;
390
 
    enum_check_fields orig_count_cuted_fields= session->count_cuted_fields;
 
389
    ulong orig_sql_mode= thd->variables.sql_mode;
 
390
    enum_check_fields orig_count_cuted_fields= thd->count_cuted_fields;
391
391
    uint64_t orig_field_val= 0; /* original field value if valid */
392
392
 
393
393
    /* For comparison purposes allow invalid dates like 2000-01-32 */
394
 
    session->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) | 
 
394
    thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) | 
395
395
                             MODE_INVALID_DATES;
396
 
    session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
396
    thd->count_cuted_fields= CHECK_FIELD_IGNORE;
397
397
 
398
398
    /*
399
399
      Store the value of the field if it references an outer field because
406
406
      Item *tmp= new Item_int_with_ref(field->val_int(), *item,
407
407
                                       test(field->flags & UNSIGNED_FLAG));
408
408
      if (tmp)
409
 
        session->change_item_tree(item, tmp);
 
409
        thd->change_item_tree(item, tmp);
410
410
      result= 1;                                        // Item was replaced
411
411
    }
412
412
    /* Restore the original field value. */
416
416
      /* orig_field_val must be a valid value that can be restored back. */
417
417
      assert(!result);
418
418
    }
419
 
    session->variables.sql_mode= orig_sql_mode;
420
 
    session->count_cuted_fields= orig_count_cuted_fields;
 
419
    thd->variables.sql_mode= orig_sql_mode;
 
420
    thd->count_cuted_fields= orig_count_cuted_fields;
421
421
  }
422
422
  return result;
423
423
}
426
426
void Item_bool_func2::fix_length_and_dec()
427
427
{
428
428
  max_length= 1;                                     // Function returns 0 or 1
429
 
  Session *session;
 
429
  THD *thd;
430
430
 
431
431
  /*
432
432
    As some compare functions are generated after sql_yacc,
464
464
    return;
465
465
  }
466
466
 
467
 
  session= current_session;
 
467
  thd= current_thd;
468
468
 
469
469
  if (args[0]->real_item()->type() == FIELD_ITEM)
470
470
  {
472
472
    if (field_item->field->can_be_compared_as_int64_t() &&
473
473
        !(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
474
474
    {
475
 
      if (convert_constant_item(session, field_item, &args[1]))
 
475
      if (convert_constant_item(thd, field_item, &args[1]))
476
476
      {
477
477
        cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
478
478
                         INT_RESULT);           // Works for all types.
488
488
          !(field_item->is_datetime() &&
489
489
            args[0]->result_type() == STRING_RESULT))
490
490
      {
491
 
        if (convert_constant_item(session, field_item, &args[0]))
 
491
        if (convert_constant_item(thd, field_item, &args[0]))
492
492
        {
493
493
          cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
494
494
                           INT_RESULT); // Works for all types.
510
510
  switch (type) {
511
511
  case ROW_RESULT:
512
512
  {
513
 
    uint32_t n= (*a)->cols();
 
513
    uint n= (*a)->cols();
514
514
    if (n != (*b)->cols())
515
515
    {
516
516
      my_error(ER_OPERAND_COLUMNS, MYF(0), n);
519
519
    }
520
520
    if (!(comparators= new Arg_comparator[n]))
521
521
      return 1;
522
 
    for (uint32_t i=0; i < n; i++)
 
522
    for (uint i=0; i < n; i++)
523
523
    {
524
524
      if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
525
525
      {
561
561
        which would be transformed to:
562
562
        WHERE col= 'j'
563
563
      */
564
 
      (*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
565
 
      (*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
 
564
      (*a)->walk(&Item::set_no_const_sub, false, (uchar*) 0);
 
565
      (*b)->walk(&Item::set_no_const_sub, false, (uchar*) 0);
566
566
    }
567
567
    break;
568
568
  }
590
590
  {
591
591
    if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
592
592
    {
593
 
      precision= 5 / log_10[cmax((*a)->decimals, (*b)->decimals) + 1];
 
593
      precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
594
594
      if (func == &Arg_comparator::compare_real)
595
595
        func= &Arg_comparator::compare_real_fixed;
596
596
      else if (func == &Arg_comparator::compare_e_real)
608
608
/**
609
609
  @brief Convert date provided in a string to the int representation.
610
610
 
611
 
  @param[in]   session        thread handle
 
611
  @param[in]   thd        thread handle
612
612
  @param[in]   str        a string to convert
613
613
  @param[in]   warn_type  type of the timestamp for issuing the warning
614
614
  @param[in]   warn_name  field name for issuing the warning
628
628
*/
629
629
 
630
630
static uint64_t
631
 
get_date_from_str(Session *session, String *str, enum enum_drizzle_timestamp_type warn_type,
 
631
get_date_from_str(THD *thd, String *str, timestamp_type warn_type,
632
632
                  char *warn_name, bool *error_arg)
633
633
{
634
634
  uint64_t value= 0;
635
635
  int error;
636
636
  DRIZZLE_TIME l_time;
637
 
  enum enum_drizzle_timestamp_type ret;
 
637
  enum_drizzle_timestamp_type ret;
638
638
 
639
639
  ret= str_to_datetime(str->ptr(), str->length(), &l_time,
640
640
                       (TIME_FUZZY_DATE | MODE_INVALID_DATES |
641
 
                        (session->variables.sql_mode & MODE_NO_ZERO_DATE)),
 
641
                        (thd->variables.sql_mode & MODE_NO_ZERO_DATE)),
642
642
                       &error);
643
643
 
644
644
  if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
657
657
  }
658
658
 
659
659
  if (error > 0)
660
 
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
660
    make_truncated_value_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
661
661
                                 str->ptr(), str->length(),
662
662
                                 warn_type, warn_name);
663
663
 
734
734
        (str_arg->type() != Item::FUNC_ITEM ||
735
735
        ((Item_func*)str_arg)->functype() != Item_func::GUSERVAR_FUNC))
736
736
    {
737
 
      Session *session= current_session;
 
737
      THD *thd= current_thd;
738
738
      uint64_t value;
739
739
      bool error;
740
740
      String tmp, *str_val= 0;
741
 
      enum enum_drizzle_timestamp_type t_type= (date_arg->field_type() == DRIZZLE_TYPE_NEWDATE ?
 
741
      timestamp_type t_type= (date_arg->field_type() == DRIZZLE_TYPE_NEWDATE ?
742
742
                              DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME);
743
743
 
744
744
      str_val= str_arg->val_str(&tmp);
745
745
      if (str_arg->null_value)
746
746
        return CMP_DATE_DFLT;
747
 
      value= get_date_from_str(session, str_val, t_type, date_arg->name, &error);
 
747
      value= get_date_from_str(thd, str_val, t_type, date_arg->name, &error);
748
748
      if (error)
749
749
        return CMP_DATE_DFLT;
750
750
      if (const_value)
760
760
 
761
761
  SYNOPSIS
762
762
    get_time_value()
763
 
    session                 thread handle
 
763
    thd                 thread handle
764
764
    item_arg   [in/out] item to retrieve TIME value from
765
765
    cache_arg  [in/out] pointer to place to store the cache item to
766
766
    warn_item  [in]     unused
781
781
*/
782
782
 
783
783
uint64_t
784
 
get_time_value(Session *session __attribute__((unused)),
 
784
get_time_value(THD *thd __attribute__((unused)),
785
785
               Item ***item_arg, Item **cache_arg,
786
786
               Item *warn_item __attribute__((unused)),
787
787
               bool *is_null)
829
829
 
830
830
  if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
831
831
  {
832
 
    session= current_session;
 
832
    thd= current_thd;
833
833
    owner= owner_arg;
834
834
    a_type= (*a)->field_type();
835
835
    b_type= (*b)->field_type();
863
863
           (*b)->field_type() == DRIZZLE_TYPE_TIME)
864
864
  {
865
865
    /* Compare TIME values as integers. */
866
 
    session= current_session;
 
866
    thd= current_thd;
867
867
    owner= owner_arg;
868
868
    a_cache= 0;
869
869
    b_cache= 0;
879
879
 
880
880
void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
881
881
{
882
 
  session= current_session;
 
882
  thd= current_thd;
883
883
  /* A caller will handle null values by itself. */
884
884
  owner= NULL;
885
885
  a= a1;
899
899
 
900
900
  SYNOPSIS
901
901
    get_datetime_value()
902
 
    session                 thread handle
 
902
    thd                 thread handle
903
903
    item_arg   [in/out] item to retrieve DATETIME value from
904
904
    cache_arg  [in/out] pointer to place to store the caching item to
905
905
    warn_item  [in]     item for issuing the conversion warning
924
924
*/
925
925
 
926
926
uint64_t
927
 
get_datetime_value(Session *session, Item ***item_arg, Item **cache_arg,
 
927
get_datetime_value(THD *thd, Item ***item_arg, Item **cache_arg,
928
928
                   Item *warn_item, bool *is_null)
929
929
{
930
930
  uint64_t value= 0;
963
963
  {
964
964
    bool error;
965
965
    enum_field_types f_type= warn_item->field_type();
966
 
    enum enum_drizzle_timestamp_type t_type= f_type ==
 
966
    timestamp_type t_type= f_type ==
967
967
      DRIZZLE_TYPE_NEWDATE ? DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME;
968
 
    value= get_date_from_str(session, str, t_type, warn_item->name, &error);
 
968
    value= get_date_from_str(thd, str, t_type, warn_item->name, &error);
969
969
    /*
970
970
      If str did not contain a valid date according to the current
971
971
      SQL_MODE, get_date_from_str() has already thrown a warning,
1017
1017
  uint64_t a_value, b_value;
1018
1018
 
1019
1019
  /* Get DATE/DATETIME/TIME value of the 'a' item. */
1020
 
  a_value= (*get_value_func)(session, &a, &a_cache, *b, &is_null);
 
1020
  a_value= (*get_value_func)(thd, &a, &a_cache, *b, &is_null);
1021
1021
  if (!is_nulls_eq && is_null)
1022
1022
  {
1023
1023
    if (owner)
1026
1026
  }
1027
1027
 
1028
1028
  /* Get DATE/DATETIME/TIME value of the 'b' item. */
1029
 
  b_value= (*get_value_func)(session, &b, &b_cache, *a, &is_null);
 
1029
  b_value= (*get_value_func)(thd, &b, &b_cache, *a, &is_null);
1030
1030
  if (is_null)
1031
1031
  {
1032
1032
    if (owner)
1079
1079
    if ((res2= (*b)->val_str(&owner->tmp_value2)))
1080
1080
    {
1081
1081
      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));
 
1082
      uint res1_length= res1->length();
 
1083
      uint res2_length= res2->length();
 
1084
      int cmp= memcmp(res1->ptr(), res2->ptr(), min(res1_length,res2_length));
1085
1085
      return cmp ? cmp : (int) (res1_length - res2_length);
1086
1086
    }
1087
1087
  }
1337
1337
  bool was_null= 0;
1338
1338
  (*a)->bring_value();
1339
1339
  (*b)->bring_value();
1340
 
  uint32_t n= (*a)->cols();
1341
 
  for (uint32_t i= 0; i<n; i++)
 
1340
  uint n= (*a)->cols();
 
1341
  for (uint i= 0; i<n; i++)
1342
1342
  {
1343
1343
    res= comparators[i].compare();
1344
1344
    if (owner->null_value)
1380
1380
{
1381
1381
  (*a)->bring_value();
1382
1382
  (*b)->bring_value();
1383
 
  uint32_t n= (*a)->cols();
1384
 
  for (uint32_t i= 0; i<n; i++)
 
1383
  uint n= (*a)->cols();
 
1384
  for (uint i= 0; i<n; i++)
1385
1385
  {
1386
1386
    if (!comparators[i].compare())
1387
1387
      return 0;
1443
1443
}
1444
1444
 
1445
1445
 
1446
 
bool Item_in_optimizer::fix_left(Session *session, Item **ref __attribute__((unused)))
 
1446
bool Item_in_optimizer::fix_left(THD *thd, Item **ref __attribute__((unused)))
1447
1447
{
1448
 
  if ((!args[0]->fixed && args[0]->fix_fields(session, args)) ||
 
1448
  if ((!args[0]->fixed && args[0]->fix_fields(thd, args)) ||
1449
1449
      (!cache && !(cache= Item_cache::get_cache(args[0]))))
1450
1450
    return 1;
1451
1451
 
1459
1459
  }
1460
1460
  else
1461
1461
  {
1462
 
    uint32_t n= cache->cols();
1463
 
    for (uint32_t i= 0; i < n; i++)
 
1462
    uint n= cache->cols();
 
1463
    for (uint i= 0; i < n; i++)
1464
1464
    {
1465
1465
      if (args[0]->element_index(i)->used_tables())
1466
1466
        ((Item_cache *)cache->element_index(i))->set_used_tables(OUTER_REF_TABLE_BIT);
1477
1477
}
1478
1478
 
1479
1479
 
1480
 
bool Item_in_optimizer::fix_fields(Session *session, Item **ref)
 
1480
bool Item_in_optimizer::fix_fields(THD *thd, Item **ref)
1481
1481
{
1482
1482
  assert(fixed == 0);
1483
 
  if (fix_left(session, ref))
 
1483
  if (fix_left(thd, ref))
1484
1484
    return true;
1485
1485
  if (args[0]->maybe_null)
1486
1486
    maybe_null=1;
1487
1487
 
1488
 
  if (!args[1]->fixed && args[1]->fix_fields(session, args+1))
 
1488
  if (!args[1]->fixed && args[1]->fix_fields(thd, args+1))
1489
1489
    return true;
1490
1490
  Item_in_subselect * sub= (Item_in_subselect *)args[1];
1491
1491
  if (args[0]->cols() != sub->engine->cols())
1547
1547
        }
1548
1548
        else
1549
1549
        {
1550
 
          uint32_t i;
1551
 
          uint32_t ncols= cache->cols();
 
1550
          uint i;
 
1551
          uint ncols= cache->cols();
1552
1552
          /*
1553
1553
            Turn off the predicates that are based on column compares for
1554
1554
            which the left part is currently NULL
1621
1621
    @retval NULL if an error occurred
1622
1622
*/
1623
1623
 
1624
 
Item *Item_in_optimizer::transform(Item_transformer transformer, unsigned char *argument)
 
1624
Item *Item_in_optimizer::transform(Item_transformer transformer, uchar *argument)
1625
1625
{
1626
1626
  Item *new_item;
1627
1627
 
1632
1632
  if (!new_item)
1633
1633
    return 0;
1634
1634
  /*
1635
 
    Session::change_item_tree() should be called only if the tree was
 
1635
    THD::change_item_tree() should be called only if the tree was
1636
1636
    really transformed, i.e. when a new item has been created.
1637
1637
    Otherwise we'll be allocating a lot of unnecessary memory for
1638
1638
    change records at each execution.
1639
1639
  */
1640
1640
  if ((*args) != new_item)
1641
 
    current_session->change_item_tree(args, new_item);
 
1641
    current_thd->change_item_tree(args, new_item);
1642
1642
 
1643
1643
  /*
1644
1644
    Transform the right IN operand which should be an Item_in_subselect or a
1753
1753
    return 0;
1754
1754
  if (negated != ((Item_func_opt_neg *) item_func)->negated)
1755
1755
    return 0;
1756
 
  for (uint32_t i=0; i < arg_count ; i++)
 
1756
  for (uint i=0; i < arg_count ; i++)
1757
1757
    if (!args[i]->eq(item_func->arguments()[i], binary_cmp))
1758
1758
      return 0;
1759
1759
  return 1;
1762
1762
 
1763
1763
void Item_func_interval::fix_length_and_dec()
1764
1764
{
1765
 
  uint32_t rows= row->cols();
 
1765
  uint rows= row->cols();
1766
1766
  
1767
1767
  use_decimal_comparison= ((row->element_index(0)->result_type() ==
1768
1768
                            DECIMAL_RESULT) ||
1772
1772
  {
1773
1773
    bool not_null_consts= true;
1774
1774
 
1775
 
    for (uint32_t i= 1; not_null_consts && i < rows; i++)
 
1775
    for (uint i= 1; not_null_consts && i < rows; i++)
1776
1776
    {
1777
1777
      Item *el= row->element_index(i);
1778
1778
      not_null_consts&= el->const_item() & !el->is_null();
1784
1784
    {
1785
1785
      if (use_decimal_comparison)
1786
1786
      {
1787
 
        for (uint32_t i= 1; i < rows; i++)
 
1787
        for (uint i= 1; i < rows; i++)
1788
1788
        {
1789
1789
          Item *el= row->element_index(i);
1790
1790
          interval_range *range= intervals + (i-1);
1809
1809
      }
1810
1810
      else
1811
1811
      {
1812
 
        for (uint32_t i= 1; i < rows; i++)
 
1812
        for (uint i= 1; i < rows; i++)
1813
1813
        {
1814
1814
          intervals[i-1].dbl= row->element_index(i)->val_real();
1815
1815
        }
1844
1844
  assert(fixed == 1);
1845
1845
  double value;
1846
1846
  my_decimal dec_buf, *dec= NULL;
1847
 
  uint32_t i;
 
1847
  uint i;
1848
1848
 
1849
1849
  if (use_decimal_comparison)
1850
1850
  {
1862
1862
 
1863
1863
  if (intervals)
1864
1864
  {                                     // Use binary search to find interval
1865
 
    uint32_t start,end;
 
1865
    uint start,end;
1866
1866
    start= 0;
1867
1867
    end=   row->cols()-2;
1868
1868
    while (start != end)
1869
1869
    {
1870
 
      uint32_t mid= (start + end + 1) / 2;
 
1870
      uint mid= (start + end + 1) / 2;
1871
1871
      interval_range *range= intervals + mid;
1872
1872
      bool cmp_result;
1873
1873
      /*
1926
1926
    The function saves in ref the pointer to the item or to a newly created
1927
1927
    item that is considered as a replacement for the original one.
1928
1928
 
1929
 
  @param session     reference to the global context of the query thread
 
1929
  @param thd     reference to the global context of the query thread
1930
1930
  @param ref     pointer to Item* variable where pointer to resulting "fixed"
1931
1931
                 item is to be assigned
1932
1932
 
1946
1946
    1   got error
1947
1947
*/
1948
1948
 
1949
 
bool Item_func_between::fix_fields(Session *session, Item **ref)
 
1949
bool Item_func_between::fix_fields(THD *thd, Item **ref)
1950
1950
{
1951
 
  if (Item_func_opt_neg::fix_fields(session, ref))
 
1951
  if (Item_func_opt_neg::fix_fields(thd, ref))
1952
1952
    return 1;
1953
1953
 
1954
 
  session->lex->current_select->between_count++;
 
1954
  thd->lex->current_select->between_count++;
1955
1955
 
1956
1956
  /* not_null_tables_cache == union(T1(e),T1(e1),T1(e2)) */
1957
1957
  if (pred_level && !negated)
1973
1973
  bool datetime_found= false;
1974
1974
  int time_items_found= 0;
1975
1975
  compare_as_dates= true;
1976
 
  Session *session= current_session;
 
1976
  THD *thd= current_thd;
1977
1977
 
1978
1978
  /*
1979
1979
    As some compare functions are generated after sql_yacc,
2020
2020
    cmp_type= INT_RESULT;
2021
2021
  }
2022
2022
  else if (args[0]->real_item()->type() == FIELD_ITEM &&
2023
 
           session->lex->sql_command != SQLCOM_SHOW_CREATE)
 
2023
           thd->lex->sql_command != SQLCOM_SHOW_CREATE)
2024
2024
  {
2025
2025
    Item_field *field_item= (Item_field*) (args[0]->real_item());
2026
2026
    if (field_item->field->can_be_compared_as_int64_t())
2029
2029
        The following can't be recoded with || as convert_constant_item
2030
2030
        changes the argument
2031
2031
      */
2032
 
      if (convert_constant_item(session, field_item, &args[1]))
 
2032
      if (convert_constant_item(thd, field_item, &args[1]))
2033
2033
        cmp_type=INT_RESULT;                    // Works for all types.
2034
 
      if (convert_constant_item(session, field_item, &args[2]))
 
2034
      if (convert_constant_item(thd, field_item, &args[2]))
2035
2035
        cmp_type=INT_RESULT;                    // Works for all types.
2036
2036
    }
2037
2037
  }
2166
2166
{
2167
2167
  agg_result_type(&hybrid_type, args, 2);
2168
2168
  maybe_null=args[1]->maybe_null;
2169
 
  decimals= cmax(args[0]->decimals, args[1]->decimals);
 
2169
  decimals= max(args[0]->decimals, args[1]->decimals);
2170
2170
  unsigned_flag= args[0]->unsigned_flag && args[1]->unsigned_flag;
2171
2171
 
2172
2172
  if (hybrid_type == DECIMAL_RESULT || hybrid_type == INT_RESULT) 
2177
2177
    int len1= args[1]->max_length - args[1]->decimals
2178
2178
      - (args[1]->unsigned_flag ? 0 : 1);
2179
2179
 
2180
 
    max_length= cmax(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
 
2180
    max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2181
2181
  }
2182
2182
  else
2183
 
    max_length= cmax(args[0]->max_length, args[1]->max_length);
 
2183
    max_length= max(args[0]->max_length, args[1]->max_length);
2184
2184
 
2185
2185
  switch (hybrid_type) {
2186
2186
  case STRING_RESULT:
2200
2200
}
2201
2201
 
2202
2202
 
2203
 
uint32_t Item_func_ifnull::decimal_precision() const
 
2203
uint Item_func_ifnull::decimal_precision() const
2204
2204
{
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);
 
2205
  int max_int_part=max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
 
2206
  return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2207
2207
}
2208
2208
 
2209
2209
 
2293
2293
    The function saves in ref the pointer to the item or to a newly created
2294
2294
    item that is considered as a replacement for the original one.
2295
2295
 
2296
 
  @param session     reference to the global context of the query thread
 
2296
  @param thd     reference to the global context of the query thread
2297
2297
  @param ref     pointer to Item* variable where pointer to resulting "fixed"
2298
2298
                 item is to be assigned
2299
2299
 
2312
2312
*/
2313
2313
 
2314
2314
bool
2315
 
Item_func_if::fix_fields(Session *session, Item **ref)
 
2315
Item_func_if::fix_fields(THD *thd, Item **ref)
2316
2316
{
2317
2317
  assert(fixed == 0);
2318
2318
  args[0]->top_level_item();
2319
2319
 
2320
 
  if (Item_func::fix_fields(session, ref))
 
2320
  if (Item_func::fix_fields(thd, ref))
2321
2321
    return 1;
2322
2322
 
2323
2323
  not_null_tables_cache= (args[1]->not_null_tables() &
2331
2331
Item_func_if::fix_length_and_dec()
2332
2332
{
2333
2333
  maybe_null=args[1]->maybe_null || args[2]->maybe_null;
2334
 
  decimals= cmax(args[1]->decimals, args[2]->decimals);
 
2334
  decimals= max(args[1]->decimals, args[2]->decimals);
2335
2335
  unsigned_flag=args[1]->unsigned_flag && args[2]->unsigned_flag;
2336
2336
 
2337
2337
  enum Item_result arg1_type=args[1]->result_type();
2375
2375
    int len2= args[2]->max_length - args[2]->decimals
2376
2376
      - (args[2]->unsigned_flag ? 0 : 1);
2377
2377
 
2378
 
    max_length=cmax(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
 
2378
    max_length=max(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
2379
2379
  }
2380
2380
  else
2381
 
    max_length= cmax(args[1]->max_length, args[2]->max_length);
 
2381
    max_length= max(args[1]->max_length, args[2]->max_length);
2382
2382
}
2383
2383
 
2384
2384
 
2385
 
uint32_t Item_func_if::decimal_precision() const
 
2385
uint Item_func_if::decimal_precision() const
2386
2386
{
2387
 
  int precision=(cmax(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
 
2387
  int precision=(max(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
2388
2388
                 decimals);
2389
 
  return cmin(precision, DECIMAL_MAX_PRECISION);
 
2389
  return min(precision, DECIMAL_MAX_PRECISION);
2390
2390
}
2391
2391
 
2392
2392
 
2554
2554
 
2555
2555
Item *Item_func_case::find_item(String *str __attribute__((unused)))
2556
2556
{
2557
 
  uint32_t value_added_map= 0;
 
2557
  uint value_added_map= 0;
2558
2558
 
2559
2559
  if (first_expr_num == -1)
2560
2560
  {
2561
 
    for (uint32_t i=0 ; i < ncases ; i+=2)
 
2561
    for (uint i=0 ; i < ncases ; i+=2)
2562
2562
    {
2563
2563
      // No expression between CASE and the first WHEN
2564
2564
      if (args[i]->val_bool())
2569
2569
  else
2570
2570
  {
2571
2571
    /* Compare every WHEN argument with it and return the first match */
2572
 
    for (uint32_t i=0 ; i < ncases ; i+=2)
 
2572
    for (uint i=0 ; i < ncases ; i+=2)
2573
2573
    {
2574
2574
      cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
2575
2575
      assert(cmp_type != ROW_RESULT);
2665
2665
}
2666
2666
 
2667
2667
 
2668
 
bool Item_func_case::fix_fields(Session *session, Item **ref)
 
2668
bool Item_func_case::fix_fields(THD *thd, Item **ref)
2669
2669
{
2670
2670
  /*
2671
2671
    buff should match stack usage from
2672
2672
    Item_func_case::val_int() -> Item_func_case::find_item()
2673
2673
  */
2674
 
  unsigned char buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(int64_t)*2];
2675
 
  bool res= Item_func::fix_fields(session, ref);
 
2674
  uchar buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(int64_t)*2];
 
2675
  bool res= Item_func::fix_fields(thd, ref);
2676
2676
  /*
2677
2677
    Call check_stack_overrun after fix_fields to be sure that stack variable
2678
2678
    is not optimized away
2679
2679
  */
2680
 
  if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
 
2680
  if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2681
2681
    return true;                                // Fatal error flag is set!
2682
2682
  return res;
2683
2683
}
2693
2693
 
2694
2694
void Item_func_case::agg_num_lengths(Item *arg)
2695
2695
{
2696
 
  uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
 
2696
  uint len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2697
2697
                                           arg->unsigned_flag) - arg->decimals;
2698
2698
  set_if_bigger(max_length, len); 
2699
2699
  set_if_bigger(decimals, arg->decimals);
2704
2704
void Item_func_case::fix_length_and_dec()
2705
2705
{
2706
2706
  Item **agg;
2707
 
  uint32_t nagg;
2708
 
  uint32_t found_types= 0;
 
2707
  uint nagg;
 
2708
  uint found_types= 0;
2709
2709
  if (!(agg= (Item**) sql_alloc(sizeof(Item*)*(ncases+1))))
2710
2710
    return;
2711
2711
  
2732
2732
  */
2733
2733
  if (first_expr_num != -1)
2734
2734
  {
2735
 
    uint32_t i;
 
2735
    uint i;
2736
2736
    agg[0]= args[first_expr_num];
2737
2737
    left_result_type= agg[0]->result_type();
2738
2738
 
2766
2766
  unsigned_flag= true;
2767
2767
  if (cached_result_type == STRING_RESULT)
2768
2768
  {
2769
 
    for (uint32_t i= 0; i < ncases; i+= 2)
 
2769
    for (uint i= 0; i < ncases; i+= 2)
2770
2770
      agg_str_lengths(args[i + 1]);
2771
2771
    if (else_expr_num != -1)
2772
2772
      agg_str_lengths(args[else_expr_num]);
2773
2773
  }
2774
2774
  else
2775
2775
  {
2776
 
    for (uint32_t i= 0; i < ncases; i+= 2)
 
2776
    for (uint i= 0; i < ncases; i+= 2)
2777
2777
      agg_num_lengths(args[i + 1]);
2778
2778
    if (else_expr_num != -1) 
2779
2779
      agg_num_lengths(args[else_expr_num]);
2783
2783
}
2784
2784
 
2785
2785
 
2786
 
uint32_t Item_func_case::decimal_precision() const
 
2786
uint Item_func_case::decimal_precision() const
2787
2787
{
2788
2788
  int max_int_part=0;
2789
 
  for (uint32_t i=0 ; i < ncases ; i+=2)
 
2789
  for (uint i=0 ; i < ncases ; i+=2)
2790
2790
    set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
2791
2791
 
2792
2792
  if (else_expr_num != -1) 
2793
2793
    set_if_bigger(max_int_part, args[else_expr_num]->decimal_int_part());
2794
 
  return cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
 
2794
  return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2795
2795
}
2796
2796
 
2797
2797
 
2808
2808
    args[first_expr_num]->print(str, query_type);
2809
2809
    str->append(' ');
2810
2810
  }
2811
 
  for (uint32_t i=0 ; i < ncases ; i+=2)
 
2811
  for (uint i=0 ; i < ncases ; i+=2)
2812
2812
  {
2813
2813
    str->append(STRING_WITH_LEN("when "));
2814
2814
    args[i]->print(str, query_type);
2828
2828
 
2829
2829
void Item_func_case::cleanup()
2830
2830
{
2831
 
  uint32_t i;
 
2831
  uint i;
2832
2832
  Item_func::cleanup();
2833
2833
  for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
2834
2834
  {
2847
2847
{
2848
2848
  assert(fixed == 1);
2849
2849
  null_value=0;
2850
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
2850
  for (uint i=0 ; i < arg_count ; i++)
2851
2851
  {
2852
2852
    String *res;
2853
2853
    if ((res=args[i]->val_str(str)))
2861
2861
{
2862
2862
  assert(fixed == 1);
2863
2863
  null_value=0;
2864
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
2864
  for (uint i=0 ; i < arg_count ; i++)
2865
2865
  {
2866
2866
    int64_t res=args[i]->val_int();
2867
2867
    if (!args[i]->null_value)
2875
2875
{
2876
2876
  assert(fixed == 1);
2877
2877
  null_value=0;
2878
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
2878
  for (uint i=0 ; i < arg_count ; i++)
2879
2879
  {
2880
2880
    double res= args[i]->val_real();
2881
2881
    if (!args[i]->null_value)
2890
2890
{
2891
2891
  assert(fixed == 1);
2892
2892
  null_value= 0;
2893
 
  for (uint32_t i= 0; i < arg_count; i++)
 
2893
  for (uint i= 0; i < arg_count; i++)
2894
2894
  {
2895
2895
    my_decimal *res= args[i]->val_decimal(decimal_value);
2896
2896
    if (!args[i]->null_value)
3052
3052
 
3053
3053
int in_vector::find(Item *item)
3054
3054
{
3055
 
  unsigned char *result=get_value(item);
 
3055
  uchar *result=get_value(item);
3056
3056
  if (!result || !used_count)
3057
3057
    return 0;                           // Null value
3058
3058
 
3059
 
  uint32_t start,end;
 
3059
  uint start,end;
3060
3060
  start=0; end=used_count-1;
3061
3061
  while (start != end)
3062
3062
  {
3063
 
    uint32_t mid=(start+end+1)/2;
 
3063
    uint mid=(start+end+1)/2;
3064
3064
    int res;
3065
3065
    if ((res=(*compare)(collation, base+mid*size, result)) == 0)
3066
3066
      return 1;
3072
3072
  return (int) ((*compare)(collation, base+start*size, result) == 0);
3073
3073
}
3074
3074
 
3075
 
in_string::in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs)
 
3075
in_string::in_string(uint elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs)
3076
3076
  :in_vector(elements, sizeof(String), cmp_func, cs),
3077
3077
   tmp(buff, sizeof(buff), &my_charset_bin)
3078
3078
{}
3082
3082
  if (base)
3083
3083
  {
3084
3084
    // base was allocated with help of sql_alloc => following is OK
3085
 
    for (uint32_t i=0 ; i < count ; i++)
 
3085
    for (uint i=0 ; i < count ; i++)
3086
3086
      ((String*) base)[i].free();
3087
3087
  }
3088
3088
}
3089
3089
 
3090
 
void in_string::set(uint32_t pos,Item *item)
 
3090
void in_string::set(uint pos,Item *item)
3091
3091
{
3092
3092
  String *str=((String*) base)+pos;
3093
3093
  String *res=item->val_str(str);
3110
3110
}
3111
3111
 
3112
3112
 
3113
 
unsigned char *in_string::get_value(Item *item)
 
3113
uchar *in_string::get_value(Item *item)
3114
3114
{
3115
 
  return (unsigned char*) item->val_str(&tmp);
 
3115
  return (uchar*) item->val_str(&tmp);
3116
3116
}
3117
3117
 
3118
 
in_row::in_row(uint32_t elements, Item * item __attribute__((unused)))
 
3118
in_row::in_row(uint elements, Item * item __attribute__((unused)))
3119
3119
{
3120
3120
  base= (char*) new cmp_item_row[count= elements];
3121
3121
  size= sizeof(cmp_item_row);
3134
3134
    delete [] (cmp_item_row*) base;
3135
3135
}
3136
3136
 
3137
 
unsigned char *in_row::get_value(Item *item)
 
3137
uchar *in_row::get_value(Item *item)
3138
3138
{
3139
3139
  tmp.store_value(item);
3140
3140
  if (item->is_null())
3141
3141
    return 0;
3142
 
  return (unsigned char *)&tmp;
 
3142
  return (uchar *)&tmp;
3143
3143
}
3144
3144
 
3145
 
void in_row::set(uint32_t pos, Item *item)
 
3145
void in_row::set(uint pos, Item *item)
3146
3146
{
3147
3147
  ((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3148
3148
  return;
3149
3149
}
3150
3150
 
3151
 
in_int64_t::in_int64_t(uint32_t elements)
 
3151
in_int64_t::in_int64_t(uint elements)
3152
3152
  :in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3153
3153
{}
3154
3154
 
3155
 
void in_int64_t::set(uint32_t pos,Item *item)
 
3155
void in_int64_t::set(uint pos,Item *item)
3156
3156
{
3157
3157
  struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3158
3158
  
3160
3160
  buff->unsigned_flag= item->unsigned_flag;
3161
3161
}
3162
3162
 
3163
 
unsigned char *in_int64_t::get_value(Item *item)
 
3163
uchar *in_int64_t::get_value(Item *item)
3164
3164
{
3165
3165
  tmp.val= item->val_int();
3166
3166
  if (item->null_value)
3167
3167
    return 0;
3168
3168
  tmp.unsigned_flag= item->unsigned_flag;
3169
 
  return (unsigned char*) &tmp;
 
3169
  return (uchar*) &tmp;
3170
3170
}
3171
3171
 
3172
 
void in_datetime::set(uint32_t pos,Item *item)
 
3172
void in_datetime::set(uint pos,Item *item)
3173
3173
{
3174
3174
  Item **tmp_item= &item;
3175
3175
  bool is_null;
3176
3176
  struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3177
3177
 
3178
 
  buff->val= get_datetime_value(session, &tmp_item, 0, warn_item, &is_null);
 
3178
  buff->val= get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
3179
3179
  buff->unsigned_flag= 1L;
3180
3180
}
3181
3181
 
3182
 
unsigned char *in_datetime::get_value(Item *item)
 
3182
uchar *in_datetime::get_value(Item *item)
3183
3183
{
3184
3184
  bool is_null;
3185
3185
  Item **tmp_item= lval_cache ? &lval_cache : &item;
3186
 
  tmp.val= get_datetime_value(session, &tmp_item, &lval_cache, warn_item, &is_null);
 
3186
  tmp.val= get_datetime_value(thd, &tmp_item, &lval_cache, warn_item, &is_null);
3187
3187
  if (item->null_value)
3188
3188
    return 0;
3189
3189
  tmp.unsigned_flag= 1L;
3190
 
  return (unsigned char*) &tmp;
 
3190
  return (uchar*) &tmp;
3191
3191
}
3192
3192
 
3193
 
in_double::in_double(uint32_t elements)
 
3193
in_double::in_double(uint elements)
3194
3194
  :in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
3195
3195
{}
3196
3196
 
3197
 
void in_double::set(uint32_t pos,Item *item)
 
3197
void in_double::set(uint pos,Item *item)
3198
3198
{
3199
3199
  ((double*) base)[pos]= item->val_real();
3200
3200
}
3201
3201
 
3202
 
unsigned char *in_double::get_value(Item *item)
 
3202
uchar *in_double::get_value(Item *item)
3203
3203
{
3204
3204
  tmp= item->val_real();
3205
3205
  if (item->null_value)
3206
3206
    return 0;                                   /* purecov: inspected */
3207
 
  return (unsigned char*) &tmp;
 
3207
  return (uchar*) &tmp;
3208
3208
}
3209
3209
 
3210
3210
 
3211
 
in_decimal::in_decimal(uint32_t elements)
 
3211
in_decimal::in_decimal(uint elements)
3212
3212
  :in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3213
3213
{}
3214
3214
 
3215
3215
 
3216
 
void in_decimal::set(uint32_t pos, Item *item)
 
3216
void in_decimal::set(uint pos, Item *item)
3217
3217
{
3218
3218
  /* as far as 'item' is constant, we can store reference on my_decimal */
3219
3219
  my_decimal *dec= ((my_decimal *)base) + pos;
3226
3226
}
3227
3227
 
3228
3228
 
3229
 
unsigned char *in_decimal::get_value(Item *item)
 
3229
uchar *in_decimal::get_value(Item *item)
3230
3230
{
3231
3231
  my_decimal *result= item->val_decimal(&val);
3232
3232
  if (item->null_value)
3233
3233
    return 0;
3234
 
  return (unsigned char *)result;
 
3234
  return (uchar *)result;
3235
3235
}
3236
3236
 
3237
3237
 
3282
3282
{
3283
3283
  if (comparators)
3284
3284
  {
3285
 
    for (uint32_t i= 0; i < n; i++)
 
3285
    for (uint i= 0; i < n; i++)
3286
3286
    {
3287
3287
      if (comparators[i])
3288
3288
        delete comparators[i];
3295
3295
void cmp_item_row::alloc_comparators()
3296
3296
{
3297
3297
  if (!comparators)
3298
 
    comparators= (cmp_item **) current_session->calloc(sizeof(cmp_item *)*n);
 
3298
    comparators= (cmp_item **) current_thd->calloc(sizeof(cmp_item *)*n);
3299
3299
}
3300
3300
 
3301
3301
 
3307
3307
  {
3308
3308
    item->bring_value();
3309
3309
    item->null_value= 0;
3310
 
    for (uint32_t i=0; i < n; i++)
 
3310
    for (uint i=0; i < n; i++)
3311
3311
    {
3312
3312
      if (!comparators[i])
3313
3313
        if (!(comparators[i]=
3335
3335
  {
3336
3336
    item->bring_value();
3337
3337
    item->null_value= 0;
3338
 
    for (uint32_t i=0; i < n; i++)
 
3338
    for (uint i=0; i < n; i++)
3339
3339
    {
3340
3340
      if (!(comparators[i]= tmpl->comparators[i]->make_same()))
3341
3341
        break;                                  // new failed
3357
3357
  }
3358
3358
  bool was_null= 0;
3359
3359
  arg->bring_value();
3360
 
  for (uint32_t i=0; i < n; i++)
 
3360
  for (uint i=0; i < n; i++)
3361
3361
  {
3362
3362
    if (comparators[i]->cmp(arg->element_index(i)))
3363
3363
    {
3373
3373
int cmp_item_row::compare(cmp_item *c)
3374
3374
{
3375
3375
  cmp_item_row *l_cmp= (cmp_item_row *) c;
3376
 
  for (uint32_t i=0; i < n; i++)
 
3376
  for (uint i=0; i < n; i++)
3377
3377
  {
3378
3378
    int res;
3379
3379
    if ((res= comparators[i]->compare(l_cmp->comparators[i])))
3418
3418
{
3419
3419
  bool is_null;
3420
3420
  Item **tmp_item= lval_cache ? &lval_cache : &item;
3421
 
  value= get_datetime_value(session, &tmp_item, &lval_cache, warn_item, &is_null);
 
3421
  value= get_datetime_value(thd, &tmp_item, &lval_cache, warn_item, &is_null);
3422
3422
}
3423
3423
 
3424
3424
 
3427
3427
  bool is_null;
3428
3428
  Item **tmp_item= &arg;
3429
3429
  return value !=
3430
 
    get_datetime_value(session, &tmp_item, 0, warn_item, &is_null);
 
3430
    get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
3431
3431
}
3432
3432
 
3433
3433
 
3464
3464
    The function saves in ref the pointer to the item or to a newly created
3465
3465
    item that is considered as a replacement for the original one.
3466
3466
 
3467
 
  @param session     reference to the global context of the query thread
 
3467
  @param thd     reference to the global context of the query thread
3468
3468
  @param ref     pointer to Item* variable where pointer to resulting "fixed"
3469
3469
                 item is to be assigned
3470
3470
 
3485
3485
*/
3486
3486
 
3487
3487
bool
3488
 
Item_func_in::fix_fields(Session *session, Item **ref)
 
3488
Item_func_in::fix_fields(THD *thd, Item **ref)
3489
3489
{
3490
3490
  Item **arg, **arg_end;
3491
3491
 
3492
 
  if (Item_func_opt_neg::fix_fields(session, ref))
 
3492
  if (Item_func_opt_neg::fix_fields(thd, ref))
3493
3493
    return 1;
3494
3494
 
3495
3495
  /* not_null_tables_cache == union(T1(e),union(T1(ei))) */
3508
3508
static int srtcmp_in(const CHARSET_INFO * const cs, const String *x,const String *y)
3509
3509
{
3510
3510
  return cs->coll->strnncollsp(cs,
3511
 
                               (unsigned char *) x->ptr(),x->length(),
3512
 
                               (unsigned char *) y->ptr(),y->length(), 0);
 
3511
                               (uchar *) x->ptr(),x->length(),
 
3512
                               (uchar *) y->ptr(),y->length(), 0);
3513
3513
}
3514
3514
 
3515
3515
 
3517
3517
{
3518
3518
  Item **arg, **arg_end;
3519
3519
  bool const_itm= 1;
3520
 
  Session *session= current_session;
 
3520
  THD *thd= current_thd;
3521
3521
  bool datetime_found= false;
3522
3522
  /* true <=> arguments values will be compared as DATETIMEs. */
3523
3523
  bool compare_as_datetime= false;
3524
3524
  Item *date_arg= 0;
3525
 
  uint32_t found_types= 0;
3526
 
  uint32_t type_cnt= 0, i;
 
3525
  uint found_types= 0;
 
3526
  uint type_cnt= 0, i;
3527
3527
  Item_result cmp_type= STRING_RESULT;
3528
3528
  left_result_type= args[0]->result_type();
3529
3529
  if (!(found_types= collect_cmp_types(args, arg_count)))
3579
3579
    /* All DATE/DATETIME fields/functions has the STRING result type. */
3580
3580
    if (cmp_type == STRING_RESULT || cmp_type == ROW_RESULT)
3581
3581
    {
3582
 
      uint32_t col, cols= args[0]->cols();
 
3582
      uint col, cols= args[0]->cols();
3583
3583
 
3584
3584
      for (col= 0; col < cols; col++)
3585
3585
      {
3655
3655
        comparison type accordingly.
3656
3656
      */  
3657
3657
      if (args[0]->real_item()->type() == FIELD_ITEM &&
3658
 
          session->lex->sql_command != SQLCOM_SHOW_CREATE &&
 
3658
          thd->lex->sql_command != SQLCOM_SHOW_CREATE &&
3659
3659
          cmp_type != INT_RESULT)
3660
3660
      {
3661
3661
        Item_field *field_item= (Item_field*) (args[0]->real_item());
3664
3664
          bool all_converted= true;
3665
3665
          for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3666
3666
          {
3667
 
            if (!convert_constant_item (session, field_item, &arg[0]))
 
3667
            if (!convert_constant_item (thd, field_item, &arg[0]))
3668
3668
              all_converted= false;
3669
3669
          }
3670
3670
          if (all_converted)
3698
3698
        return;
3699
3699
      }
3700
3700
    }
3701
 
    if (array && !(session->is_fatal_error))            // If not EOM
 
3701
    if (array && !(thd->is_fatal_error))                // If not EOM
3702
3702
    {
3703
 
      uint32_t j=0;
3704
 
      for (uint32_t i=1 ; i < arg_count ; i++)
 
3703
      uint j=0;
 
3704
      for (uint i=1 ; i < arg_count ; i++)
3705
3705
      {
3706
3706
        array->set(j,args[i]);
3707
3707
        if (!args[i]->null_value)                       // Skip NULL values
3780
3780
{
3781
3781
  cmp_item *in_item;
3782
3782
  assert(fixed == 1);
3783
 
  uint32_t value_added_map= 0;
 
3783
  uint value_added_map= 0;
3784
3784
  if (array)
3785
3785
  {
3786
3786
    int tmp=array->find(args[0]);
3788
3788
    return (int64_t) (!null_value && tmp != negated);
3789
3789
  }
3790
3790
 
3791
 
  for (uint32_t i= 1 ; i < arg_count ; i++)
 
3791
  for (uint i= 1 ; i < arg_count ; i++)
3792
3792
  {
3793
3793
    Item_result cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
3794
3794
    in_item= cmp_items[(uint)cmp_type];
3850
3850
  return (int64_t) (arg1 & arg2);
3851
3851
}
3852
3852
 
3853
 
Item_cond::Item_cond(Session *session, Item_cond *item)
3854
 
  :Item_bool_func(session, item),
 
3853
Item_cond::Item_cond(THD *thd, Item_cond *item)
 
3854
  :Item_bool_func(thd, item),
3855
3855
   abort_on_null(item->abort_on_null),
3856
3856
   and_tables_cache(item->and_tables_cache)
3857
3857
{
3861
3861
}
3862
3862
 
3863
3863
 
3864
 
void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
 
3864
void Item_cond::copy_andor_arguments(THD *thd, Item_cond *item)
3865
3865
{
3866
3866
  List_iterator_fast<Item> li(item->list);
3867
3867
  while (Item *it= li++)
3868
 
    list.push_back(it->copy_andor_structure(session));
 
3868
    list.push_back(it->copy_andor_structure(thd));
3869
3869
}
3870
3870
 
3871
3871
 
3872
3872
bool
3873
 
Item_cond::fix_fields(Session *session, Item **ref __attribute__((unused)))
 
3873
Item_cond::fix_fields(THD *thd, Item **ref __attribute__((unused)))
3874
3874
{
3875
3875
  assert(fixed == 0);
3876
3876
  List_iterator<Item> li(list);
3877
3877
  Item *item;
3878
 
  void *orig_session_marker= session->session_marker;
3879
 
  unsigned char buff[sizeof(char*)];                    // Max local vars in function
 
3878
  void *orig_thd_marker= thd->thd_marker;
 
3879
  uchar buff[sizeof(char*)];                    // Max local vars in function
3880
3880
  not_null_tables_cache= used_tables_cache= 0;
3881
3881
  const_item_cache= 1;
3882
3882
 
3883
3883
  if (functype() == COND_OR_FUNC)
3884
 
    session->session_marker= 0;
 
3884
    thd->thd_marker= 0;
3885
3885
  /*
3886
3886
    and_table_cache is the value that Item_cond_or() returns for
3887
3887
    not_null_tables()
3888
3888
  */
3889
3889
  and_tables_cache= ~(table_map) 0;
3890
3890
 
3891
 
  if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
 
3891
  if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
3892
3892
    return true;                                // Fatal error flag is set!
3893
3893
  /*
3894
3894
    The following optimization reduces the depth of an AND-OR tree.
3921
3921
 
3922
3922
    // item can be substituted in fix_fields
3923
3923
    if ((!item->fixed &&
3924
 
         item->fix_fields(session, li.ref())) ||
 
3924
         item->fix_fields(thd, li.ref())) ||
3925
3925
        (item= *li.ref())->check_cols(1))
3926
3926
      return true; /* purecov: inspected */
3927
3927
    used_tables_cache|=     item->used_tables();
3939
3939
    if (item->maybe_null)
3940
3940
      maybe_null=1;
3941
3941
  }
3942
 
  session->lex->current_select->cond_count+= list.elements;
3943
 
  session->session_marker= orig_session_marker;
 
3942
  thd->lex->current_select->cond_count+= list.elements;
 
3943
  thd->thd_marker= orig_thd_marker;
3944
3944
  fix_length_and_dec();
3945
3945
  fixed= 1;
3946
3946
  return false;
3979
3979
}
3980
3980
 
3981
3981
 
3982
 
bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
 
3982
bool Item_cond::walk(Item_processor processor, bool walk_subquery, uchar *arg)
3983
3983
{
3984
3984
  List_iterator_fast<Item> li(list);
3985
3985
  Item *item;
4008
4008
    Item returned as the result of transformation of the root node 
4009
4009
*/
4010
4010
 
4011
 
Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
 
4011
Item *Item_cond::transform(Item_transformer transformer, uchar *arg)
4012
4012
{
4013
4013
  List_iterator<Item> li(list);
4014
4014
  Item *item;
4019
4019
      return 0;
4020
4020
 
4021
4021
    /*
4022
 
      Session::change_item_tree() should be called only if the tree was
 
4022
      THD::change_item_tree() should be called only if the tree was
4023
4023
      really transformed, i.e. when a new item has been created.
4024
4024
      Otherwise we'll be allocating a lot of unnecessary memory for
4025
4025
      change records at each execution.
4026
4026
    */
4027
4027
    if (new_item != item)
4028
 
      current_session->change_item_tree(li.ref(), new_item);
 
4028
      current_thd->change_item_tree(li.ref(), new_item);
4029
4029
  }
4030
4030
  return Item_func::transform(transformer, arg);
4031
4031
}
4055
4055
    Item returned as the result of transformation of the root node 
4056
4056
*/
4057
4057
 
4058
 
Item *Item_cond::compile(Item_analyzer analyzer, unsigned char **arg_p,
4059
 
                         Item_transformer transformer, unsigned char *arg_t)
 
4058
Item *Item_cond::compile(Item_analyzer analyzer, uchar **arg_p,
 
4059
                         Item_transformer transformer, uchar *arg_t)
4060
4060
{
4061
4061
  if (!(this->*analyzer)(arg_p))
4062
4062
    return 0;
4069
4069
      The same parameter value of arg_p must be passed
4070
4070
      to analyze any argument of the condition formula.
4071
4071
    */   
4072
 
    unsigned char *arg_v= *arg_p;
 
4072
    uchar *arg_v= *arg_p;
4073
4073
    Item *new_item= item->compile(analyzer, &arg_v, transformer, arg_t);
4074
4074
    if (new_item && new_item != item)
4075
4075
      li.replace(new_item);
4109
4109
  (Calculation done by update_sum_func() and copy_sum_funcs() in
4110
4110
  sql_select.cc)
4111
4111
 
4112
 
  @param session                        Thread handler
 
4112
  @param thd                    Thread handler
4113
4113
  @param ref_pointer_array      Pointer to array of reference fields
4114
4114
  @param fields         All fields in select
4115
4115
 
4118
4118
    that have or refer (HAVING) to a SUM expression.
4119
4119
*/
4120
4120
 
4121
 
void Item_cond::split_sum_func(Session *session, Item **ref_pointer_array,
 
4121
void Item_cond::split_sum_func(THD *thd, Item **ref_pointer_array,
4122
4122
                               List<Item> &fields)
4123
4123
{
4124
4124
  List_iterator<Item> li(list);
4125
4125
  Item *item;
4126
4126
  while ((item= li++))
4127
 
    item->split_sum_func2(session, ref_pointer_array, fields, li.ref(), true);
 
4127
    item->split_sum_func2(thd, ref_pointer_array, fields, li.ref(), true);
4128
4128
}
4129
4129
 
4130
4130
 
4169
4169
}
4170
4170
 
4171
4171
 
4172
 
void Item_cond::neg_arguments(Session *session)
 
4172
void Item_cond::neg_arguments(THD *thd)
4173
4173
{
4174
4174
  List_iterator<Item> li(list);
4175
4175
  Item *item;
4176
4176
  while ((item= li++))          /* Apply not transformation to the arguments */
4177
4177
  {
4178
 
    Item *new_item= item->neg_transformer(session);
 
4178
    Item *new_item= item->neg_transformer(thd);
4179
4179
    if (!new_item)
4180
4180
    {
4181
4181
      if (!(new_item= new Item_func_not(item)))
4182
4182
        return;                                 // Fatal OEM error
4183
4183
    }
4184
 
    li.replace(new_item);
 
4184
    VOID(li.replace(new_item));
4185
4185
  }
4186
4186
}
4187
4187
 
4399
4399
}
4400
4400
 
4401
4401
 
4402
 
bool Item_func_like::fix_fields(Session *session, Item **ref)
 
4402
bool Item_func_like::fix_fields(THD *thd, Item **ref)
4403
4403
{
4404
4404
  assert(fixed == 0);
4405
 
  if (Item_bool_func2::fix_fields(session, ref) ||
4406
 
      escape_item->fix_fields(session, &escape_item))
 
4405
  if (Item_bool_func2::fix_fields(thd, ref) ||
 
4406
      escape_item->fix_fields(thd, &escape_item))
4407
4407
    return true;
4408
4408
 
4409
4409
  if (!escape_item->const_during_execution())
4429
4429
        const CHARSET_INFO * const cs= escape_str->charset();
4430
4430
        my_wc_t wc;
4431
4431
        int rc= cs->cset->mb_wc(cs, &wc,
4432
 
                                (const unsigned char*) escape_str->ptr(),
4433
 
                                (const unsigned char*) escape_str->ptr() +
 
4432
                                (const uchar*) escape_str->ptr(),
 
4433
                                (const uchar*) escape_str->ptr() +
4434
4434
                                               escape_str->length());
4435
4435
        escape= (int) (rc > 0 ? wc : '\\');
4436
4436
      }
4447
4447
                                         escape_str->charset(), cs, &unused))
4448
4448
        {
4449
4449
          char ch;
4450
 
          uint32_t errors;
 
4450
          uint errors;
4451
4451
          uint32_t cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
4452
4452
                                          escape_str->length(),
4453
4453
                                          escape_str->charset(), &errors);
4490
4490
      {
4491
4491
        pattern     = first + 1;
4492
4492
        pattern_len = (int) len - 2;
4493
 
        int *suff = (int*) session->alloc((int) (sizeof(int)*
 
4493
        int *suff = (int*) thd->alloc((int) (sizeof(int)*
4494
4494
                                      ((pattern_len + 1)*2+
4495
4495
                                      alphabet_size)));
4496
4496
        bmGs      = suff + pattern_len + 1;
4510
4510
}
4511
4511
 
4512
4512
#ifdef LIKE_CMP_TOUPPER
4513
 
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
 
4513
#define likeconv(cs,A) (uchar) (cs)->toupper(A)
4514
4514
#else
4515
 
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
 
4515
#define likeconv(cs,A) (uchar) (cs)->sort_order[(uchar) (A)]
4516
4516
#endif
4517
4517
 
4518
4518
 
4541
4541
      else
4542
4542
      {
4543
4543
        if (i < g)
4544
 
          g = i; // g = cmin(i, g)
 
4544
          g = i; // g = min(i, g)
4545
4545
        f = i;
4546
4546
        while (g >= 0 && pattern[g] == pattern[g + plm1 - f])
4547
4547
          g--;
4560
4560
      else
4561
4561
      {
4562
4562
        if (i < g)
4563
 
          g = i; // g = cmin(i, g)
 
4563
          g = i; // g = min(i, g)
4564
4564
        f = i;
4565
4565
        while (g >= 0 &&
4566
4566
               likeconv(cs, pattern[g]) == likeconv(cs, pattern[g + plm1 - f]))
4634
4634
  if (!cs->sort_order)
4635
4635
  {
4636
4636
    for (j = 0; j < plm1; j++)
4637
 
      bmBc[(uint) (unsigned char) pattern[j]] = plm1 - j;
 
4637
      bmBc[(uint) (uchar) pattern[j]] = plm1 - j;
4638
4638
  }
4639
4639
  else
4640
4640
  {
4680
4680
 
4681
4681
      register const int v = plm1 - i;
4682
4682
      turboShift = u - v;
4683
 
      bcShift    = bmBc[(uint) (unsigned char) text[i + j]] - plm1 + i;
 
4683
      bcShift    = bmBc[(uint) (uchar) text[i + j]] - plm1 + i;
4684
4684
      shift      = (turboShift > bcShift) ? turboShift : bcShift;
4685
4685
      shift      = (shift > bmGs[i]) ? shift : bmGs[i];
4686
4686
      if (shift == bmGs[i])
4688
4688
      else
4689
4689
      {
4690
4690
        if (turboShift < bcShift)
4691
 
          shift = cmax(shift, u + 1);
 
4691
          shift = max(shift, u + 1);
4692
4692
        u = 0;
4693
4693
      }
4694
4694
      j+= shift;
4713
4713
      turboShift = u - v;
4714
4714
      bcShift    = bmBc[(uint) likeconv(cs, text[i + j])] - plm1 + i;
4715
4715
      shift      = (turboShift > bcShift) ? turboShift : bcShift;
4716
 
      shift      = cmax(shift, bmGs[i]);
 
4716
      shift      = max(shift, bmGs[i]);
4717
4717
      if (shift == bmGs[i])
4718
4718
        u = (pattern_len - shift < v) ? pattern_len - shift : v;
4719
4719
      else
4720
4720
      {
4721
4721
        if (turboShift < bcShift)
4722
 
          shift = cmax(shift, u + 1);
 
4722
          shift = max(shift, u + 1);
4723
4723
        u = 0;
4724
4724
      }
4725
4725
      j+= shift;
4783
4783
       IS NOT NULL(a)     -> IS NULL(a)
4784
4784
    @endverbatim
4785
4785
 
4786
 
  @param session                thread handler
 
4786
  @param thd            thread handler
4787
4787
 
4788
4788
  @return
4789
4789
    New item or
4790
4790
    NULL if we cannot apply NOT transformation (see Item::neg_transformer()).
4791
4791
*/
4792
4792
 
4793
 
Item *Item_func_not::neg_transformer(Session *session __attribute__((unused)))  /* NOT(x)  ->  x */
 
4793
Item *Item_func_not::neg_transformer(THD *thd __attribute__((unused)))  /* NOT(x)  ->  x */
4794
4794
{
4795
4795
  return args[0];
4796
4796
}
4797
4797
 
4798
4798
 
4799
 
Item *Item_bool_rowready_func2::neg_transformer(Session *session __attribute__((unused)))
 
4799
Item *Item_bool_rowready_func2::neg_transformer(THD *thd __attribute__((unused)))
4800
4800
{
4801
4801
  Item *item= negated_item();
4802
4802
  return item;
4806
4806
/**
4807
4807
  a IS NULL  ->  a IS NOT NULL.
4808
4808
*/
4809
 
Item *Item_func_isnull::neg_transformer(Session *session __attribute__((unused)))
 
4809
Item *Item_func_isnull::neg_transformer(THD *thd __attribute__((unused)))
4810
4810
{
4811
4811
  Item *item= new Item_func_isnotnull(args[0]);
4812
4812
  return item;
4816
4816
/**
4817
4817
  a IS NOT NULL  ->  a IS NULL.
4818
4818
*/
4819
 
Item *Item_func_isnotnull::neg_transformer(Session *session __attribute__((unused)))
 
4819
Item *Item_func_isnotnull::neg_transformer(THD *thd __attribute__((unused)))
4820
4820
{
4821
4821
  Item *item= new Item_func_isnull(args[0]);
4822
4822
  return item;
4823
4823
}
4824
4824
 
4825
4825
 
4826
 
Item *Item_cond_and::neg_transformer(Session *session)  /* NOT(a AND b AND ...)  -> */
 
4826
Item *Item_cond_and::neg_transformer(THD *thd)  /* NOT(a AND b AND ...)  -> */
4827
4827
                                        /* NOT a OR NOT b OR ... */
4828
4828
{
4829
 
  neg_arguments(session);
 
4829
  neg_arguments(thd);
4830
4830
  Item *item= new Item_cond_or(list);
4831
4831
  return item;
4832
4832
}
4833
4833
 
4834
4834
 
4835
 
Item *Item_cond_or::neg_transformer(Session *session)   /* NOT(a OR b OR ...)  -> */
 
4835
Item *Item_cond_or::neg_transformer(THD *thd)   /* NOT(a OR b OR ...)  -> */
4836
4836
                                        /* NOT a AND NOT b AND ... */
4837
4837
{
4838
 
  neg_arguments(session);
 
4838
  neg_arguments(thd);
4839
4839
  Item *item= new Item_cond_and(list);
4840
4840
  return item;
4841
4841
}
4842
4842
 
4843
4843
 
4844
 
Item *Item_func_nop_all::neg_transformer(Session *session __attribute__((unused)))
 
4844
Item *Item_func_nop_all::neg_transformer(THD *thd __attribute__((unused)))
4845
4845
{
4846
4846
  /* "NOT (e $cmp$ ANY (SELECT ...)) -> e $rev_cmp$" ALL (SELECT ...) */
4847
4847
  Item_func_not_all *new_item= new Item_func_not_all(args[0]);
4852
4852
  return new_item;
4853
4853
}
4854
4854
 
4855
 
Item *Item_func_not_all::neg_transformer(Session *session __attribute__((unused)))
 
4855
Item *Item_func_not_all::neg_transformer(THD *thd __attribute__((unused)))
4856
4856
{
4857
4857
  /* "NOT (e $cmp$ ALL (SELECT ...)) -> e $rev_cmp$" ANY (SELECT ...) */
4858
4858
  Item_func_nop_all *new_item= new Item_func_nop_all(args[0]);
4959
4959
  fields.push_back(f);
4960
4960
}
4961
4961
 
4962
 
uint32_t Item_equal::members()
 
4962
uint Item_equal::members()
4963
4963
{
4964
4964
  return fields.elements;
4965
4965
}
5092
5092
  }
5093
5093
}
5094
5094
 
5095
 
bool Item_equal::fix_fields(Session *session __attribute__((unused)), Item **ref __attribute__((unused)))
 
5095
bool Item_equal::fix_fields(THD *thd __attribute__((unused)), Item **ref __attribute__((unused)))
5096
5096
{
5097
5097
  List_iterator_fast<Item_field> li(fields);
5098
5098
  Item *item;
5156
5156
                                      item->collation.collation);
5157
5157
}
5158
5158
 
5159
 
bool Item_equal::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
 
5159
bool Item_equal::walk(Item_processor processor, bool walk_subquery, uchar *arg)
5160
5160
{
5161
5161
  List_iterator_fast<Item_field> it(fields);
5162
5162
  Item *item;
5168
5168
  return Item_func::walk(processor, walk_subquery, arg);
5169
5169
}
5170
5170
 
5171
 
Item *Item_equal::transform(Item_transformer transformer, unsigned char *arg)
 
5171
Item *Item_equal::transform(Item_transformer transformer, uchar *arg)
5172
5172
{
5173
5173
  List_iterator<Item_field> it(fields);
5174
5174
  Item *item;
5179
5179
      return 0;
5180
5180
 
5181
5181
    /*
5182
 
      Session::change_item_tree() should be called only if the tree was
 
5182
      THD::change_item_tree() should be called only if the tree was
5183
5183
      really transformed, i.e. when a new item has been created.
5184
5184
      Otherwise we'll be allocating a lot of unnecessary memory for
5185
5185
      change records at each execution.
5186
5186
    */
5187
5187
    if (new_item != item)
5188
 
      current_session->change_item_tree((Item **) it.ref(), new_item);
 
5188
      current_thd->change_item_tree((Item **) it.ref(), new_item);
5189
5189
  }
5190
5190
  return Item_func::transform(transformer, arg);
5191
5191
}