~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/cmpfunc.cc

  • Committer: Brian Aker
  • Date: 2011-01-24 23:10:42 UTC
  • mto: This revision was merged to the branch mainline in revision 2113.
  • Revision ID: brian@tangent.org-20110124231042-hg2zx5cq1angsgjy
Minor cleanup, drop one of two needs for current session during parsing.

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
 
498
493
void Item_bool_func2::fix_length_and_dec()
499
494
{
500
495
  max_length= 1;                                     // Function returns 0 or 1
 
496
  Session *session;
501
497
 
502
498
  /*
503
499
    As some compare functions are generated after sql_yacc,
535
531
    return;
536
532
  }
537
533
 
 
534
  session= current_session;
538
535
  Item_field *field_item= NULL;
539
536
 
540
537
  if (args[0]->real_item()->type() == FIELD_ITEM)
543
540
    if (field_item->field->can_be_compared_as_int64_t() &&
544
541
        !(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
545
542
    {
546
 
      if (convert_constant_item(&getSession(), field_item, &args[1]))
 
543
      if (convert_constant_item(session, field_item, &args[1]))
547
544
      {
548
545
        cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
549
546
                         INT_RESULT);           // Works for all types.
559
556
          !(field_item->is_datetime() &&
560
557
            args[0]->result_type() == STRING_RESULT))
561
558
      {
562
 
        if (convert_constant_item(&getSession(), field_item, &args[0]))
 
559
        if (convert_constant_item(session, field_item, &args[0]))
563
560
        {
564
561
          cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
565
562
                           INT_RESULT); // Works for all types.
572
569
  set_cmp_func();
573
570
}
574
571
 
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
572
 
589
573
int Arg_comparator::set_compare_func(Item_bool_func2 *item, Item_result type)
590
574
{
717
701
                  char *warn_name, bool *error_arg)
718
702
{
719
703
  int64_t value= 0;
720
 
  type::cut_t error= type::VALID;
 
704
  int error;
721
705
  type::Time l_time;
722
706
  type::timestamp_t ret;
723
707
 
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);
 
708
  ret= str_to_datetime(str->ptr(), str->length(), &l_time,
 
709
                       (TIME_FUZZY_DATE | MODE_INVALID_DATES |
 
710
                        (session->variables.sql_mode & MODE_NO_ZERO_DATE)),
 
711
                       &error);
727
712
 
728
713
  if (ret == type::DRIZZLE_TIMESTAMP_DATETIME || ret == type::DRIZZLE_TIMESTAMP_DATE)
729
714
  {
737
722
  else
738
723
  {
739
724
    *error_arg= true;
740
 
    error= type::CUT;                                   /* force warning */
 
725
    error= 1;                                   /* force warning */
741
726
  }
742
727
 
743
 
  if (error != type::VALID)
 
728
  if (error > 0)
744
729
  {
745
730
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
746
731
                                 str->ptr(), str->length(),
887
872
 
888
873
  if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
889
874
  {
 
875
    session= current_session;
890
876
    owner= owner_arg;
891
877
    a_type= (*a)->field_type();
892
878
    b_type= (*b)->field_type();
924
910
 
925
911
void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
926
912
{
 
913
  session= current_session;
927
914
  /* A caller will handle null values by itself. */
928
915
  owner= NULL;
929
916
  a= a1;
1684
1671
    change records at each execution.
1685
1672
  */
1686
1673
  if ((*args) != new_item)
1687
 
    getSession().change_item_tree(args, new_item);
 
1674
    current_session->change_item_tree(args, new_item);
1688
1675
 
1689
1676
  /*
1690
1677
    Transform the right IN operand which should be an Item_in_subselect or a
1997
1984
  if (Item_func_opt_neg::fix_fields(session, ref))
1998
1985
    return 1;
1999
1986
 
2000
 
  session->getLex()->current_select->between_count++;
 
1987
  session->lex->current_select->between_count++;
2001
1988
 
2002
1989
  /* not_null_tables_cache == union(T1(e),T1(e1),T1(e2)) */
2003
1990
  if (pred_level && !negated)
2018
2005
  int i;
2019
2006
  bool datetime_found= false;
2020
2007
  compare_as_dates= true;
 
2008
  Session *session= current_session;
2021
2009
 
2022
2010
  /*
2023
2011
    As some compare functions are generated after sql_yacc,
2064
2052
        The following can't be recoded with || as convert_constant_item
2065
2053
        changes the argument
2066
2054
      */
2067
 
      if (convert_constant_item(&getSession(), field_item, &args[1]))
 
2055
      if (convert_constant_item(session, field_item, &args[1]))
2068
2056
        cmp_type=INT_RESULT;                    // Works for all types.
2069
 
      if (convert_constant_item(&getSession(), field_item, &args[2]))
 
2057
      if (convert_constant_item(session, field_item, &args[2]))
2070
2058
        cmp_type=INT_RESULT;                    // Works for all types.
2071
2059
    }
2072
2060
  }
3217
3205
  return (unsigned char*) &tmp;
3218
3206
}
3219
3207
 
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
3208
void in_datetime::set(uint32_t pos, Item *item)
3228
3209
{
3229
3210
  Item **tmp_item= &item;
3574
3555
{
3575
3556
  Item **arg, **arg_end;
3576
3557
  bool const_itm= 1;
 
3558
  Session *session= current_session;
3577
3559
  bool datetime_found= false;
3578
3560
  /* true <=> arguments values will be compared as DATETIMEs. */
3579
3561
  bool compare_as_datetime= false;
3721
3703
          bool all_converted= true;
3722
3704
          for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3723
3705
          {
3724
 
            if (!convert_constant_item (&getSession(), field_item, &arg[0]))
 
3706
            if (!convert_constant_item (session, field_item, &arg[0]))
3725
3707
              all_converted= false;
3726
3708
          }
3727
3709
          if (all_converted)
3758
3740
      }
3759
3741
    }
3760
3742
 
3761
 
    if (array && !(getSession().is_fatal_error))                // If not EOM
 
3743
    if (array && !(session->is_fatal_error))            // If not EOM
3762
3744
    {
3763
3745
      uint32_t j=0;
3764
3746
      for (uint32_t arg_num=1 ; arg_num < arg_count ; arg_num++)
3886
3868
 
3887
3869
void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
3888
3870
{
3889
 
  List<Item>::iterator li(item->list.begin());
 
3871
  List_iterator_fast<Item> li(item->list);
3890
3872
  while (Item *it= li++)
3891
3873
    list.push_back(it->copy_andor_structure(session));
3892
3874
}
3896
3878
Item_cond::fix_fields(Session *session, Item **)
3897
3879
{
3898
3880
  assert(fixed == 0);
3899
 
  List<Item>::iterator li(list.begin());
 
3881
  List_iterator<Item> li(list);
3900
3882
  Item *item;
3901
3883
  void *orig_session_marker= session->session_marker;
3902
3884
  unsigned char buff[sizeof(char*)];                    // Max local vars in function
3936
3918
           !((Item_cond*) item)->list.is_empty())
3937
3919
    {                                           // Identical function
3938
3920
      li.replace(((Item_cond*) item)->list);
3939
 
      ((Item_cond*) item)->list.clear();
 
3921
      ((Item_cond*) item)->list.empty();
3940
3922
      item= *li.ref();                          // new current item
3941
3923
    }
3942
3924
    if (abort_on_null)
3962
3944
    if (item->maybe_null)
3963
3945
      maybe_null=1;
3964
3946
  }
3965
 
  session->getLex()->current_select->cond_count+= list.elements;
 
3947
  session->lex->current_select->cond_count+= list.elements;
3966
3948
  session->session_marker= orig_session_marker;
3967
3949
  fix_length_and_dec();
3968
3950
  fixed= 1;
3972
3954
 
3973
3955
void Item_cond::fix_after_pullout(Select_Lex *new_parent, Item **)
3974
3956
{
3975
 
  List<Item>::iterator li(list.begin());
 
3957
  List_iterator<Item> li(list);
3976
3958
  Item *item;
3977
3959
 
3978
3960
  used_tables_cache=0;
4004
3986
 
4005
3987
bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
4006
3988
{
4007
 
  List<Item>::iterator li(list.begin());
 
3989
  List_iterator_fast<Item> li(list);
4008
3990
  Item *item;
4009
3991
  while ((item= li++))
4010
3992
    if (item->walk(processor, walk_subquery, arg))
4033
4015
 
4034
4016
Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
4035
4017
{
4036
 
  List<Item>::iterator li(list.begin());
 
4018
  List_iterator<Item> li(list);
4037
4019
  Item *item;
4038
4020
  while ((item= li++))
4039
4021
  {
4048
4030
      change records at each execution.
4049
4031
    */
4050
4032
    if (new_item != item)
4051
 
      getSession().change_item_tree(li.ref(), new_item);
 
4033
      current_session->change_item_tree(li.ref(), new_item);
4052
4034
  }
4053
4035
  return Item_func::transform(transformer, arg);
4054
4036
}
4084
4066
  if (!(this->*analyzer)(arg_p))
4085
4067
    return 0;
4086
4068
 
4087
 
  List<Item>::iterator li(list.begin());
 
4069
  List_iterator<Item> li(list);
4088
4070
  Item *item;
4089
4071
  while ((item= li++))
4090
4072
  {
4103
4085
void Item_cond::traverse_cond(Cond_traverser traverser,
4104
4086
                              void *arg, traverse_order order)
4105
4087
{
4106
 
  List<Item>::iterator li(list.begin());
 
4088
  List_iterator<Item> li(list);
4107
4089
  Item *item;
4108
4090
 
4109
4091
  switch (order) {
4144
4126
void Item_cond::split_sum_func(Session *session, Item **ref_pointer_array,
4145
4127
                               List<Item> &fields)
4146
4128
{
4147
 
  List<Item>::iterator li(list.begin());
 
4129
  List_iterator<Item> li(list);
4148
4130
  Item *item;
4149
4131
  while ((item= li++))
4150
4132
    item->split_sum_func(session, ref_pointer_array,
4161
4143
 
4162
4144
void Item_cond::update_used_tables()
4163
4145
{
4164
 
  List<Item>::iterator li(list.begin());
 
4146
  List_iterator_fast<Item> li(list);
4165
4147
  Item *item;
4166
4148
 
4167
4149
  used_tables_cache=0;
4178
4160
void Item_cond::print(String *str, enum_query_type query_type)
4179
4161
{
4180
4162
  str->append('(');
4181
 
  List<Item>::iterator li(list.begin());
 
4163
  List_iterator_fast<Item> li(list);
4182
4164
  Item *item;
4183
4165
  if ((item=li++))
4184
4166
    item->print(str, query_type);
4195
4177
 
4196
4178
void Item_cond::neg_arguments(Session *session)
4197
4179
{
4198
 
  List<Item>::iterator li(list.begin());
 
4180
  List_iterator<Item> li(list);
4199
4181
  Item *item;
4200
4182
  while ((item= li++))          /* Apply not transformation to the arguments */
4201
4183
  {
4233
4215
int64_t Item_cond_and::val_int()
4234
4216
{
4235
4217
  assert(fixed == 1);
4236
 
  List<Item>::iterator li(list.begin());
 
4218
  List_iterator_fast<Item> li(list);
4237
4219
  Item *item;
4238
4220
  null_value= 0;
4239
4221
  while ((item=li++))
4251
4233
int64_t Item_cond_or::val_int()
4252
4234
{
4253
4235
  assert(fixed == 1);
4254
 
  List<Item>::iterator li(list.begin());
 
4236
  List_iterator_fast<Item> li(list);
4255
4237
  Item *item;
4256
4238
  null_value=0;
4257
4239
  while ((item=li++))
4488
4470
      {
4489
4471
        pattern     = first + 1;
4490
4472
        pattern_len = (int) len - 2;
4491
 
        int *suff = (int*) session->getMemRoot()->allocate((int) (sizeof(int)*
4492
 
                                                                  ((pattern_len + 1)*2+
4493
 
                                                                   alphabet_size)));
 
4473
        int *suff = (int*) session->alloc((int) (sizeof(int)*
 
4474
                                      ((pattern_len + 1)*2+
 
4475
                                      alphabet_size)));
4494
4476
        bmGs      = suff + pattern_len + 1;
4495
4477
        bmBc      = bmGs + pattern_len + 1;
4496
4478
        turboBM_compute_good_suffix_shifts(suff);
4507
4489
  Item_bool_func2::cleanup();
4508
4490
}
4509
4491
 
4510
 
static unsigned char likeconv(const CHARSET_INFO *cs, unsigned char a)
4511
 
{
4512
4492
#ifdef LIKE_CMP_TOUPPER
4513
 
  return cs->toupper(a);
 
4493
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
4514
4494
#else
4515
 
  return cs->sort_order[a];
 
4495
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
4516
4496
#endif
4517
 
}
 
4497
 
4518
4498
 
4519
4499
/**
4520
4500
  Precomputation dependent only on pattern_len.
4651
4631
 
4652
4632
bool Item_func_like::turboBM_matches(const char* text, int text_len) const
4653
4633
{
4654
 
  int bcShift;
4655
 
  int turboShift;
 
4634
  register int bcShift;
 
4635
  register int turboShift;
4656
4636
  int shift = pattern_len;
4657
4637
  int j     = 0;
4658
4638
  int u     = 0;
4666
4646
  {
4667
4647
    while (j <= tlmpl)
4668
4648
    {
4669
 
      int i= plm1;
 
4649
      register int i= plm1;
4670
4650
      while (i >= 0 && pattern[i] == text[i + j])
4671
4651
      {
4672
4652
        i--;
4676
4656
      if (i < 0)
4677
4657
        return 1;
4678
4658
 
4679
 
      const int v = plm1 - i;
 
4659
      register const int v = plm1 - i;
4680
4660
      turboShift = u - v;
4681
4661
      bcShift    = bmBc[(uint32_t) (unsigned char) text[i + j]] - plm1 + i;
4682
4662
      shift      = (turboShift > bcShift) ? turboShift : bcShift;
4697
4677
  {
4698
4678
    while (j <= tlmpl)
4699
4679
    {
4700
 
      int i = plm1;
 
4680
      register int i = plm1;
4701
4681
      while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
4702
4682
      {
4703
4683
        i--;
4708
4688
      if (i < 0)
4709
4689
        return 1;
4710
4690
 
4711
 
      const int v= plm1 - i;
 
4691
      register const int v= plm1 - i;
4712
4692
      turboShift= u - v;
4713
4693
      bcShift= bmBc[(uint32_t) likeconv(cs, text[i + j])] - plm1 + i;
4714
4694
      shift= (turboShift > bcShift) ? turboShift : bcShift;
4749
4729
int64_t Item_cond_xor::val_int()
4750
4730
{
4751
4731
  assert(fixed == 1);
4752
 
  List<Item>::iterator li(list.begin());
 
4732
  List_iterator<Item> li(list);
4753
4733
  Item *item;
4754
4734
  int result=0;
4755
4735
  null_value=0;
4929
4909
  : item::function::Boolean(), eval_item(0), cond_false(0)
4930
4910
{
4931
4911
  const_item_cache= false;
4932
 
  List<Item_field>::iterator li(item_equal->fields.begin());
 
4912
  List_iterator_fast<Item_field> li(item_equal->fields);
4933
4913
  Item_field *item;
4934
4914
  while ((item= li++))
4935
4915
  {
4981
4961
 
4982
4962
bool Item_equal::contains(Field *field)
4983
4963
{
4984
 
  List<Item_field>::iterator it(fields.begin());
 
4964
  List_iterator_fast<Item_field> it(fields);
4985
4965
  Item_field *item;
4986
4966
  while ((item= it++))
4987
4967
  {
5040
5020
void Item_equal::sort(Item_field_cmpfunc cmp, void *arg)
5041
5021
{
5042
5022
  bool swap;
5043
 
  List<Item_field>::iterator it(fields.begin());
 
5023
  List_iterator<Item_field> it(fields);
5044
5024
  do
5045
5025
  {
5046
5026
    Item_field *item1= it++;
5064
5044
        ref1= ref2;
5065
5045
      }
5066
5046
    }
5067
 
    it= fields.begin();
 
5047
    it.rewind();
5068
5048
  } while (swap);
5069
5049
}
5070
5050
 
5081
5061
 
5082
5062
void Item_equal::update_const()
5083
5063
{
5084
 
  List<Item_field>::iterator it(fields.begin());
 
5064
  List_iterator<Item_field> it(fields);
5085
5065
  Item *item;
5086
5066
  while ((item= it++))
5087
5067
  {
5095
5075
 
5096
5076
bool Item_equal::fix_fields(Session *, Item **)
5097
5077
{
5098
 
  List<Item_field>::iterator li(fields.begin());
 
5078
  List_iterator_fast<Item_field> li(fields);
5099
5079
  Item *item;
5100
5080
  not_null_tables_cache= used_tables_cache= 0;
5101
5081
  const_item_cache= false;
5115
5095
 
5116
5096
void Item_equal::update_used_tables()
5117
5097
{
5118
 
  List<Item_field>::iterator li(fields.begin());
 
5098
  List_iterator_fast<Item_field> li(fields);
5119
5099
  Item *item;
5120
5100
  not_null_tables_cache= used_tables_cache= 0;
5121
5101
  if ((const_item_cache= cond_false))
5133
5113
  Item_field *item_field;
5134
5114
  if (cond_false)
5135
5115
    return 0;
5136
 
  List<Item_field>::iterator it(fields.begin());
 
5116
  List_iterator_fast<Item_field> it(fields);
5137
5117
  Item *item= const_item ? const_item : it++;
 
5118
  if ((null_value= item->null_value))
 
5119
    return 0;
5138
5120
  eval_item->store_value(item);
5139
 
  if ((null_value= item->null_value))
5140
 
    return 0;
5141
5121
  while ((item_field= it++))
5142
5122
  {
5143
5123
    /* Skip fields of non-const tables. They haven't been read yet */
5144
5124
    if (item_field->field->getTable()->const_table)
5145
5125
    {
5146
 
      if (eval_item->cmp(item_field) || (null_value= item_field->null_value))
 
5126
      if ((null_value= item_field->null_value) || eval_item->cmp(item_field))
5147
5127
        return 0;
5148
5128
    }
5149
5129
  }
5159
5139
 
5160
5140
bool Item_equal::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
5161
5141
{
5162
 
  List<Item_field>::iterator it(fields.begin());
 
5142
  List_iterator_fast<Item_field> it(fields);
5163
5143
  Item *item;
5164
5144
  while ((item= it++))
5165
5145
  {
5171
5151
 
5172
5152
Item *Item_equal::transform(Item_transformer transformer, unsigned char *arg)
5173
5153
{
5174
 
  List<Item_field>::iterator it(fields.begin());
 
5154
  List_iterator<Item_field> it(fields);
5175
5155
  Item *item;
5176
5156
  while ((item= it++))
5177
5157
  {
5186
5166
      change records at each execution.
5187
5167
    */
5188
5168
    if (new_item != item)
5189
 
      getSession().change_item_tree((Item **) it.ref(), new_item);
 
5169
      current_session->change_item_tree((Item **) it.ref(), new_item);
5190
5170
  }
5191
5171
  return Item_func::transform(transformer, arg);
5192
5172
}
5195
5175
{
5196
5176
  str->append(func_name());
5197
5177
  str->append('(');
5198
 
  List<Item_field>::iterator it(fields.begin());
 
5178
  List_iterator_fast<Item_field> it(fields);
5199
5179
  Item *item;
5200
5180
  if (const_item)
5201
5181
    const_item->print(str, query_type);
5213
5193
  str->append(')');
5214
5194
}
5215
5195
 
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
5196
} /* namespace drizzled */