~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/cmpfunc.cc

  • Committer: Brian Aker
  • Date: 2010-12-17 00:08:06 UTC
  • mfrom: (2002.1.4 clean)
  • Revision ID: brian@tangent.org-20101217000806-fa6kmggjnhsl4q85
Rollup for field encapsulation, monty fix for bzrignore, and Andrew bug
fixes.

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 <config.h>
25
 
 
26
 
#include <drizzled/cached_item.h>
27
 
#include <drizzled/check_stack_overrun.h>
28
 
#include <drizzled/current_session.h>
29
 
#include <drizzled/error.h>
30
 
#include <drizzled/internal/my_sys.h>
31
 
#include <drizzled/item/cache_int.h>
32
 
#include <drizzled/item/cmpfunc.h>
33
 
#include <drizzled/item/int_with_ref.h>
34
 
#include <drizzled/item/subselect.h>
35
 
#include <drizzled/session.h>
36
 
#include <drizzled/sql_select.h>
37
 
#include <drizzled/temporal.h>
38
 
#include <drizzled/time_functions.h>
39
 
 
 
24
#include "config.h"
 
25
#include "drizzled/sql_select.h"
 
26
#include "drizzled/error.h"
 
27
#include "drizzled/temporal.h"
 
28
#include "drizzled/item/cmpfunc.h"
 
29
#include "drizzled/cached_item.h"
 
30
#include "drizzled/item/cache_int.h"
 
31
#include "drizzled/item/int_with_ref.h"
 
32
#include "drizzled/check_stack_overrun.h"
 
33
#include "drizzled/time_functions.h"
 
34
#include "drizzled/internal/my_sys.h"
40
35
#include <math.h>
41
36
#include <algorithm>
42
37
 
468
463
      the call to save_in_field below overrides that value.
469
464
    */
470
465
    if (field_item->depended_from)
471
 
    {
472
466
      orig_field_val= field->val_int();
473
 
    }
474
 
 
475
467
    if (!(*item)->is_null() && !(*item)->save_in_field(field, 1))
476
468
    {
477
469
      Item *tmp= new Item_int_with_ref(field->val_int(), *item,
480
472
        session->change_item_tree(item, tmp);
481
473
      result= 1;                                        // Item was replaced
482
474
    }
483
 
 
484
475
    /* Restore the original field value. */
485
476
    if (field_item->depended_from)
486
477
    {
487
 
      result= field->store(orig_field_val, field->isUnsigned());
 
478
      result= field->store(orig_field_val, true);
488
479
      /* orig_field_val must be a valid value that can be restored back. */
489
480
      assert(!result);
490
481
    }
498
489
void Item_bool_func2::fix_length_and_dec()
499
490
{
500
491
  max_length= 1;                                     // Function returns 0 or 1
 
492
  Session *session;
501
493
 
502
494
  /*
503
495
    As some compare functions are generated after sql_yacc,
535
527
    return;
536
528
  }
537
529
 
 
530
  session= current_session;
538
531
  Item_field *field_item= NULL;
539
532
 
540
533
  if (args[0]->real_item()->type() == FIELD_ITEM)
543
536
    if (field_item->field->can_be_compared_as_int64_t() &&
544
537
        !(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
545
538
    {
546
 
      if (convert_constant_item(&getSession(), field_item, &args[1]))
 
539
      if (convert_constant_item(session, field_item, &args[1]))
547
540
      {
548
541
        cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
549
542
                         INT_RESULT);           // Works for all types.
559
552
          !(field_item->is_datetime() &&
560
553
            args[0]->result_type() == STRING_RESULT))
561
554
      {
562
 
        if (convert_constant_item(&getSession(), field_item, &args[0]))
 
555
        if (convert_constant_item(session, field_item, &args[0]))
563
556
        {
564
557
          cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
565
558
                           INT_RESULT); // Works for all types.
572
565
  set_cmp_func();
573
566
}
574
567
 
575
 
Arg_comparator::Arg_comparator():
576
 
  session(current_session),
577
 
  a_cache(0),
578
 
  b_cache(0)
579
 
{}
580
 
 
581
 
Arg_comparator::Arg_comparator(Item **a1, Item **a2):
582
 
  a(a1),
583
 
  b(a2),
584
 
  session(current_session),
585
 
  a_cache(0),
586
 
  b_cache(0)
587
 
{}
588
568
 
589
569
int Arg_comparator::set_compare_func(Item_bool_func2 *item, Item_result type)
590
570
{
591
571
  owner= item;
592
572
  func= comparator_matrix[type]
593
 
    [test(owner->functype() == Item_func::EQUAL_FUNC)];
594
 
 
 
573
                         [test(owner->functype() == Item_func::EQUAL_FUNC)];
595
574
  switch (type) {
596
575
  case ROW_RESULT:
597
 
    {
598
 
      uint32_t n= (*a)->cols();
599
 
      if (n != (*b)->cols())
600
 
      {
601
 
        my_error(ER_OPERAND_COLUMNS, MYF(0), n);
602
 
        comparators= 0;
603
 
        return 1;
604
 
      }
605
 
      if (!(comparators= new Arg_comparator[n]))
606
 
        return 1;
607
 
      for (uint32_t i=0; i < n; i++)
608
 
      {
609
 
        if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
610
 
        {
611
 
          my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
612
 
          return 1;
613
 
        }
614
 
        comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
615
 
      }
616
 
      break;
617
 
    }
618
 
 
 
576
  {
 
577
    uint32_t n= (*a)->cols();
 
578
    if (n != (*b)->cols())
 
579
    {
 
580
      my_error(ER_OPERAND_COLUMNS, MYF(0), n);
 
581
      comparators= 0;
 
582
      return 1;
 
583
    }
 
584
    if (!(comparators= new Arg_comparator[n]))
 
585
      return 1;
 
586
    for (uint32_t i=0; i < n; i++)
 
587
    {
 
588
      if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
 
589
      {
 
590
        my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
 
591
        return 1;
 
592
      }
 
593
      comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
 
594
    }
 
595
    break;
 
596
  }
619
597
  case STRING_RESULT:
 
598
  {
 
599
    /*
 
600
      We must set cmp_charset here as we may be called from for an automatic
 
601
      generated item, like in natural join
 
602
    */
 
603
    if (cmp_collation.set((*a)->collation, (*b)->collation) ||
 
604
        cmp_collation.derivation == DERIVATION_NONE)
 
605
    {
 
606
      my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
 
607
      return 1;
 
608
    }
 
609
    if (cmp_collation.collation == &my_charset_bin)
620
610
    {
621
611
      /*
622
 
        We must set cmp_charset here as we may be called from for an automatic
623
 
        generated item, like in natural join
 
612
        We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
 
613
        without removing end space
624
614
      */
625
 
      if (cmp_collation.set((*a)->collation, (*b)->collation) ||
626
 
          cmp_collation.derivation == DERIVATION_NONE)
627
 
      {
628
 
        my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
629
 
        return 1;
630
 
      }
631
 
      if (cmp_collation.collation == &my_charset_bin)
632
 
      {
633
 
        /*
634
 
          We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
635
 
          without removing end space
636
 
        */
637
 
        if (func == &Arg_comparator::compare_string)
638
 
          func= &Arg_comparator::compare_binary_string;
639
 
        else if (func == &Arg_comparator::compare_e_string)
640
 
          func= &Arg_comparator::compare_e_binary_string;
 
615
      if (func == &Arg_comparator::compare_string)
 
616
        func= &Arg_comparator::compare_binary_string;
 
617
      else if (func == &Arg_comparator::compare_e_string)
 
618
        func= &Arg_comparator::compare_e_binary_string;
641
619
 
642
 
        /*
643
 
          As this is binary compassion, mark all fields that they can't be
644
 
          transformed. Otherwise we would get into trouble with comparisons
645
 
like:
646
 
WHERE col= 'j' AND col LIKE BINARY 'j'
647
 
which would be transformed to:
648
 
WHERE col= 'j'
 
620
      /*
 
621
        As this is binary compassion, mark all fields that they can't be
 
622
        transformed. Otherwise we would get into trouble with comparisons
 
623
        like:
 
624
        WHERE col= 'j' AND col LIKE BINARY 'j'
 
625
        which would be transformed to:
 
626
        WHERE col= 'j'
649
627
      */
650
 
        (*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
651
 
        (*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
652
 
      }
653
 
      break;
 
628
      (*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
 
629
      (*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
654
630
    }
 
631
    break;
 
632
  }
655
633
  case INT_RESULT:
656
 
    {
657
 
      if (func == &Arg_comparator::compare_int_signed)
658
 
      {
659
 
        if ((*a)->unsigned_flag)
660
 
          func= (((*b)->unsigned_flag)?
661
 
                 &Arg_comparator::compare_int_unsigned :
662
 
                 &Arg_comparator::compare_int_unsigned_signed);
663
 
        else if ((*b)->unsigned_flag)
664
 
          func= &Arg_comparator::compare_int_signed_unsigned;
665
 
      }
666
 
      else if (func== &Arg_comparator::compare_e_int)
667
 
      {
668
 
        if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
669
 
          func= &Arg_comparator::compare_e_int_diff_signedness;
670
 
      }
671
 
      break;
672
 
    }
 
634
  {
 
635
    if (func == &Arg_comparator::compare_int_signed)
 
636
    {
 
637
      if ((*a)->unsigned_flag)
 
638
        func= (((*b)->unsigned_flag)?
 
639
               &Arg_comparator::compare_int_unsigned :
 
640
               &Arg_comparator::compare_int_unsigned_signed);
 
641
      else if ((*b)->unsigned_flag)
 
642
        func= &Arg_comparator::compare_int_signed_unsigned;
 
643
    }
 
644
    else if (func== &Arg_comparator::compare_e_int)
 
645
    {
 
646
      if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
 
647
        func= &Arg_comparator::compare_e_int_diff_signedness;
 
648
    }
 
649
    break;
 
650
  }
673
651
  case DECIMAL_RESULT:
674
652
    break;
675
653
  case REAL_RESULT:
 
654
  {
 
655
    if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
676
656
    {
677
 
      if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
678
 
      {
679
 
        precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
680
 
        if (func == &Arg_comparator::compare_real)
681
 
          func= &Arg_comparator::compare_real_fixed;
682
 
        else if (func == &Arg_comparator::compare_e_real)
683
 
          func= &Arg_comparator::compare_e_real_fixed;
684
 
      }
685
 
      break;
 
657
      precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
 
658
      if (func == &Arg_comparator::compare_real)
 
659
        func= &Arg_comparator::compare_real_fixed;
 
660
      else if (func == &Arg_comparator::compare_e_real)
 
661
        func= &Arg_comparator::compare_e_real_fixed;
686
662
    }
687
 
  }
688
 
 
 
663
    break;
 
664
  }
 
665
  default:
 
666
    assert(0);
 
667
  }
689
668
  return 0;
690
669
}
691
670
 
712
691
    converted value. 0 on error and on zero-dates -- check 'failure'
713
692
*/
714
693
 
715
 
static int64_t
716
 
get_date_from_str(Session *session, String *str, type::timestamp_t warn_type,
 
694
static uint64_t
 
695
get_date_from_str(Session *session, String *str, enum enum_drizzle_timestamp_type warn_type,
717
696
                  char *warn_name, bool *error_arg)
718
697
{
719
 
  int64_t value= 0;
720
 
  type::cut_t error= type::VALID;
721
 
  type::Time l_time;
722
 
  type::timestamp_t ret;
723
 
 
724
 
  ret= l_time.store(str->ptr(), str->length(),
725
 
                    (TIME_FUZZY_DATE | MODE_INVALID_DATES | (session->variables.sql_mode & MODE_NO_ZERO_DATE)),
726
 
                    error);
727
 
 
728
 
  if (ret == type::DRIZZLE_TIMESTAMP_DATETIME || ret == type::DRIZZLE_TIMESTAMP_DATE)
 
698
  uint64_t value= 0;
 
699
  int error;
 
700
  DRIZZLE_TIME l_time;
 
701
  enum enum_drizzle_timestamp_type ret;
 
702
 
 
703
  ret= str_to_datetime(str->ptr(), str->length(), &l_time,
 
704
                       (TIME_FUZZY_DATE | MODE_INVALID_DATES |
 
705
                        (session->variables.sql_mode & MODE_NO_ZERO_DATE)),
 
706
                       &error);
 
707
 
 
708
  if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
729
709
  {
730
710
    /*
731
711
      Do not return yet, we may still want to throw a "trailing garbage"
732
712
      warning.
733
713
    */
734
714
    *error_arg= false;
735
 
    l_time.convert(value);
 
715
    value= TIME_to_uint64_t_datetime(&l_time);
736
716
  }
737
717
  else
738
718
  {
739
719
    *error_arg= true;
740
 
    error= type::CUT;                                   /* force warning */
 
720
    error= 1;                                   /* force warning */
741
721
  }
742
722
 
743
 
  if (error != type::VALID)
 
723
  if (error > 0)
744
724
  {
745
725
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
746
726
                                 str->ptr(), str->length(),
785
765
 
786
766
enum Arg_comparator::enum_date_cmp_type
787
767
Arg_comparator::can_compare_as_dates(Item *in_a, Item *in_b,
788
 
                                     int64_t *const_value)
 
768
                                     uint64_t *const_value)
789
769
{
790
770
  enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
791
771
  Item *str_arg= 0, *date_arg= 0;
796
776
  if (in_a->is_datetime())
797
777
  {
798
778
    if (in_b->is_datetime())
799
 
    {
800
779
      cmp_type= CMP_DATE_WITH_DATE;
801
 
    }
802
780
    else if (in_b->result_type() == STRING_RESULT)
803
781
    {
804
782
      cmp_type= CMP_DATE_WITH_STR;
840
818
       * Does a uint64_t conversion really have to happen here?  Fields return int64_t
841
819
       * from val_int(), not uint64_t...
842
820
       */
843
 
      int64_t value;
 
821
      uint64_t value;
844
822
      String *str_val;
845
823
      String tmp;
846
824
      /* DateTime used to pick up as many string conversion possibilities as possible. */
866
844
      }
867
845
 
868
846
      /* String conversion was good.  Convert to an integer for comparison purposes. */
869
 
      temporal.to_int64_t(&value);
 
847
      int64_t int_value;
 
848
      temporal.to_int64_t(&int_value);
 
849
      value= (uint64_t) int_value;
870
850
 
871
851
      if (const_value)
872
852
        *const_value= value;
880
860
                                        Item **a1, Item **a2,
881
861
                                        Item_result type)
882
862
{
883
 
  enum_date_cmp_type cmp_type;
884
 
  int64_t const_value= -1;
 
863
  enum enum_date_cmp_type cmp_type;
 
864
  uint64_t const_value= (uint64_t)-1;
885
865
  a= a1;
886
866
  b= a2;
887
867
 
888
868
  if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
889
869
  {
 
870
    session= current_session;
890
871
    owner= owner_arg;
891
872
    a_type= (*a)->field_type();
892
873
    b_type= (*b)->field_type();
893
874
    a_cache= 0;
894
875
    b_cache= 0;
895
876
 
896
 
    if (const_value != -1)
 
877
    if (const_value != (uint64_t)-1)
897
878
    {
898
879
      Item_cache_int *cache= new Item_cache_int();
899
880
      /* Mark the cache as non-const to prevent re-caching. */
914
895
    is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC);
915
896
    func= &Arg_comparator::compare_datetime;
916
897
    get_value_func= &get_datetime_value;
917
 
 
918
898
    return 0;
919
899
  }
920
900
 
924
904
 
925
905
void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
926
906
{
 
907
  session= current_session;
927
908
  /* A caller will handle null values by itself. */
928
909
  owner= NULL;
929
910
  a= a1;
967
948
    obtained value
968
949
*/
969
950
 
970
 
int64_t
 
951
uint64_t
971
952
get_datetime_value(Session *session, Item ***item_arg, Item **cache_arg,
972
953
                   Item *warn_item, bool *is_null)
973
954
{
974
 
  int64_t value= 0;
 
955
  uint64_t value= 0;
975
956
  String buf, *str= 0;
976
957
  Item *item= **item_arg;
977
958
 
995
976
    str= item->val_str(&buf);
996
977
    *is_null= item->null_value;
997
978
  }
998
 
 
999
979
  if (*is_null)
1000
980
    return ~(uint64_t) 0;
1001
 
 
1002
981
  /*
1003
982
    Convert strings to the integer DATE/DATETIME representation.
1004
983
    Even if both dates provided in strings we can't compare them directly as
1009
988
  {
1010
989
    bool error;
1011
990
    enum_field_types f_type= warn_item->field_type();
1012
 
    type::timestamp_t t_type= f_type == DRIZZLE_TYPE_DATE ? type::DRIZZLE_TIMESTAMP_DATE : type::DRIZZLE_TIMESTAMP_DATETIME;
 
991
    enum enum_drizzle_timestamp_type t_type= f_type ==
 
992
      DRIZZLE_TYPE_DATE ? DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME;
1013
993
    value= get_date_from_str(session, str, t_type, warn_item->name, &error);
1014
994
    /*
1015
995
      If str did not contain a valid date according to the current
1032
1012
    *cache_arg= cache;
1033
1013
    *item_arg= cache_arg;
1034
1014
  }
1035
 
 
1036
1015
  return value;
1037
1016
}
1038
1017
 
1189
1168
 
1190
1169
int Arg_comparator::compare_decimal()
1191
1170
{
1192
 
  type::Decimal value1;
1193
 
  type::Decimal *val1= (*a)->val_decimal(&value1);
 
1171
  my_decimal value1;
 
1172
  my_decimal *val1= (*a)->val_decimal(&value1);
1194
1173
  if (!(*a)->null_value)
1195
1174
  {
1196
 
    type::Decimal value2;
1197
 
    type::Decimal *val2= (*b)->val_decimal(&value2);
 
1175
    my_decimal value2;
 
1176
    my_decimal *val2= (*b)->val_decimal(&value2);
1198
1177
    if (!(*b)->null_value)
1199
1178
    {
1200
1179
      owner->null_value= 0;
1201
 
      return class_decimal_cmp(val1, val2);
 
1180
      return my_decimal_cmp(val1, val2);
1202
1181
    }
1203
1182
  }
1204
1183
  owner->null_value= 1;
1216
1195
 
1217
1196
int Arg_comparator::compare_e_decimal()
1218
1197
{
1219
 
  type::Decimal value1, value2;
1220
 
  type::Decimal *val1= (*a)->val_decimal(&value1);
1221
 
  type::Decimal *val2= (*b)->val_decimal(&value2);
 
1198
  my_decimal value1, value2;
 
1199
  my_decimal *val1= (*a)->val_decimal(&value1);
 
1200
  my_decimal *val2= (*b)->val_decimal(&value2);
1222
1201
  if ((*a)->null_value || (*b)->null_value)
1223
1202
    return test((*a)->null_value && (*b)->null_value);
1224
 
  return test(class_decimal_cmp(val1, val2) == 0);
 
1203
  return test(my_decimal_cmp(val1, val2) == 0);
1225
1204
}
1226
1205
 
1227
1206
 
1631
1610
 
1632
1611
void Item_in_optimizer::cleanup()
1633
1612
{
1634
 
  item::function::Boolean::cleanup();
 
1613
  Item_bool_func::cleanup();
1635
1614
  if (!save_cache)
1636
1615
    cache= 0;
1637
1616
  return;
1684
1663
    change records at each execution.
1685
1664
  */
1686
1665
  if ((*args) != new_item)
1687
 
    getSession().change_item_tree(args, new_item);
 
1666
    current_session->change_item_tree(args, new_item);
1688
1667
 
1689
1668
  /*
1690
1669
    Transform the right IN operand which should be an Item_in_subselect or a
1839
1818
          {
1840
1819
            range->type= DECIMAL_RESULT;
1841
1820
            range->dec.init();
1842
 
            type::Decimal *dec= el->val_decimal(&range->dec);
 
1821
            my_decimal *dec= el->val_decimal(&range->dec);
1843
1822
            if (dec != &range->dec)
1844
1823
            {
1845
1824
              range->dec= *dec;
1889
1868
{
1890
1869
  assert(fixed == 1);
1891
1870
  double value;
1892
 
  type::Decimal dec_buf, *dec= NULL;
 
1871
  my_decimal dec_buf, *dec= NULL;
1893
1872
  uint32_t i;
1894
1873
 
1895
1874
  if (use_decimal_comparison)
1897
1876
    dec= row->element_index(0)->val_decimal(&dec_buf);
1898
1877
    if (row->element_index(0)->null_value)
1899
1878
      return -1;
1900
 
    class_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
 
1879
    my_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
1901
1880
  }
1902
1881
  else
1903
1882
  {
1922
1901
        and we are comparing against a decimal
1923
1902
      */
1924
1903
      if (dec && range->type == DECIMAL_RESULT)
1925
 
        cmp_result= class_decimal_cmp(&range->dec, dec) <= 0;
 
1904
        cmp_result= my_decimal_cmp(&range->dec, dec) <= 0;
1926
1905
      else
1927
1906
        cmp_result= (range->dbl <= value);
1928
1907
      if (cmp_result)
1932
1911
    }
1933
1912
    interval_range *range= intervals+start;
1934
1913
    return ((dec && range->type == DECIMAL_RESULT) ?
1935
 
            class_decimal_cmp(dec, &range->dec) < 0 :
 
1914
            my_decimal_cmp(dec, &range->dec) < 0 :
1936
1915
            value < range->dbl) ? 0 : start + 1;
1937
1916
  }
1938
1917
 
1943
1922
        ((el->result_type() == DECIMAL_RESULT) ||
1944
1923
         (el->result_type() == INT_RESULT)))
1945
1924
    {
1946
 
      type::Decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
 
1925
      my_decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
1947
1926
      /* Skip NULL ranges. */
1948
1927
      if (el->null_value)
1949
1928
        continue;
1950
 
      if (class_decimal_cmp(e_dec, dec) > 0)
 
1929
      if (my_decimal_cmp(e_dec, dec) > 0)
1951
1930
        return i - 1;
1952
1931
    }
1953
1932
    else
1997
1976
  if (Item_func_opt_neg::fix_fields(session, ref))
1998
1977
    return 1;
1999
1978
 
2000
 
  session->getLex()->current_select->between_count++;
 
1979
  session->lex->current_select->between_count++;
2001
1980
 
2002
1981
  /* not_null_tables_cache == union(T1(e),T1(e1),T1(e2)) */
2003
1982
  if (pred_level && !negated)
2018
1997
  int i;
2019
1998
  bool datetime_found= false;
2020
1999
  compare_as_dates= true;
 
2000
  Session *session= current_session;
2021
2001
 
2022
2002
  /*
2023
2003
    As some compare functions are generated after sql_yacc,
2064
2044
        The following can't be recoded with || as convert_constant_item
2065
2045
        changes the argument
2066
2046
      */
2067
 
      if (convert_constant_item(&getSession(), field_item, &args[1]))
 
2047
      if (convert_constant_item(session, field_item, &args[1]))
2068
2048
        cmp_type=INT_RESULT;                    // Works for all types.
2069
 
      if (convert_constant_item(&getSession(), field_item, &args[2]))
 
2049
      if (convert_constant_item(session, field_item, &args[2]))
2070
2050
        cmp_type=INT_RESULT;                    // Works for all types.
2071
2051
    }
2072
2052
  }
2143
2123
  }
2144
2124
  else if (cmp_type == DECIMAL_RESULT)
2145
2125
  {
2146
 
    type::Decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
 
2126
    my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2147
2127
               a_buf, *a_dec, b_buf, *b_dec;
2148
2128
    if ((null_value=args[0]->null_value))
2149
2129
      return 0;
2150
2130
    a_dec= args[1]->val_decimal(&a_buf);
2151
2131
    b_dec= args[2]->val_decimal(&b_buf);
2152
2132
    if (!args[1]->null_value && !args[2]->null_value)
2153
 
      return (int64_t) ((class_decimal_cmp(dec, a_dec) >= 0 &&
2154
 
                          class_decimal_cmp(dec, b_dec) <= 0) != negated);
 
2133
      return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
 
2134
                          my_decimal_cmp(dec, b_dec) <= 0) != negated);
2155
2135
    if (args[1]->null_value && args[2]->null_value)
2156
2136
      null_value=1;
2157
2137
    else if (args[1]->null_value)
2158
 
      null_value= (class_decimal_cmp(dec, b_dec) <= 0);
 
2138
      null_value= (my_decimal_cmp(dec, b_dec) <= 0);
2159
2139
    else
2160
 
      null_value= (class_decimal_cmp(dec, a_dec) >= 0);
 
2140
      null_value= (my_decimal_cmp(dec, a_dec) >= 0);
2161
2141
  }
2162
2142
  else
2163
2143
  {
2215
2195
    max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2216
2196
  }
2217
2197
  else
2218
 
  {
2219
2198
    max_length= max(args[0]->max_length, args[1]->max_length);
2220
 
  }
2221
2199
 
2222
2200
  switch (hybrid_type)
2223
2201
  {
2224
2202
  case STRING_RESULT:
2225
2203
    agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2226
2204
    break;
2227
 
 
2228
2205
  case DECIMAL_RESULT:
2229
2206
  case REAL_RESULT:
2230
2207
    break;
2231
 
 
2232
2208
  case INT_RESULT:
2233
2209
    decimals= 0;
2234
2210
    break;
2235
 
 
2236
2211
  case ROW_RESULT:
 
2212
  default:
2237
2213
    assert(0);
2238
2214
  }
2239
 
 
2240
2215
  cached_field_type= agg_field_type(args, 2);
2241
2216
}
2242
2217
 
2291
2266
}
2292
2267
 
2293
2268
 
2294
 
type::Decimal *Item_func_ifnull::decimal_op(type::Decimal *decimal_value)
 
2269
my_decimal *Item_func_ifnull::decimal_op(my_decimal *decimal_value)
2295
2270
{
2296
2271
  assert(fixed == 1);
2297
 
  type::Decimal *value= args[0]->val_decimal(decimal_value);
 
2272
  my_decimal *value= args[0]->val_decimal(decimal_value);
2298
2273
  if (!args[0]->null_value)
2299
2274
  {
2300
2275
    null_value= 0;
2464
2439
}
2465
2440
 
2466
2441
 
2467
 
type::Decimal *
2468
 
Item_func_if::val_decimal(type::Decimal *decimal_value)
 
2442
my_decimal *
 
2443
Item_func_if::val_decimal(my_decimal *decimal_value)
2469
2444
{
2470
2445
  assert(fixed == 1);
2471
2446
  Item *arg= args[0]->val_bool() ? args[1] : args[2];
2472
 
  type::Decimal *value= arg->val_decimal(decimal_value);
 
2447
  my_decimal *value= arg->val_decimal(decimal_value);
2473
2448
  null_value= arg->null_value;
2474
2449
  return value;
2475
2450
}
2549
2524
}
2550
2525
 
2551
2526
 
2552
 
type::Decimal *
2553
 
Item_func_nullif::val_decimal(type::Decimal * decimal_value)
 
2527
my_decimal *
 
2528
Item_func_nullif::val_decimal(my_decimal * decimal_value)
2554
2529
{
2555
2530
  assert(fixed == 1);
2556
 
  type::Decimal *res;
 
2531
  my_decimal *res;
2557
2532
  if (!cmp.compare())
2558
2533
  {
2559
2534
    null_value=1;
2686
2661
}
2687
2662
 
2688
2663
 
2689
 
type::Decimal *Item_func_case::val_decimal(type::Decimal *decimal_value)
 
2664
my_decimal *Item_func_case::val_decimal(my_decimal *decimal_value)
2690
2665
{
2691
2666
  assert(fixed == 1);
2692
2667
  char buff[MAX_FIELD_WIDTH];
2693
2668
  String dummy_str(buff, sizeof(buff), default_charset());
2694
2669
  Item *item= find_item(&dummy_str);
2695
 
  type::Decimal *res;
 
2670
  my_decimal *res;
2696
2671
 
2697
2672
  if (!item)
2698
2673
  {
2735
2710
 
2736
2711
void Item_func_case::agg_num_lengths(Item *arg)
2737
2712
{
2738
 
  uint32_t len= class_decimal_length_to_precision(arg->max_length, arg->decimals,
 
2713
  uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2739
2714
                                           arg->unsigned_flag) - arg->decimals;
2740
2715
  set_if_bigger(max_length, len);
2741
2716
  set_if_bigger(decimals, arg->decimals);
2818
2793
      agg_num_lengths(args[i + 1]);
2819
2794
    if (else_expr_num != -1)
2820
2795
      agg_num_lengths(args[else_expr_num]);
2821
 
    max_length= class_decimal_precision_to_length(max_length + decimals, decimals,
 
2796
    max_length= my_decimal_precision_to_length(max_length + decimals, decimals,
2822
2797
                                               unsigned_flag);
2823
2798
  }
2824
2799
}
2925
2900
}
2926
2901
 
2927
2902
 
2928
 
type::Decimal *Item_func_coalesce::decimal_op(type::Decimal *decimal_value)
 
2903
my_decimal *Item_func_coalesce::decimal_op(my_decimal *decimal_value)
2929
2904
{
2930
2905
  assert(fixed == 1);
2931
2906
  null_value= 0;
2932
2907
  for (uint32_t i= 0; i < arg_count; i++)
2933
2908
  {
2934
 
    type::Decimal *res= args[i]->val_decimal(decimal_value);
 
2909
    my_decimal *res= args[i]->val_decimal(decimal_value);
2935
2910
    if (!args[i]->null_value)
2936
2911
      return res;
2937
2912
  }
2944
2919
{
2945
2920
  cached_field_type= agg_field_type(args, arg_count);
2946
2921
  agg_result_type(&hybrid_type, args, arg_count);
2947
 
 
2948
2922
  switch (hybrid_type) {
2949
2923
  case STRING_RESULT:
2950
2924
    count_only_length();
2951
2925
    decimals= NOT_FIXED_DEC;
2952
2926
    agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1);
2953
2927
    break;
2954
 
 
2955
2928
  case DECIMAL_RESULT:
2956
2929
    count_decimal_length();
2957
2930
    break;
2958
 
 
2959
2931
  case REAL_RESULT:
2960
2932
    count_real_length();
2961
2933
    break;
2962
 
 
2963
2934
  case INT_RESULT:
2964
2935
    count_only_length();
2965
2936
    decimals= 0;
2966
2937
    break;
2967
 
 
2968
2938
  case ROW_RESULT:
 
2939
  default:
2969
2940
    assert(0);
2970
2941
  }
2971
2942
}
3080
3051
}
3081
3052
 
3082
3053
 
3083
 
static int cmp_decimal(void *, type::Decimal *a, type::Decimal *b)
 
3054
static int cmp_decimal(void *, my_decimal *a, my_decimal *b)
3084
3055
{
3085
3056
  /*
3086
3057
    We need call of fixing buffer pointer, because fast sort just copy
3088
3059
  */
3089
3060
  a->fix_buffer_pointer();
3090
3061
  b->fix_buffer_pointer();
3091
 
  return class_decimal_cmp(a, b);
 
3062
  return my_decimal_cmp(a, b);
3092
3063
}
3093
3064
 
3094
3065
 
3196
3167
  return;
3197
3168
}
3198
3169
 
3199
 
in_int64_t::in_int64_t(uint32_t elements) :
3200
 
  in_vector(elements, sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
 
3170
in_int64_t::in_int64_t(uint32_t elements)
 
3171
  :in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3201
3172
{}
3202
3173
 
3203
3174
void in_int64_t::set(uint32_t pos,Item *item)
3217
3188
  return (unsigned char*) &tmp;
3218
3189
}
3219
3190
 
3220
 
in_datetime::in_datetime(Item *warn_item_arg, uint32_t elements) :
3221
 
  in_int64_t(elements),
3222
 
  session(current_session),
3223
 
  warn_item(warn_item_arg),
3224
 
  lval_cache(0)
3225
 
{}
3226
 
 
3227
 
void in_datetime::set(uint32_t pos, Item *item)
 
3191
void in_datetime::set(uint32_t pos,Item *item)
3228
3192
{
3229
3193
  Item **tmp_item= &item;
3230
3194
  bool is_null;
3264
3228
 
3265
3229
 
3266
3230
in_decimal::in_decimal(uint32_t elements)
3267
 
  :in_vector(elements, sizeof(type::Decimal),(qsort2_cmp) cmp_decimal, 0)
 
3231
  :in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3268
3232
{}
3269
3233
 
3270
3234
 
3271
3235
void in_decimal::set(uint32_t pos, Item *item)
3272
3236
{
3273
 
  /* as far as 'item' is constant, we can store reference on type::Decimal */
3274
 
  type::Decimal *dec= ((type::Decimal *)base) + pos;
 
3237
  /* as far as 'item' is constant, we can store reference on my_decimal */
 
3238
  my_decimal *dec= ((my_decimal *)base) + pos;
3275
3239
  dec->len= DECIMAL_BUFF_LENGTH;
3276
3240
  dec->fix_buffer_pointer();
3277
 
  type::Decimal *res= item->val_decimal(dec);
 
3241
  my_decimal *res= item->val_decimal(dec);
3278
3242
  /* if item->val_decimal() is evaluated to NULL then res == 0 */
3279
3243
  if (!item->null_value && res != dec)
3280
 
    class_decimal2decimal(res, dec);
 
3244
    my_decimal2decimal(res, dec);
3281
3245
}
3282
3246
 
3283
3247
 
3284
3248
unsigned char *in_decimal::get_value(Item *item)
3285
3249
{
3286
 
  type::Decimal *result= item->val_decimal(&val);
 
3250
  my_decimal *result= item->val_decimal(&val);
3287
3251
  if (item->null_value)
3288
3252
    return 0;
3289
3253
  return (unsigned char *)result;
3296
3260
  switch (type) {
3297
3261
  case STRING_RESULT:
3298
3262
    return new cmp_item_sort_string(cs);
3299
 
 
3300
3263
  case INT_RESULT:
3301
3264
    return new cmp_item_int;
3302
 
 
3303
3265
  case REAL_RESULT:
3304
3266
    return new cmp_item_real;
3305
 
 
3306
3267
  case ROW_RESULT:
3307
3268
    return new cmp_item_row;
3308
 
 
3309
3269
  case DECIMAL_RESULT:
3310
3270
    return new cmp_item_decimal;
 
3271
  default:
 
3272
    assert(0);
 
3273
    break;
3311
3274
  }
3312
 
 
3313
3275
  return 0; // to satisfy compiler :)
3314
3276
}
3315
3277
 
3442
3404
 
3443
3405
void cmp_item_decimal::store_value(Item *item)
3444
3406
{
3445
 
  type::Decimal *val= item->val_decimal(&value);
 
3407
  my_decimal *val= item->val_decimal(&value);
3446
3408
  /* val may be zero if item is nnull */
3447
3409
  if (val && val != &value)
3448
 
    class_decimal2decimal(val, &value);
 
3410
    my_decimal2decimal(val, &value);
3449
3411
}
3450
3412
 
3451
3413
 
3452
3414
int cmp_item_decimal::cmp(Item *arg)
3453
3415
{
3454
 
  type::Decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
 
3416
  my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3455
3417
  if (arg->null_value)
3456
3418
    return 1;
3457
 
  return class_decimal_cmp(&value, tmp);
 
3419
  return my_decimal_cmp(&value, tmp);
3458
3420
}
3459
3421
 
3460
3422
 
3461
3423
int cmp_item_decimal::compare(cmp_item *arg)
3462
3424
{
3463
3425
  cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3464
 
  return class_decimal_cmp(&value, &l_cmp->value);
 
3426
  return my_decimal_cmp(&value, &l_cmp->value);
3465
3427
}
3466
3428
 
3467
3429
 
3574
3536
{
3575
3537
  Item **arg, **arg_end;
3576
3538
  bool const_itm= 1;
 
3539
  Session *session= current_session;
3577
3540
  bool datetime_found= false;
3578
3541
  /* true <=> arguments values will be compared as DATETIMEs. */
3579
3542
  bool compare_as_datetime= false;
3700
3663
  if (type_cnt == 1 && const_itm && !nulls_in_row())
3701
3664
  {
3702
3665
    if (compare_as_datetime)
3703
 
    {
3704
3666
      array= new in_datetime(date_arg, arg_count - 1);
3705
 
    }
3706
3667
    else
3707
3668
    {
3708
3669
      /*
3721
3682
          bool all_converted= true;
3722
3683
          for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3723
3684
          {
3724
 
            if (!convert_constant_item (&getSession(), field_item, &arg[0]))
 
3685
            if (!convert_constant_item (session, field_item, &arg[0]))
3725
3686
              all_converted= false;
3726
3687
          }
3727
3688
          if (all_converted)
3728
3689
            cmp_type= INT_RESULT;
3729
3690
        }
3730
3691
      }
3731
 
 
3732
3692
      switch (cmp_type) {
3733
3693
      case STRING_RESULT:
3734
3694
        array=new in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
3735
3695
                            cmp_collation.collation);
3736
3696
        break;
3737
 
 
3738
3697
      case INT_RESULT:
3739
3698
        array= new in_int64_t(arg_count-1);
3740
3699
        break;
3741
 
 
3742
3700
      case REAL_RESULT:
3743
3701
        array= new in_double(arg_count-1);
3744
3702
        break;
3745
 
 
3746
3703
      case ROW_RESULT:
3747
3704
        /*
3748
3705
          The row comparator was created at the beginning but only DATETIME
3751
3708
        */
3752
3709
        ((in_row*)array)->tmp.store_value(args[0]);
3753
3710
        break;
3754
 
 
3755
3711
      case DECIMAL_RESULT:
3756
3712
        array= new in_decimal(arg_count - 1);
3757
3713
        break;
 
3714
      default:
 
3715
        assert(0);
 
3716
        return;
3758
3717
      }
3759
3718
    }
3760
 
 
3761
 
    if (array && !(getSession().is_fatal_error))                // If not EOM
 
3719
    if (array && !(session->is_fatal_error))            // If not EOM
3762
3720
    {
3763
3721
      uint32_t j=0;
3764
3722
      for (uint32_t arg_num=1 ; arg_num < arg_count ; arg_num++)
3765
3723
      {
3766
 
        if (!args[arg_num]->null_value)                 // Skip NULL values
 
3724
        if (!args[arg_num]->null_value)                 // Skip NULL values
3767
3725
        {
3768
3726
          array->set(j,args[arg_num]);
3769
 
          j++;
 
3727
          j++;
3770
3728
        }
3771
 
        else
3772
 
          have_null= 1;
 
3729
        else
 
3730
          have_null= 1;
3773
3731
      }
3774
3732
      if ((array->used_count= j))
3775
 
        array->sort();
 
3733
        array->sort();
3776
3734
    }
3777
3735
  }
3778
3736
  else
3874
3832
 
3875
3833
 
3876
3834
Item_cond::Item_cond(Session *session, Item_cond *item)
3877
 
  :item::function::Boolean(session, item),
 
3835
  :Item_bool_func(session, item),
3878
3836
   abort_on_null(item->abort_on_null),
3879
3837
   and_tables_cache(item->and_tables_cache)
3880
3838
{
3886
3844
 
3887
3845
void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
3888
3846
{
3889
 
  List<Item>::iterator li(item->list.begin());
 
3847
  List_iterator_fast<Item> li(item->list);
3890
3848
  while (Item *it= li++)
3891
3849
    list.push_back(it->copy_andor_structure(session));
3892
3850
}
3896
3854
Item_cond::fix_fields(Session *session, Item **)
3897
3855
{
3898
3856
  assert(fixed == 0);
3899
 
  List<Item>::iterator li(list.begin());
 
3857
  List_iterator<Item> li(list);
3900
3858
  Item *item;
3901
3859
  void *orig_session_marker= session->session_marker;
3902
3860
  unsigned char buff[sizeof(char*)];                    // Max local vars in function
3903
3861
  not_null_tables_cache= used_tables_cache= 0;
3904
 
  const_item_cache= true;
 
3862
  const_item_cache= 1;
3905
3863
 
3906
3864
  if (functype() == COND_OR_FUNC)
3907
3865
    session->session_marker= 0;
3936
3894
           !((Item_cond*) item)->list.is_empty())
3937
3895
    {                                           // Identical function
3938
3896
      li.replace(((Item_cond*) item)->list);
3939
 
      ((Item_cond*) item)->list.clear();
 
3897
      ((Item_cond*) item)->list.empty();
3940
3898
      item= *li.ref();                          // new current item
3941
3899
    }
3942
3900
    if (abort_on_null)
3962
3920
    if (item->maybe_null)
3963
3921
      maybe_null=1;
3964
3922
  }
3965
 
  session->getLex()->current_select->cond_count+= list.elements;
 
3923
  session->lex->current_select->cond_count+= list.elements;
3966
3924
  session->session_marker= orig_session_marker;
3967
3925
  fix_length_and_dec();
3968
3926
  fixed= 1;
3972
3930
 
3973
3931
void Item_cond::fix_after_pullout(Select_Lex *new_parent, Item **)
3974
3932
{
3975
 
  List<Item>::iterator li(list.begin());
 
3933
  List_iterator<Item> li(list);
3976
3934
  Item *item;
3977
3935
 
3978
3936
  used_tables_cache=0;
3979
 
  const_item_cache= true;
 
3937
  const_item_cache=1;
3980
3938
 
3981
3939
  and_tables_cache= ~(table_map) 0; // Here and below we do as fix_fields does
3982
3940
  not_null_tables_cache= 0;
4004
3962
 
4005
3963
bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
4006
3964
{
4007
 
  List<Item>::iterator li(list.begin());
 
3965
  List_iterator_fast<Item> li(list);
4008
3966
  Item *item;
4009
3967
  while ((item= li++))
4010
3968
    if (item->walk(processor, walk_subquery, arg))
4033
3991
 
4034
3992
Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
4035
3993
{
4036
 
  List<Item>::iterator li(list.begin());
 
3994
  List_iterator<Item> li(list);
4037
3995
  Item *item;
4038
3996
  while ((item= li++))
4039
3997
  {
4048
4006
      change records at each execution.
4049
4007
    */
4050
4008
    if (new_item != item)
4051
 
      getSession().change_item_tree(li.ref(), new_item);
 
4009
      current_session->change_item_tree(li.ref(), new_item);
4052
4010
  }
4053
4011
  return Item_func::transform(transformer, arg);
4054
4012
}
4084
4042
  if (!(this->*analyzer)(arg_p))
4085
4043
    return 0;
4086
4044
 
4087
 
  List<Item>::iterator li(list.begin());
 
4045
  List_iterator<Item> li(list);
4088
4046
  Item *item;
4089
4047
  while ((item= li++))
4090
4048
  {
4103
4061
void Item_cond::traverse_cond(Cond_traverser traverser,
4104
4062
                              void *arg, traverse_order order)
4105
4063
{
4106
 
  List<Item>::iterator li(list.begin());
 
4064
  List_iterator<Item> li(list);
4107
4065
  Item *item;
4108
4066
 
4109
4067
  switch (order) {
4144
4102
void Item_cond::split_sum_func(Session *session, Item **ref_pointer_array,
4145
4103
                               List<Item> &fields)
4146
4104
{
4147
 
  List<Item>::iterator li(list.begin());
 
4105
  List_iterator<Item> li(list);
4148
4106
  Item *item;
4149
4107
  while ((item= li++))
4150
4108
    item->split_sum_func(session, ref_pointer_array,
4161
4119
 
4162
4120
void Item_cond::update_used_tables()
4163
4121
{
4164
 
  List<Item>::iterator li(list.begin());
 
4122
  List_iterator_fast<Item> li(list);
4165
4123
  Item *item;
4166
4124
 
4167
4125
  used_tables_cache=0;
4168
 
  const_item_cache= true;
 
4126
  const_item_cache=1;
4169
4127
  while ((item=li++))
4170
4128
  {
4171
4129
    item->update_used_tables();
4178
4136
void Item_cond::print(String *str, enum_query_type query_type)
4179
4137
{
4180
4138
  str->append('(');
4181
 
  List<Item>::iterator li(list.begin());
 
4139
  List_iterator_fast<Item> li(list);
4182
4140
  Item *item;
4183
4141
  if ((item=li++))
4184
4142
    item->print(str, query_type);
4195
4153
 
4196
4154
void Item_cond::neg_arguments(Session *session)
4197
4155
{
4198
 
  List<Item>::iterator li(list.begin());
 
4156
  List_iterator<Item> li(list);
4199
4157
  Item *item;
4200
4158
  while ((item= li++))          /* Apply not transformation to the arguments */
4201
4159
  {
4233
4191
int64_t Item_cond_and::val_int()
4234
4192
{
4235
4193
  assert(fixed == 1);
4236
 
  List<Item>::iterator li(list.begin());
 
4194
  List_iterator_fast<Item> li(list);
4237
4195
  Item *item;
4238
4196
  null_value= 0;
4239
4197
  while ((item=li++))
4251
4209
int64_t Item_cond_or::val_int()
4252
4210
{
4253
4211
  assert(fixed == 1);
4254
 
  List<Item>::iterator li(list.begin());
 
4212
  List_iterator_fast<Item> li(list);
4255
4213
  Item *item;
4256
4214
  null_value=0;
4257
4215
  while ((item=li++))
4488
4446
      {
4489
4447
        pattern     = first + 1;
4490
4448
        pattern_len = (int) len - 2;
4491
 
        int *suff = (int*) session->getMemRoot()->allocate((int) (sizeof(int)*
4492
 
                                                                  ((pattern_len + 1)*2+
4493
 
                                                                   alphabet_size)));
 
4449
        int *suff = (int*) session->alloc((int) (sizeof(int)*
 
4450
                                      ((pattern_len + 1)*2+
 
4451
                                      alphabet_size)));
4494
4452
        bmGs      = suff + pattern_len + 1;
4495
4453
        bmBc      = bmGs + pattern_len + 1;
4496
4454
        turboBM_compute_good_suffix_shifts(suff);
4507
4465
  Item_bool_func2::cleanup();
4508
4466
}
4509
4467
 
4510
 
static unsigned char likeconv(const CHARSET_INFO *cs, unsigned char a)
4511
 
{
4512
4468
#ifdef LIKE_CMP_TOUPPER
4513
 
  return cs->toupper(a);
 
4469
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
4514
4470
#else
4515
 
  return cs->sort_order[a];
 
4471
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
4516
4472
#endif
4517
 
}
 
4473
 
4518
4474
 
4519
4475
/**
4520
4476
  Precomputation dependent only on pattern_len.
4651
4607
 
4652
4608
bool Item_func_like::turboBM_matches(const char* text, int text_len) const
4653
4609
{
4654
 
  int bcShift;
4655
 
  int turboShift;
 
4610
  register int bcShift;
 
4611
  register int turboShift;
4656
4612
  int shift = pattern_len;
4657
4613
  int j     = 0;
4658
4614
  int u     = 0;
4666
4622
  {
4667
4623
    while (j <= tlmpl)
4668
4624
    {
4669
 
      int i= plm1;
 
4625
      register int i= plm1;
4670
4626
      while (i >= 0 && pattern[i] == text[i + j])
4671
4627
      {
4672
4628
        i--;
4676
4632
      if (i < 0)
4677
4633
        return 1;
4678
4634
 
4679
 
      const int v = plm1 - i;
 
4635
      register const int v = plm1 - i;
4680
4636
      turboShift = u - v;
4681
4637
      bcShift    = bmBc[(uint32_t) (unsigned char) text[i + j]] - plm1 + i;
4682
4638
      shift      = (turboShift > bcShift) ? turboShift : bcShift;
4697
4653
  {
4698
4654
    while (j <= tlmpl)
4699
4655
    {
4700
 
      int i = plm1;
 
4656
      register int i = plm1;
4701
4657
      while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
4702
4658
      {
4703
4659
        i--;
4708
4664
      if (i < 0)
4709
4665
        return 1;
4710
4666
 
4711
 
      const int v= plm1 - i;
 
4667
      register const int v= plm1 - i;
4712
4668
      turboShift= u - v;
4713
4669
      bcShift= bmBc[(uint32_t) likeconv(cs, text[i + j])] - plm1 + i;
4714
4670
      shift= (turboShift > bcShift) ? turboShift : bcShift;
4749
4705
int64_t Item_cond_xor::val_int()
4750
4706
{
4751
4707
  assert(fixed == 1);
4752
 
  List<Item>::iterator li(list.begin());
 
4708
  List_iterator<Item> li(list);
4753
4709
  Item *item;
4754
4710
  int result=0;
4755
4711
  null_value=0;
4909
4865
}
4910
4866
 
4911
4867
Item_equal::Item_equal(Item_field *f1, Item_field *f2)
4912
 
  : item::function::Boolean(), const_item(0), eval_item(0), cond_false(0)
 
4868
  : Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
4913
4869
{
4914
 
  const_item_cache= false;
 
4870
  const_item_cache= 0;
4915
4871
  fields.push_back(f1);
4916
4872
  fields.push_back(f2);
4917
4873
}
4918
4874
 
4919
4875
Item_equal::Item_equal(Item *c, Item_field *f)
4920
 
  : item::function::Boolean(), eval_item(0), cond_false(0)
 
4876
  : Item_bool_func(), eval_item(0), cond_false(0)
4921
4877
{
4922
 
  const_item_cache= false;
 
4878
  const_item_cache= 0;
4923
4879
  fields.push_back(f);
4924
4880
  const_item= c;
4925
4881
}
4926
4882
 
4927
4883
 
4928
4884
Item_equal::Item_equal(Item_equal *item_equal)
4929
 
  : item::function::Boolean(), eval_item(0), cond_false(0)
 
4885
  : Item_bool_func(), eval_item(0), cond_false(0)
4930
4886
{
4931
 
  const_item_cache= false;
4932
 
  List<Item_field>::iterator li(item_equal->fields.begin());
 
4887
  const_item_cache= 0;
 
4888
  List_iterator_fast<Item_field> li(item_equal->fields);
4933
4889
  Item_field *item;
4934
4890
  while ((item= li++))
4935
4891
  {
4952
4908
  func->set_cmp_func();
4953
4909
  func->quick_fix_field();
4954
4910
  if ((cond_false= !func->val_int()))
4955
 
    const_item_cache= true;
 
4911
    const_item_cache= 1;
4956
4912
}
4957
4913
 
4958
4914
void Item_equal::add(Item_field *f)
4981
4937
 
4982
4938
bool Item_equal::contains(Field *field)
4983
4939
{
4984
 
  List<Item_field>::iterator it(fields.begin());
 
4940
  List_iterator_fast<Item_field> it(fields);
4985
4941
  Item_field *item;
4986
4942
  while ((item= it++))
4987
4943
  {
5040
4996
void Item_equal::sort(Item_field_cmpfunc cmp, void *arg)
5041
4997
{
5042
4998
  bool swap;
5043
 
  List<Item_field>::iterator it(fields.begin());
 
4999
  List_iterator<Item_field> it(fields);
5044
5000
  do
5045
5001
  {
5046
5002
    Item_field *item1= it++;
5064
5020
        ref1= ref2;
5065
5021
      }
5066
5022
    }
5067
 
    it= fields.begin();
 
5023
    it.rewind();
5068
5024
  } while (swap);
5069
5025
}
5070
5026
 
5081
5037
 
5082
5038
void Item_equal::update_const()
5083
5039
{
5084
 
  List<Item_field>::iterator it(fields.begin());
 
5040
  List_iterator<Item_field> it(fields);
5085
5041
  Item *item;
5086
5042
  while ((item= it++))
5087
5043
  {
5095
5051
 
5096
5052
bool Item_equal::fix_fields(Session *, Item **)
5097
5053
{
5098
 
  List<Item_field>::iterator li(fields.begin());
 
5054
  List_iterator_fast<Item_field> li(fields);
5099
5055
  Item *item;
5100
5056
  not_null_tables_cache= used_tables_cache= 0;
5101
 
  const_item_cache= false;
 
5057
  const_item_cache= 0;
5102
5058
  while ((item= li++))
5103
5059
  {
5104
5060
    table_map tmp_table_map;
5115
5071
 
5116
5072
void Item_equal::update_used_tables()
5117
5073
{
5118
 
  List<Item_field>::iterator li(fields.begin());
 
5074
  List_iterator_fast<Item_field> li(fields);
5119
5075
  Item *item;
5120
5076
  not_null_tables_cache= used_tables_cache= 0;
5121
5077
  if ((const_item_cache= cond_false))
5133
5089
  Item_field *item_field;
5134
5090
  if (cond_false)
5135
5091
    return 0;
5136
 
  List<Item_field>::iterator it(fields.begin());
 
5092
  List_iterator_fast<Item_field> it(fields);
5137
5093
  Item *item= const_item ? const_item : it++;
 
5094
  if ((null_value= item->null_value))
 
5095
    return 0;
5138
5096
  eval_item->store_value(item);
5139
 
  if ((null_value= item->null_value))
5140
 
    return 0;
5141
5097
  while ((item_field= it++))
5142
5098
  {
5143
5099
    /* Skip fields of non-const tables. They haven't been read yet */
5144
5100
    if (item_field->field->getTable()->const_table)
5145
5101
    {
5146
 
      if (eval_item->cmp(item_field) || (null_value= item_field->null_value))
 
5102
      if ((null_value= item_field->null_value) || eval_item->cmp(item_field))
5147
5103
        return 0;
5148
5104
    }
5149
5105
  }
5159
5115
 
5160
5116
bool Item_equal::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
5161
5117
{
5162
 
  List<Item_field>::iterator it(fields.begin());
 
5118
  List_iterator_fast<Item_field> it(fields);
5163
5119
  Item *item;
5164
5120
  while ((item= it++))
5165
5121
  {
5171
5127
 
5172
5128
Item *Item_equal::transform(Item_transformer transformer, unsigned char *arg)
5173
5129
{
5174
 
  List<Item_field>::iterator it(fields.begin());
 
5130
  List_iterator<Item_field> it(fields);
5175
5131
  Item *item;
5176
5132
  while ((item= it++))
5177
5133
  {
5186
5142
      change records at each execution.
5187
5143
    */
5188
5144
    if (new_item != item)
5189
 
      getSession().change_item_tree((Item **) it.ref(), new_item);
 
5145
      current_session->change_item_tree((Item **) it.ref(), new_item);
5190
5146
  }
5191
5147
  return Item_func::transform(transformer, arg);
5192
5148
}
5195
5151
{
5196
5152
  str->append(func_name());
5197
5153
  str->append('(');
5198
 
  List<Item_field>::iterator it(fields.begin());
 
5154
  List_iterator_fast<Item_field> it(fields);
5199
5155
  Item *item;
5200
5156
  if (const_item)
5201
5157
    const_item->print(str, query_type);
5213
5169
  str->append(')');
5214
5170
}
5215
5171
 
5216
 
cmp_item_datetime::cmp_item_datetime(Item *warn_item_arg) :
5217
 
  session(current_session),
5218
 
  warn_item(warn_item_arg),
5219
 
  lval_cache(0)
5220
 
{}
5221
 
 
5222
5172
} /* namespace drizzled */