~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/cmpfunc.cc

  • Committer: Brian Aker
  • Date: 2011-02-03 18:12:09 UTC
  • mfrom: (2134.1.9 timestamp)
  • mto: This revision was merged to the branch mainline in revision 2140.
  • Revision ID: brian@tangent.org-20110203181209-xr7i2079pdmu33hd
Merge in table tree.

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
{
887
871
 
888
872
  if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
889
873
  {
 
874
    session= current_session;
890
875
    owner= owner_arg;
891
876
    a_type= (*a)->field_type();
892
877
    b_type= (*b)->field_type();
924
909
 
925
910
void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
926
911
{
 
912
  session= current_session;
927
913
  /* A caller will handle null values by itself. */
928
914
  owner= NULL;
929
915
  a= a1;
1684
1670
    change records at each execution.
1685
1671
  */
1686
1672
  if ((*args) != new_item)
1687
 
    getSession().change_item_tree(args, new_item);
 
1673
    current_session->change_item_tree(args, new_item);
1688
1674
 
1689
1675
  /*
1690
1676
    Transform the right IN operand which should be an Item_in_subselect or a
1997
1983
  if (Item_func_opt_neg::fix_fields(session, ref))
1998
1984
    return 1;
1999
1985
 
2000
 
  session->getLex()->current_select->between_count++;
 
1986
  session->lex->current_select->between_count++;
2001
1987
 
2002
1988
  /* not_null_tables_cache == union(T1(e),T1(e1),T1(e2)) */
2003
1989
  if (pred_level && !negated)
2018
2004
  int i;
2019
2005
  bool datetime_found= false;
2020
2006
  compare_as_dates= true;
 
2007
  Session *session= current_session;
2021
2008
 
2022
2009
  /*
2023
2010
    As some compare functions are generated after sql_yacc,
2064
2051
        The following can't be recoded with || as convert_constant_item
2065
2052
        changes the argument
2066
2053
      */
2067
 
      if (convert_constant_item(&getSession(), field_item, &args[1]))
 
2054
      if (convert_constant_item(session, field_item, &args[1]))
2068
2055
        cmp_type=INT_RESULT;                    // Works for all types.
2069
 
      if (convert_constant_item(&getSession(), field_item, &args[2]))
 
2056
      if (convert_constant_item(session, field_item, &args[2]))
2070
2057
        cmp_type=INT_RESULT;                    // Works for all types.
2071
2058
    }
2072
2059
  }
3217
3204
  return (unsigned char*) &tmp;
3218
3205
}
3219
3206
 
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
3207
void in_datetime::set(uint32_t pos, Item *item)
3228
3208
{
3229
3209
  Item **tmp_item= &item;
3574
3554
{
3575
3555
  Item **arg, **arg_end;
3576
3556
  bool const_itm= 1;
 
3557
  Session *session= current_session;
3577
3558
  bool datetime_found= false;
3578
3559
  /* true <=> arguments values will be compared as DATETIMEs. */
3579
3560
  bool compare_as_datetime= false;
3721
3702
          bool all_converted= true;
3722
3703
          for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3723
3704
          {
3724
 
            if (!convert_constant_item (&getSession(), field_item, &arg[0]))
 
3705
            if (!convert_constant_item (session, field_item, &arg[0]))
3725
3706
              all_converted= false;
3726
3707
          }
3727
3708
          if (all_converted)
3758
3739
      }
3759
3740
    }
3760
3741
 
3761
 
    if (array && !(getSession().is_fatal_error))                // If not EOM
 
3742
    if (array && !(session->is_fatal_error))            // If not EOM
3762
3743
    {
3763
3744
      uint32_t j=0;
3764
3745
      for (uint32_t arg_num=1 ; arg_num < arg_count ; arg_num++)
3886
3867
 
3887
3868
void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
3888
3869
{
3889
 
  List<Item>::iterator li(item->list.begin());
 
3870
  List_iterator_fast<Item> li(item->list);
3890
3871
  while (Item *it= li++)
3891
3872
    list.push_back(it->copy_andor_structure(session));
3892
3873
}
3896
3877
Item_cond::fix_fields(Session *session, Item **)
3897
3878
{
3898
3879
  assert(fixed == 0);
3899
 
  List<Item>::iterator li(list.begin());
 
3880
  List_iterator<Item> li(list);
3900
3881
  Item *item;
3901
3882
  void *orig_session_marker= session->session_marker;
3902
3883
  unsigned char buff[sizeof(char*)];                    // Max local vars in function
3936
3917
           !((Item_cond*) item)->list.is_empty())
3937
3918
    {                                           // Identical function
3938
3919
      li.replace(((Item_cond*) item)->list);
3939
 
      ((Item_cond*) item)->list.clear();
 
3920
      ((Item_cond*) item)->list.empty();
3940
3921
      item= *li.ref();                          // new current item
3941
3922
    }
3942
3923
    if (abort_on_null)
3962
3943
    if (item->maybe_null)
3963
3944
      maybe_null=1;
3964
3945
  }
3965
 
  session->getLex()->current_select->cond_count+= list.elements;
 
3946
  session->lex->current_select->cond_count+= list.elements;
3966
3947
  session->session_marker= orig_session_marker;
3967
3948
  fix_length_and_dec();
3968
3949
  fixed= 1;
3972
3953
 
3973
3954
void Item_cond::fix_after_pullout(Select_Lex *new_parent, Item **)
3974
3955
{
3975
 
  List<Item>::iterator li(list.begin());
 
3956
  List_iterator<Item> li(list);
3976
3957
  Item *item;
3977
3958
 
3978
3959
  used_tables_cache=0;
4004
3985
 
4005
3986
bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
4006
3987
{
4007
 
  List<Item>::iterator li(list.begin());
 
3988
  List_iterator_fast<Item> li(list);
4008
3989
  Item *item;
4009
3990
  while ((item= li++))
4010
3991
    if (item->walk(processor, walk_subquery, arg))
4033
4014
 
4034
4015
Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
4035
4016
{
4036
 
  List<Item>::iterator li(list.begin());
 
4017
  List_iterator<Item> li(list);
4037
4018
  Item *item;
4038
4019
  while ((item= li++))
4039
4020
  {
4048
4029
      change records at each execution.
4049
4030
    */
4050
4031
    if (new_item != item)
4051
 
      getSession().change_item_tree(li.ref(), new_item);
 
4032
      current_session->change_item_tree(li.ref(), new_item);
4052
4033
  }
4053
4034
  return Item_func::transform(transformer, arg);
4054
4035
}
4084
4065
  if (!(this->*analyzer)(arg_p))
4085
4066
    return 0;
4086
4067
 
4087
 
  List<Item>::iterator li(list.begin());
 
4068
  List_iterator<Item> li(list);
4088
4069
  Item *item;
4089
4070
  while ((item= li++))
4090
4071
  {
4103
4084
void Item_cond::traverse_cond(Cond_traverser traverser,
4104
4085
                              void *arg, traverse_order order)
4105
4086
{
4106
 
  List<Item>::iterator li(list.begin());
 
4087
  List_iterator<Item> li(list);
4107
4088
  Item *item;
4108
4089
 
4109
4090
  switch (order) {
4144
4125
void Item_cond::split_sum_func(Session *session, Item **ref_pointer_array,
4145
4126
                               List<Item> &fields)
4146
4127
{
4147
 
  List<Item>::iterator li(list.begin());
 
4128
  List_iterator<Item> li(list);
4148
4129
  Item *item;
4149
4130
  while ((item= li++))
4150
4131
    item->split_sum_func(session, ref_pointer_array,
4161
4142
 
4162
4143
void Item_cond::update_used_tables()
4163
4144
{
4164
 
  List<Item>::iterator li(list.begin());
 
4145
  List_iterator_fast<Item> li(list);
4165
4146
  Item *item;
4166
4147
 
4167
4148
  used_tables_cache=0;
4178
4159
void Item_cond::print(String *str, enum_query_type query_type)
4179
4160
{
4180
4161
  str->append('(');
4181
 
  List<Item>::iterator li(list.begin());
 
4162
  List_iterator_fast<Item> li(list);
4182
4163
  Item *item;
4183
4164
  if ((item=li++))
4184
4165
    item->print(str, query_type);
4195
4176
 
4196
4177
void Item_cond::neg_arguments(Session *session)
4197
4178
{
4198
 
  List<Item>::iterator li(list.begin());
 
4179
  List_iterator<Item> li(list);
4199
4180
  Item *item;
4200
4181
  while ((item= li++))          /* Apply not transformation to the arguments */
4201
4182
  {
4233
4214
int64_t Item_cond_and::val_int()
4234
4215
{
4235
4216
  assert(fixed == 1);
4236
 
  List<Item>::iterator li(list.begin());
 
4217
  List_iterator_fast<Item> li(list);
4237
4218
  Item *item;
4238
4219
  null_value= 0;
4239
4220
  while ((item=li++))
4251
4232
int64_t Item_cond_or::val_int()
4252
4233
{
4253
4234
  assert(fixed == 1);
4254
 
  List<Item>::iterator li(list.begin());
 
4235
  List_iterator_fast<Item> li(list);
4255
4236
  Item *item;
4256
4237
  null_value=0;
4257
4238
  while ((item=li++))
4488
4469
      {
4489
4470
        pattern     = first + 1;
4490
4471
        pattern_len = (int) len - 2;
4491
 
        int *suff = (int*) session->getMemRoot()->allocate((int) (sizeof(int)*
4492
 
                                                                  ((pattern_len + 1)*2+
4493
 
                                                                   alphabet_size)));
 
4472
        int *suff = (int*) session->alloc((int) (sizeof(int)*
 
4473
                                      ((pattern_len + 1)*2+
 
4474
                                      alphabet_size)));
4494
4475
        bmGs      = suff + pattern_len + 1;
4495
4476
        bmBc      = bmGs + pattern_len + 1;
4496
4477
        turboBM_compute_good_suffix_shifts(suff);
4507
4488
  Item_bool_func2::cleanup();
4508
4489
}
4509
4490
 
4510
 
static unsigned char likeconv(const CHARSET_INFO *cs, unsigned char a)
4511
 
{
4512
4491
#ifdef LIKE_CMP_TOUPPER
4513
 
  return cs->toupper(a);
 
4492
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
4514
4493
#else
4515
 
  return cs->sort_order[a];
 
4494
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
4516
4495
#endif
4517
 
}
 
4496
 
4518
4497
 
4519
4498
/**
4520
4499
  Precomputation dependent only on pattern_len.
4651
4630
 
4652
4631
bool Item_func_like::turboBM_matches(const char* text, int text_len) const
4653
4632
{
4654
 
  int bcShift;
4655
 
  int turboShift;
 
4633
  register int bcShift;
 
4634
  register int turboShift;
4656
4635
  int shift = pattern_len;
4657
4636
  int j     = 0;
4658
4637
  int u     = 0;
4666
4645
  {
4667
4646
    while (j <= tlmpl)
4668
4647
    {
4669
 
      int i= plm1;
 
4648
      register int i= plm1;
4670
4649
      while (i >= 0 && pattern[i] == text[i + j])
4671
4650
      {
4672
4651
        i--;
4676
4655
      if (i < 0)
4677
4656
        return 1;
4678
4657
 
4679
 
      const int v = plm1 - i;
 
4658
      register const int v = plm1 - i;
4680
4659
      turboShift = u - v;
4681
4660
      bcShift    = bmBc[(uint32_t) (unsigned char) text[i + j]] - plm1 + i;
4682
4661
      shift      = (turboShift > bcShift) ? turboShift : bcShift;
4697
4676
  {
4698
4677
    while (j <= tlmpl)
4699
4678
    {
4700
 
      int i = plm1;
 
4679
      register int i = plm1;
4701
4680
      while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
4702
4681
      {
4703
4682
        i--;
4708
4687
      if (i < 0)
4709
4688
        return 1;
4710
4689
 
4711
 
      const int v= plm1 - i;
 
4690
      register const int v= plm1 - i;
4712
4691
      turboShift= u - v;
4713
4692
      bcShift= bmBc[(uint32_t) likeconv(cs, text[i + j])] - plm1 + i;
4714
4693
      shift= (turboShift > bcShift) ? turboShift : bcShift;
4749
4728
int64_t Item_cond_xor::val_int()
4750
4729
{
4751
4730
  assert(fixed == 1);
4752
 
  List<Item>::iterator li(list.begin());
 
4731
  List_iterator<Item> li(list);
4753
4732
  Item *item;
4754
4733
  int result=0;
4755
4734
  null_value=0;
4929
4908
  : item::function::Boolean(), eval_item(0), cond_false(0)
4930
4909
{
4931
4910
  const_item_cache= false;
4932
 
  List<Item_field>::iterator li(item_equal->fields.begin());
 
4911
  List_iterator_fast<Item_field> li(item_equal->fields);
4933
4912
  Item_field *item;
4934
4913
  while ((item= li++))
4935
4914
  {
4981
4960
 
4982
4961
bool Item_equal::contains(Field *field)
4983
4962
{
4984
 
  List<Item_field>::iterator it(fields.begin());
 
4963
  List_iterator_fast<Item_field> it(fields);
4985
4964
  Item_field *item;
4986
4965
  while ((item= it++))
4987
4966
  {
5040
5019
void Item_equal::sort(Item_field_cmpfunc cmp, void *arg)
5041
5020
{
5042
5021
  bool swap;
5043
 
  List<Item_field>::iterator it(fields.begin());
 
5022
  List_iterator<Item_field> it(fields);
5044
5023
  do
5045
5024
  {
5046
5025
    Item_field *item1= it++;
5064
5043
        ref1= ref2;
5065
5044
      }
5066
5045
    }
5067
 
    it= fields.begin();
 
5046
    it.rewind();
5068
5047
  } while (swap);
5069
5048
}
5070
5049
 
5081
5060
 
5082
5061
void Item_equal::update_const()
5083
5062
{
5084
 
  List<Item_field>::iterator it(fields.begin());
 
5063
  List_iterator<Item_field> it(fields);
5085
5064
  Item *item;
5086
5065
  while ((item= it++))
5087
5066
  {
5095
5074
 
5096
5075
bool Item_equal::fix_fields(Session *, Item **)
5097
5076
{
5098
 
  List<Item_field>::iterator li(fields.begin());
 
5077
  List_iterator_fast<Item_field> li(fields);
5099
5078
  Item *item;
5100
5079
  not_null_tables_cache= used_tables_cache= 0;
5101
5080
  const_item_cache= false;
5115
5094
 
5116
5095
void Item_equal::update_used_tables()
5117
5096
{
5118
 
  List<Item_field>::iterator li(fields.begin());
 
5097
  List_iterator_fast<Item_field> li(fields);
5119
5098
  Item *item;
5120
5099
  not_null_tables_cache= used_tables_cache= 0;
5121
5100
  if ((const_item_cache= cond_false))
5133
5112
  Item_field *item_field;
5134
5113
  if (cond_false)
5135
5114
    return 0;
5136
 
  List<Item_field>::iterator it(fields.begin());
 
5115
  List_iterator_fast<Item_field> it(fields);
5137
5116
  Item *item= const_item ? const_item : it++;
5138
5117
  eval_item->store_value(item);
5139
5118
  if ((null_value= item->null_value))
5159
5138
 
5160
5139
bool Item_equal::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
5161
5140
{
5162
 
  List<Item_field>::iterator it(fields.begin());
 
5141
  List_iterator_fast<Item_field> it(fields);
5163
5142
  Item *item;
5164
5143
  while ((item= it++))
5165
5144
  {
5171
5150
 
5172
5151
Item *Item_equal::transform(Item_transformer transformer, unsigned char *arg)
5173
5152
{
5174
 
  List<Item_field>::iterator it(fields.begin());
 
5153
  List_iterator<Item_field> it(fields);
5175
5154
  Item *item;
5176
5155
  while ((item= it++))
5177
5156
  {
5186
5165
      change records at each execution.
5187
5166
    */
5188
5167
    if (new_item != item)
5189
 
      getSession().change_item_tree((Item **) it.ref(), new_item);
 
5168
      current_session->change_item_tree((Item **) it.ref(), new_item);
5190
5169
  }
5191
5170
  return Item_func::transform(transformer, arg);
5192
5171
}
5195
5174
{
5196
5175
  str->append(func_name());
5197
5176
  str->append('(');
5198
 
  List<Item_field>::iterator it(fields.begin());
 
5177
  List_iterator_fast<Item_field> it(fields);
5199
5178
  Item *item;
5200
5179
  if (const_item)
5201
5180
    const_item->print(str, query_type);
5213
5192
  str->append(')');
5214
5193
}
5215
5194
 
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
5195
} /* namespace drizzled */