~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/cmpfunc.cc

  • Committer: Mark Atwood
  • Date: 2010-06-24 03:15:21 UTC
  • mto: (1637.2.4 build)
  • mto: This revision was merged to the branch mainline in revision 1639.
  • Revision ID: me@mark.atwood.name-20100624031521-gafmppfbf5afm68w
new syslog module, with plugins for query log, error message, and SYSLOG() function

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/**
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(),
784
764
*/
785
765
 
786
766
enum Arg_comparator::enum_date_cmp_type
787
 
Arg_comparator::can_compare_as_dates(Item *in_a, Item *in_b,
788
 
                                     int64_t *const_value)
 
767
Arg_comparator::can_compare_as_dates(Item *a, Item *b, uint64_t *const_value)
789
768
{
790
769
  enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
791
770
  Item *str_arg= 0, *date_arg= 0;
792
771
 
793
 
  if (in_a->type() == Item::ROW_ITEM || in_b->type() == Item::ROW_ITEM)
 
772
  if (a->type() == Item::ROW_ITEM || b->type() == Item::ROW_ITEM)
794
773
    return CMP_DATE_DFLT;
795
774
 
796
 
  if (in_a->is_datetime())
 
775
  if (a->is_datetime())
797
776
  {
798
 
    if (in_b->is_datetime())
799
 
    {
 
777
    if (b->is_datetime())
800
778
      cmp_type= CMP_DATE_WITH_DATE;
801
 
    }
802
 
    else if (in_b->result_type() == STRING_RESULT)
 
779
    else if (b->result_type() == STRING_RESULT)
803
780
    {
804
781
      cmp_type= CMP_DATE_WITH_STR;
805
 
      date_arg= in_a;
806
 
      str_arg= in_b;
 
782
      date_arg= a;
 
783
      str_arg= b;
807
784
    }
808
785
  }
809
 
  else if (in_b->is_datetime() && in_a->result_type() == STRING_RESULT)
 
786
  else if (b->is_datetime() && a->result_type() == STRING_RESULT)
810
787
  {
811
788
    cmp_type= CMP_STR_WITH_DATE;
812
 
    date_arg= in_b;
813
 
    str_arg= in_a;
 
789
    date_arg= b;
 
790
    str_arg= a;
814
791
  }
815
792
 
816
793
  if (cmp_type != CMP_DATE_DFLT)
840
817
       * Does a uint64_t conversion really have to happen here?  Fields return int64_t
841
818
       * from val_int(), not uint64_t...
842
819
       */
843
 
      int64_t value;
 
820
      uint64_t value;
844
821
      String *str_val;
845
822
      String tmp;
846
823
      /* DateTime used to pick up as many string conversion possibilities as possible. */
866
843
      }
867
844
 
868
845
      /* String conversion was good.  Convert to an integer for comparison purposes. */
869
 
      temporal.to_int64_t(&value);
 
846
      int64_t int_value;
 
847
      temporal.to_int64_t(&int_value);
 
848
      value= (uint64_t) int_value;
870
849
 
871
850
      if (const_value)
872
851
        *const_value= value;
880
859
                                        Item **a1, Item **a2,
881
860
                                        Item_result type)
882
861
{
883
 
  enum_date_cmp_type cmp_type;
884
 
  int64_t const_value= -1;
 
862
  enum enum_date_cmp_type cmp_type;
 
863
  uint64_t const_value= (uint64_t)-1;
885
864
  a= a1;
886
865
  b= a2;
887
866
 
888
867
  if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
889
868
  {
 
869
    session= current_session;
890
870
    owner= owner_arg;
891
871
    a_type= (*a)->field_type();
892
872
    b_type= (*b)->field_type();
893
873
    a_cache= 0;
894
874
    b_cache= 0;
895
875
 
896
 
    if (const_value != -1)
 
876
    if (const_value != (uint64_t)-1)
897
877
    {
898
878
      Item_cache_int *cache= new Item_cache_int();
899
879
      /* Mark the cache as non-const to prevent re-caching. */
914
894
    is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC);
915
895
    func= &Arg_comparator::compare_datetime;
916
896
    get_value_func= &get_datetime_value;
917
 
 
918
897
    return 0;
919
898
  }
920
899
 
924
903
 
925
904
void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
926
905
{
 
906
  session= current_session;
927
907
  /* A caller will handle null values by itself. */
928
908
  owner= NULL;
929
909
  a= a1;
967
947
    obtained value
968
948
*/
969
949
 
970
 
int64_t
 
950
uint64_t
971
951
get_datetime_value(Session *session, Item ***item_arg, Item **cache_arg,
972
952
                   Item *warn_item, bool *is_null)
973
953
{
974
 
  int64_t value= 0;
 
954
  uint64_t value= 0;
975
955
  String buf, *str= 0;
976
956
  Item *item= **item_arg;
977
957
 
995
975
    str= item->val_str(&buf);
996
976
    *is_null= item->null_value;
997
977
  }
998
 
 
999
978
  if (*is_null)
1000
979
    return ~(uint64_t) 0;
1001
 
 
1002
980
  /*
1003
981
    Convert strings to the integer DATE/DATETIME representation.
1004
982
    Even if both dates provided in strings we can't compare them directly as
1009
987
  {
1010
988
    bool error;
1011
989
    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;
 
990
    enum enum_drizzle_timestamp_type t_type= f_type ==
 
991
      DRIZZLE_TYPE_DATE ? DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME;
1013
992
    value= get_date_from_str(session, str, t_type, warn_item->name, &error);
1014
993
    /*
1015
994
      If str did not contain a valid date according to the current
1032
1011
    *cache_arg= cache;
1033
1012
    *item_arg= cache_arg;
1034
1013
  }
1035
 
 
1036
1014
  return value;
1037
1015
}
1038
1016
 
1189
1167
 
1190
1168
int Arg_comparator::compare_decimal()
1191
1169
{
1192
 
  type::Decimal value1;
1193
 
  type::Decimal *val1= (*a)->val_decimal(&value1);
 
1170
  my_decimal value1;
 
1171
  my_decimal *val1= (*a)->val_decimal(&value1);
1194
1172
  if (!(*a)->null_value)
1195
1173
  {
1196
 
    type::Decimal value2;
1197
 
    type::Decimal *val2= (*b)->val_decimal(&value2);
 
1174
    my_decimal value2;
 
1175
    my_decimal *val2= (*b)->val_decimal(&value2);
1198
1176
    if (!(*b)->null_value)
1199
1177
    {
1200
1178
      owner->null_value= 0;
1201
 
      return class_decimal_cmp(val1, val2);
 
1179
      return my_decimal_cmp(val1, val2);
1202
1180
    }
1203
1181
  }
1204
1182
  owner->null_value= 1;
1216
1194
 
1217
1195
int Arg_comparator::compare_e_decimal()
1218
1196
{
1219
 
  type::Decimal value1, value2;
1220
 
  type::Decimal *val1= (*a)->val_decimal(&value1);
1221
 
  type::Decimal *val2= (*b)->val_decimal(&value2);
 
1197
  my_decimal value1, value2;
 
1198
  my_decimal *val1= (*a)->val_decimal(&value1);
 
1199
  my_decimal *val2= (*b)->val_decimal(&value2);
1222
1200
  if ((*a)->null_value || (*b)->null_value)
1223
1201
    return test((*a)->null_value && (*b)->null_value);
1224
 
  return test(class_decimal_cmp(val1, val2) == 0);
 
1202
  return test(my_decimal_cmp(val1, val2) == 0);
1225
1203
}
1226
1204
 
1227
1205
 
1631
1609
 
1632
1610
void Item_in_optimizer::cleanup()
1633
1611
{
1634
 
  item::function::Boolean::cleanup();
 
1612
  Item_bool_func::cleanup();
1635
1613
  if (!save_cache)
1636
1614
    cache= 0;
1637
1615
  return;
1684
1662
    change records at each execution.
1685
1663
  */
1686
1664
  if ((*args) != new_item)
1687
 
    getSession().change_item_tree(args, new_item);
 
1665
    current_session->change_item_tree(args, new_item);
1688
1666
 
1689
1667
  /*
1690
1668
    Transform the right IN operand which should be an Item_in_subselect or a
1839
1817
          {
1840
1818
            range->type= DECIMAL_RESULT;
1841
1819
            range->dec.init();
1842
 
            type::Decimal *dec= el->val_decimal(&range->dec);
 
1820
            my_decimal *dec= el->val_decimal(&range->dec);
1843
1821
            if (dec != &range->dec)
1844
1822
            {
1845
1823
              range->dec= *dec;
1889
1867
{
1890
1868
  assert(fixed == 1);
1891
1869
  double value;
1892
 
  type::Decimal dec_buf, *dec= NULL;
 
1870
  my_decimal dec_buf, *dec= NULL;
1893
1871
  uint32_t i;
1894
1872
 
1895
1873
  if (use_decimal_comparison)
1897
1875
    dec= row->element_index(0)->val_decimal(&dec_buf);
1898
1876
    if (row->element_index(0)->null_value)
1899
1877
      return -1;
1900
 
    class_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
 
1878
    my_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
1901
1879
  }
1902
1880
  else
1903
1881
  {
1922
1900
        and we are comparing against a decimal
1923
1901
      */
1924
1902
      if (dec && range->type == DECIMAL_RESULT)
1925
 
        cmp_result= class_decimal_cmp(&range->dec, dec) <= 0;
 
1903
        cmp_result= my_decimal_cmp(&range->dec, dec) <= 0;
1926
1904
      else
1927
1905
        cmp_result= (range->dbl <= value);
1928
1906
      if (cmp_result)
1932
1910
    }
1933
1911
    interval_range *range= intervals+start;
1934
1912
    return ((dec && range->type == DECIMAL_RESULT) ?
1935
 
            class_decimal_cmp(dec, &range->dec) < 0 :
 
1913
            my_decimal_cmp(dec, &range->dec) < 0 :
1936
1914
            value < range->dbl) ? 0 : start + 1;
1937
1915
  }
1938
1916
 
1943
1921
        ((el->result_type() == DECIMAL_RESULT) ||
1944
1922
         (el->result_type() == INT_RESULT)))
1945
1923
    {
1946
 
      type::Decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
 
1924
      my_decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
1947
1925
      /* Skip NULL ranges. */
1948
1926
      if (el->null_value)
1949
1927
        continue;
1950
 
      if (class_decimal_cmp(e_dec, dec) > 0)
 
1928
      if (my_decimal_cmp(e_dec, dec) > 0)
1951
1929
        return i - 1;
1952
1930
    }
1953
1931
    else
1997
1975
  if (Item_func_opt_neg::fix_fields(session, ref))
1998
1976
    return 1;
1999
1977
 
2000
 
  session->getLex()->current_select->between_count++;
 
1978
  session->lex->current_select->between_count++;
2001
1979
 
2002
1980
  /* not_null_tables_cache == union(T1(e),T1(e1),T1(e2)) */
2003
1981
  if (pred_level && !negated)
2018
1996
  int i;
2019
1997
  bool datetime_found= false;
2020
1998
  compare_as_dates= true;
 
1999
  Session *session= current_session;
2021
2000
 
2022
2001
  /*
2023
2002
    As some compare functions are generated after sql_yacc,
2055
2034
    ge_cmp.set_datetime_cmp_func(args, args + 1);
2056
2035
    le_cmp.set_datetime_cmp_func(args, args + 2);
2057
2036
  }
2058
 
  else if (args[0]->real_item()->type() == FIELD_ITEM)
 
2037
  else if (args[0]->real_item()->type() == FIELD_ITEM &&
 
2038
           session->lex->sql_command != SQLCOM_SHOW_CREATE)
2059
2039
  {
2060
2040
    Item_field *field_item= (Item_field*) (args[0]->real_item());
2061
2041
    if (field_item->field->can_be_compared_as_int64_t())
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);
2774
2749
  */
2775
2750
  if (first_expr_num != -1)
2776
2751
  {
 
2752
    uint32_t i;
2777
2753
    agg[0]= args[first_expr_num];
2778
2754
    left_result_type= agg[0]->result_type();
2779
2755
 
2783
2759
    if (!(found_types= collect_cmp_types(agg, nagg)))
2784
2760
      return;
2785
2761
 
2786
 
    for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
 
2762
    for (i= 0; i <= (uint32_t)DECIMAL_RESULT; i++)
2787
2763
    {
2788
2764
      if (found_types & (1 << i) && !cmp_items[i])
2789
2765
      {
2818
2794
      agg_num_lengths(args[i + 1]);
2819
2795
    if (else_expr_num != -1)
2820
2796
      agg_num_lengths(args[else_expr_num]);
2821
 
    max_length= class_decimal_precision_to_length(max_length + decimals, decimals,
 
2797
    max_length= my_decimal_precision_to_length(max_length + decimals, decimals,
2822
2798
                                               unsigned_flag);
2823
2799
  }
2824
2800
}
2869
2845
 
2870
2846
void Item_func_case::cleanup()
2871
2847
{
 
2848
  uint32_t i;
2872
2849
  Item_func::cleanup();
2873
 
  for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
 
2850
  for (i= 0; i <= (uint32_t)DECIMAL_RESULT; i++)
2874
2851
  {
2875
2852
    delete cmp_items[i];
2876
2853
    cmp_items[i]= 0;
2877
2854
  }
 
2855
  return;
2878
2856
}
2879
2857
 
2880
2858
 
2925
2903
}
2926
2904
 
2927
2905
 
2928
 
type::Decimal *Item_func_coalesce::decimal_op(type::Decimal *decimal_value)
 
2906
my_decimal *Item_func_coalesce::decimal_op(my_decimal *decimal_value)
2929
2907
{
2930
2908
  assert(fixed == 1);
2931
2909
  null_value= 0;
2932
2910
  for (uint32_t i= 0; i < arg_count; i++)
2933
2911
  {
2934
 
    type::Decimal *res= args[i]->val_decimal(decimal_value);
 
2912
    my_decimal *res= args[i]->val_decimal(decimal_value);
2935
2913
    if (!args[i]->null_value)
2936
2914
      return res;
2937
2915
  }
2944
2922
{
2945
2923
  cached_field_type= agg_field_type(args, arg_count);
2946
2924
  agg_result_type(&hybrid_type, args, arg_count);
2947
 
 
2948
2925
  switch (hybrid_type) {
2949
2926
  case STRING_RESULT:
2950
2927
    count_only_length();
2951
2928
    decimals= NOT_FIXED_DEC;
2952
2929
    agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1);
2953
2930
    break;
2954
 
 
2955
2931
  case DECIMAL_RESULT:
2956
2932
    count_decimal_length();
2957
2933
    break;
2958
 
 
2959
2934
  case REAL_RESULT:
2960
2935
    count_real_length();
2961
2936
    break;
2962
 
 
2963
2937
  case INT_RESULT:
2964
2938
    count_only_length();
2965
2939
    decimals= 0;
2966
2940
    break;
2967
 
 
2968
2941
  case ROW_RESULT:
 
2942
  default:
2969
2943
    assert(0);
2970
2944
  }
2971
2945
}
3080
3054
}
3081
3055
 
3082
3056
 
3083
 
static int cmp_decimal(void *, type::Decimal *a, type::Decimal *b)
 
3057
static int cmp_decimal(void *, my_decimal *a, my_decimal *b)
3084
3058
{
3085
3059
  /*
3086
3060
    We need call of fixing buffer pointer, because fast sort just copy
3088
3062
  */
3089
3063
  a->fix_buffer_pointer();
3090
3064
  b->fix_buffer_pointer();
3091
 
  return class_decimal_cmp(a, b);
 
3065
  return my_decimal_cmp(a, b);
3092
3066
}
3093
3067
 
3094
3068
 
3196
3170
  return;
3197
3171
}
3198
3172
 
3199
 
in_int64_t::in_int64_t(uint32_t elements) :
3200
 
  in_vector(elements, sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
 
3173
in_int64_t::in_int64_t(uint32_t elements)
 
3174
  :in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3201
3175
{}
3202
3176
 
3203
3177
void in_int64_t::set(uint32_t pos,Item *item)
3217
3191
  return (unsigned char*) &tmp;
3218
3192
}
3219
3193
 
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)
 
3194
void in_datetime::set(uint32_t pos,Item *item)
3228
3195
{
3229
3196
  Item **tmp_item= &item;
3230
3197
  bool is_null;
3264
3231
 
3265
3232
 
3266
3233
in_decimal::in_decimal(uint32_t elements)
3267
 
  :in_vector(elements, sizeof(type::Decimal),(qsort2_cmp) cmp_decimal, 0)
 
3234
  :in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3268
3235
{}
3269
3236
 
3270
3237
 
3271
3238
void in_decimal::set(uint32_t pos, Item *item)
3272
3239
{
3273
 
  /* as far as 'item' is constant, we can store reference on type::Decimal */
3274
 
  type::Decimal *dec= ((type::Decimal *)base) + pos;
 
3240
  /* as far as 'item' is constant, we can store reference on my_decimal */
 
3241
  my_decimal *dec= ((my_decimal *)base) + pos;
3275
3242
  dec->len= DECIMAL_BUFF_LENGTH;
3276
3243
  dec->fix_buffer_pointer();
3277
 
  type::Decimal *res= item->val_decimal(dec);
 
3244
  my_decimal *res= item->val_decimal(dec);
3278
3245
  /* if item->val_decimal() is evaluated to NULL then res == 0 */
3279
3246
  if (!item->null_value && res != dec)
3280
 
    class_decimal2decimal(res, dec);
 
3247
    my_decimal2decimal(res, dec);
3281
3248
}
3282
3249
 
3283
3250
 
3284
3251
unsigned char *in_decimal::get_value(Item *item)
3285
3252
{
3286
 
  type::Decimal *result= item->val_decimal(&val);
 
3253
  my_decimal *result= item->val_decimal(&val);
3287
3254
  if (item->null_value)
3288
3255
    return 0;
3289
3256
  return (unsigned char *)result;
3296
3263
  switch (type) {
3297
3264
  case STRING_RESULT:
3298
3265
    return new cmp_item_sort_string(cs);
3299
 
 
3300
3266
  case INT_RESULT:
3301
3267
    return new cmp_item_int;
3302
 
 
3303
3268
  case REAL_RESULT:
3304
3269
    return new cmp_item_real;
3305
 
 
3306
3270
  case ROW_RESULT:
3307
3271
    return new cmp_item_row;
3308
 
 
3309
3272
  case DECIMAL_RESULT:
3310
3273
    return new cmp_item_decimal;
 
3274
  default:
 
3275
    assert(0);
 
3276
    break;
3311
3277
  }
3312
 
 
3313
3278
  return 0; // to satisfy compiler :)
3314
3279
}
3315
3280
 
3442
3407
 
3443
3408
void cmp_item_decimal::store_value(Item *item)
3444
3409
{
3445
 
  type::Decimal *val= item->val_decimal(&value);
 
3410
  my_decimal *val= item->val_decimal(&value);
3446
3411
  /* val may be zero if item is nnull */
3447
3412
  if (val && val != &value)
3448
 
    class_decimal2decimal(val, &value);
 
3413
    my_decimal2decimal(val, &value);
3449
3414
}
3450
3415
 
3451
3416
 
3452
3417
int cmp_item_decimal::cmp(Item *arg)
3453
3418
{
3454
 
  type::Decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
 
3419
  my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3455
3420
  if (arg->null_value)
3456
3421
    return 1;
3457
 
  return class_decimal_cmp(&value, tmp);
 
3422
  return my_decimal_cmp(&value, tmp);
3458
3423
}
3459
3424
 
3460
3425
 
3461
3426
int cmp_item_decimal::compare(cmp_item *arg)
3462
3427
{
3463
3428
  cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3464
 
  return class_decimal_cmp(&value, &l_cmp->value);
 
3429
  return my_decimal_cmp(&value, &l_cmp->value);
3465
3430
}
3466
3431
 
3467
3432
 
3574
3539
{
3575
3540
  Item **arg, **arg_end;
3576
3541
  bool const_itm= 1;
 
3542
  Session *session= current_session;
3577
3543
  bool datetime_found= false;
3578
3544
  /* true <=> arguments values will be compared as DATETIMEs. */
3579
3545
  bool compare_as_datetime= false;
3580
3546
  Item *date_arg= 0;
3581
3547
  uint32_t found_types= 0;
3582
 
  uint32_t type_cnt= 0;
 
3548
  uint32_t type_cnt= 0, i;
3583
3549
  Item_result cmp_type= STRING_RESULT;
3584
3550
  left_result_type= args[0]->result_type();
3585
3551
  if (!(found_types= collect_cmp_types(args, arg_count, true)))
3593
3559
      break;
3594
3560
    }
3595
3561
  }
3596
 
  for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
 
3562
  for (i= 0; i <= (uint32_t)DECIMAL_RESULT; i++)
3597
3563
  {
3598
3564
    if (found_types & 1 << i)
3599
3565
    {
3700
3666
  if (type_cnt == 1 && const_itm && !nulls_in_row())
3701
3667
  {
3702
3668
    if (compare_as_datetime)
3703
 
    {
3704
3669
      array= new in_datetime(date_arg, arg_count - 1);
3705
 
    }
3706
3670
    else
3707
3671
    {
3708
3672
      /*
3713
3677
        comparison type accordingly.
3714
3678
      */
3715
3679
      if (args[0]->real_item()->type() == FIELD_ITEM &&
 
3680
          session->lex->sql_command != SQLCOM_SHOW_CREATE &&
3716
3681
          cmp_type != INT_RESULT)
3717
3682
      {
3718
3683
        Item_field *field_item= (Item_field*) (args[0]->real_item());
3721
3686
          bool all_converted= true;
3722
3687
          for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3723
3688
          {
3724
 
            if (!convert_constant_item (&getSession(), field_item, &arg[0]))
 
3689
            if (!convert_constant_item (session, field_item, &arg[0]))
3725
3690
              all_converted= false;
3726
3691
          }
3727
3692
          if (all_converted)
3728
3693
            cmp_type= INT_RESULT;
3729
3694
        }
3730
3695
      }
3731
 
 
3732
3696
      switch (cmp_type) {
3733
3697
      case STRING_RESULT:
3734
3698
        array=new in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
3735
3699
                            cmp_collation.collation);
3736
3700
        break;
3737
 
 
3738
3701
      case INT_RESULT:
3739
3702
        array= new in_int64_t(arg_count-1);
3740
3703
        break;
3741
 
 
3742
3704
      case REAL_RESULT:
3743
3705
        array= new in_double(arg_count-1);
3744
3706
        break;
3745
 
 
3746
3707
      case ROW_RESULT:
3747
3708
        /*
3748
3709
          The row comparator was created at the beginning but only DATETIME
3751
3712
        */
3752
3713
        ((in_row*)array)->tmp.store_value(args[0]);
3753
3714
        break;
3754
 
 
3755
3715
      case DECIMAL_RESULT:
3756
3716
        array= new in_decimal(arg_count - 1);
3757
3717
        break;
 
3718
      default:
 
3719
        assert(0);
 
3720
        return;
3758
3721
      }
3759
3722
    }
3760
 
 
3761
 
    if (array && !(getSession().is_fatal_error))                // If not EOM
 
3723
    if (array && !(session->is_fatal_error))            // If not EOM
3762
3724
    {
3763
3725
      uint32_t j=0;
3764
3726
      for (uint32_t arg_num=1 ; arg_num < arg_count ; arg_num++)
3765
3727
      {
3766
 
        if (!args[arg_num]->null_value)                 // Skip NULL values
 
3728
        if (!args[arg_num]->null_value)                 // Skip NULL values
3767
3729
        {
3768
3730
          array->set(j,args[arg_num]);
3769
 
          j++;
 
3731
          j++;
3770
3732
        }
3771
 
        else
3772
 
          have_null= 1;
 
3733
        else
 
3734
          have_null= 1;
3773
3735
      }
3774
3736
      if ((array->used_count= j))
3775
 
        array->sort();
 
3737
        array->sort();
3776
3738
    }
3777
3739
  }
3778
3740
  else
3781
3743
      cmp_items[STRING_RESULT]= new cmp_item_datetime(date_arg);
3782
3744
    else
3783
3745
    {
3784
 
      for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
 
3746
      for (i= 0; i <= (uint32_t) DECIMAL_RESULT; i++)
3785
3747
      {
3786
3748
        if (found_types & (1 << i) && !cmp_items[i])
3787
3749
        {
3874
3836
 
3875
3837
 
3876
3838
Item_cond::Item_cond(Session *session, Item_cond *item)
3877
 
  :item::function::Boolean(session, item),
 
3839
  :Item_bool_func(session, item),
3878
3840
   abort_on_null(item->abort_on_null),
3879
3841
   and_tables_cache(item->and_tables_cache)
3880
3842
{
3886
3848
 
3887
3849
void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
3888
3850
{
3889
 
  List<Item>::iterator li(item->list.begin());
 
3851
  List_iterator_fast<Item> li(item->list);
3890
3852
  while (Item *it= li++)
3891
3853
    list.push_back(it->copy_andor_structure(session));
3892
3854
}
3896
3858
Item_cond::fix_fields(Session *session, Item **)
3897
3859
{
3898
3860
  assert(fixed == 0);
3899
 
  List<Item>::iterator li(list.begin());
 
3861
  List_iterator<Item> li(list);
3900
3862
  Item *item;
3901
3863
  void *orig_session_marker= session->session_marker;
3902
3864
  unsigned char buff[sizeof(char*)];                    // Max local vars in function
3903
3865
  not_null_tables_cache= used_tables_cache= 0;
3904
 
  const_item_cache= true;
 
3866
  const_item_cache= 1;
3905
3867
 
3906
3868
  if (functype() == COND_OR_FUNC)
3907
3869
    session->session_marker= 0;
3936
3898
           !((Item_cond*) item)->list.is_empty())
3937
3899
    {                                           // Identical function
3938
3900
      li.replace(((Item_cond*) item)->list);
3939
 
      ((Item_cond*) item)->list.clear();
 
3901
      ((Item_cond*) item)->list.empty();
3940
3902
      item= *li.ref();                          // new current item
3941
3903
    }
3942
3904
    if (abort_on_null)
3962
3924
    if (item->maybe_null)
3963
3925
      maybe_null=1;
3964
3926
  }
3965
 
  session->getLex()->current_select->cond_count+= list.elements;
 
3927
  session->lex->current_select->cond_count+= list.elements;
3966
3928
  session->session_marker= orig_session_marker;
3967
3929
  fix_length_and_dec();
3968
3930
  fixed= 1;
3972
3934
 
3973
3935
void Item_cond::fix_after_pullout(Select_Lex *new_parent, Item **)
3974
3936
{
3975
 
  List<Item>::iterator li(list.begin());
 
3937
  List_iterator<Item> li(list);
3976
3938
  Item *item;
3977
3939
 
3978
3940
  used_tables_cache=0;
3979
 
  const_item_cache= true;
 
3941
  const_item_cache=1;
3980
3942
 
3981
3943
  and_tables_cache= ~(table_map) 0; // Here and below we do as fix_fields does
3982
3944
  not_null_tables_cache= 0;
4004
3966
 
4005
3967
bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
4006
3968
{
4007
 
  List<Item>::iterator li(list.begin());
 
3969
  List_iterator_fast<Item> li(list);
4008
3970
  Item *item;
4009
3971
  while ((item= li++))
4010
3972
    if (item->walk(processor, walk_subquery, arg))
4033
3995
 
4034
3996
Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
4035
3997
{
4036
 
  List<Item>::iterator li(list.begin());
 
3998
  List_iterator<Item> li(list);
4037
3999
  Item *item;
4038
4000
  while ((item= li++))
4039
4001
  {
4048
4010
      change records at each execution.
4049
4011
    */
4050
4012
    if (new_item != item)
4051
 
      getSession().change_item_tree(li.ref(), new_item);
 
4013
      current_session->change_item_tree(li.ref(), new_item);
4052
4014
  }
4053
4015
  return Item_func::transform(transformer, arg);
4054
4016
}
4084
4046
  if (!(this->*analyzer)(arg_p))
4085
4047
    return 0;
4086
4048
 
4087
 
  List<Item>::iterator li(list.begin());
 
4049
  List_iterator<Item> li(list);
4088
4050
  Item *item;
4089
4051
  while ((item= li++))
4090
4052
  {
4103
4065
void Item_cond::traverse_cond(Cond_traverser traverser,
4104
4066
                              void *arg, traverse_order order)
4105
4067
{
4106
 
  List<Item>::iterator li(list.begin());
 
4068
  List_iterator<Item> li(list);
4107
4069
  Item *item;
4108
4070
 
4109
4071
  switch (order) {
4144
4106
void Item_cond::split_sum_func(Session *session, Item **ref_pointer_array,
4145
4107
                               List<Item> &fields)
4146
4108
{
4147
 
  List<Item>::iterator li(list.begin());
 
4109
  List_iterator<Item> li(list);
4148
4110
  Item *item;
4149
4111
  while ((item= li++))
4150
4112
    item->split_sum_func(session, ref_pointer_array,
4161
4123
 
4162
4124
void Item_cond::update_used_tables()
4163
4125
{
4164
 
  List<Item>::iterator li(list.begin());
 
4126
  List_iterator_fast<Item> li(list);
4165
4127
  Item *item;
4166
4128
 
4167
4129
  used_tables_cache=0;
4168
 
  const_item_cache= true;
 
4130
  const_item_cache=1;
4169
4131
  while ((item=li++))
4170
4132
  {
4171
4133
    item->update_used_tables();
4178
4140
void Item_cond::print(String *str, enum_query_type query_type)
4179
4141
{
4180
4142
  str->append('(');
4181
 
  List<Item>::iterator li(list.begin());
 
4143
  List_iterator_fast<Item> li(list);
4182
4144
  Item *item;
4183
4145
  if ((item=li++))
4184
4146
    item->print(str, query_type);
4195
4157
 
4196
4158
void Item_cond::neg_arguments(Session *session)
4197
4159
{
4198
 
  List<Item>::iterator li(list.begin());
 
4160
  List_iterator<Item> li(list);
4199
4161
  Item *item;
4200
4162
  while ((item= li++))          /* Apply not transformation to the arguments */
4201
4163
  {
4233
4195
int64_t Item_cond_and::val_int()
4234
4196
{
4235
4197
  assert(fixed == 1);
4236
 
  List<Item>::iterator li(list.begin());
 
4198
  List_iterator_fast<Item> li(list);
4237
4199
  Item *item;
4238
4200
  null_value= 0;
4239
4201
  while ((item=li++))
4251
4213
int64_t Item_cond_or::val_int()
4252
4214
{
4253
4215
  assert(fixed == 1);
4254
 
  List<Item>::iterator li(list.begin());
 
4216
  List_iterator_fast<Item> li(list);
4255
4217
  Item *item;
4256
4218
  null_value=0;
4257
4219
  while ((item=li++))
4488
4450
      {
4489
4451
        pattern     = first + 1;
4490
4452
        pattern_len = (int) len - 2;
4491
 
        int *suff = (int*) session->getMemRoot()->allocate((int) (sizeof(int)*
4492
 
                                                                  ((pattern_len + 1)*2+
4493
 
                                                                   alphabet_size)));
 
4453
        int *suff = (int*) session->alloc((int) (sizeof(int)*
 
4454
                                      ((pattern_len + 1)*2+
 
4455
                                      alphabet_size)));
4494
4456
        bmGs      = suff + pattern_len + 1;
4495
4457
        bmBc      = bmGs + pattern_len + 1;
4496
4458
        turboBM_compute_good_suffix_shifts(suff);
4507
4469
  Item_bool_func2::cleanup();
4508
4470
}
4509
4471
 
4510
 
static unsigned char likeconv(const CHARSET_INFO *cs, unsigned char a)
4511
 
{
4512
4472
#ifdef LIKE_CMP_TOUPPER
4513
 
  return cs->toupper(a);
 
4473
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
4514
4474
#else
4515
 
  return cs->sort_order[a];
 
4475
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
4516
4476
#endif
4517
 
}
 
4477
 
4518
4478
 
4519
4479
/**
4520
4480
  Precomputation dependent only on pattern_len.
4651
4611
 
4652
4612
bool Item_func_like::turboBM_matches(const char* text, int text_len) const
4653
4613
{
4654
 
  int bcShift;
4655
 
  int turboShift;
 
4614
  register int bcShift;
 
4615
  register int turboShift;
4656
4616
  int shift = pattern_len;
4657
4617
  int j     = 0;
4658
4618
  int u     = 0;
4666
4626
  {
4667
4627
    while (j <= tlmpl)
4668
4628
    {
4669
 
      int i= plm1;
 
4629
      register int i= plm1;
4670
4630
      while (i >= 0 && pattern[i] == text[i + j])
4671
4631
      {
4672
4632
        i--;
4676
4636
      if (i < 0)
4677
4637
        return 1;
4678
4638
 
4679
 
      const int v = plm1 - i;
 
4639
      register const int v = plm1 - i;
4680
4640
      turboShift = u - v;
4681
4641
      bcShift    = bmBc[(uint32_t) (unsigned char) text[i + j]] - plm1 + i;
4682
4642
      shift      = (turboShift > bcShift) ? turboShift : bcShift;
4697
4657
  {
4698
4658
    while (j <= tlmpl)
4699
4659
    {
4700
 
      int i = plm1;
 
4660
      register int i = plm1;
4701
4661
      while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
4702
4662
      {
4703
4663
        i--;
4708
4668
      if (i < 0)
4709
4669
        return 1;
4710
4670
 
4711
 
      const int v= plm1 - i;
 
4671
      register const int v= plm1 - i;
4712
4672
      turboShift= u - v;
4713
4673
      bcShift= bmBc[(uint32_t) likeconv(cs, text[i + j])] - plm1 + i;
4714
4674
      shift= (turboShift > bcShift) ? turboShift : bcShift;
4749
4709
int64_t Item_cond_xor::val_int()
4750
4710
{
4751
4711
  assert(fixed == 1);
4752
 
  List<Item>::iterator li(list.begin());
 
4712
  List_iterator<Item> li(list);
4753
4713
  Item *item;
4754
4714
  int result=0;
4755
4715
  null_value=0;
4909
4869
}
4910
4870
 
4911
4871
Item_equal::Item_equal(Item_field *f1, Item_field *f2)
4912
 
  : item::function::Boolean(), const_item(0), eval_item(0), cond_false(0)
 
4872
  : Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
4913
4873
{
4914
 
  const_item_cache= false;
 
4874
  const_item_cache= 0;
4915
4875
  fields.push_back(f1);
4916
4876
  fields.push_back(f2);
4917
4877
}
4918
4878
 
4919
4879
Item_equal::Item_equal(Item *c, Item_field *f)
4920
 
  : item::function::Boolean(), eval_item(0), cond_false(0)
 
4880
  : Item_bool_func(), eval_item(0), cond_false(0)
4921
4881
{
4922
 
  const_item_cache= false;
 
4882
  const_item_cache= 0;
4923
4883
  fields.push_back(f);
4924
4884
  const_item= c;
4925
4885
}
4926
4886
 
4927
4887
 
4928
4888
Item_equal::Item_equal(Item_equal *item_equal)
4929
 
  : item::function::Boolean(), eval_item(0), cond_false(0)
 
4889
  : Item_bool_func(), eval_item(0), cond_false(0)
4930
4890
{
4931
 
  const_item_cache= false;
4932
 
  List<Item_field>::iterator li(item_equal->fields.begin());
 
4891
  const_item_cache= 0;
 
4892
  List_iterator_fast<Item_field> li(item_equal->fields);
4933
4893
  Item_field *item;
4934
4894
  while ((item= li++))
4935
4895
  {
4952
4912
  func->set_cmp_func();
4953
4913
  func->quick_fix_field();
4954
4914
  if ((cond_false= !func->val_int()))
4955
 
    const_item_cache= true;
 
4915
    const_item_cache= 1;
4956
4916
}
4957
4917
 
4958
4918
void Item_equal::add(Item_field *f)
4981
4941
 
4982
4942
bool Item_equal::contains(Field *field)
4983
4943
{
4984
 
  List<Item_field>::iterator it(fields.begin());
 
4944
  List_iterator_fast<Item_field> it(fields);
4985
4945
  Item_field *item;
4986
4946
  while ((item= it++))
4987
4947
  {
5040
5000
void Item_equal::sort(Item_field_cmpfunc cmp, void *arg)
5041
5001
{
5042
5002
  bool swap;
5043
 
  List<Item_field>::iterator it(fields.begin());
 
5003
  List_iterator<Item_field> it(fields);
5044
5004
  do
5045
5005
  {
5046
5006
    Item_field *item1= it++;
5064
5024
        ref1= ref2;
5065
5025
      }
5066
5026
    }
5067
 
    it= fields.begin();
 
5027
    it.rewind();
5068
5028
  } while (swap);
5069
5029
}
5070
5030
 
5081
5041
 
5082
5042
void Item_equal::update_const()
5083
5043
{
5084
 
  List<Item_field>::iterator it(fields.begin());
 
5044
  List_iterator<Item_field> it(fields);
5085
5045
  Item *item;
5086
5046
  while ((item= it++))
5087
5047
  {
5095
5055
 
5096
5056
bool Item_equal::fix_fields(Session *, Item **)
5097
5057
{
5098
 
  List<Item_field>::iterator li(fields.begin());
 
5058
  List_iterator_fast<Item_field> li(fields);
5099
5059
  Item *item;
5100
5060
  not_null_tables_cache= used_tables_cache= 0;
5101
 
  const_item_cache= false;
 
5061
  const_item_cache= 0;
5102
5062
  while ((item= li++))
5103
5063
  {
5104
5064
    table_map tmp_table_map;
5115
5075
 
5116
5076
void Item_equal::update_used_tables()
5117
5077
{
5118
 
  List<Item_field>::iterator li(fields.begin());
 
5078
  List_iterator_fast<Item_field> li(fields);
5119
5079
  Item *item;
5120
5080
  not_null_tables_cache= used_tables_cache= 0;
5121
5081
  if ((const_item_cache= cond_false))
5133
5093
  Item_field *item_field;
5134
5094
  if (cond_false)
5135
5095
    return 0;
5136
 
  List<Item_field>::iterator it(fields.begin());
 
5096
  List_iterator_fast<Item_field> it(fields);
5137
5097
  Item *item= const_item ? const_item : it++;
 
5098
  if ((null_value= item->null_value))
 
5099
    return 0;
5138
5100
  eval_item->store_value(item);
5139
 
  if ((null_value= item->null_value))
5140
 
    return 0;
5141
5101
  while ((item_field= it++))
5142
5102
  {
5143
5103
    /* Skip fields of non-const tables. They haven't been read yet */
5144
 
    if (item_field->field->getTable()->const_table)
 
5104
    if (item_field->field->table->const_table)
5145
5105
    {
5146
 
      if (eval_item->cmp(item_field) || (null_value= item_field->null_value))
 
5106
      if ((null_value= item_field->null_value) || eval_item->cmp(item_field))
5147
5107
        return 0;
5148
5108
    }
5149
5109
  }
5159
5119
 
5160
5120
bool Item_equal::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
5161
5121
{
5162
 
  List<Item_field>::iterator it(fields.begin());
 
5122
  List_iterator_fast<Item_field> it(fields);
5163
5123
  Item *item;
5164
5124
  while ((item= it++))
5165
5125
  {
5171
5131
 
5172
5132
Item *Item_equal::transform(Item_transformer transformer, unsigned char *arg)
5173
5133
{
5174
 
  List<Item_field>::iterator it(fields.begin());
 
5134
  List_iterator<Item_field> it(fields);
5175
5135
  Item *item;
5176
5136
  while ((item= it++))
5177
5137
  {
5186
5146
      change records at each execution.
5187
5147
    */
5188
5148
    if (new_item != item)
5189
 
      getSession().change_item_tree((Item **) it.ref(), new_item);
 
5149
      current_session->change_item_tree((Item **) it.ref(), new_item);
5190
5150
  }
5191
5151
  return Item_func::transform(transformer, arg);
5192
5152
}
5195
5155
{
5196
5156
  str->append(func_name());
5197
5157
  str->append('(');
5198
 
  List<Item_field>::iterator it(fields.begin());
 
5158
  List_iterator_fast<Item_field> it(fields);
5199
5159
  Item *item;
5200
5160
  if (const_item)
5201
5161
    const_item->print(str, query_type);
5213
5173
  str->append(')');
5214
5174
}
5215
5175
 
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
5176
} /* namespace drizzled */