~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/cmpfunc.cc

  • Committer: Brian Aker
  • Date: 2011-01-06 05:17:09 UTC
  • Revision ID: brian@tangent.org-20110106051709-oa0se8ur02uc6i9o
Added native functions into the function table.

Show diffs side-by-side

added added

removed removed

Lines of Context:
493
493
void Item_bool_func2::fix_length_and_dec()
494
494
{
495
495
  max_length= 1;                                     // Function returns 0 or 1
 
496
  Session *session;
496
497
 
497
498
  /*
498
499
    As some compare functions are generated after sql_yacc,
530
531
    return;
531
532
  }
532
533
 
 
534
  session= current_session;
533
535
  Item_field *field_item= NULL;
534
536
 
535
537
  if (args[0]->real_item()->type() == FIELD_ITEM)
538
540
    if (field_item->field->can_be_compared_as_int64_t() &&
539
541
        !(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
540
542
    {
541
 
      if (convert_constant_item(&getSession(), field_item, &args[1]))
 
543
      if (convert_constant_item(session, field_item, &args[1]))
542
544
      {
543
545
        cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
544
546
                         INT_RESULT);           // Works for all types.
554
556
          !(field_item->is_datetime() &&
555
557
            args[0]->result_type() == STRING_RESULT))
556
558
      {
557
 
        if (convert_constant_item(&getSession(), field_item, &args[0]))
 
559
        if (convert_constant_item(session, field_item, &args[0]))
558
560
        {
559
561
          cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
560
562
                           INT_RESULT); // Works for all types.
694
696
    converted value. 0 on error and on zero-dates -- check 'failure'
695
697
*/
696
698
 
697
 
static int64_t
698
 
get_date_from_str(Session *session, String *str, type::timestamp_t warn_type,
 
699
static uint64_t
 
700
get_date_from_str(Session *session, String *str, enum enum_drizzle_timestamp_type warn_type,
699
701
                  char *warn_name, bool *error_arg)
700
702
{
701
 
  int64_t value= 0;
702
 
  type::cut_t error= type::VALID;
 
703
  uint64_t value= 0;
 
704
  int error;
703
705
  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)
 
706
  enum enum_drizzle_timestamp_type ret;
 
707
 
 
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);
 
712
 
 
713
  if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
711
714
  {
712
715
    /*
713
716
      Do not return yet, we may still want to throw a "trailing garbage"
714
717
      warning.
715
718
    */
716
719
    *error_arg= false;
717
 
    l_time.convert(value);
 
720
    value= TIME_to_uint64_t_datetime(&l_time);
718
721
  }
719
722
  else
720
723
  {
721
724
    *error_arg= true;
722
 
    error= type::CUT;                                   /* force warning */
 
725
    error= 1;                                   /* force warning */
723
726
  }
724
727
 
725
 
  if (error != type::VALID)
 
728
  if (error > 0)
726
729
  {
727
730
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
728
731
                                 str->ptr(), str->length(),
767
770
 
768
771
enum Arg_comparator::enum_date_cmp_type
769
772
Arg_comparator::can_compare_as_dates(Item *in_a, Item *in_b,
770
 
                                     int64_t *const_value)
 
773
                                     uint64_t *const_value)
771
774
{
772
775
  enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
773
776
  Item *str_arg= 0, *date_arg= 0;
778
781
  if (in_a->is_datetime())
779
782
  {
780
783
    if (in_b->is_datetime())
781
 
    {
782
784
      cmp_type= CMP_DATE_WITH_DATE;
783
 
    }
784
785
    else if (in_b->result_type() == STRING_RESULT)
785
786
    {
786
787
      cmp_type= CMP_DATE_WITH_STR;
822
823
       * Does a uint64_t conversion really have to happen here?  Fields return int64_t
823
824
       * from val_int(), not uint64_t...
824
825
       */
825
 
      int64_t value;
 
826
      uint64_t value;
826
827
      String *str_val;
827
828
      String tmp;
828
829
      /* DateTime used to pick up as many string conversion possibilities as possible. */
848
849
      }
849
850
 
850
851
      /* String conversion was good.  Convert to an integer for comparison purposes. */
851
 
      temporal.to_int64_t(&value);
 
852
      int64_t int_value;
 
853
      temporal.to_int64_t(&int_value);
 
854
      value= (uint64_t) int_value;
852
855
 
853
856
      if (const_value)
854
857
        *const_value= value;
862
865
                                        Item **a1, Item **a2,
863
866
                                        Item_result type)
864
867
{
865
 
  enum_date_cmp_type cmp_type;
866
 
  int64_t const_value= -1;
 
868
  enum enum_date_cmp_type cmp_type;
 
869
  uint64_t const_value= (uint64_t)-1;
867
870
  a= a1;
868
871
  b= a2;
869
872
 
870
873
  if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
871
874
  {
 
875
    session= current_session;
872
876
    owner= owner_arg;
873
877
    a_type= (*a)->field_type();
874
878
    b_type= (*b)->field_type();
875
879
    a_cache= 0;
876
880
    b_cache= 0;
877
881
 
878
 
    if (const_value != -1)
 
882
    if (const_value != (uint64_t)-1)
879
883
    {
880
884
      Item_cache_int *cache= new Item_cache_int();
881
885
      /* Mark the cache as non-const to prevent re-caching. */
896
900
    is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC);
897
901
    func= &Arg_comparator::compare_datetime;
898
902
    get_value_func= &get_datetime_value;
899
 
 
900
903
    return 0;
901
904
  }
902
905
 
906
909
 
907
910
void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
908
911
{
 
912
  session= current_session;
909
913
  /* A caller will handle null values by itself. */
910
914
  owner= NULL;
911
915
  a= a1;
949
953
    obtained value
950
954
*/
951
955
 
952
 
int64_t
 
956
uint64_t
953
957
get_datetime_value(Session *session, Item ***item_arg, Item **cache_arg,
954
958
                   Item *warn_item, bool *is_null)
955
959
{
956
 
  int64_t value= 0;
 
960
  uint64_t value= 0;
957
961
  String buf, *str= 0;
958
962
  Item *item= **item_arg;
959
963
 
977
981
    str= item->val_str(&buf);
978
982
    *is_null= item->null_value;
979
983
  }
980
 
 
981
984
  if (*is_null)
982
985
    return ~(uint64_t) 0;
983
 
 
984
986
  /*
985
987
    Convert strings to the integer DATE/DATETIME representation.
986
988
    Even if both dates provided in strings we can't compare them directly as
991
993
  {
992
994
    bool error;
993
995
    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;
 
996
    enum enum_drizzle_timestamp_type t_type= f_type ==
 
997
      DRIZZLE_TYPE_DATE ? DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME;
995
998
    value= get_date_from_str(session, str, t_type, warn_item->name, &error);
996
999
    /*
997
1000
      If str did not contain a valid date according to the current
1014
1017
    *cache_arg= cache;
1015
1018
    *item_arg= cache_arg;
1016
1019
  }
1017
 
 
1018
1020
  return value;
1019
1021
}
1020
1022
 
1666
1668
    change records at each execution.
1667
1669
  */
1668
1670
  if ((*args) != new_item)
1669
 
    getSession().change_item_tree(args, new_item);
 
1671
    current_session->change_item_tree(args, new_item);
1670
1672
 
1671
1673
  /*
1672
1674
    Transform the right IN operand which should be an Item_in_subselect or a
2000
2002
  int i;
2001
2003
  bool datetime_found= false;
2002
2004
  compare_as_dates= true;
 
2005
  Session *session= current_session;
2003
2006
 
2004
2007
  /*
2005
2008
    As some compare functions are generated after sql_yacc,
2046
2049
        The following can't be recoded with || as convert_constant_item
2047
2050
        changes the argument
2048
2051
      */
2049
 
      if (convert_constant_item(&getSession(), field_item, &args[1]))
 
2052
      if (convert_constant_item(session, field_item, &args[1]))
2050
2053
        cmp_type=INT_RESULT;                    // Works for all types.
2051
 
      if (convert_constant_item(&getSession(), field_item, &args[2]))
 
2054
      if (convert_constant_item(session, field_item, &args[2]))
2052
2055
        cmp_type=INT_RESULT;                    // Works for all types.
2053
2056
    }
2054
2057
  }
3178
3181
  return;
3179
3182
}
3180
3183
 
3181
 
in_int64_t::in_int64_t(uint32_t elements) :
3182
 
  in_vector(elements, sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
 
3184
in_int64_t::in_int64_t(uint32_t elements)
 
3185
  :in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3183
3186
{}
3184
3187
 
3185
3188
void in_int64_t::set(uint32_t pos,Item *item)
3199
3202
  return (unsigned char*) &tmp;
3200
3203
}
3201
3204
 
3202
 
void in_datetime::set(uint32_t pos, Item *item)
 
3205
void in_datetime::set(uint32_t pos,Item *item)
3203
3206
{
3204
3207
  Item **tmp_item= &item;
3205
3208
  bool is_null;
3549
3552
{
3550
3553
  Item **arg, **arg_end;
3551
3554
  bool const_itm= 1;
 
3555
  Session *session= current_session;
3552
3556
  bool datetime_found= false;
3553
3557
  /* true <=> arguments values will be compared as DATETIMEs. */
3554
3558
  bool compare_as_datetime= false;
3696
3700
          bool all_converted= true;
3697
3701
          for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3698
3702
          {
3699
 
            if (!convert_constant_item (&getSession(), field_item, &arg[0]))
 
3703
            if (!convert_constant_item (session, field_item, &arg[0]))
3700
3704
              all_converted= false;
3701
3705
          }
3702
3706
          if (all_converted)
3733
3737
      }
3734
3738
    }
3735
3739
 
3736
 
    if (array && !(getSession().is_fatal_error))                // If not EOM
 
3740
    if (array && !(session->is_fatal_error))            // If not EOM
3737
3741
    {
3738
3742
      uint32_t j=0;
3739
3743
      for (uint32_t arg_num=1 ; arg_num < arg_count ; arg_num++)
4023
4027
      change records at each execution.
4024
4028
    */
4025
4029
    if (new_item != item)
4026
 
      getSession().change_item_tree(li.ref(), new_item);
 
4030
      current_session->change_item_tree(li.ref(), new_item);
4027
4031
  }
4028
4032
  return Item_func::transform(transformer, arg);
4029
4033
}
4463
4467
      {
4464
4468
        pattern     = first + 1;
4465
4469
        pattern_len = (int) len - 2;
4466
 
        int *suff = (int*) session->getMemRoot()->allocate((int) (sizeof(int)*
4467
 
                                                                  ((pattern_len + 1)*2+
4468
 
                                                                   alphabet_size)));
 
4470
        int *suff = (int*) session->alloc((int) (sizeof(int)*
 
4471
                                      ((pattern_len + 1)*2+
 
4472
                                      alphabet_size)));
4469
4473
        bmGs      = suff + pattern_len + 1;
4470
4474
        bmBc      = bmGs + pattern_len + 1;
4471
4475
        turboBM_compute_good_suffix_shifts(suff);
4482
4486
  Item_bool_func2::cleanup();
4483
4487
}
4484
4488
 
4485
 
static unsigned char likeconv(const CHARSET_INFO *cs, unsigned char a)
4486
 
{
4487
4489
#ifdef LIKE_CMP_TOUPPER
4488
 
  return cs->toupper(a);
 
4490
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
4489
4491
#else
4490
 
  return cs->sort_order[a];
 
4492
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
4491
4493
#endif
4492
 
}
 
4494
 
4493
4495
 
4494
4496
/**
4495
4497
  Precomputation dependent only on pattern_len.
5110
5112
    return 0;
5111
5113
  List_iterator_fast<Item_field> it(fields);
5112
5114
  Item *item= const_item ? const_item : it++;
 
5115
  if ((null_value= item->null_value))
 
5116
    return 0;
5113
5117
  eval_item->store_value(item);
5114
 
  if ((null_value= item->null_value))
5115
 
    return 0;
5116
5118
  while ((item_field= it++))
5117
5119
  {
5118
5120
    /* Skip fields of non-const tables. They haven't been read yet */
5119
5121
    if (item_field->field->getTable()->const_table)
5120
5122
    {
5121
 
      if (eval_item->cmp(item_field) || (null_value= item_field->null_value))
 
5123
      if ((null_value= item_field->null_value) || eval_item->cmp(item_field))
5122
5124
        return 0;
5123
5125
    }
5124
5126
  }
5161
5163
      change records at each execution.
5162
5164
    */
5163
5165
    if (new_item != item)
5164
 
      getSession().change_item_tree((Item **) it.ref(), new_item);
 
5166
      current_session->change_item_tree((Item **) it.ref(), new_item);
5165
5167
  }
5166
5168
  return Item_func::transform(transformer, arg);
5167
5169
}