~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/cmpfunc.cc

  • Committer: Brian Aker
  • Date: 2010-12-18 10:14:05 UTC
  • mfrom: (2008.1.3 clean)
  • Revision ID: brian@tangent.org-20101218101405-qjbse29shi9coklg
Merge of user identifier work

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
  This file defines all compare functions
22
22
*/
23
23
 
24
 
#include <config.h>
25
 
 
26
 
#include <drizzled/cached_item.h>
27
 
#include <drizzled/check_stack_overrun.h>
28
 
#include <drizzled/current_session.h>
29
 
#include <drizzled/error.h>
30
 
#include <drizzled/internal/my_sys.h>
31
 
#include <drizzled/item/cache_int.h>
32
 
#include <drizzled/item/cmpfunc.h>
33
 
#include <drizzled/item/int_with_ref.h>
34
 
#include <drizzled/item/subselect.h>
35
 
#include <drizzled/session.h>
36
 
#include <drizzled/sql_select.h>
37
 
#include <drizzled/temporal.h>
38
 
#include <drizzled/time_functions.h>
39
 
 
 
24
#include "config.h"
 
25
#include "drizzled/sql_select.h"
 
26
#include "drizzled/error.h"
 
27
#include "drizzled/temporal.h"
 
28
#include "drizzled/item/cmpfunc.h"
 
29
#include "drizzled/cached_item.h"
 
30
#include "drizzled/item/cache_int.h"
 
31
#include "drizzled/item/int_with_ref.h"
 
32
#include "drizzled/check_stack_overrun.h"
 
33
#include "drizzled/time_functions.h"
 
34
#include "drizzled/internal/my_sys.h"
40
35
#include <math.h>
41
36
#include <algorithm>
42
37
 
468
463
      the call to save_in_field below overrides that value.
469
464
    */
470
465
    if (field_item->depended_from)
471
 
    {
472
466
      orig_field_val= field->val_int();
473
 
    }
474
 
 
475
467
    if (!(*item)->is_null() && !(*item)->save_in_field(field, 1))
476
468
    {
477
469
      Item *tmp= new Item_int_with_ref(field->val_int(), *item,
480
472
        session->change_item_tree(item, tmp);
481
473
      result= 1;                                        // Item was replaced
482
474
    }
483
 
 
484
475
    /* Restore the original field value. */
485
476
    if (field_item->depended_from)
486
477
    {
487
 
      result= field->store(orig_field_val, field->isUnsigned());
 
478
      result= field->store(orig_field_val, true);
488
479
      /* orig_field_val must be a valid value that can be restored back. */
489
480
      assert(!result);
490
481
    }
498
489
void Item_bool_func2::fix_length_and_dec()
499
490
{
500
491
  max_length= 1;                                     // Function returns 0 or 1
 
492
  Session *session;
501
493
 
502
494
  /*
503
495
    As some compare functions are generated after sql_yacc,
535
527
    return;
536
528
  }
537
529
 
 
530
  session= current_session;
538
531
  Item_field *field_item= NULL;
539
532
 
540
533
  if (args[0]->real_item()->type() == FIELD_ITEM)
543
536
    if (field_item->field->can_be_compared_as_int64_t() &&
544
537
        !(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
545
538
    {
546
 
      if (convert_constant_item(&getSession(), field_item, &args[1]))
 
539
      if (convert_constant_item(session, field_item, &args[1]))
547
540
      {
548
541
        cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
549
542
                         INT_RESULT);           // Works for all types.
559
552
          !(field_item->is_datetime() &&
560
553
            args[0]->result_type() == STRING_RESULT))
561
554
      {
562
 
        if (convert_constant_item(&getSession(), field_item, &args[0]))
 
555
        if (convert_constant_item(session, field_item, &args[0]))
563
556
        {
564
557
          cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
565
558
                           INT_RESULT); // Works for all types.
572
565
  set_cmp_func();
573
566
}
574
567
 
575
 
Arg_comparator::Arg_comparator():
576
 
  session(current_session),
577
 
  a_cache(0),
578
 
  b_cache(0)
579
 
{}
580
 
 
581
 
Arg_comparator::Arg_comparator(Item **a1, Item **a2):
582
 
  a(a1),
583
 
  b(a2),
584
 
  session(current_session),
585
 
  a_cache(0),
586
 
  b_cache(0)
587
 
{}
588
568
 
589
569
int Arg_comparator::set_compare_func(Item_bool_func2 *item, Item_result type)
590
570
{
712
692
    converted value. 0 on error and on zero-dates -- check 'failure'
713
693
*/
714
694
 
715
 
static int64_t
716
 
get_date_from_str(Session *session, String *str, type::timestamp_t warn_type,
 
695
static uint64_t
 
696
get_date_from_str(Session *session, String *str, enum enum_drizzle_timestamp_type warn_type,
717
697
                  char *warn_name, bool *error_arg)
718
698
{
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)
 
699
  uint64_t value= 0;
 
700
  int error;
 
701
  DRIZZLE_TIME l_time;
 
702
  enum enum_drizzle_timestamp_type ret;
 
703
 
 
704
  ret= str_to_datetime(str->ptr(), str->length(), &l_time,
 
705
                       (TIME_FUZZY_DATE | MODE_INVALID_DATES |
 
706
                        (session->variables.sql_mode & MODE_NO_ZERO_DATE)),
 
707
                       &error);
 
708
 
 
709
  if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
729
710
  {
730
711
    /*
731
712
      Do not return yet, we may still want to throw a "trailing garbage"
732
713
      warning.
733
714
    */
734
715
    *error_arg= false;
735
 
    l_time.convert(value);
 
716
    value= TIME_to_uint64_t_datetime(&l_time);
736
717
  }
737
718
  else
738
719
  {
739
720
    *error_arg= true;
740
 
    error= type::CUT;                                   /* force warning */
 
721
    error= 1;                                   /* force warning */
741
722
  }
742
723
 
743
 
  if (error != type::VALID)
 
724
  if (error > 0)
744
725
  {
745
726
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
746
727
                                 str->ptr(), str->length(),
785
766
 
786
767
enum Arg_comparator::enum_date_cmp_type
787
768
Arg_comparator::can_compare_as_dates(Item *in_a, Item *in_b,
788
 
                                     int64_t *const_value)
 
769
                                     uint64_t *const_value)
789
770
{
790
771
  enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
791
772
  Item *str_arg= 0, *date_arg= 0;
796
777
  if (in_a->is_datetime())
797
778
  {
798
779
    if (in_b->is_datetime())
799
 
    {
800
780
      cmp_type= CMP_DATE_WITH_DATE;
801
 
    }
802
781
    else if (in_b->result_type() == STRING_RESULT)
803
782
    {
804
783
      cmp_type= CMP_DATE_WITH_STR;
840
819
       * Does a uint64_t conversion really have to happen here?  Fields return int64_t
841
820
       * from val_int(), not uint64_t...
842
821
       */
843
 
      int64_t value;
 
822
      uint64_t value;
844
823
      String *str_val;
845
824
      String tmp;
846
825
      /* DateTime used to pick up as many string conversion possibilities as possible. */
866
845
      }
867
846
 
868
847
      /* String conversion was good.  Convert to an integer for comparison purposes. */
869
 
      temporal.to_int64_t(&value);
 
848
      int64_t int_value;
 
849
      temporal.to_int64_t(&int_value);
 
850
      value= (uint64_t) int_value;
870
851
 
871
852
      if (const_value)
872
853
        *const_value= value;
880
861
                                        Item **a1, Item **a2,
881
862
                                        Item_result type)
882
863
{
883
 
  enum_date_cmp_type cmp_type;
884
 
  int64_t const_value= -1;
 
864
  enum enum_date_cmp_type cmp_type;
 
865
  uint64_t const_value= (uint64_t)-1;
885
866
  a= a1;
886
867
  b= a2;
887
868
 
888
869
  if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
889
870
  {
 
871
    session= current_session;
890
872
    owner= owner_arg;
891
873
    a_type= (*a)->field_type();
892
874
    b_type= (*b)->field_type();
893
875
    a_cache= 0;
894
876
    b_cache= 0;
895
877
 
896
 
    if (const_value != -1)
 
878
    if (const_value != (uint64_t)-1)
897
879
    {
898
880
      Item_cache_int *cache= new Item_cache_int();
899
881
      /* Mark the cache as non-const to prevent re-caching. */
914
896
    is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC);
915
897
    func= &Arg_comparator::compare_datetime;
916
898
    get_value_func= &get_datetime_value;
917
 
 
918
899
    return 0;
919
900
  }
920
901
 
924
905
 
925
906
void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
926
907
{
 
908
  session= current_session;
927
909
  /* A caller will handle null values by itself. */
928
910
  owner= NULL;
929
911
  a= a1;
967
949
    obtained value
968
950
*/
969
951
 
970
 
int64_t
 
952
uint64_t
971
953
get_datetime_value(Session *session, Item ***item_arg, Item **cache_arg,
972
954
                   Item *warn_item, bool *is_null)
973
955
{
974
 
  int64_t value= 0;
 
956
  uint64_t value= 0;
975
957
  String buf, *str= 0;
976
958
  Item *item= **item_arg;
977
959
 
995
977
    str= item->val_str(&buf);
996
978
    *is_null= item->null_value;
997
979
  }
998
 
 
999
980
  if (*is_null)
1000
981
    return ~(uint64_t) 0;
1001
 
 
1002
982
  /*
1003
983
    Convert strings to the integer DATE/DATETIME representation.
1004
984
    Even if both dates provided in strings we can't compare them directly as
1009
989
  {
1010
990
    bool error;
1011
991
    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;
 
992
    enum enum_drizzle_timestamp_type t_type= f_type ==
 
993
      DRIZZLE_TYPE_DATE ? DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME;
1013
994
    value= get_date_from_str(session, str, t_type, warn_item->name, &error);
1014
995
    /*
1015
996
      If str did not contain a valid date according to the current
1032
1013
    *cache_arg= cache;
1033
1014
    *item_arg= cache_arg;
1034
1015
  }
1035
 
 
1036
1016
  return value;
1037
1017
}
1038
1018
 
1189
1169
 
1190
1170
int Arg_comparator::compare_decimal()
1191
1171
{
1192
 
  type::Decimal value1;
1193
 
  type::Decimal *val1= (*a)->val_decimal(&value1);
 
1172
  my_decimal value1;
 
1173
  my_decimal *val1= (*a)->val_decimal(&value1);
1194
1174
  if (!(*a)->null_value)
1195
1175
  {
1196
 
    type::Decimal value2;
1197
 
    type::Decimal *val2= (*b)->val_decimal(&value2);
 
1176
    my_decimal value2;
 
1177
    my_decimal *val2= (*b)->val_decimal(&value2);
1198
1178
    if (!(*b)->null_value)
1199
1179
    {
1200
1180
      owner->null_value= 0;
1201
 
      return class_decimal_cmp(val1, val2);
 
1181
      return my_decimal_cmp(val1, val2);
1202
1182
    }
1203
1183
  }
1204
1184
  owner->null_value= 1;
1216
1196
 
1217
1197
int Arg_comparator::compare_e_decimal()
1218
1198
{
1219
 
  type::Decimal value1, value2;
1220
 
  type::Decimal *val1= (*a)->val_decimal(&value1);
1221
 
  type::Decimal *val2= (*b)->val_decimal(&value2);
 
1199
  my_decimal value1, value2;
 
1200
  my_decimal *val1= (*a)->val_decimal(&value1);
 
1201
  my_decimal *val2= (*b)->val_decimal(&value2);
1222
1202
  if ((*a)->null_value || (*b)->null_value)
1223
1203
    return test((*a)->null_value && (*b)->null_value);
1224
 
  return test(class_decimal_cmp(val1, val2) == 0);
 
1204
  return test(my_decimal_cmp(val1, val2) == 0);
1225
1205
}
1226
1206
 
1227
1207
 
1631
1611
 
1632
1612
void Item_in_optimizer::cleanup()
1633
1613
{
1634
 
  item::function::Boolean::cleanup();
 
1614
  Item_bool_func::cleanup();
1635
1615
  if (!save_cache)
1636
1616
    cache= 0;
1637
1617
  return;
1684
1664
    change records at each execution.
1685
1665
  */
1686
1666
  if ((*args) != new_item)
1687
 
    getSession().change_item_tree(args, new_item);
 
1667
    current_session->change_item_tree(args, new_item);
1688
1668
 
1689
1669
  /*
1690
1670
    Transform the right IN operand which should be an Item_in_subselect or a
1839
1819
          {
1840
1820
            range->type= DECIMAL_RESULT;
1841
1821
            range->dec.init();
1842
 
            type::Decimal *dec= el->val_decimal(&range->dec);
 
1822
            my_decimal *dec= el->val_decimal(&range->dec);
1843
1823
            if (dec != &range->dec)
1844
1824
            {
1845
1825
              range->dec= *dec;
1889
1869
{
1890
1870
  assert(fixed == 1);
1891
1871
  double value;
1892
 
  type::Decimal dec_buf, *dec= NULL;
 
1872
  my_decimal dec_buf, *dec= NULL;
1893
1873
  uint32_t i;
1894
1874
 
1895
1875
  if (use_decimal_comparison)
1897
1877
    dec= row->element_index(0)->val_decimal(&dec_buf);
1898
1878
    if (row->element_index(0)->null_value)
1899
1879
      return -1;
1900
 
    class_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
 
1880
    my_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
1901
1881
  }
1902
1882
  else
1903
1883
  {
1922
1902
        and we are comparing against a decimal
1923
1903
      */
1924
1904
      if (dec && range->type == DECIMAL_RESULT)
1925
 
        cmp_result= class_decimal_cmp(&range->dec, dec) <= 0;
 
1905
        cmp_result= my_decimal_cmp(&range->dec, dec) <= 0;
1926
1906
      else
1927
1907
        cmp_result= (range->dbl <= value);
1928
1908
      if (cmp_result)
1932
1912
    }
1933
1913
    interval_range *range= intervals+start;
1934
1914
    return ((dec && range->type == DECIMAL_RESULT) ?
1935
 
            class_decimal_cmp(dec, &range->dec) < 0 :
 
1915
            my_decimal_cmp(dec, &range->dec) < 0 :
1936
1916
            value < range->dbl) ? 0 : start + 1;
1937
1917
  }
1938
1918
 
1943
1923
        ((el->result_type() == DECIMAL_RESULT) ||
1944
1924
         (el->result_type() == INT_RESULT)))
1945
1925
    {
1946
 
      type::Decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
 
1926
      my_decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
1947
1927
      /* Skip NULL ranges. */
1948
1928
      if (el->null_value)
1949
1929
        continue;
1950
 
      if (class_decimal_cmp(e_dec, dec) > 0)
 
1930
      if (my_decimal_cmp(e_dec, dec) > 0)
1951
1931
        return i - 1;
1952
1932
    }
1953
1933
    else
1997
1977
  if (Item_func_opt_neg::fix_fields(session, ref))
1998
1978
    return 1;
1999
1979
 
2000
 
  session->getLex()->current_select->between_count++;
 
1980
  session->lex->current_select->between_count++;
2001
1981
 
2002
1982
  /* not_null_tables_cache == union(T1(e),T1(e1),T1(e2)) */
2003
1983
  if (pred_level && !negated)
2018
1998
  int i;
2019
1999
  bool datetime_found= false;
2020
2000
  compare_as_dates= true;
 
2001
  Session *session= current_session;
2021
2002
 
2022
2003
  /*
2023
2004
    As some compare functions are generated after sql_yacc,
2064
2045
        The following can't be recoded with || as convert_constant_item
2065
2046
        changes the argument
2066
2047
      */
2067
 
      if (convert_constant_item(&getSession(), field_item, &args[1]))
 
2048
      if (convert_constant_item(session, field_item, &args[1]))
2068
2049
        cmp_type=INT_RESULT;                    // Works for all types.
2069
 
      if (convert_constant_item(&getSession(), field_item, &args[2]))
 
2050
      if (convert_constant_item(session, field_item, &args[2]))
2070
2051
        cmp_type=INT_RESULT;                    // Works for all types.
2071
2052
    }
2072
2053
  }
2143
2124
  }
2144
2125
  else if (cmp_type == DECIMAL_RESULT)
2145
2126
  {
2146
 
    type::Decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
 
2127
    my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2147
2128
               a_buf, *a_dec, b_buf, *b_dec;
2148
2129
    if ((null_value=args[0]->null_value))
2149
2130
      return 0;
2150
2131
    a_dec= args[1]->val_decimal(&a_buf);
2151
2132
    b_dec= args[2]->val_decimal(&b_buf);
2152
2133
    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);
 
2134
      return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
 
2135
                          my_decimal_cmp(dec, b_dec) <= 0) != negated);
2155
2136
    if (args[1]->null_value && args[2]->null_value)
2156
2137
      null_value=1;
2157
2138
    else if (args[1]->null_value)
2158
 
      null_value= (class_decimal_cmp(dec, b_dec) <= 0);
 
2139
      null_value= (my_decimal_cmp(dec, b_dec) <= 0);
2159
2140
    else
2160
 
      null_value= (class_decimal_cmp(dec, a_dec) >= 0);
 
2141
      null_value= (my_decimal_cmp(dec, a_dec) >= 0);
2161
2142
  }
2162
2143
  else
2163
2144
  {
2291
2272
}
2292
2273
 
2293
2274
 
2294
 
type::Decimal *Item_func_ifnull::decimal_op(type::Decimal *decimal_value)
 
2275
my_decimal *Item_func_ifnull::decimal_op(my_decimal *decimal_value)
2295
2276
{
2296
2277
  assert(fixed == 1);
2297
 
  type::Decimal *value= args[0]->val_decimal(decimal_value);
 
2278
  my_decimal *value= args[0]->val_decimal(decimal_value);
2298
2279
  if (!args[0]->null_value)
2299
2280
  {
2300
2281
    null_value= 0;
2464
2445
}
2465
2446
 
2466
2447
 
2467
 
type::Decimal *
2468
 
Item_func_if::val_decimal(type::Decimal *decimal_value)
 
2448
my_decimal *
 
2449
Item_func_if::val_decimal(my_decimal *decimal_value)
2469
2450
{
2470
2451
  assert(fixed == 1);
2471
2452
  Item *arg= args[0]->val_bool() ? args[1] : args[2];
2472
 
  type::Decimal *value= arg->val_decimal(decimal_value);
 
2453
  my_decimal *value= arg->val_decimal(decimal_value);
2473
2454
  null_value= arg->null_value;
2474
2455
  return value;
2475
2456
}
2549
2530
}
2550
2531
 
2551
2532
 
2552
 
type::Decimal *
2553
 
Item_func_nullif::val_decimal(type::Decimal * decimal_value)
 
2533
my_decimal *
 
2534
Item_func_nullif::val_decimal(my_decimal * decimal_value)
2554
2535
{
2555
2536
  assert(fixed == 1);
2556
 
  type::Decimal *res;
 
2537
  my_decimal *res;
2557
2538
  if (!cmp.compare())
2558
2539
  {
2559
2540
    null_value=1;
2686
2667
}
2687
2668
 
2688
2669
 
2689
 
type::Decimal *Item_func_case::val_decimal(type::Decimal *decimal_value)
 
2670
my_decimal *Item_func_case::val_decimal(my_decimal *decimal_value)
2690
2671
{
2691
2672
  assert(fixed == 1);
2692
2673
  char buff[MAX_FIELD_WIDTH];
2693
2674
  String dummy_str(buff, sizeof(buff), default_charset());
2694
2675
  Item *item= find_item(&dummy_str);
2695
 
  type::Decimal *res;
 
2676
  my_decimal *res;
2696
2677
 
2697
2678
  if (!item)
2698
2679
  {
2735
2716
 
2736
2717
void Item_func_case::agg_num_lengths(Item *arg)
2737
2718
{
2738
 
  uint32_t len= class_decimal_length_to_precision(arg->max_length, arg->decimals,
 
2719
  uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2739
2720
                                           arg->unsigned_flag) - arg->decimals;
2740
2721
  set_if_bigger(max_length, len);
2741
2722
  set_if_bigger(decimals, arg->decimals);
2818
2799
      agg_num_lengths(args[i + 1]);
2819
2800
    if (else_expr_num != -1)
2820
2801
      agg_num_lengths(args[else_expr_num]);
2821
 
    max_length= class_decimal_precision_to_length(max_length + decimals, decimals,
 
2802
    max_length= my_decimal_precision_to_length(max_length + decimals, decimals,
2822
2803
                                               unsigned_flag);
2823
2804
  }
2824
2805
}
2925
2906
}
2926
2907
 
2927
2908
 
2928
 
type::Decimal *Item_func_coalesce::decimal_op(type::Decimal *decimal_value)
 
2909
my_decimal *Item_func_coalesce::decimal_op(my_decimal *decimal_value)
2929
2910
{
2930
2911
  assert(fixed == 1);
2931
2912
  null_value= 0;
2932
2913
  for (uint32_t i= 0; i < arg_count; i++)
2933
2914
  {
2934
 
    type::Decimal *res= args[i]->val_decimal(decimal_value);
 
2915
    my_decimal *res= args[i]->val_decimal(decimal_value);
2935
2916
    if (!args[i]->null_value)
2936
2917
      return res;
2937
2918
  }
3080
3061
}
3081
3062
 
3082
3063
 
3083
 
static int cmp_decimal(void *, type::Decimal *a, type::Decimal *b)
 
3064
static int cmp_decimal(void *, my_decimal *a, my_decimal *b)
3084
3065
{
3085
3066
  /*
3086
3067
    We need call of fixing buffer pointer, because fast sort just copy
3088
3069
  */
3089
3070
  a->fix_buffer_pointer();
3090
3071
  b->fix_buffer_pointer();
3091
 
  return class_decimal_cmp(a, b);
 
3072
  return my_decimal_cmp(a, b);
3092
3073
}
3093
3074
 
3094
3075
 
3196
3177
  return;
3197
3178
}
3198
3179
 
3199
 
in_int64_t::in_int64_t(uint32_t elements) :
3200
 
  in_vector(elements, sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
 
3180
in_int64_t::in_int64_t(uint32_t elements)
 
3181
  :in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3201
3182
{}
3202
3183
 
3203
3184
void in_int64_t::set(uint32_t pos,Item *item)
3217
3198
  return (unsigned char*) &tmp;
3218
3199
}
3219
3200
 
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)
 
3201
void in_datetime::set(uint32_t pos,Item *item)
3228
3202
{
3229
3203
  Item **tmp_item= &item;
3230
3204
  bool is_null;
3264
3238
 
3265
3239
 
3266
3240
in_decimal::in_decimal(uint32_t elements)
3267
 
  :in_vector(elements, sizeof(type::Decimal),(qsort2_cmp) cmp_decimal, 0)
 
3241
  :in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3268
3242
{}
3269
3243
 
3270
3244
 
3271
3245
void in_decimal::set(uint32_t pos, Item *item)
3272
3246
{
3273
 
  /* as far as 'item' is constant, we can store reference on type::Decimal */
3274
 
  type::Decimal *dec= ((type::Decimal *)base) + pos;
 
3247
  /* as far as 'item' is constant, we can store reference on my_decimal */
 
3248
  my_decimal *dec= ((my_decimal *)base) + pos;
3275
3249
  dec->len= DECIMAL_BUFF_LENGTH;
3276
3250
  dec->fix_buffer_pointer();
3277
 
  type::Decimal *res= item->val_decimal(dec);
 
3251
  my_decimal *res= item->val_decimal(dec);
3278
3252
  /* if item->val_decimal() is evaluated to NULL then res == 0 */
3279
3253
  if (!item->null_value && res != dec)
3280
 
    class_decimal2decimal(res, dec);
 
3254
    my_decimal2decimal(res, dec);
3281
3255
}
3282
3256
 
3283
3257
 
3284
3258
unsigned char *in_decimal::get_value(Item *item)
3285
3259
{
3286
 
  type::Decimal *result= item->val_decimal(&val);
 
3260
  my_decimal *result= item->val_decimal(&val);
3287
3261
  if (item->null_value)
3288
3262
    return 0;
3289
3263
  return (unsigned char *)result;
3442
3416
 
3443
3417
void cmp_item_decimal::store_value(Item *item)
3444
3418
{
3445
 
  type::Decimal *val= item->val_decimal(&value);
 
3419
  my_decimal *val= item->val_decimal(&value);
3446
3420
  /* val may be zero if item is nnull */
3447
3421
  if (val && val != &value)
3448
 
    class_decimal2decimal(val, &value);
 
3422
    my_decimal2decimal(val, &value);
3449
3423
}
3450
3424
 
3451
3425
 
3452
3426
int cmp_item_decimal::cmp(Item *arg)
3453
3427
{
3454
 
  type::Decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
 
3428
  my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3455
3429
  if (arg->null_value)
3456
3430
    return 1;
3457
 
  return class_decimal_cmp(&value, tmp);
 
3431
  return my_decimal_cmp(&value, tmp);
3458
3432
}
3459
3433
 
3460
3434
 
3461
3435
int cmp_item_decimal::compare(cmp_item *arg)
3462
3436
{
3463
3437
  cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3464
 
  return class_decimal_cmp(&value, &l_cmp->value);
 
3438
  return my_decimal_cmp(&value, &l_cmp->value);
3465
3439
}
3466
3440
 
3467
3441
 
3574
3548
{
3575
3549
  Item **arg, **arg_end;
3576
3550
  bool const_itm= 1;
 
3551
  Session *session= current_session;
3577
3552
  bool datetime_found= false;
3578
3553
  /* true <=> arguments values will be compared as DATETIMEs. */
3579
3554
  bool compare_as_datetime= false;
3721
3696
          bool all_converted= true;
3722
3697
          for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3723
3698
          {
3724
 
            if (!convert_constant_item (&getSession(), field_item, &arg[0]))
 
3699
            if (!convert_constant_item (session, field_item, &arg[0]))
3725
3700
              all_converted= false;
3726
3701
          }
3727
3702
          if (all_converted)
3758
3733
      }
3759
3734
    }
3760
3735
 
3761
 
    if (array && !(getSession().is_fatal_error))                // If not EOM
 
3736
    if (array && !(session->is_fatal_error))            // If not EOM
3762
3737
    {
3763
3738
      uint32_t j=0;
3764
3739
      for (uint32_t arg_num=1 ; arg_num < arg_count ; arg_num++)
3874
3849
 
3875
3850
 
3876
3851
Item_cond::Item_cond(Session *session, Item_cond *item)
3877
 
  :item::function::Boolean(session, item),
 
3852
  :Item_bool_func(session, item),
3878
3853
   abort_on_null(item->abort_on_null),
3879
3854
   and_tables_cache(item->and_tables_cache)
3880
3855
{
3886
3861
 
3887
3862
void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
3888
3863
{
3889
 
  List<Item>::iterator li(item->list.begin());
 
3864
  List_iterator_fast<Item> li(item->list);
3890
3865
  while (Item *it= li++)
3891
3866
    list.push_back(it->copy_andor_structure(session));
3892
3867
}
3896
3871
Item_cond::fix_fields(Session *session, Item **)
3897
3872
{
3898
3873
  assert(fixed == 0);
3899
 
  List<Item>::iterator li(list.begin());
 
3874
  List_iterator<Item> li(list);
3900
3875
  Item *item;
3901
3876
  void *orig_session_marker= session->session_marker;
3902
3877
  unsigned char buff[sizeof(char*)];                    // Max local vars in function
3903
3878
  not_null_tables_cache= used_tables_cache= 0;
3904
 
  const_item_cache= true;
 
3879
  const_item_cache= 1;
3905
3880
 
3906
3881
  if (functype() == COND_OR_FUNC)
3907
3882
    session->session_marker= 0;
3936
3911
           !((Item_cond*) item)->list.is_empty())
3937
3912
    {                                           // Identical function
3938
3913
      li.replace(((Item_cond*) item)->list);
3939
 
      ((Item_cond*) item)->list.clear();
 
3914
      ((Item_cond*) item)->list.empty();
3940
3915
      item= *li.ref();                          // new current item
3941
3916
    }
3942
3917
    if (abort_on_null)
3962
3937
    if (item->maybe_null)
3963
3938
      maybe_null=1;
3964
3939
  }
3965
 
  session->getLex()->current_select->cond_count+= list.elements;
 
3940
  session->lex->current_select->cond_count+= list.elements;
3966
3941
  session->session_marker= orig_session_marker;
3967
3942
  fix_length_and_dec();
3968
3943
  fixed= 1;
3972
3947
 
3973
3948
void Item_cond::fix_after_pullout(Select_Lex *new_parent, Item **)
3974
3949
{
3975
 
  List<Item>::iterator li(list.begin());
 
3950
  List_iterator<Item> li(list);
3976
3951
  Item *item;
3977
3952
 
3978
3953
  used_tables_cache=0;
3979
 
  const_item_cache= true;
 
3954
  const_item_cache=1;
3980
3955
 
3981
3956
  and_tables_cache= ~(table_map) 0; // Here and below we do as fix_fields does
3982
3957
  not_null_tables_cache= 0;
4004
3979
 
4005
3980
bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
4006
3981
{
4007
 
  List<Item>::iterator li(list.begin());
 
3982
  List_iterator_fast<Item> li(list);
4008
3983
  Item *item;
4009
3984
  while ((item= li++))
4010
3985
    if (item->walk(processor, walk_subquery, arg))
4033
4008
 
4034
4009
Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
4035
4010
{
4036
 
  List<Item>::iterator li(list.begin());
 
4011
  List_iterator<Item> li(list);
4037
4012
  Item *item;
4038
4013
  while ((item= li++))
4039
4014
  {
4048
4023
      change records at each execution.
4049
4024
    */
4050
4025
    if (new_item != item)
4051
 
      getSession().change_item_tree(li.ref(), new_item);
 
4026
      current_session->change_item_tree(li.ref(), new_item);
4052
4027
  }
4053
4028
  return Item_func::transform(transformer, arg);
4054
4029
}
4084
4059
  if (!(this->*analyzer)(arg_p))
4085
4060
    return 0;
4086
4061
 
4087
 
  List<Item>::iterator li(list.begin());
 
4062
  List_iterator<Item> li(list);
4088
4063
  Item *item;
4089
4064
  while ((item= li++))
4090
4065
  {
4103
4078
void Item_cond::traverse_cond(Cond_traverser traverser,
4104
4079
                              void *arg, traverse_order order)
4105
4080
{
4106
 
  List<Item>::iterator li(list.begin());
 
4081
  List_iterator<Item> li(list);
4107
4082
  Item *item;
4108
4083
 
4109
4084
  switch (order) {
4144
4119
void Item_cond::split_sum_func(Session *session, Item **ref_pointer_array,
4145
4120
                               List<Item> &fields)
4146
4121
{
4147
 
  List<Item>::iterator li(list.begin());
 
4122
  List_iterator<Item> li(list);
4148
4123
  Item *item;
4149
4124
  while ((item= li++))
4150
4125
    item->split_sum_func(session, ref_pointer_array,
4161
4136
 
4162
4137
void Item_cond::update_used_tables()
4163
4138
{
4164
 
  List<Item>::iterator li(list.begin());
 
4139
  List_iterator_fast<Item> li(list);
4165
4140
  Item *item;
4166
4141
 
4167
4142
  used_tables_cache=0;
4168
 
  const_item_cache= true;
 
4143
  const_item_cache=1;
4169
4144
  while ((item=li++))
4170
4145
  {
4171
4146
    item->update_used_tables();
4178
4153
void Item_cond::print(String *str, enum_query_type query_type)
4179
4154
{
4180
4155
  str->append('(');
4181
 
  List<Item>::iterator li(list.begin());
 
4156
  List_iterator_fast<Item> li(list);
4182
4157
  Item *item;
4183
4158
  if ((item=li++))
4184
4159
    item->print(str, query_type);
4195
4170
 
4196
4171
void Item_cond::neg_arguments(Session *session)
4197
4172
{
4198
 
  List<Item>::iterator li(list.begin());
 
4173
  List_iterator<Item> li(list);
4199
4174
  Item *item;
4200
4175
  while ((item= li++))          /* Apply not transformation to the arguments */
4201
4176
  {
4233
4208
int64_t Item_cond_and::val_int()
4234
4209
{
4235
4210
  assert(fixed == 1);
4236
 
  List<Item>::iterator li(list.begin());
 
4211
  List_iterator_fast<Item> li(list);
4237
4212
  Item *item;
4238
4213
  null_value= 0;
4239
4214
  while ((item=li++))
4251
4226
int64_t Item_cond_or::val_int()
4252
4227
{
4253
4228
  assert(fixed == 1);
4254
 
  List<Item>::iterator li(list.begin());
 
4229
  List_iterator_fast<Item> li(list);
4255
4230
  Item *item;
4256
4231
  null_value=0;
4257
4232
  while ((item=li++))
4488
4463
      {
4489
4464
        pattern     = first + 1;
4490
4465
        pattern_len = (int) len - 2;
4491
 
        int *suff = (int*) session->getMemRoot()->allocate((int) (sizeof(int)*
4492
 
                                                                  ((pattern_len + 1)*2+
4493
 
                                                                   alphabet_size)));
 
4466
        int *suff = (int*) session->alloc((int) (sizeof(int)*
 
4467
                                      ((pattern_len + 1)*2+
 
4468
                                      alphabet_size)));
4494
4469
        bmGs      = suff + pattern_len + 1;
4495
4470
        bmBc      = bmGs + pattern_len + 1;
4496
4471
        turboBM_compute_good_suffix_shifts(suff);
4507
4482
  Item_bool_func2::cleanup();
4508
4483
}
4509
4484
 
4510
 
static unsigned char likeconv(const CHARSET_INFO *cs, unsigned char a)
4511
 
{
4512
4485
#ifdef LIKE_CMP_TOUPPER
4513
 
  return cs->toupper(a);
 
4486
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
4514
4487
#else
4515
 
  return cs->sort_order[a];
 
4488
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
4516
4489
#endif
4517
 
}
 
4490
 
4518
4491
 
4519
4492
/**
4520
4493
  Precomputation dependent only on pattern_len.
4651
4624
 
4652
4625
bool Item_func_like::turboBM_matches(const char* text, int text_len) const
4653
4626
{
4654
 
  int bcShift;
4655
 
  int turboShift;
 
4627
  register int bcShift;
 
4628
  register int turboShift;
4656
4629
  int shift = pattern_len;
4657
4630
  int j     = 0;
4658
4631
  int u     = 0;
4666
4639
  {
4667
4640
    while (j <= tlmpl)
4668
4641
    {
4669
 
      int i= plm1;
 
4642
      register int i= plm1;
4670
4643
      while (i >= 0 && pattern[i] == text[i + j])
4671
4644
      {
4672
4645
        i--;
4676
4649
      if (i < 0)
4677
4650
        return 1;
4678
4651
 
4679
 
      const int v = plm1 - i;
 
4652
      register const int v = plm1 - i;
4680
4653
      turboShift = u - v;
4681
4654
      bcShift    = bmBc[(uint32_t) (unsigned char) text[i + j]] - plm1 + i;
4682
4655
      shift      = (turboShift > bcShift) ? turboShift : bcShift;
4697
4670
  {
4698
4671
    while (j <= tlmpl)
4699
4672
    {
4700
 
      int i = plm1;
 
4673
      register int i = plm1;
4701
4674
      while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
4702
4675
      {
4703
4676
        i--;
4708
4681
      if (i < 0)
4709
4682
        return 1;
4710
4683
 
4711
 
      const int v= plm1 - i;
 
4684
      register const int v= plm1 - i;
4712
4685
      turboShift= u - v;
4713
4686
      bcShift= bmBc[(uint32_t) likeconv(cs, text[i + j])] - plm1 + i;
4714
4687
      shift= (turboShift > bcShift) ? turboShift : bcShift;
4749
4722
int64_t Item_cond_xor::val_int()
4750
4723
{
4751
4724
  assert(fixed == 1);
4752
 
  List<Item>::iterator li(list.begin());
 
4725
  List_iterator<Item> li(list);
4753
4726
  Item *item;
4754
4727
  int result=0;
4755
4728
  null_value=0;
4909
4882
}
4910
4883
 
4911
4884
Item_equal::Item_equal(Item_field *f1, Item_field *f2)
4912
 
  : item::function::Boolean(), const_item(0), eval_item(0), cond_false(0)
 
4885
  : Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
4913
4886
{
4914
 
  const_item_cache= false;
 
4887
  const_item_cache= 0;
4915
4888
  fields.push_back(f1);
4916
4889
  fields.push_back(f2);
4917
4890
}
4918
4891
 
4919
4892
Item_equal::Item_equal(Item *c, Item_field *f)
4920
 
  : item::function::Boolean(), eval_item(0), cond_false(0)
 
4893
  : Item_bool_func(), eval_item(0), cond_false(0)
4921
4894
{
4922
 
  const_item_cache= false;
 
4895
  const_item_cache= 0;
4923
4896
  fields.push_back(f);
4924
4897
  const_item= c;
4925
4898
}
4926
4899
 
4927
4900
 
4928
4901
Item_equal::Item_equal(Item_equal *item_equal)
4929
 
  : item::function::Boolean(), eval_item(0), cond_false(0)
 
4902
  : Item_bool_func(), eval_item(0), cond_false(0)
4930
4903
{
4931
 
  const_item_cache= false;
4932
 
  List<Item_field>::iterator li(item_equal->fields.begin());
 
4904
  const_item_cache= 0;
 
4905
  List_iterator_fast<Item_field> li(item_equal->fields);
4933
4906
  Item_field *item;
4934
4907
  while ((item= li++))
4935
4908
  {
4952
4925
  func->set_cmp_func();
4953
4926
  func->quick_fix_field();
4954
4927
  if ((cond_false= !func->val_int()))
4955
 
    const_item_cache= true;
 
4928
    const_item_cache= 1;
4956
4929
}
4957
4930
 
4958
4931
void Item_equal::add(Item_field *f)
4981
4954
 
4982
4955
bool Item_equal::contains(Field *field)
4983
4956
{
4984
 
  List<Item_field>::iterator it(fields.begin());
 
4957
  List_iterator_fast<Item_field> it(fields);
4985
4958
  Item_field *item;
4986
4959
  while ((item= it++))
4987
4960
  {
5040
5013
void Item_equal::sort(Item_field_cmpfunc cmp, void *arg)
5041
5014
{
5042
5015
  bool swap;
5043
 
  List<Item_field>::iterator it(fields.begin());
 
5016
  List_iterator<Item_field> it(fields);
5044
5017
  do
5045
5018
  {
5046
5019
    Item_field *item1= it++;
5064
5037
        ref1= ref2;
5065
5038
      }
5066
5039
    }
5067
 
    it= fields.begin();
 
5040
    it.rewind();
5068
5041
  } while (swap);
5069
5042
}
5070
5043
 
5081
5054
 
5082
5055
void Item_equal::update_const()
5083
5056
{
5084
 
  List<Item_field>::iterator it(fields.begin());
 
5057
  List_iterator<Item_field> it(fields);
5085
5058
  Item *item;
5086
5059
  while ((item= it++))
5087
5060
  {
5095
5068
 
5096
5069
bool Item_equal::fix_fields(Session *, Item **)
5097
5070
{
5098
 
  List<Item_field>::iterator li(fields.begin());
 
5071
  List_iterator_fast<Item_field> li(fields);
5099
5072
  Item *item;
5100
5073
  not_null_tables_cache= used_tables_cache= 0;
5101
 
  const_item_cache= false;
 
5074
  const_item_cache= 0;
5102
5075
  while ((item= li++))
5103
5076
  {
5104
5077
    table_map tmp_table_map;
5115
5088
 
5116
5089
void Item_equal::update_used_tables()
5117
5090
{
5118
 
  List<Item_field>::iterator li(fields.begin());
 
5091
  List_iterator_fast<Item_field> li(fields);
5119
5092
  Item *item;
5120
5093
  not_null_tables_cache= used_tables_cache= 0;
5121
5094
  if ((const_item_cache= cond_false))
5133
5106
  Item_field *item_field;
5134
5107
  if (cond_false)
5135
5108
    return 0;
5136
 
  List<Item_field>::iterator it(fields.begin());
 
5109
  List_iterator_fast<Item_field> it(fields);
5137
5110
  Item *item= const_item ? const_item : it++;
 
5111
  if ((null_value= item->null_value))
 
5112
    return 0;
5138
5113
  eval_item->store_value(item);
5139
 
  if ((null_value= item->null_value))
5140
 
    return 0;
5141
5114
  while ((item_field= it++))
5142
5115
  {
5143
5116
    /* Skip fields of non-const tables. They haven't been read yet */
5144
5117
    if (item_field->field->getTable()->const_table)
5145
5118
    {
5146
 
      if (eval_item->cmp(item_field) || (null_value= item_field->null_value))
 
5119
      if ((null_value= item_field->null_value) || eval_item->cmp(item_field))
5147
5120
        return 0;
5148
5121
    }
5149
5122
  }
5159
5132
 
5160
5133
bool Item_equal::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
5161
5134
{
5162
 
  List<Item_field>::iterator it(fields.begin());
 
5135
  List_iterator_fast<Item_field> it(fields);
5163
5136
  Item *item;
5164
5137
  while ((item= it++))
5165
5138
  {
5171
5144
 
5172
5145
Item *Item_equal::transform(Item_transformer transformer, unsigned char *arg)
5173
5146
{
5174
 
  List<Item_field>::iterator it(fields.begin());
 
5147
  List_iterator<Item_field> it(fields);
5175
5148
  Item *item;
5176
5149
  while ((item= it++))
5177
5150
  {
5186
5159
      change records at each execution.
5187
5160
    */
5188
5161
    if (new_item != item)
5189
 
      getSession().change_item_tree((Item **) it.ref(), new_item);
 
5162
      current_session->change_item_tree((Item **) it.ref(), new_item);
5190
5163
  }
5191
5164
  return Item_func::transform(transformer, arg);
5192
5165
}
5195
5168
{
5196
5169
  str->append(func_name());
5197
5170
  str->append('(');
5198
 
  List<Item_field>::iterator it(fields.begin());
 
5171
  List_iterator_fast<Item_field> it(fields);
5199
5172
  Item *item;
5200
5173
  if (const_item)
5201
5174
    const_item->print(str, query_type);
5213
5186
  str->append(')');
5214
5187
}
5215
5188
 
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
5189
} /* namespace drizzled */