~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/cmpfunc.cc

  • Committer: Stewart Smith
  • Date: 2009-06-16 00:44:35 UTC
  • mto: (1119.2.6 merge)
  • mto: This revision was merged to the branch mainline in revision 1124.
  • Revision ID: stewart@flamingspork.com-20090616004435-t1vust6erhco7edc
make comment_index test not leave tables behind after running

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"
 
24
#include "drizzled/server_includes.h"
25
25
#include "drizzled/sql_select.h"
26
26
#include "drizzled/error.h"
27
27
#include "drizzled/temporal.h"
30
30
#include "drizzled/item/cache_int.h"
31
31
#include "drizzled/item/int_with_ref.h"
32
32
#include "drizzled/check_stack_overrun.h"
33
 
#include "drizzled/time_functions.h"
34
 
#include "drizzled/internal/my_sys.h"
35
 
#include <math.h>
 
33
 
36
34
#include <algorithm>
37
35
 
38
36
using namespace std;
39
37
 
40
 
namespace drizzled
41
 
{
42
 
 
43
 
extern const double log_10[309];
44
38
 
45
39
static Eq_creator eq_creator;
46
40
static Ne_creator ne_creator;
209
203
    collect_cmp_types()
210
204
      items             Array of items to collect types from
211
205
      nitems            Number of items in the array
212
 
      skip_nulls        Don't collect types of NULL items if TRUE
213
206
 
214
207
  DESCRIPTION
215
208
    This function collects different result types for comparison of the first
220
213
    Bitmap of collected types - otherwise
221
214
*/
222
215
 
223
 
static uint32_t collect_cmp_types(Item **items, uint32_t nitems, bool skip_nulls= false)
 
216
static uint32_t collect_cmp_types(Item **items, uint32_t nitems)
224
217
{
225
218
  uint32_t i;
226
219
  uint32_t found_types;
229
222
  found_types= 0;
230
223
  for (i= 1; i < nitems ; i++)
231
224
  {
232
 
    if (skip_nulls && items[i]->type() == Item::NULL_ITEM)
233
 
      continue; // Skip NULL constant items
234
225
    if ((left_result == ROW_RESULT ||
235
226
         items[i]->result_type() == ROW_RESULT) &&
236
227
        cmp_row_type(items[0], items[i]))
238
229
    found_types|= 1<< (uint32_t)item_cmp_type(left_result,
239
230
                                           items[i]->result_type());
240
231
  }
241
 
  /*
242
 
   Even if all right-hand items are NULLs and we are skipping them all, we need
243
 
   at least one type bit in the found_type bitmask.
244
 
  */
245
 
  if (skip_nulls && !found_types)
246
 
    found_types= 1 << (uint)left_result;
247
232
  return found_types;
248
233
}
249
234
 
463
448
      the call to save_in_field below overrides that value.
464
449
    */
465
450
    if (field_item->depended_from)
466
 
    {
467
451
      orig_field_val= field->val_int();
468
 
    }
469
 
 
470
452
    if (!(*item)->is_null() && !(*item)->save_in_field(field, 1))
471
453
    {
472
454
      Item *tmp= new Item_int_with_ref(field->val_int(), *item,
475
457
        session->change_item_tree(item, tmp);
476
458
      result= 1;                                        // Item was replaced
477
459
    }
478
 
 
479
460
    /* Restore the original field value. */
480
461
    if (field_item->depended_from)
481
462
    {
482
 
      result= field->store(orig_field_val, field->isUnsigned());
 
463
      result= field->store(orig_field_val, true);
483
464
      /* orig_field_val must be a valid value that can be restored back. */
484
465
      assert(!result);
485
466
    }
493
474
void Item_bool_func2::fix_length_and_dec()
494
475
{
495
476
  max_length= 1;                                     // Function returns 0 or 1
 
477
  Session *session;
496
478
 
497
479
  /*
498
480
    As some compare functions are generated after sql_yacc,
530
512
    return;
531
513
  }
532
514
 
 
515
  session= current_session;
533
516
  Item_field *field_item= NULL;
534
517
 
535
518
  if (args[0]->real_item()->type() == FIELD_ITEM)
538
521
    if (field_item->field->can_be_compared_as_int64_t() &&
539
522
        !(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
540
523
    {
541
 
      if (convert_constant_item(&getSession(), field_item, &args[1]))
 
524
      if (convert_constant_item(session, field_item, &args[1]))
542
525
      {
543
526
        cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
544
527
                         INT_RESULT);           // Works for all types.
554
537
          !(field_item->is_datetime() &&
555
538
            args[0]->result_type() == STRING_RESULT))
556
539
      {
557
 
        if (convert_constant_item(&getSession(), field_item, &args[0]))
 
540
        if (convert_constant_item(session, field_item, &args[0]))
558
541
        {
559
542
          cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
560
543
                           INT_RESULT); // Works for all types.
572
555
{
573
556
  owner= item;
574
557
  func= comparator_matrix[type]
575
 
    [test(owner->functype() == Item_func::EQUAL_FUNC)];
576
 
 
 
558
                         [test(owner->functype() == Item_func::EQUAL_FUNC)];
577
559
  switch (type) {
578
560
  case ROW_RESULT:
579
 
    {
580
 
      uint32_t n= (*a)->cols();
581
 
      if (n != (*b)->cols())
582
 
      {
583
 
        my_error(ER_OPERAND_COLUMNS, MYF(0), n);
584
 
        comparators= 0;
585
 
        return 1;
586
 
      }
587
 
      if (!(comparators= new Arg_comparator[n]))
588
 
        return 1;
589
 
      for (uint32_t i=0; i < n; i++)
590
 
      {
591
 
        if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
592
 
        {
593
 
          my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
594
 
          return 1;
595
 
        }
596
 
        comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
597
 
      }
598
 
      break;
599
 
    }
600
 
 
 
561
  {
 
562
    uint32_t n= (*a)->cols();
 
563
    if (n != (*b)->cols())
 
564
    {
 
565
      my_error(ER_OPERAND_COLUMNS, MYF(0), n);
 
566
      comparators= 0;
 
567
      return 1;
 
568
    }
 
569
    if (!(comparators= new Arg_comparator[n]))
 
570
      return 1;
 
571
    for (uint32_t i=0; i < n; i++)
 
572
    {
 
573
      if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
 
574
      {
 
575
        my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
 
576
        return 1;
 
577
      }
 
578
      comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
 
579
    }
 
580
    break;
 
581
  }
601
582
  case STRING_RESULT:
 
583
  {
 
584
    /*
 
585
      We must set cmp_charset here as we may be called from for an automatic
 
586
      generated item, like in natural join
 
587
    */
 
588
    if (cmp_collation.set((*a)->collation, (*b)->collation) ||
 
589
        cmp_collation.derivation == DERIVATION_NONE)
 
590
    {
 
591
      my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
 
592
      return 1;
 
593
    }
 
594
    if (cmp_collation.collation == &my_charset_bin)
602
595
    {
603
596
      /*
604
 
        We must set cmp_charset here as we may be called from for an automatic
605
 
        generated item, like in natural join
 
597
        We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
 
598
        without removing end space
606
599
      */
607
 
      if (cmp_collation.set((*a)->collation, (*b)->collation) ||
608
 
          cmp_collation.derivation == DERIVATION_NONE)
609
 
      {
610
 
        my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
611
 
        return 1;
612
 
      }
613
 
      if (cmp_collation.collation == &my_charset_bin)
614
 
      {
615
 
        /*
616
 
          We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
617
 
          without removing end space
618
 
        */
619
 
        if (func == &Arg_comparator::compare_string)
620
 
          func= &Arg_comparator::compare_binary_string;
621
 
        else if (func == &Arg_comparator::compare_e_string)
622
 
          func= &Arg_comparator::compare_e_binary_string;
 
600
      if (func == &Arg_comparator::compare_string)
 
601
        func= &Arg_comparator::compare_binary_string;
 
602
      else if (func == &Arg_comparator::compare_e_string)
 
603
        func= &Arg_comparator::compare_e_binary_string;
623
604
 
624
 
        /*
625
 
          As this is binary compassion, mark all fields that they can't be
626
 
          transformed. Otherwise we would get into trouble with comparisons
627
 
like:
628
 
WHERE col= 'j' AND col LIKE BINARY 'j'
629
 
which would be transformed to:
630
 
WHERE col= 'j'
 
605
      /*
 
606
        As this is binary compassion, mark all fields that they can't be
 
607
        transformed. Otherwise we would get into trouble with comparisons
 
608
        like:
 
609
        WHERE col= 'j' AND col LIKE BINARY 'j'
 
610
        which would be transformed to:
 
611
        WHERE col= 'j'
631
612
      */
632
 
        (*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
633
 
        (*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
634
 
      }
635
 
      break;
 
613
      (*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
 
614
      (*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
636
615
    }
 
616
    break;
 
617
  }
637
618
  case INT_RESULT:
638
 
    {
639
 
      if (func == &Arg_comparator::compare_int_signed)
640
 
      {
641
 
        if ((*a)->unsigned_flag)
642
 
          func= (((*b)->unsigned_flag)?
643
 
                 &Arg_comparator::compare_int_unsigned :
644
 
                 &Arg_comparator::compare_int_unsigned_signed);
645
 
        else if ((*b)->unsigned_flag)
646
 
          func= &Arg_comparator::compare_int_signed_unsigned;
647
 
      }
648
 
      else if (func== &Arg_comparator::compare_e_int)
649
 
      {
650
 
        if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
651
 
          func= &Arg_comparator::compare_e_int_diff_signedness;
652
 
      }
653
 
      break;
654
 
    }
 
619
  {
 
620
    if (func == &Arg_comparator::compare_int_signed)
 
621
    {
 
622
      if ((*a)->unsigned_flag)
 
623
        func= (((*b)->unsigned_flag)?
 
624
               &Arg_comparator::compare_int_unsigned :
 
625
               &Arg_comparator::compare_int_unsigned_signed);
 
626
      else if ((*b)->unsigned_flag)
 
627
        func= &Arg_comparator::compare_int_signed_unsigned;
 
628
    }
 
629
    else if (func== &Arg_comparator::compare_e_int)
 
630
    {
 
631
      if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
 
632
        func= &Arg_comparator::compare_e_int_diff_signedness;
 
633
    }
 
634
    break;
 
635
  }
655
636
  case DECIMAL_RESULT:
656
637
    break;
657
638
  case REAL_RESULT:
 
639
  {
 
640
    if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
658
641
    {
659
 
      if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
660
 
      {
661
 
        precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
662
 
        if (func == &Arg_comparator::compare_real)
663
 
          func= &Arg_comparator::compare_real_fixed;
664
 
        else if (func == &Arg_comparator::compare_e_real)
665
 
          func= &Arg_comparator::compare_e_real_fixed;
666
 
      }
667
 
      break;
 
642
      precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
 
643
      if (func == &Arg_comparator::compare_real)
 
644
        func= &Arg_comparator::compare_real_fixed;
 
645
      else if (func == &Arg_comparator::compare_e_real)
 
646
        func= &Arg_comparator::compare_e_real_fixed;
668
647
    }
669
 
  }
670
 
 
 
648
    break;
 
649
  }
 
650
  default:
 
651
    assert(0);
 
652
  }
671
653
  return 0;
672
654
}
673
655
 
694
676
    converted value. 0 on error and on zero-dates -- check 'failure'
695
677
*/
696
678
 
697
 
static int64_t
698
 
get_date_from_str(Session *session, String *str, type::timestamp_t warn_type,
 
679
static uint64_t
 
680
get_date_from_str(Session *session, String *str, enum enum_drizzle_timestamp_type warn_type,
699
681
                  char *warn_name, bool *error_arg)
700
682
{
701
 
  int64_t value= 0;
702
 
  type::cut_t error= type::VALID;
703
 
  type::Time l_time;
704
 
  type::timestamp_t ret;
705
 
 
706
 
  ret= l_time.store(str->ptr(), str->length(),
707
 
                    (TIME_FUZZY_DATE | MODE_INVALID_DATES | (session->variables.sql_mode & MODE_NO_ZERO_DATE)),
708
 
                    error);
709
 
 
710
 
  if (ret == type::DRIZZLE_TIMESTAMP_DATETIME || ret == type::DRIZZLE_TIMESTAMP_DATE)
 
683
  uint64_t value= 0;
 
684
  int error;
 
685
  DRIZZLE_TIME l_time;
 
686
  enum enum_drizzle_timestamp_type ret;
 
687
 
 
688
  ret= str_to_datetime(str->ptr(), str->length(), &l_time,
 
689
                       (TIME_FUZZY_DATE | MODE_INVALID_DATES |
 
690
                        (session->variables.sql_mode & MODE_NO_ZERO_DATE)),
 
691
                       &error);
 
692
 
 
693
  if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
711
694
  {
712
695
    /*
713
696
      Do not return yet, we may still want to throw a "trailing garbage"
714
697
      warning.
715
698
    */
716
699
    *error_arg= false;
717
 
    l_time.convert(value);
 
700
    value= TIME_to_uint64_t_datetime(&l_time);
718
701
  }
719
702
  else
720
703
  {
721
704
    *error_arg= true;
722
 
    error= type::CUT;                                   /* force warning */
 
705
    error= 1;                                   /* force warning */
723
706
  }
724
707
 
725
 
  if (error != type::VALID)
726
 
  {
 
708
  if (error > 0)
727
709
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
728
710
                                 str->ptr(), str->length(),
729
711
                                 warn_type, warn_name);
730
 
  }
731
712
 
732
713
  return value;
733
714
}
766
747
*/
767
748
 
768
749
enum Arg_comparator::enum_date_cmp_type
769
 
Arg_comparator::can_compare_as_dates(Item *in_a, Item *in_b,
770
 
                                     int64_t *const_value)
 
750
Arg_comparator::can_compare_as_dates(Item *a, Item *b, uint64_t *const_value)
771
751
{
772
752
  enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
773
753
  Item *str_arg= 0, *date_arg= 0;
774
754
 
775
 
  if (in_a->type() == Item::ROW_ITEM || in_b->type() == Item::ROW_ITEM)
 
755
  if (a->type() == Item::ROW_ITEM || b->type() == Item::ROW_ITEM)
776
756
    return CMP_DATE_DFLT;
777
757
 
778
 
  if (in_a->is_datetime())
 
758
  if (a->is_datetime())
779
759
  {
780
 
    if (in_b->is_datetime())
781
 
    {
 
760
    if (b->is_datetime())
782
761
      cmp_type= CMP_DATE_WITH_DATE;
783
 
    }
784
 
    else if (in_b->result_type() == STRING_RESULT)
 
762
    else if (b->result_type() == STRING_RESULT)
785
763
    {
786
764
      cmp_type= CMP_DATE_WITH_STR;
787
 
      date_arg= in_a;
788
 
      str_arg= in_b;
 
765
      date_arg= a;
 
766
      str_arg= b;
789
767
    }
790
768
  }
791
 
  else if (in_b->is_datetime() && in_a->result_type() == STRING_RESULT)
 
769
  else if (b->is_datetime() && a->result_type() == STRING_RESULT)
792
770
  {
793
771
    cmp_type= CMP_STR_WITH_DATE;
794
 
    date_arg= in_b;
795
 
    str_arg= in_a;
 
772
    date_arg= b;
 
773
    str_arg= a;
796
774
  }
797
775
 
798
776
  if (cmp_type != CMP_DATE_DFLT)
822
800
       * Does a uint64_t conversion really have to happen here?  Fields return int64_t
823
801
       * from val_int(), not uint64_t...
824
802
       */
825
 
      int64_t value;
 
803
      uint64_t value;
826
804
      String *str_val;
827
805
      String tmp;
828
806
      /* DateTime used to pick up as many string conversion possibilities as possible. */
829
 
      DateTime temporal;
 
807
      drizzled::DateTime temporal;
830
808
 
831
809
      str_val= str_arg->val_str(&tmp);
832
810
      if (! str_val)
848
826
      }
849
827
 
850
828
      /* String conversion was good.  Convert to an integer for comparison purposes. */
851
 
      temporal.to_int64_t(&value);
 
829
      int64_t int_value;
 
830
      temporal.to_int64_t(&int_value);
 
831
      value= (uint64_t) int_value;
852
832
 
853
833
      if (const_value)
854
834
        *const_value= value;
862
842
                                        Item **a1, Item **a2,
863
843
                                        Item_result type)
864
844
{
865
 
  enum_date_cmp_type cmp_type;
866
 
  int64_t const_value= -1;
 
845
  enum enum_date_cmp_type cmp_type;
 
846
  uint64_t const_value= (uint64_t)-1;
867
847
  a= a1;
868
848
  b= a2;
869
849
 
870
850
  if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
871
851
  {
 
852
    session= current_session;
872
853
    owner= owner_arg;
873
854
    a_type= (*a)->field_type();
874
855
    b_type= (*b)->field_type();
875
856
    a_cache= 0;
876
857
    b_cache= 0;
877
858
 
878
 
    if (const_value != -1)
 
859
    if (const_value != (uint64_t)-1)
879
860
    {
880
861
      Item_cache_int *cache= new Item_cache_int();
881
862
      /* Mark the cache as non-const to prevent re-caching. */
896
877
    is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC);
897
878
    func= &Arg_comparator::compare_datetime;
898
879
    get_value_func= &get_datetime_value;
899
 
 
900
880
    return 0;
901
881
  }
902
882
 
906
886
 
907
887
void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
908
888
{
 
889
  session= current_session;
909
890
  /* A caller will handle null values by itself. */
910
891
  owner= NULL;
911
892
  a= a1;
949
930
    obtained value
950
931
*/
951
932
 
952
 
int64_t
 
933
uint64_t
953
934
get_datetime_value(Session *session, Item ***item_arg, Item **cache_arg,
954
935
                   Item *warn_item, bool *is_null)
955
936
{
956
 
  int64_t value= 0;
 
937
  uint64_t value= 0;
957
938
  String buf, *str= 0;
958
939
  Item *item= **item_arg;
959
940
 
977
958
    str= item->val_str(&buf);
978
959
    *is_null= item->null_value;
979
960
  }
980
 
 
981
961
  if (*is_null)
982
962
    return ~(uint64_t) 0;
983
 
 
984
963
  /*
985
964
    Convert strings to the integer DATE/DATETIME representation.
986
965
    Even if both dates provided in strings we can't compare them directly as
991
970
  {
992
971
    bool error;
993
972
    enum_field_types f_type= warn_item->field_type();
994
 
    type::timestamp_t t_type= f_type == DRIZZLE_TYPE_DATE ? type::DRIZZLE_TIMESTAMP_DATE : type::DRIZZLE_TIMESTAMP_DATETIME;
 
973
    enum enum_drizzle_timestamp_type t_type= f_type ==
 
974
      DRIZZLE_TYPE_DATE ? DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME;
995
975
    value= get_date_from_str(session, str, t_type, warn_item->name, &error);
996
976
    /*
997
977
      If str did not contain a valid date according to the current
1014
994
    *cache_arg= cache;
1015
995
    *item_arg= cache_arg;
1016
996
  }
1017
 
 
1018
997
  return value;
1019
998
}
1020
999
 
1068
1047
  /* Compare values. */
1069
1048
  if (is_nulls_eq)
1070
1049
    return (a_value == b_value);
1071
 
  return (a_value < b_value) ? -1 : ((a_value > b_value) ? 1 : 0);
 
1050
  return a_value < b_value ? -1 : (a_value > b_value ? 1 : 0);
1072
1051
}
1073
1052
 
1074
1053
 
1171
1150
 
1172
1151
int Arg_comparator::compare_decimal()
1173
1152
{
1174
 
  type::Decimal value1;
1175
 
  type::Decimal *val1= (*a)->val_decimal(&value1);
 
1153
  my_decimal value1;
 
1154
  my_decimal *val1= (*a)->val_decimal(&value1);
1176
1155
  if (!(*a)->null_value)
1177
1156
  {
1178
 
    type::Decimal value2;
1179
 
    type::Decimal *val2= (*b)->val_decimal(&value2);
 
1157
    my_decimal value2;
 
1158
    my_decimal *val2= (*b)->val_decimal(&value2);
1180
1159
    if (!(*b)->null_value)
1181
1160
    {
1182
1161
      owner->null_value= 0;
1183
 
      return class_decimal_cmp(val1, val2);
 
1162
      return my_decimal_cmp(val1, val2);
1184
1163
    }
1185
1164
  }
1186
1165
  owner->null_value= 1;
1198
1177
 
1199
1178
int Arg_comparator::compare_e_decimal()
1200
1179
{
1201
 
  type::Decimal value1, value2;
1202
 
  type::Decimal *val1= (*a)->val_decimal(&value1);
1203
 
  type::Decimal *val2= (*b)->val_decimal(&value2);
 
1180
  my_decimal value1, value2;
 
1181
  my_decimal *val1= (*a)->val_decimal(&value1);
 
1182
  my_decimal *val2= (*b)->val_decimal(&value2);
1204
1183
  if ((*a)->null_value || (*b)->null_value)
1205
1184
    return test((*a)->null_value && (*b)->null_value);
1206
 
  return test(class_decimal_cmp(val1, val2) == 0);
 
1185
  return test(my_decimal_cmp(val1, val2) == 0);
1207
1186
}
1208
1187
 
1209
1188
 
1613
1592
 
1614
1593
void Item_in_optimizer::cleanup()
1615
1594
{
1616
 
  item::function::Boolean::cleanup();
 
1595
  Item_bool_func::cleanup();
1617
1596
  if (!save_cache)
1618
1597
    cache= 0;
1619
1598
  return;
1666
1645
    change records at each execution.
1667
1646
  */
1668
1647
  if ((*args) != new_item)
1669
 
    getSession().change_item_tree(args, new_item);
 
1648
    current_session->change_item_tree(args, new_item);
1670
1649
 
1671
1650
  /*
1672
1651
    Transform the right IN operand which should be an Item_in_subselect or a
1808
1787
 
1809
1788
    if (not_null_consts &&
1810
1789
        (intervals=
1811
 
          (interval_range*) memory::sql_alloc(sizeof(interval_range) * (rows - 1))))
 
1790
          (interval_range*) sql_alloc(sizeof(interval_range) * (rows - 1))))
1812
1791
    {
1813
1792
      if (use_decimal_comparison)
1814
1793
      {
1821
1800
          {
1822
1801
            range->type= DECIMAL_RESULT;
1823
1802
            range->dec.init();
1824
 
            type::Decimal *dec= el->val_decimal(&range->dec);
 
1803
            my_decimal *dec= el->val_decimal(&range->dec);
1825
1804
            if (dec != &range->dec)
1826
1805
            {
1827
1806
              range->dec= *dec;
1871
1850
{
1872
1851
  assert(fixed == 1);
1873
1852
  double value;
1874
 
  type::Decimal dec_buf, *dec= NULL;
 
1853
  my_decimal dec_buf, *dec= NULL;
1875
1854
  uint32_t i;
1876
1855
 
1877
1856
  if (use_decimal_comparison)
1879
1858
    dec= row->element_index(0)->val_decimal(&dec_buf);
1880
1859
    if (row->element_index(0)->null_value)
1881
1860
      return -1;
1882
 
    class_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
 
1861
    my_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
1883
1862
  }
1884
1863
  else
1885
1864
  {
1904
1883
        and we are comparing against a decimal
1905
1884
      */
1906
1885
      if (dec && range->type == DECIMAL_RESULT)
1907
 
        cmp_result= class_decimal_cmp(&range->dec, dec) <= 0;
 
1886
        cmp_result= my_decimal_cmp(&range->dec, dec) <= 0;
1908
1887
      else
1909
1888
        cmp_result= (range->dbl <= value);
1910
1889
      if (cmp_result)
1914
1893
    }
1915
1894
    interval_range *range= intervals+start;
1916
1895
    return ((dec && range->type == DECIMAL_RESULT) ?
1917
 
            class_decimal_cmp(dec, &range->dec) < 0 :
 
1896
            my_decimal_cmp(dec, &range->dec) < 0 :
1918
1897
            value < range->dbl) ? 0 : start + 1;
1919
1898
  }
1920
1899
 
1925
1904
        ((el->result_type() == DECIMAL_RESULT) ||
1926
1905
         (el->result_type() == INT_RESULT)))
1927
1906
    {
1928
 
      type::Decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
 
1907
      my_decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
1929
1908
      /* Skip NULL ranges. */
1930
1909
      if (el->null_value)
1931
1910
        continue;
1932
 
      if (class_decimal_cmp(e_dec, dec) > 0)
 
1911
      if (my_decimal_cmp(e_dec, dec) > 0)
1933
1912
        return i - 1;
1934
1913
    }
1935
1914
    else
2000
1979
  int i;
2001
1980
  bool datetime_found= false;
2002
1981
  compare_as_dates= true;
 
1982
  Session *session= current_session;
2003
1983
 
2004
1984
  /*
2005
1985
    As some compare functions are generated after sql_yacc,
2037
2017
    ge_cmp.set_datetime_cmp_func(args, args + 1);
2038
2018
    le_cmp.set_datetime_cmp_func(args, args + 2);
2039
2019
  }
2040
 
  else if (args[0]->real_item()->type() == FIELD_ITEM)
 
2020
  else if (args[0]->real_item()->type() == FIELD_ITEM &&
 
2021
           session->lex->sql_command != SQLCOM_SHOW_CREATE)
2041
2022
  {
2042
2023
    Item_field *field_item= (Item_field*) (args[0]->real_item());
2043
2024
    if (field_item->field->can_be_compared_as_int64_t())
2046
2027
        The following can't be recoded with || as convert_constant_item
2047
2028
        changes the argument
2048
2029
      */
2049
 
      if (convert_constant_item(&getSession(), field_item, &args[1]))
 
2030
      if (convert_constant_item(session, field_item, &args[1]))
2050
2031
        cmp_type=INT_RESULT;                    // Works for all types.
2051
 
      if (convert_constant_item(&getSession(), field_item, &args[2]))
 
2032
      if (convert_constant_item(session, field_item, &args[2]))
2052
2033
        cmp_type=INT_RESULT;                    // Works for all types.
2053
2034
    }
2054
2035
  }
2107
2088
  {
2108
2089
    int64_t value=args[0]->val_int(), a, b;
2109
2090
    if ((null_value=args[0]->null_value))
2110
 
      return 0;
 
2091
      return 0;                                 /* purecov: inspected */
2111
2092
    a=args[1]->val_int();
2112
2093
    b=args[2]->val_int();
2113
2094
    if (!args[1]->null_value && !args[2]->null_value)
2125
2106
  }
2126
2107
  else if (cmp_type == DECIMAL_RESULT)
2127
2108
  {
2128
 
    type::Decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
 
2109
    my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2129
2110
               a_buf, *a_dec, b_buf, *b_dec;
2130
2111
    if ((null_value=args[0]->null_value))
2131
 
      return 0;
 
2112
      return 0;                                 /* purecov: inspected */
2132
2113
    a_dec= args[1]->val_decimal(&a_buf);
2133
2114
    b_dec= args[2]->val_decimal(&b_buf);
2134
2115
    if (!args[1]->null_value && !args[2]->null_value)
2135
 
      return (int64_t) ((class_decimal_cmp(dec, a_dec) >= 0 &&
2136
 
                          class_decimal_cmp(dec, b_dec) <= 0) != negated);
 
2116
      return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
 
2117
                          my_decimal_cmp(dec, b_dec) <= 0) != negated);
2137
2118
    if (args[1]->null_value && args[2]->null_value)
2138
2119
      null_value=1;
2139
2120
    else if (args[1]->null_value)
2140
 
      null_value= (class_decimal_cmp(dec, b_dec) <= 0);
 
2121
      null_value= (my_decimal_cmp(dec, b_dec) <= 0);
2141
2122
    else
2142
 
      null_value= (class_decimal_cmp(dec, a_dec) >= 0);
 
2123
      null_value= (my_decimal_cmp(dec, a_dec) >= 0);
2143
2124
  }
2144
2125
  else
2145
2126
  {
2146
2127
    double value= args[0]->val_real(),a,b;
2147
2128
    if ((null_value=args[0]->null_value))
2148
 
      return 0;
 
2129
      return 0;                                 /* purecov: inspected */
2149
2130
    a= args[1]->val_real();
2150
2131
    b= args[2]->val_real();
2151
2132
    if (!args[1]->null_value && !args[2]->null_value)
2197
2178
    max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2198
2179
  }
2199
2180
  else
2200
 
  {
2201
2181
    max_length= max(args[0]->max_length, args[1]->max_length);
2202
 
  }
2203
2182
 
2204
2183
  switch (hybrid_type)
2205
2184
  {
2206
2185
  case STRING_RESULT:
2207
2186
    agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2208
2187
    break;
2209
 
 
2210
2188
  case DECIMAL_RESULT:
2211
2189
  case REAL_RESULT:
2212
2190
    break;
2213
 
 
2214
2191
  case INT_RESULT:
2215
2192
    decimals= 0;
2216
2193
    break;
2217
 
 
2218
2194
  case ROW_RESULT:
 
2195
  default:
2219
2196
    assert(0);
2220
2197
  }
2221
 
 
2222
2198
  cached_field_type= agg_field_type(args, 2);
2223
2199
}
2224
2200
 
2273
2249
}
2274
2250
 
2275
2251
 
2276
 
type::Decimal *Item_func_ifnull::decimal_op(type::Decimal *decimal_value)
 
2252
my_decimal *Item_func_ifnull::decimal_op(my_decimal *decimal_value)
2277
2253
{
2278
2254
  assert(fixed == 1);
2279
 
  type::Decimal *value= args[0]->val_decimal(decimal_value);
 
2255
  my_decimal *value= args[0]->val_decimal(decimal_value);
2280
2256
  if (!args[0]->null_value)
2281
2257
  {
2282
2258
    null_value= 0;
2446
2422
}
2447
2423
 
2448
2424
 
2449
 
type::Decimal *
2450
 
Item_func_if::val_decimal(type::Decimal *decimal_value)
 
2425
my_decimal *
 
2426
Item_func_if::val_decimal(my_decimal *decimal_value)
2451
2427
{
2452
2428
  assert(fixed == 1);
2453
2429
  Item *arg= args[0]->val_bool() ? args[1] : args[2];
2454
 
  type::Decimal *value= arg->val_decimal(decimal_value);
 
2430
  my_decimal *value= arg->val_decimal(decimal_value);
2455
2431
  null_value= arg->null_value;
2456
2432
  return value;
2457
2433
}
2531
2507
}
2532
2508
 
2533
2509
 
2534
 
type::Decimal *
2535
 
Item_func_nullif::val_decimal(type::Decimal * decimal_value)
 
2510
my_decimal *
 
2511
Item_func_nullif::val_decimal(my_decimal * decimal_value)
2536
2512
{
2537
2513
  assert(fixed == 1);
2538
 
  type::Decimal *res;
 
2514
  my_decimal *res;
2539
2515
  if (!cmp.compare())
2540
2516
  {
2541
2517
    null_value=1;
2668
2644
}
2669
2645
 
2670
2646
 
2671
 
type::Decimal *Item_func_case::val_decimal(type::Decimal *decimal_value)
 
2647
my_decimal *Item_func_case::val_decimal(my_decimal *decimal_value)
2672
2648
{
2673
2649
  assert(fixed == 1);
2674
2650
  char buff[MAX_FIELD_WIDTH];
2675
2651
  String dummy_str(buff, sizeof(buff), default_charset());
2676
2652
  Item *item= find_item(&dummy_str);
2677
 
  type::Decimal *res;
 
2653
  my_decimal *res;
2678
2654
 
2679
2655
  if (!item)
2680
2656
  {
2717
2693
 
2718
2694
void Item_func_case::agg_num_lengths(Item *arg)
2719
2695
{
2720
 
  uint32_t len= class_decimal_length_to_precision(arg->max_length, arg->decimals,
 
2696
  uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2721
2697
                                           arg->unsigned_flag) - arg->decimals;
2722
2698
  set_if_bigger(max_length, len);
2723
2699
  set_if_bigger(decimals, arg->decimals);
2730
2706
  Item **agg;
2731
2707
  uint32_t nagg;
2732
2708
  uint32_t found_types= 0;
2733
 
  if (!(agg= (Item**) memory::sql_alloc(sizeof(Item*)*(ncases+1))))
 
2709
  if (!(agg= (Item**) sql_alloc(sizeof(Item*)*(ncases+1))))
2734
2710
    return;
2735
2711
 
2736
2712
  /*
2756
2732
  */
2757
2733
  if (first_expr_num != -1)
2758
2734
  {
 
2735
    uint32_t i;
2759
2736
    agg[0]= args[first_expr_num];
2760
2737
    left_result_type= agg[0]->result_type();
2761
2738
 
2765
2742
    if (!(found_types= collect_cmp_types(agg, nagg)))
2766
2743
      return;
2767
2744
 
2768
 
    for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
 
2745
    for (i= 0; i <= (uint32_t)DECIMAL_RESULT; i++)
2769
2746
    {
2770
2747
      if (found_types & (1 << i) && !cmp_items[i])
2771
2748
      {
2800
2777
      agg_num_lengths(args[i + 1]);
2801
2778
    if (else_expr_num != -1)
2802
2779
      agg_num_lengths(args[else_expr_num]);
2803
 
    max_length= class_decimal_precision_to_length(max_length + decimals, decimals,
 
2780
    max_length= my_decimal_precision_to_length(max_length + decimals, decimals,
2804
2781
                                               unsigned_flag);
2805
2782
  }
2806
2783
}
2851
2828
 
2852
2829
void Item_func_case::cleanup()
2853
2830
{
 
2831
  uint32_t i;
2854
2832
  Item_func::cleanup();
2855
 
  for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
 
2833
  for (i= 0; i <= (uint32_t)DECIMAL_RESULT; i++)
2856
2834
  {
2857
2835
    delete cmp_items[i];
2858
2836
    cmp_items[i]= 0;
2859
2837
  }
 
2838
  return;
2860
2839
}
2861
2840
 
2862
2841
 
2907
2886
}
2908
2887
 
2909
2888
 
2910
 
type::Decimal *Item_func_coalesce::decimal_op(type::Decimal *decimal_value)
 
2889
my_decimal *Item_func_coalesce::decimal_op(my_decimal *decimal_value)
2911
2890
{
2912
2891
  assert(fixed == 1);
2913
2892
  null_value= 0;
2914
2893
  for (uint32_t i= 0; i < arg_count; i++)
2915
2894
  {
2916
 
    type::Decimal *res= args[i]->val_decimal(decimal_value);
 
2895
    my_decimal *res= args[i]->val_decimal(decimal_value);
2917
2896
    if (!args[i]->null_value)
2918
2897
      return res;
2919
2898
  }
2926
2905
{
2927
2906
  cached_field_type= agg_field_type(args, arg_count);
2928
2907
  agg_result_type(&hybrid_type, args, arg_count);
2929
 
 
2930
2908
  switch (hybrid_type) {
2931
2909
  case STRING_RESULT:
2932
2910
    count_only_length();
2933
2911
    decimals= NOT_FIXED_DEC;
2934
2912
    agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1);
2935
2913
    break;
2936
 
 
2937
2914
  case DECIMAL_RESULT:
2938
2915
    count_decimal_length();
2939
2916
    break;
2940
 
 
2941
2917
  case REAL_RESULT:
2942
2918
    count_real_length();
2943
2919
    break;
2944
 
 
2945
2920
  case INT_RESULT:
2946
2921
    count_only_length();
2947
2922
    decimals= 0;
2948
2923
    break;
2949
 
 
2950
2924
  case ROW_RESULT:
 
2925
  default:
2951
2926
    assert(0);
2952
2927
  }
2953
2928
}
3062
3037
}
3063
3038
 
3064
3039
 
3065
 
static int cmp_decimal(void *, type::Decimal *a, type::Decimal *b)
 
3040
static int cmp_decimal(void *, my_decimal *a, my_decimal *b)
3066
3041
{
3067
3042
  /*
3068
3043
    We need call of fixing buffer pointer, because fast sort just copy
3070
3045
  */
3071
3046
  a->fix_buffer_pointer();
3072
3047
  b->fix_buffer_pointer();
3073
 
  return class_decimal_cmp(a, b);
3074
 
}
3075
 
 
3076
 
 
3077
 
void in_vector::sort()
3078
 
{
3079
 
  internal::my_qsort2(base,used_count,size,compare, (void *) collation);
 
3048
  return my_decimal_cmp(a, b);
3080
3049
}
3081
3050
 
3082
3051
 
3111
3080
{
3112
3081
  if (base)
3113
3082
  {
3114
 
    // base was allocated with help of memory::sql_alloc => following is OK
 
3083
    // base was allocated with help of sql_alloc => following is OK
3115
3084
    for (uint32_t i=0 ; i < count ; i++)
3116
3085
      ((String*) base)[i].free();
3117
3086
  }
3178
3147
  return;
3179
3148
}
3180
3149
 
3181
 
in_int64_t::in_int64_t(uint32_t elements) :
3182
 
  in_vector(elements, sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
 
3150
in_int64_t::in_int64_t(uint32_t elements)
 
3151
  :in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3183
3152
{}
3184
3153
 
3185
3154
void in_int64_t::set(uint32_t pos,Item *item)
3199
3168
  return (unsigned char*) &tmp;
3200
3169
}
3201
3170
 
3202
 
void in_datetime::set(uint32_t pos, Item *item)
 
3171
void in_datetime::set(uint32_t pos,Item *item)
3203
3172
{
3204
3173
  Item **tmp_item= &item;
3205
3174
  bool is_null;
3233
3202
{
3234
3203
  tmp= item->val_real();
3235
3204
  if (item->null_value)
3236
 
    return 0;
 
3205
    return 0;                                   /* purecov: inspected */
3237
3206
  return (unsigned char*) &tmp;
3238
3207
}
3239
3208
 
3240
3209
 
3241
3210
in_decimal::in_decimal(uint32_t elements)
3242
 
  :in_vector(elements, sizeof(type::Decimal),(qsort2_cmp) cmp_decimal, 0)
 
3211
  :in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3243
3212
{}
3244
3213
 
3245
3214
 
3246
3215
void in_decimal::set(uint32_t pos, Item *item)
3247
3216
{
3248
 
  /* as far as 'item' is constant, we can store reference on type::Decimal */
3249
 
  type::Decimal *dec= ((type::Decimal *)base) + pos;
 
3217
  /* as far as 'item' is constant, we can store reference on my_decimal */
 
3218
  my_decimal *dec= ((my_decimal *)base) + pos;
3250
3219
  dec->len= DECIMAL_BUFF_LENGTH;
3251
3220
  dec->fix_buffer_pointer();
3252
 
  type::Decimal *res= item->val_decimal(dec);
 
3221
  my_decimal *res= item->val_decimal(dec);
3253
3222
  /* if item->val_decimal() is evaluated to NULL then res == 0 */
3254
3223
  if (!item->null_value && res != dec)
3255
 
    class_decimal2decimal(res, dec);
 
3224
    my_decimal2decimal(res, dec);
3256
3225
}
3257
3226
 
3258
3227
 
3259
3228
unsigned char *in_decimal::get_value(Item *item)
3260
3229
{
3261
 
  type::Decimal *result= item->val_decimal(&val);
 
3230
  my_decimal *result= item->val_decimal(&val);
3262
3231
  if (item->null_value)
3263
3232
    return 0;
3264
3233
  return (unsigned char *)result;
3271
3240
  switch (type) {
3272
3241
  case STRING_RESULT:
3273
3242
    return new cmp_item_sort_string(cs);
3274
 
 
3275
3243
  case INT_RESULT:
3276
3244
    return new cmp_item_int;
3277
 
 
3278
3245
  case REAL_RESULT:
3279
3246
    return new cmp_item_real;
3280
 
 
3281
3247
  case ROW_RESULT:
3282
3248
    return new cmp_item_row;
3283
 
 
3284
3249
  case DECIMAL_RESULT:
3285
3250
    return new cmp_item_decimal;
 
3251
  default:
 
3252
    assert(0);
 
3253
    break;
3286
3254
  }
3287
 
 
3288
3255
  return 0; // to satisfy compiler :)
3289
3256
}
3290
3257
 
3363
3330
    return;
3364
3331
  }
3365
3332
  n= tmpl->n;
3366
 
  if ((comparators= (cmp_item **) memory::sql_alloc(sizeof(cmp_item *)*n)))
 
3333
  if ((comparators= (cmp_item **) sql_alloc(sizeof(cmp_item *)*n)))
3367
3334
  {
3368
3335
    item->bring_value();
3369
3336
    item->null_value= 0;
3417
3384
 
3418
3385
void cmp_item_decimal::store_value(Item *item)
3419
3386
{
3420
 
  type::Decimal *val= item->val_decimal(&value);
 
3387
  my_decimal *val= item->val_decimal(&value);
3421
3388
  /* val may be zero if item is nnull */
3422
3389
  if (val && val != &value)
3423
 
    class_decimal2decimal(val, &value);
 
3390
    my_decimal2decimal(val, &value);
3424
3391
}
3425
3392
 
3426
3393
 
3427
3394
int cmp_item_decimal::cmp(Item *arg)
3428
3395
{
3429
 
  type::Decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
 
3396
  my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3430
3397
  if (arg->null_value)
3431
3398
    return 1;
3432
 
  return class_decimal_cmp(&value, tmp);
 
3399
  return my_decimal_cmp(&value, tmp);
3433
3400
}
3434
3401
 
3435
3402
 
3436
3403
int cmp_item_decimal::compare(cmp_item *arg)
3437
3404
{
3438
3405
  cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3439
 
  return class_decimal_cmp(&value, &l_cmp->value);
 
3406
  return my_decimal_cmp(&value, &l_cmp->value);
3440
3407
}
3441
3408
 
3442
3409
 
3549
3516
{
3550
3517
  Item **arg, **arg_end;
3551
3518
  bool const_itm= 1;
 
3519
  Session *session= current_session;
3552
3520
  bool datetime_found= false;
3553
3521
  /* true <=> arguments values will be compared as DATETIMEs. */
3554
3522
  bool compare_as_datetime= false;
3555
3523
  Item *date_arg= 0;
3556
3524
  uint32_t found_types= 0;
3557
 
  uint32_t type_cnt= 0;
 
3525
  uint32_t type_cnt= 0, i;
3558
3526
  Item_result cmp_type= STRING_RESULT;
3559
3527
  left_result_type= args[0]->result_type();
3560
 
  if (!(found_types= collect_cmp_types(args, arg_count, true)))
 
3528
  if (!(found_types= collect_cmp_types(args, arg_count)))
3561
3529
    return;
3562
3530
 
3563
3531
  for (arg= args + 1, arg_end= args + arg_count; arg != arg_end ; arg++)
3568
3536
      break;
3569
3537
    }
3570
3538
  }
3571
 
  for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
 
3539
  for (i= 0; i <= (uint32_t)DECIMAL_RESULT; i++)
3572
3540
  {
3573
3541
    if (found_types & 1 << i)
3574
3542
    {
3675
3643
  if (type_cnt == 1 && const_itm && !nulls_in_row())
3676
3644
  {
3677
3645
    if (compare_as_datetime)
3678
 
    {
3679
3646
      array= new in_datetime(date_arg, arg_count - 1);
3680
 
    }
3681
3647
    else
3682
3648
    {
3683
3649
      /*
3688
3654
        comparison type accordingly.
3689
3655
      */
3690
3656
      if (args[0]->real_item()->type() == FIELD_ITEM &&
 
3657
          session->lex->sql_command != SQLCOM_SHOW_CREATE &&
3691
3658
          cmp_type != INT_RESULT)
3692
3659
      {
3693
3660
        Item_field *field_item= (Item_field*) (args[0]->real_item());
3696
3663
          bool all_converted= true;
3697
3664
          for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3698
3665
          {
3699
 
            if (!convert_constant_item (&getSession(), field_item, &arg[0]))
 
3666
            if (!convert_constant_item (session, field_item, &arg[0]))
3700
3667
              all_converted= false;
3701
3668
          }
3702
3669
          if (all_converted)
3703
3670
            cmp_type= INT_RESULT;
3704
3671
        }
3705
3672
      }
3706
 
 
3707
3673
      switch (cmp_type) {
3708
3674
      case STRING_RESULT:
3709
3675
        array=new in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
3710
3676
                            cmp_collation.collation);
3711
3677
        break;
3712
 
 
3713
3678
      case INT_RESULT:
3714
3679
        array= new in_int64_t(arg_count-1);
3715
3680
        break;
3716
 
 
3717
3681
      case REAL_RESULT:
3718
3682
        array= new in_double(arg_count-1);
3719
3683
        break;
3720
 
 
3721
3684
      case ROW_RESULT:
3722
3685
        /*
3723
3686
          The row comparator was created at the beginning but only DATETIME
3726
3689
        */
3727
3690
        ((in_row*)array)->tmp.store_value(args[0]);
3728
3691
        break;
3729
 
 
3730
3692
      case DECIMAL_RESULT:
3731
3693
        array= new in_decimal(arg_count - 1);
3732
3694
        break;
 
3695
      default:
 
3696
        assert(0);
 
3697
        return;
3733
3698
      }
3734
3699
    }
3735
 
 
3736
 
    if (array && !(getSession().is_fatal_error))                // If not EOM
 
3700
    if (array && !(session->is_fatal_error))            // If not EOM
3737
3701
    {
3738
3702
      uint32_t j=0;
3739
3703
      for (uint32_t arg_num=1 ; arg_num < arg_count ; arg_num++)
3740
3704
      {
3741
 
        if (!args[arg_num]->null_value)                 // Skip NULL values
3742
 
        {
3743
 
          array->set(j,args[arg_num]);
3744
 
          j++;
3745
 
        }
3746
 
        else
3747
 
          have_null= 1;
 
3705
        array->set(j,args[arg_num]);
 
3706
        if (!args[arg_num]->null_value)                 // Skip NULL values
 
3707
          j++;
 
3708
        else
 
3709
          have_null= 1;
3748
3710
      }
3749
3711
      if ((array->used_count= j))
3750
 
        array->sort();
 
3712
        array->sort();
3751
3713
    }
3752
3714
  }
3753
3715
  else
3756
3718
      cmp_items[STRING_RESULT]= new cmp_item_datetime(date_arg);
3757
3719
    else
3758
3720
    {
3759
 
      for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
 
3721
      for (i= 0; i <= (uint32_t) DECIMAL_RESULT; i++)
3760
3722
      {
3761
3723
        if (found_types & (1 << i) && !cmp_items[i])
3762
3724
        {
3849
3811
 
3850
3812
 
3851
3813
Item_cond::Item_cond(Session *session, Item_cond *item)
3852
 
  :item::function::Boolean(session, item),
 
3814
  :Item_bool_func(session, item),
3853
3815
   abort_on_null(item->abort_on_null),
3854
3816
   and_tables_cache(item->and_tables_cache)
3855
3817
{
3876
3838
  void *orig_session_marker= session->session_marker;
3877
3839
  unsigned char buff[sizeof(char*)];                    // Max local vars in function
3878
3840
  not_null_tables_cache= used_tables_cache= 0;
3879
 
  const_item_cache= true;
 
3841
  const_item_cache= 1;
3880
3842
 
3881
3843
  if (functype() == COND_OR_FUNC)
3882
3844
    session->session_marker= 0;
3921
3883
    if ((!item->fixed &&
3922
3884
         item->fix_fields(session, li.ref())) ||
3923
3885
        (item= *li.ref())->check_cols(1))
3924
 
      return true;
 
3886
      return true; /* purecov: inspected */
3925
3887
    used_tables_cache|=     item->used_tables();
3926
3888
    if (item->const_item())
3927
3889
      and_tables_cache= (table_map) 0;
3951
3913
  Item *item;
3952
3914
 
3953
3915
  used_tables_cache=0;
3954
 
  const_item_cache= true;
 
3916
  const_item_cache=1;
3955
3917
 
3956
3918
  and_tables_cache= ~(table_map) 0; // Here and below we do as fix_fields does
3957
3919
  not_null_tables_cache= 0;
4023
3985
      change records at each execution.
4024
3986
    */
4025
3987
    if (new_item != item)
4026
 
      getSession().change_item_tree(li.ref(), new_item);
 
3988
      current_session->change_item_tree(li.ref(), new_item);
4027
3989
  }
4028
3990
  return Item_func::transform(transformer, arg);
4029
3991
}
4140
4102
  Item *item;
4141
4103
 
4142
4104
  used_tables_cache=0;
4143
 
  const_item_cache= true;
 
4105
  const_item_cache=1;
4144
4106
  while ((item=li++))
4145
4107
  {
4146
4108
    item->update_used_tables();
4372
4334
                    res->ptr(),res->ptr()+res->length(),
4373
4335
                    res2->ptr(),res2->ptr()+res2->length(),
4374
4336
                    make_escape_code(cmp.cmp_collation.collation, escape),
4375
 
                    internal::wild_one,internal::wild_many) ? 0 : 1;
 
4337
                    wild_one,wild_many) ? 0 : 1;
4376
4338
}
4377
4339
 
4378
4340
 
4389
4351
    if (!res2)
4390
4352
      return OPTIMIZE_NONE;
4391
4353
 
4392
 
    if (*res2->ptr() != internal::wild_many)
 
4354
    if (*res2->ptr() != wild_many)
4393
4355
    {
4394
 
      if (args[0]->result_type() != STRING_RESULT || *res2->ptr() != internal::wild_one)
 
4356
      if (args[0]->result_type() != STRING_RESULT || *res2->ptr() != wild_one)
4395
4357
        return OPTIMIZE_OP;
4396
4358
    }
4397
4359
  }
4419
4381
    String *escape_str= escape_item->val_str(&tmp_value1);
4420
4382
    if (escape_str)
4421
4383
    {
4422
 
      escape= (char *)memory::sql_alloc(escape_str->length());
 
4384
      escape= (char *)sql_alloc(escape_str->length());
4423
4385
      strcpy(escape, escape_str->ptr()); 
4424
4386
    }
4425
4387
    else
4426
4388
    {
4427
 
      escape= (char *)memory::sql_alloc(1);
 
4389
      escape= (char *)sql_alloc(1);
4428
4390
      strcpy(escape, "\\");
4429
4391
    } 
4430
4392
   
4447
4409
      */
4448
4410
 
4449
4411
      if (len > MIN_TURBOBM_PATTERN_LEN + 2 &&
4450
 
          *first == internal::wild_many &&
4451
 
          *last  == internal::wild_many)
 
4412
          *first == wild_many &&
 
4413
          *last  == wild_many)
4452
4414
      {
4453
4415
        const char* tmp = first + 1;
4454
 
        for (; *tmp != internal::wild_many && *tmp != internal::wild_one; tmp++)
 
4416
        for (; *tmp != wild_many && *tmp != wild_one; tmp++)
4455
4417
        {
4456
4418
          if (escape == tmp)
4457
4419
            break;
4463
4425
      {
4464
4426
        pattern     = first + 1;
4465
4427
        pattern_len = (int) len - 2;
4466
 
        int *suff = (int*) session->getMemRoot()->allocate((int) (sizeof(int)*
4467
 
                                                                  ((pattern_len + 1)*2+
4468
 
                                                                   alphabet_size)));
 
4428
        int *suff = (int*) session->alloc((int) (sizeof(int)*
 
4429
                                      ((pattern_len + 1)*2+
 
4430
                                      alphabet_size)));
4469
4431
        bmGs      = suff + pattern_len + 1;
4470
4432
        bmBc      = bmGs + pattern_len + 1;
4471
4433
        turboBM_compute_good_suffix_shifts(suff);
4482
4444
  Item_bool_func2::cleanup();
4483
4445
}
4484
4446
 
4485
 
static unsigned char likeconv(const CHARSET_INFO *cs, unsigned char a)
4486
 
{
4487
4447
#ifdef LIKE_CMP_TOUPPER
4488
 
  return cs->toupper(a);
 
4448
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
4489
4449
#else
4490
 
  return cs->sort_order[a];
 
4450
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
4491
4451
#endif
4492
 
}
 
4452
 
4493
4453
 
4494
4454
/**
4495
4455
  Precomputation dependent only on pattern_len.
4884
4844
}
4885
4845
 
4886
4846
Item_equal::Item_equal(Item_field *f1, Item_field *f2)
4887
 
  : item::function::Boolean(), const_item(0), eval_item(0), cond_false(0)
 
4847
  : Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
4888
4848
{
4889
 
  const_item_cache= false;
 
4849
  const_item_cache= 0;
4890
4850
  fields.push_back(f1);
4891
4851
  fields.push_back(f2);
4892
4852
}
4893
4853
 
4894
4854
Item_equal::Item_equal(Item *c, Item_field *f)
4895
 
  : item::function::Boolean(), eval_item(0), cond_false(0)
 
4855
  : Item_bool_func(), eval_item(0), cond_false(0)
4896
4856
{
4897
 
  const_item_cache= false;
 
4857
  const_item_cache= 0;
4898
4858
  fields.push_back(f);
4899
4859
  const_item= c;
4900
4860
}
4901
4861
 
4902
4862
 
4903
4863
Item_equal::Item_equal(Item_equal *item_equal)
4904
 
  : item::function::Boolean(), eval_item(0), cond_false(0)
 
4864
  : Item_bool_func(), eval_item(0), cond_false(0)
4905
4865
{
4906
 
  const_item_cache= false;
 
4866
  const_item_cache= 0;
4907
4867
  List_iterator_fast<Item_field> li(item_equal->fields);
4908
4868
  Item_field *item;
4909
4869
  while ((item= li++))
4927
4887
  func->set_cmp_func();
4928
4888
  func->quick_fix_field();
4929
4889
  if ((cond_false= !func->val_int()))
4930
 
    const_item_cache= true;
 
4890
    const_item_cache= 1;
4931
4891
}
4932
4892
 
4933
4893
void Item_equal::add(Item_field *f)
5073
5033
  List_iterator_fast<Item_field> li(fields);
5074
5034
  Item *item;
5075
5035
  not_null_tables_cache= used_tables_cache= 0;
5076
 
  const_item_cache= false;
 
5036
  const_item_cache= 0;
5077
5037
  while ((item= li++))
5078
5038
  {
5079
5039
    table_map tmp_table_map;
5110
5070
    return 0;
5111
5071
  List_iterator_fast<Item_field> it(fields);
5112
5072
  Item *item= const_item ? const_item : it++;
 
5073
  if ((null_value= item->null_value))
 
5074
    return 0;
5113
5075
  eval_item->store_value(item);
5114
 
  if ((null_value= item->null_value))
5115
 
    return 0;
5116
5076
  while ((item_field= it++))
5117
5077
  {
5118
5078
    /* Skip fields of non-const tables. They haven't been read yet */
5119
 
    if (item_field->field->getTable()->const_table)
 
5079
    if (item_field->field->table->const_table)
5120
5080
    {
5121
 
      if (eval_item->cmp(item_field) || (null_value= item_field->null_value))
 
5081
      if ((null_value= item_field->null_value) || eval_item->cmp(item_field))
5122
5082
        return 0;
5123
5083
    }
5124
5084
  }
5161
5121
      change records at each execution.
5162
5122
    */
5163
5123
    if (new_item != item)
5164
 
      getSession().change_item_tree((Item **) it.ref(), new_item);
 
5124
      current_session->change_item_tree((Item **) it.ref(), new_item);
5165
5125
  }
5166
5126
  return Item_func::transform(transformer, arg);
5167
5127
}
5188
5148
  str->append(')');
5189
5149
}
5190
5150
 
5191
 
} /* namespace drizzled */