21
21
This file defines all compare functions
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"
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>
36
41
#include <algorithm>
540
543
if (field_item->field->can_be_compared_as_int64_t() &&
541
544
!(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
543
if (convert_constant_item(session, field_item, &args[1]))
546
if (convert_constant_item(&getSession(), field_item, &args[1]))
545
548
cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
546
549
INT_RESULT); // Works for all types.
556
559
!(field_item->is_datetime() &&
557
560
args[0]->result_type() == STRING_RESULT))
559
if (convert_constant_item(session, field_item, &args[0]))
562
if (convert_constant_item(&getSession(), field_item, &args[0]))
561
564
cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
562
565
INT_RESULT); // Works for all types.
575
Arg_comparator::Arg_comparator():
576
session(current_session),
581
Arg_comparator::Arg_comparator(Item **a1, Item **a2):
584
session(current_session),
573
589
int Arg_comparator::set_compare_func(Item_bool_func2 *item, Item_result type)
696
712
converted value. 0 on error and on zero-dates -- check 'failure'
700
get_date_from_str(Session *session, String *str, enum enum_drizzle_timestamp_type warn_type,
716
get_date_from_str(Session *session, String *str, type::timestamp_t warn_type,
701
717
char *warn_name, bool *error_arg)
706
enum enum_drizzle_timestamp_type ret;
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)),
713
if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
720
type::cut_t error= type::VALID;
722
type::timestamp_t ret;
724
ret= l_time.store(str->ptr(), str->length(),
725
(TIME_FUZZY_DATE | MODE_INVALID_DATES | (session->variables.sql_mode & MODE_NO_ZERO_DATE)),
728
if (ret == type::DRIZZLE_TIMESTAMP_DATETIME || ret == type::DRIZZLE_TIMESTAMP_DATE)
716
731
Do not return yet, we may still want to throw a "trailing garbage"
719
734
*error_arg= false;
720
value= TIME_to_uint64_t_datetime(&l_time);
735
l_time.convert(value);
724
739
*error_arg= true;
725
error= 1; /* force warning */
740
error= type::CUT; /* force warning */
743
if (error != type::VALID)
730
745
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
731
746
str->ptr(), str->length(),
771
786
enum Arg_comparator::enum_date_cmp_type
772
787
Arg_comparator::can_compare_as_dates(Item *in_a, Item *in_b,
773
uint64_t *const_value)
788
int64_t *const_value)
775
790
enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
776
791
Item *str_arg= 0, *date_arg= 0;
865
880
Item **a1, Item **a2,
866
881
Item_result type)
868
enum enum_date_cmp_type cmp_type;
869
uint64_t const_value= (uint64_t)-1;
883
enum_date_cmp_type cmp_type;
884
int64_t const_value= -1;
873
888
if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
875
session= current_session;
876
890
owner= owner_arg;
877
891
a_type= (*a)->field_type();
878
892
b_type= (*b)->field_type();
882
if (const_value != (uint64_t)-1)
896
if (const_value != -1)
884
898
Item_cache_int *cache= new Item_cache_int();
885
899
/* Mark the cache as non-const to prevent re-caching. */
995
1011
enum_field_types f_type= warn_item->field_type();
996
enum enum_drizzle_timestamp_type t_type= f_type ==
997
DRIZZLE_TYPE_DATE ? DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME;
1012
type::timestamp_t t_type= f_type == DRIZZLE_TYPE_DATE ? type::DRIZZLE_TIMESTAMP_DATE : type::DRIZZLE_TIMESTAMP_DATETIME;
998
1013
value= get_date_from_str(session, str, t_type, warn_item->name, &error);
1000
1015
If str did not contain a valid date according to the current
1174
1190
int Arg_comparator::compare_decimal()
1177
my_decimal *val1= (*a)->val_decimal(&value1);
1192
type::Decimal value1;
1193
type::Decimal *val1= (*a)->val_decimal(&value1);
1178
1194
if (!(*a)->null_value)
1181
my_decimal *val2= (*b)->val_decimal(&value2);
1196
type::Decimal value2;
1197
type::Decimal *val2= (*b)->val_decimal(&value2);
1182
1198
if (!(*b)->null_value)
1184
1200
owner->null_value= 0;
1185
return my_decimal_cmp(val1, val2);
1201
return class_decimal_cmp(val1, val2);
1188
1204
owner->null_value= 1;
1201
1217
int Arg_comparator::compare_e_decimal()
1203
my_decimal value1, value2;
1204
my_decimal *val1= (*a)->val_decimal(&value1);
1205
my_decimal *val2= (*b)->val_decimal(&value2);
1219
type::Decimal value1, value2;
1220
type::Decimal *val1= (*a)->val_decimal(&value1);
1221
type::Decimal *val2= (*b)->val_decimal(&value2);
1206
1222
if ((*a)->null_value || (*b)->null_value)
1207
1223
return test((*a)->null_value && (*b)->null_value);
1208
return test(my_decimal_cmp(val1, val2) == 0);
1224
return test(class_decimal_cmp(val1, val2) == 0);
1668
1684
change records at each execution.
1670
1686
if ((*args) != new_item)
1671
current_session->change_item_tree(args, new_item);
1687
getSession().change_item_tree(args, new_item);
1674
1690
Transform the right IN operand which should be an Item_in_subselect or a
1881
1897
dec= row->element_index(0)->val_decimal(&dec_buf);
1882
1898
if (row->element_index(0)->null_value)
1884
my_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
1900
class_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
1906
1922
and we are comparing against a decimal
1908
1924
if (dec && range->type == DECIMAL_RESULT)
1909
cmp_result= my_decimal_cmp(&range->dec, dec) <= 0;
1925
cmp_result= class_decimal_cmp(&range->dec, dec) <= 0;
1911
1927
cmp_result= (range->dbl <= value);
1912
1928
if (cmp_result)
1917
1933
interval_range *range= intervals+start;
1918
1934
return ((dec && range->type == DECIMAL_RESULT) ?
1919
my_decimal_cmp(dec, &range->dec) < 0 :
1935
class_decimal_cmp(dec, &range->dec) < 0 :
1920
1936
value < range->dbl) ? 0 : start + 1;
1927
1943
((el->result_type() == DECIMAL_RESULT) ||
1928
1944
(el->result_type() == INT_RESULT)))
1930
my_decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
1946
type::Decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
1931
1947
/* Skip NULL ranges. */
1932
1948
if (el->null_value)
1934
if (my_decimal_cmp(e_dec, dec) > 0)
1950
if (class_decimal_cmp(e_dec, dec) > 0)
1981
1997
if (Item_func_opt_neg::fix_fields(session, ref))
1984
session->lex->current_select->between_count++;
2000
session->getLex()->current_select->between_count++;
1986
2002
/* not_null_tables_cache == union(T1(e),T1(e1),T1(e2)) */
1987
2003
if (pred_level && !negated)
2049
2064
The following can't be recoded with || as convert_constant_item
2050
2065
changes the argument
2052
if (convert_constant_item(session, field_item, &args[1]))
2067
if (convert_constant_item(&getSession(), field_item, &args[1]))
2053
2068
cmp_type=INT_RESULT; // Works for all types.
2054
if (convert_constant_item(session, field_item, &args[2]))
2069
if (convert_constant_item(&getSession(), field_item, &args[2]))
2055
2070
cmp_type=INT_RESULT; // Works for all types.
2129
2144
else if (cmp_type == DECIMAL_RESULT)
2131
my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2146
type::Decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2132
2147
a_buf, *a_dec, b_buf, *b_dec;
2133
2148
if ((null_value=args[0]->null_value))
2135
2150
a_dec= args[1]->val_decimal(&a_buf);
2136
2151
b_dec= args[2]->val_decimal(&b_buf);
2137
2152
if (!args[1]->null_value && !args[2]->null_value)
2138
return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2139
my_decimal_cmp(dec, b_dec) <= 0) != negated);
2153
return (int64_t) ((class_decimal_cmp(dec, a_dec) >= 0 &&
2154
class_decimal_cmp(dec, b_dec) <= 0) != negated);
2140
2155
if (args[1]->null_value && args[2]->null_value)
2142
2157
else if (args[1]->null_value)
2143
null_value= (my_decimal_cmp(dec, b_dec) <= 0);
2158
null_value= (class_decimal_cmp(dec, b_dec) <= 0);
2145
null_value= (my_decimal_cmp(dec, a_dec) >= 0);
2160
null_value= (class_decimal_cmp(dec, a_dec) >= 0);
2279
my_decimal *Item_func_ifnull::decimal_op(my_decimal *decimal_value)
2294
type::Decimal *Item_func_ifnull::decimal_op(type::Decimal *decimal_value)
2281
2296
assert(fixed == 1);
2282
my_decimal *value= args[0]->val_decimal(decimal_value);
2297
type::Decimal *value= args[0]->val_decimal(decimal_value);
2283
2298
if (!args[0]->null_value)
2453
Item_func_if::val_decimal(my_decimal *decimal_value)
2468
Item_func_if::val_decimal(type::Decimal *decimal_value)
2455
2470
assert(fixed == 1);
2456
2471
Item *arg= args[0]->val_bool() ? args[1] : args[2];
2457
my_decimal *value= arg->val_decimal(decimal_value);
2472
type::Decimal *value= arg->val_decimal(decimal_value);
2458
2473
null_value= arg->null_value;
2674
my_decimal *Item_func_case::val_decimal(my_decimal *decimal_value)
2689
type::Decimal *Item_func_case::val_decimal(type::Decimal *decimal_value)
2676
2691
assert(fixed == 1);
2677
2692
char buff[MAX_FIELD_WIDTH];
2678
2693
String dummy_str(buff, sizeof(buff), default_charset());
2679
2694
Item *item= find_item(&dummy_str);
2721
2736
void Item_func_case::agg_num_lengths(Item *arg)
2723
uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2738
uint32_t len= class_decimal_length_to_precision(arg->max_length, arg->decimals,
2724
2739
arg->unsigned_flag) - arg->decimals;
2725
2740
set_if_bigger(max_length, len);
2726
2741
set_if_bigger(decimals, arg->decimals);
2803
2818
agg_num_lengths(args[i + 1]);
2804
2819
if (else_expr_num != -1)
2805
2820
agg_num_lengths(args[else_expr_num]);
2806
max_length= my_decimal_precision_to_length(max_length + decimals, decimals,
2821
max_length= class_decimal_precision_to_length(max_length + decimals, decimals,
2807
2822
unsigned_flag);
2913
my_decimal *Item_func_coalesce::decimal_op(my_decimal *decimal_value)
2928
type::Decimal *Item_func_coalesce::decimal_op(type::Decimal *decimal_value)
2915
2930
assert(fixed == 1);
2917
2932
for (uint32_t i= 0; i < arg_count; i++)
2919
my_decimal *res= args[i]->val_decimal(decimal_value);
2934
type::Decimal *res= args[i]->val_decimal(decimal_value);
2920
2935
if (!args[i]->null_value)
3068
static int cmp_decimal(void *, my_decimal *a, my_decimal *b)
3083
static int cmp_decimal(void *, type::Decimal *a, type::Decimal *b)
3071
3086
We need call of fixing buffer pointer, because fast sort just copy
3184
in_int64_t::in_int64_t(uint32_t elements)
3185
:in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3199
in_int64_t::in_int64_t(uint32_t elements) :
3200
in_vector(elements, sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3188
3203
void in_int64_t::set(uint32_t pos,Item *item)
3202
3217
return (unsigned char*) &tmp;
3205
void in_datetime::set(uint32_t pos,Item *item)
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),
3227
void in_datetime::set(uint32_t pos, Item *item)
3207
3229
Item **tmp_item= &item;
3244
3266
in_decimal::in_decimal(uint32_t elements)
3245
:in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3267
:in_vector(elements, sizeof(type::Decimal),(qsort2_cmp) cmp_decimal, 0)
3249
3271
void in_decimal::set(uint32_t pos, Item *item)
3251
/* as far as 'item' is constant, we can store reference on my_decimal */
3252
my_decimal *dec= ((my_decimal *)base) + pos;
3273
/* as far as 'item' is constant, we can store reference on type::Decimal */
3274
type::Decimal *dec= ((type::Decimal *)base) + pos;
3253
3275
dec->len= DECIMAL_BUFF_LENGTH;
3254
3276
dec->fix_buffer_pointer();
3255
my_decimal *res= item->val_decimal(dec);
3277
type::Decimal *res= item->val_decimal(dec);
3256
3278
/* if item->val_decimal() is evaluated to NULL then res == 0 */
3257
3279
if (!item->null_value && res != dec)
3258
my_decimal2decimal(res, dec);
3280
class_decimal2decimal(res, dec);
3262
3284
unsigned char *in_decimal::get_value(Item *item)
3264
my_decimal *result= item->val_decimal(&val);
3286
type::Decimal *result= item->val_decimal(&val);
3265
3287
if (item->null_value)
3267
3289
return (unsigned char *)result;
3421
3443
void cmp_item_decimal::store_value(Item *item)
3423
my_decimal *val= item->val_decimal(&value);
3445
type::Decimal *val= item->val_decimal(&value);
3424
3446
/* val may be zero if item is nnull */
3425
3447
if (val && val != &value)
3426
my_decimal2decimal(val, &value);
3448
class_decimal2decimal(val, &value);
3430
3452
int cmp_item_decimal::cmp(Item *arg)
3432
my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3454
type::Decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3433
3455
if (arg->null_value)
3435
return my_decimal_cmp(&value, tmp);
3457
return class_decimal_cmp(&value, tmp);
3439
3461
int cmp_item_decimal::compare(cmp_item *arg)
3441
3463
cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3442
return my_decimal_cmp(&value, &l_cmp->value);
3464
return class_decimal_cmp(&value, &l_cmp->value);
3700
3721
bool all_converted= true;
3701
3722
for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3703
if (!convert_constant_item (session, field_item, &arg[0]))
3724
if (!convert_constant_item (&getSession(), field_item, &arg[0]))
3704
3725
all_converted= false;
3706
3727
if (all_converted)
3855
3876
Item_cond::Item_cond(Session *session, Item_cond *item)
3856
:Item_bool_func(session, item),
3877
:item::function::Boolean(session, item),
3857
3878
abort_on_null(item->abort_on_null),
3858
3879
and_tables_cache(item->and_tables_cache)
3866
3887
void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
3868
List_iterator_fast<Item> li(item->list);
3889
List<Item>::iterator li(item->list.begin());
3869
3890
while (Item *it= li++)
3870
3891
list.push_back(it->copy_andor_structure(session));
3875
3896
Item_cond::fix_fields(Session *session, Item **)
3877
3898
assert(fixed == 0);
3878
List_iterator<Item> li(list);
3899
List<Item>::iterator li(list.begin());
3880
3901
void *orig_session_marker= session->session_marker;
3881
3902
unsigned char buff[sizeof(char*)]; // Max local vars in function
3882
3903
not_null_tables_cache= used_tables_cache= 0;
3883
const_item_cache= 1;
3904
const_item_cache= true;
3885
3906
if (functype() == COND_OR_FUNC)
3886
3907
session->session_marker= 0;
3915
3936
!((Item_cond*) item)->list.is_empty())
3916
3937
{ // Identical function
3917
3938
li.replace(((Item_cond*) item)->list);
3918
((Item_cond*) item)->list.empty();
3939
((Item_cond*) item)->list.clear();
3919
3940
item= *li.ref(); // new current item
3921
3942
if (abort_on_null)
3952
3973
void Item_cond::fix_after_pullout(Select_Lex *new_parent, Item **)
3954
List_iterator<Item> li(list);
3975
List<Item>::iterator li(list.begin());
3957
3978
used_tables_cache=0;
3979
const_item_cache= true;
3960
3981
and_tables_cache= ~(table_map) 0; // Here and below we do as fix_fields does
3961
3982
not_null_tables_cache= 0;
3984
4005
bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
3986
List_iterator_fast<Item> li(list);
4007
List<Item>::iterator li(list.begin());
3988
4009
while ((item= li++))
3989
4010
if (item->walk(processor, walk_subquery, arg))
4027
4048
change records at each execution.
4029
4050
if (new_item != item)
4030
current_session->change_item_tree(li.ref(), new_item);
4051
getSession().change_item_tree(li.ref(), new_item);
4032
4053
return Item_func::transform(transformer, arg);
4082
4103
void Item_cond::traverse_cond(Cond_traverser traverser,
4083
4104
void *arg, traverse_order order)
4085
List_iterator<Item> li(list);
4106
List<Item>::iterator li(list.begin());
4088
4109
switch (order) {
4123
4144
void Item_cond::split_sum_func(Session *session, Item **ref_pointer_array,
4124
4145
List<Item> &fields)
4126
List_iterator<Item> li(list);
4147
List<Item>::iterator li(list.begin());
4128
4149
while ((item= li++))
4129
4150
item->split_sum_func(session, ref_pointer_array,
4157
4178
void Item_cond::print(String *str, enum_query_type query_type)
4159
4180
str->append('(');
4160
List_iterator_fast<Item> li(list);
4181
List<Item>::iterator li(list.begin());
4162
4183
if ((item=li++))
4163
4184
item->print(str, query_type);
4468
4489
pattern = first + 1;
4469
4490
pattern_len = (int) len - 2;
4470
int *suff = (int*) session->alloc((int) (sizeof(int)*
4471
((pattern_len + 1)*2+
4491
int *suff = (int*) session->getMemRoot()->allocate((int) (sizeof(int)*
4492
((pattern_len + 1)*2+
4473
4494
bmGs = suff + pattern_len + 1;
4474
4495
bmBc = bmGs + pattern_len + 1;
4475
4496
turboBM_compute_good_suffix_shifts(suff);
4486
4507
Item_bool_func2::cleanup();
4510
static unsigned char likeconv(const CHARSET_INFO *cs, unsigned char a)
4489
4512
#ifdef LIKE_CMP_TOUPPER
4490
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
4513
return cs->toupper(a);
4492
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
4515
return cs->sort_order[a];
4497
4520
Precomputation dependent only on pattern_len.
4888
4911
Item_equal::Item_equal(Item_field *f1, Item_field *f2)
4889
: Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
4912
: item::function::Boolean(), const_item(0), eval_item(0), cond_false(0)
4891
const_item_cache= 0;
4914
const_item_cache= false;
4892
4915
fields.push_back(f1);
4893
4916
fields.push_back(f2);
4896
4919
Item_equal::Item_equal(Item *c, Item_field *f)
4897
: Item_bool_func(), eval_item(0), cond_false(0)
4920
: item::function::Boolean(), eval_item(0), cond_false(0)
4899
const_item_cache= 0;
4922
const_item_cache= false;
4900
4923
fields.push_back(f);
4905
4928
Item_equal::Item_equal(Item_equal *item_equal)
4906
: Item_bool_func(), eval_item(0), cond_false(0)
4929
: item::function::Boolean(), eval_item(0), cond_false(0)
4908
const_item_cache= 0;
4909
List_iterator_fast<Item_field> li(item_equal->fields);
4931
const_item_cache= false;
4932
List<Item_field>::iterator li(item_equal->fields.begin());
4910
4933
Item_field *item;
4911
4934
while ((item= li++))
5073
5096
bool Item_equal::fix_fields(Session *, Item **)
5075
List_iterator_fast<Item_field> li(fields);
5098
List<Item_field>::iterator li(fields.begin());
5077
5100
not_null_tables_cache= used_tables_cache= 0;
5078
const_item_cache= 0;
5101
const_item_cache= false;
5079
5102
while ((item= li++))
5081
5104
table_map tmp_table_map;
5093
5116
void Item_equal::update_used_tables()
5095
List_iterator_fast<Item_field> li(fields);
5118
List<Item_field>::iterator li(fields.begin());
5097
5120
not_null_tables_cache= used_tables_cache= 0;
5098
5121
if ((const_item_cache= cond_false))
5110
5133
Item_field *item_field;
5111
5134
if (cond_false)
5113
List_iterator_fast<Item_field> it(fields);
5136
List<Item_field>::iterator it(fields.begin());
5114
5137
Item *item= const_item ? const_item : it++;
5138
eval_item->store_value(item);
5115
5139
if ((null_value= item->null_value))
5117
eval_item->store_value(item);
5118
5141
while ((item_field= it++))
5120
5143
/* Skip fields of non-const tables. They haven't been read yet */
5121
5144
if (item_field->field->getTable()->const_table)
5123
if ((null_value= item_field->null_value) || eval_item->cmp(item_field))
5146
if (eval_item->cmp(item_field) || (null_value= item_field->null_value))
5163
5186
change records at each execution.
5165
5188
if (new_item != item)
5166
current_session->change_item_tree((Item **) it.ref(), new_item);
5189
getSession().change_item_tree((Item **) it.ref(), new_item);
5168
5191
return Item_func::transform(transformer, arg);
5173
5196
str->append(func_name());
5174
5197
str->append('(');
5175
List_iterator_fast<Item_field> it(fields);
5198
List<Item_field>::iterator it(fields.begin());
5177
5200
if (const_item)
5178
5201
const_item->print(str, query_type);
5190
5213
str->append(')');
5216
cmp_item_datetime::cmp_item_datetime(Item *warn_item_arg) :
5217
session(current_session),
5218
warn_item(warn_item_arg),
5193
5222
} /* namespace drizzled */