21
21
This file defines all compare functions
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>
48
extern const double log_10[309];
50
static Eq_creator eq_creator;
51
static Ne_creator ne_creator;
52
static Gt_creator gt_creator;
53
static Lt_creator lt_creator;
54
static Ge_creator ge_creator;
55
static Le_creator le_creator;
57
static bool convert_constant_item(Session *, Item_field *, Item **);
24
#ifdef USE_PRAGMA_IMPLEMENTATION
25
#pragma implementation // gcc: Class implementation
28
#include "mysql_priv.h"
29
#include "sql_select.h"
31
static bool convert_constant_item(THD *, Item_field *, Item **);
59
33
static Item_result item_store_type(Item_result a, Item *item,
34
my_bool unsigned_flag)
62
36
Item_result b= item->result_type();
225
198
Bitmap of collected types - otherwise
228
static uint32_t collect_cmp_types(Item **items, uint32_t nitems, bool skip_nulls= false)
201
static uint collect_cmp_types(Item **items, uint nitems)
231
uint32_t found_types;
232
205
Item_result left_result= items[0]->result_type();
233
206
assert(nitems > 1);
235
208
for (i= 1; i < nitems ; i++)
237
if (skip_nulls && items[i]->type() == Item::NULL_ITEM)
238
continue; // Skip NULL constant items
239
if ((left_result == ROW_RESULT ||
210
if ((left_result == ROW_RESULT ||
240
211
items[i]->result_type() == ROW_RESULT) &&
241
212
cmp_row_type(items[0], items[i]))
243
found_types|= 1<< (uint32_t)item_cmp_type(left_result,
214
found_types|= 1<< (uint)item_cmp_type(left_result,
244
215
items[i]->result_type());
247
Even if all right-hand items are NULLs and we are skipping them all, we need
248
at least one type bit in the found_type bitmask.
250
if (skip_nulls && !found_types)
251
found_types= 1 << (uint)left_result;
252
217
return found_types;
220
static void my_coll_agg_error(DTCollation &c1, DTCollation &c2,
223
my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
224
c1.collation->name,c1.derivation_name(),
225
c2.collation->name,c2.derivation_name(),
256
230
Item_bool_func2* Eq_creator::create(Item *a, Item *b) const
262
const Eq_creator* Eq_creator::instance()
268
236
Item_bool_func2* Ne_creator::create(Item *a, Item *b) const
270
238
return new Item_func_ne(a, b);
274
const Ne_creator* Ne_creator::instance()
280
242
Item_bool_func2* Gt_creator::create(Item *a, Item *b) const
282
244
return new Item_func_gt(a, b);
286
const Gt_creator* Gt_creator::instance()
292
248
Item_bool_func2* Lt_creator::create(Item *a, Item *b) const
294
250
return new Item_func_lt(a, b);
298
const Lt_creator* Lt_creator::instance()
304
254
Item_bool_func2* Ge_creator::create(Item *a, Item *b) const
306
256
return new Item_func_ge(a, b);
310
const Ge_creator* Ge_creator::instance()
316
260
Item_bool_func2* Le_creator::create(Item *a, Item *b) const
318
262
return new Item_func_le(a, b);
321
const Le_creator* Le_creator::instance()
329
267
Most of these returns 0LL if false and 1LL if true and
444
382
1 Item was replaced with an integer version of the item
447
static bool convert_constant_item(Session *session, Item_field *field_item,
385
static bool convert_constant_item(THD *thd, Item_field *field_item,
450
388
Field *field= field_item->field;
453
field->setWriteSet();
455
391
if (!(*item)->with_subselect && (*item)->const_item())
457
ulong orig_sql_mode= session->variables.sql_mode;
458
enum_check_fields orig_count_cuted_fields= session->count_cuted_fields;
393
TABLE *table= field->table;
394
ulong orig_sql_mode= thd->variables.sql_mode;
395
enum_check_fields orig_count_cuted_fields= thd->count_cuted_fields;
396
my_bitmap_map *old_write_map;
397
my_bitmap_map *old_read_map;
459
398
uint64_t orig_field_val= 0; /* original field value if valid */
402
old_write_map= dbug_tmp_use_all_columns(table, table->write_set);
403
old_read_map= dbug_tmp_use_all_columns(table, table->read_set);
461
405
/* For comparison purposes allow invalid dates like 2000-01-32 */
462
session->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
406
thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
463
407
MODE_INVALID_DATES;
464
session->count_cuted_fields= CHECK_FIELD_IGNORE;
408
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
467
411
Store the value of the field if it references an outer field because
468
412
the call to save_in_field below overrides that value.
470
414
if (field_item->depended_from)
472
415
orig_field_val= field->val_int();
475
416
if (!(*item)->is_null() && !(*item)->save_in_field(field, 1))
477
418
Item *tmp= new Item_int_with_ref(field->val_int(), *item,
478
419
test(field->flags & UNSIGNED_FLAG));
480
session->change_item_tree(item, tmp);
421
thd->change_item_tree(item, tmp);
481
422
result= 1; // Item was replaced
484
424
/* Restore the original field value. */
485
425
if (field_item->depended_from)
487
result= field->store(orig_field_val, field->isUnsigned());
427
result= field->store(orig_field_val, true);
488
428
/* orig_field_val must be a valid value that can be restored back. */
491
session->variables.sql_mode= orig_sql_mode;
492
session->count_cuted_fields= orig_count_cuted_fields;
431
thd->variables.sql_mode= orig_sql_mode;
432
thd->count_cuted_fields= orig_count_cuted_fields;
435
dbug_tmp_restore_column_map(table->write_set, old_write_map);
436
dbug_tmp_restore_column_map(table->read_set, old_read_map);
575
Arg_comparator::Arg_comparator():
576
session(current_session),
581
Arg_comparator::Arg_comparator(Item **a1, Item **a2):
584
session(current_session),
589
522
int Arg_comparator::set_compare_func(Item_bool_func2 *item, Item_result type)
592
525
func= comparator_matrix[type]
593
[test(owner->functype() == Item_func::EQUAL_FUNC)];
526
[test(owner->functype() == Item_func::EQUAL_FUNC)];
598
uint32_t n= (*a)->cols();
599
if (n != (*b)->cols())
601
my_error(ER_OPERAND_COLUMNS, MYF(0), n);
605
if (!(comparators= new Arg_comparator[n]))
607
for (uint32_t i=0; i < n; i++)
609
if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
611
my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
614
comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
530
uint n= (*a)->cols();
531
if (n != (*b)->cols())
533
my_error(ER_OPERAND_COLUMNS, MYF(0), n);
537
if (!(comparators= new Arg_comparator[n]))
539
for (uint i=0; i < n; i++)
541
if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
543
my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
546
comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
619
550
case STRING_RESULT:
553
We must set cmp_charset here as we may be called from for an automatic
554
generated item, like in natural join
556
if (cmp_collation.set((*a)->collation, (*b)->collation) ||
557
cmp_collation.derivation == DERIVATION_NONE)
559
my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
562
if (cmp_collation.collation == &my_charset_bin)
622
We must set cmp_charset here as we may be called from for an automatic
623
generated item, like in natural join
565
We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
566
without removing end space
625
if (cmp_collation.set((*a)->collation, (*b)->collation) ||
626
cmp_collation.derivation == DERIVATION_NONE)
628
my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
631
if (cmp_collation.collation == &my_charset_bin)
634
We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
635
without removing end space
637
if (func == &Arg_comparator::compare_string)
638
func= &Arg_comparator::compare_binary_string;
639
else if (func == &Arg_comparator::compare_e_string)
640
func= &Arg_comparator::compare_e_binary_string;
568
if (func == &Arg_comparator::compare_string)
569
func= &Arg_comparator::compare_binary_string;
570
else if (func == &Arg_comparator::compare_e_string)
571
func= &Arg_comparator::compare_e_binary_string;
643
As this is binary compassion, mark all fields that they can't be
644
transformed. Otherwise we would get into trouble with comparisons
646
WHERE col= 'j' AND col LIKE BINARY 'j'
647
which would be transformed to:
574
As this is binary compassion, mark all fields that they can't be
575
transformed. Otherwise we would get into trouble with comparisons
577
WHERE col= 'j' AND col LIKE BINARY 'j'
578
which would be transformed to:
650
(*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
651
(*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
581
(*a)->walk(&Item::set_no_const_sub, false, (uchar*) 0);
582
(*b)->walk(&Item::set_no_const_sub, false, (uchar*) 0);
657
if (func == &Arg_comparator::compare_int_signed)
659
if ((*a)->unsigned_flag)
660
func= (((*b)->unsigned_flag)?
661
&Arg_comparator::compare_int_unsigned :
662
&Arg_comparator::compare_int_unsigned_signed);
663
else if ((*b)->unsigned_flag)
664
func= &Arg_comparator::compare_int_signed_unsigned;
666
else if (func== &Arg_comparator::compare_e_int)
668
if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
669
func= &Arg_comparator::compare_e_int_diff_signedness;
588
if (func == &Arg_comparator::compare_int_signed)
590
if ((*a)->unsigned_flag)
591
func= (((*b)->unsigned_flag)?
592
&Arg_comparator::compare_int_unsigned :
593
&Arg_comparator::compare_int_unsigned_signed);
594
else if ((*b)->unsigned_flag)
595
func= &Arg_comparator::compare_int_signed_unsigned;
597
else if (func== &Arg_comparator::compare_e_int)
599
if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
600
func= &Arg_comparator::compare_e_int_diff_signedness;
673
604
case DECIMAL_RESULT:
675
606
case REAL_RESULT:
608
if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
677
if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
679
precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
680
if (func == &Arg_comparator::compare_real)
681
func= &Arg_comparator::compare_real_fixed;
682
else if (func == &Arg_comparator::compare_e_real)
683
func= &Arg_comparator::compare_e_real_fixed;
610
precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
611
if (func == &Arg_comparator::compare_real)
612
func= &Arg_comparator::compare_real_fixed;
613
else if (func == &Arg_comparator::compare_e_real)
614
func= &Arg_comparator::compare_e_real_fixed;
712
644
converted value. 0 on error and on zero-dates -- check 'failure'
716
get_date_from_str(Session *session, String *str, type::timestamp_t warn_type,
648
get_date_from_str(THD *thd, String *str, timestamp_type warn_type,
717
649
char *warn_name, bool *error_arg)
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)
654
enum_drizzle_timestamp_type ret;
656
ret= str_to_datetime(str->ptr(), str->length(), &l_time,
657
(TIME_FUZZY_DATE | MODE_INVALID_DATES |
658
(thd->variables.sql_mode &
659
(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE))),
662
if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
731
665
Do not return yet, we may still want to throw a "trailing garbage"
734
668
*error_arg= false;
735
l_time.convert(value);
669
value= TIME_to_uint64_t_datetime(&l_time);
739
673
*error_arg= true;
740
error= type::CUT; /* force warning */
674
error= 1; /* force warning */
743
if (error != type::VALID)
745
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
678
make_truncated_value_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
746
679
str->ptr(), str->length(),
747
680
warn_type, warn_name);
823
752
(str_arg->type() != Item::FUNC_ITEM ||
824
753
((Item_func*)str_arg)->functype() != Item_func::GUSERVAR_FUNC))
827
* OK, we are here if we've got a date field (or something which can be
828
* compared as a date field) on one side of the equation, and a constant
829
* string on the other side. In this case, we must verify that the constant
830
* string expression can indeed be evaluated as a datetime. If it cannot,
831
* we throw an error here and stop processsing. Bad data should ALWAYS
832
* produce an error, and no implicit conversion or truncation should take place.
834
* If the conversion to a DateTime temporal is successful, then we convert
835
* the Temporal instance to a uint64_t for the comparison operator, which
836
* compares date(times) using int64_t semantics.
840
* Does a uint64_t conversion really have to happen here? Fields return int64_t
841
* from val_int(), not uint64_t...
846
/* DateTime used to pick up as many string conversion possibilities as possible. */
755
THD *thd= current_thd;
758
String tmp, *str_val= 0;
759
timestamp_type t_type= (date_arg->field_type() == DRIZZLE_TYPE_NEWDATE ?
760
DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME);
849
762
str_val= str_arg->val_str(&tmp);
853
* If we are here, it is most likely due to the comparison item
854
* being a NULL. Although this is incorrect (SQL demands that the term IS NULL
855
* be used, not = NULL since no item can be equal to NULL).
857
* So, return gracefully.
859
return CMP_DATE_DFLT;
861
if (! temporal.from_string(str_val->c_ptr(), str_val->length()))
863
/* Chuck an error. Bad datetime input. */
864
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), str_val->c_ptr());
865
return CMP_DATE_DFLT; /* :( What else can I return... */
868
/* String conversion was good. Convert to an integer for comparison purposes. */
869
temporal.to_int64_t(&value);
763
if (str_arg->null_value)
764
return CMP_DATE_DFLT;
765
value= get_date_from_str(thd, str_val, t_type, date_arg->name, &error);
767
return CMP_DATE_DFLT;
872
769
*const_value= value;
777
Retrieves correct TIME value from the given item.
782
item_arg [in/out] item to retrieve TIME value from
783
cache_arg [in/out] pointer to place to store the cache item to
784
warn_item [in] unused
785
is_null [out] true <=> the item_arg is null
788
Retrieves the correct TIME value from given item for comparison by the
789
compare_datetime() function.
790
If item's result can be compared as int64_t then its int value is used
791
and a value returned by get_time function is used otherwise.
792
If an item is a constant one then its value is cached and it isn't
793
get parsed again. An Item_cache_int object is used for for cached values.
794
It seamlessly substitutes the original item. The cache item is marked as
795
non-constant to prevent re-caching it again.
802
get_time_value(THD *thd __attribute__((unused)),
803
Item ***item_arg, Item **cache_arg,
804
Item *warn_item __attribute__((unused)),
808
Item *item= **item_arg;
811
if (item->result_as_int64_t())
813
value= item->val_int();
814
*is_null= item->null_value;
818
*is_null= item->get_time(<ime);
819
value= !*is_null ? TIME_to_uint64_t_datetime(<ime) : 0;
822
Do not cache GET_USER_VAR() function as its const_item() may return true
823
for the current thread but it still may change during the execution.
825
if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
826
((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
828
Item_cache_int *cache= new Item_cache_int();
829
/* Mark the cache as non-const to prevent re-caching. */
830
cache->set_used_tables(1);
831
cache->store(item, value);
833
*item_arg= cache_arg;
879
839
int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg,
880
840
Item **a1, Item **a2,
881
841
Item_result type)
883
enum_date_cmp_type cmp_type;
884
int64_t const_value= -1;
843
enum enum_date_cmp_type cmp_type;
844
uint64_t const_value= (uint64_t)-1;
888
848
if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
890
851
owner= owner_arg;
891
852
a_type= (*a)->field_type();
892
853
b_type= (*b)->field_type();
896
if (const_value != -1)
857
if (const_value != (uint64_t)-1)
898
859
Item_cache_int *cache= new Item_cache_int();
899
860
/* Mark the cache as non-const to prevent re-caching. */
2144
2127
else if (cmp_type == DECIMAL_RESULT)
2146
type::Decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2129
my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2147
2130
a_buf, *a_dec, b_buf, *b_dec;
2148
2131
if ((null_value=args[0]->null_value))
2132
return 0; /* purecov: inspected */
2150
2133
a_dec= args[1]->val_decimal(&a_buf);
2151
2134
b_dec= args[2]->val_decimal(&b_buf);
2152
2135
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);
2136
return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2137
my_decimal_cmp(dec, b_dec) <= 0) != negated);
2155
2138
if (args[1]->null_value && args[2]->null_value)
2157
2140
else if (args[1]->null_value)
2158
null_value= (class_decimal_cmp(dec, b_dec) <= 0);
2141
null_value= (my_decimal_cmp(dec, b_dec) <= 0);
2160
null_value= (class_decimal_cmp(dec, a_dec) >= 0);
2143
null_value= (my_decimal_cmp(dec, a_dec) >= 0);
2164
2147
double value= args[0]->val_real(),a,b;
2165
2148
if ((null_value=args[0]->null_value))
2149
return 0; /* purecov: inspected */
2167
2150
a= args[1]->val_real();
2168
2151
b= args[2]->val_real();
2169
2152
if (!args[1]->null_value && !args[2]->null_value)
2215
2198
max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2219
2201
max_length= max(args[0]->max_length, args[1]->max_length);
2222
switch (hybrid_type)
2203
switch (hybrid_type) {
2224
2204
case STRING_RESULT:
2225
2205
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2228
2207
case DECIMAL_RESULT:
2229
2208
case REAL_RESULT:
2232
2210
case INT_RESULT:
2236
2213
case ROW_RESULT:
2240
2217
cached_field_type= agg_field_type(args, 2);
2244
uint32_t Item_func_ifnull::decimal_precision() const
2221
uint Item_func_ifnull::decimal_precision() const
2246
int max_int_part= max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2223
int max_int_part=max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2247
2224
return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2251
enum_field_types Item_func_ifnull::field_type() const
2228
enum_field_types Item_func_ifnull::field_type() const
2253
2230
return cached_field_type;
2256
Field *Item_func_ifnull::tmp_table_field(Table *table)
2233
Field *Item_func_ifnull::tmp_table_field(TABLE *table)
2258
2235
return tmp_table_field_from_field_type(table, 0);
2612
2589
/* Compare every WHEN argument with it and return the first match */
2613
for (uint32_t i=0 ; i < ncases ; i+=2)
2590
for (uint i=0 ; i < ncases ; i+=2)
2615
2592
cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
2616
2593
assert(cmp_type != ROW_RESULT);
2617
assert(cmp_items[(uint32_t)cmp_type]);
2618
if (!(value_added_map & (1<<(uint32_t)cmp_type)))
2594
assert(cmp_items[(uint)cmp_type]);
2595
if (!(value_added_map & (1<<(uint)cmp_type)))
2620
cmp_items[(uint32_t)cmp_type]->store_value(args[first_expr_num]);
2597
cmp_items[(uint)cmp_type]->store_value(args[first_expr_num]);
2621
2598
if ((null_value=args[first_expr_num]->null_value))
2622
2599
return else_expr_num != -1 ? args[else_expr_num] : 0;
2623
value_added_map|= 1<<(uint32_t)cmp_type;
2600
value_added_map|= 1<<(uint)cmp_type;
2625
if (!cmp_items[(uint32_t)cmp_type]->cmp(args[i]) && !args[i]->null_value)
2602
if (!cmp_items[(uint)cmp_type]->cmp(args[i]) && !args[i]->null_value)
2626
2603
return args[i + 1];
2736
2712
void Item_func_case::agg_num_lengths(Item *arg)
2738
uint32_t len= class_decimal_length_to_precision(arg->max_length, arg->decimals,
2714
uint len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2739
2715
arg->unsigned_flag) - arg->decimals;
2740
set_if_bigger(max_length, len);
2716
set_if_bigger(max_length, len);
2741
2717
set_if_bigger(decimals, arg->decimals);
2742
unsigned_flag= unsigned_flag && arg->unsigned_flag;
2718
unsigned_flag= unsigned_flag && arg->unsigned_flag;
2746
2722
void Item_func_case::fix_length_and_dec()
2750
uint32_t found_types= 0;
2751
if (!(agg= (Item**) memory::sql_alloc(sizeof(Item*)*(ncases+1))))
2726
uint found_types= 0;
2727
if (!(agg= (Item**) sql_alloc(sizeof(Item*)*(ncases+1))))
2755
2731
Aggregate all THEN and ELSE expression types
2756
2732
and collations when string result
2759
2735
for (nagg= 0 ; nagg < ncases/2 ; nagg++)
2760
2736
agg[nagg]= args[nagg*2+1];
2762
2738
if (else_expr_num != -1)
2763
2739
agg[nagg++]= args[else_expr_num];
2765
2741
agg_result_type(&cached_result_type, agg, nagg);
2766
2742
if ((cached_result_type == STRING_RESULT) &&
2767
2743
agg_arg_charsets(collation, agg, nagg, MY_COLL_ALLOW_CONV, 1))
2770
2746
cached_field_type= agg_field_type(agg, nagg);
2772
2748
Aggregate first expression and all THEN expression types
2802
2779
if (else_expr_num == -1 || args[else_expr_num]->maybe_null)
2807
2784
unsigned_flag= true;
2808
2785
if (cached_result_type == STRING_RESULT)
2810
for (uint32_t i= 0; i < ncases; i+= 2)
2787
for (uint i= 0; i < ncases; i+= 2)
2811
2788
agg_str_lengths(args[i + 1]);
2812
2789
if (else_expr_num != -1)
2813
2790
agg_str_lengths(args[else_expr_num]);
2817
for (uint32_t i= 0; i < ncases; i+= 2)
2794
for (uint i= 0; i < ncases; i+= 2)
2818
2795
agg_num_lengths(args[i + 1]);
2819
if (else_expr_num != -1)
2796
if (else_expr_num != -1)
2820
2797
agg_num_lengths(args[else_expr_num]);
2821
max_length= class_decimal_precision_to_length(max_length + decimals, decimals,
2798
max_length= my_decimal_precision_to_length(max_length + decimals, decimals,
2822
2799
unsigned_flag);
2827
uint32_t Item_func_case::decimal_precision() const
2804
uint Item_func_case::decimal_precision() const
2829
2806
int max_int_part=0;
2830
for (uint32_t i=0 ; i < ncases ; i+=2)
2807
for (uint i=0 ; i < ncases ; i+=2)
2831
2808
set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
2833
if (else_expr_num != -1)
2810
if (else_expr_num != -1)
2834
2811
set_if_bigger(max_int_part, args[else_expr_num]->decimal_int_part());
2835
2812
return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
3045
3020
0 left argument is equal to the right argument.
3046
3021
1 left argument is greater than the right argument.
3048
int cmp_int64_t(void *, in_int64_t::packed_int64_t *a,
3049
in_int64_t::packed_int64_t *b)
3023
int cmp_int64_t(void *cmp_arg __attribute__((unused)),
3024
in_int64_t::packed_int64_t *a,
3025
in_int64_t::packed_int64_t *b)
3051
3027
if (a->unsigned_flag != b->unsigned_flag)
3054
One of the args is unsigned and is too big to fit into the
3030
One of the args is unsigned and is too big to fit into the
3055
3031
positive signed range. Report no match.
3057
3033
if ((a->unsigned_flag && ((uint64_t) a->val) > (uint64_t) INT64_MAX) ||
3058
3034
(b->unsigned_flag && ((uint64_t) b->val) > (uint64_t) INT64_MAX))
3059
3035
return a->unsigned_flag ? 1 : -1;
3061
Although the signedness differs both args can fit into the signed
3037
Although the signedness differs both args can fit into the signed
3062
3038
positive range. Make them signed and compare as usual.
3064
3040
return cmp_longs (a->val, b->val);
3066
3042
if (a->unsigned_flag)
3182
3152
delete [] (cmp_item_row*) base;
3185
unsigned char *in_row::get_value(Item *item)
3155
uchar *in_row::get_value(Item *item)
3187
3157
tmp.store_value(item);
3188
3158
if (item->is_null())
3190
return (unsigned char *)&tmp;
3160
return (uchar *)&tmp;
3193
void in_row::set(uint32_t pos, Item *item)
3163
void in_row::set(uint pos, Item *item)
3195
3165
((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3199
in_int64_t::in_int64_t(uint32_t elements) :
3200
in_vector(elements, sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3169
in_int64_t::in_int64_t(uint elements)
3170
:in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3203
void in_int64_t::set(uint32_t pos,Item *item)
3173
void in_int64_t::set(uint pos,Item *item)
3205
3175
struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3207
3177
buff->val= item->val_int();
3208
3178
buff->unsigned_flag= item->unsigned_flag;
3211
unsigned char *in_int64_t::get_value(Item *item)
3181
uchar *in_int64_t::get_value(Item *item)
3213
3183
tmp.val= item->val_int();
3214
3184
if (item->null_value)
3216
3186
tmp.unsigned_flag= item->unsigned_flag;
3217
return (unsigned char*) &tmp;
3187
return (uchar*) &tmp;
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)
3190
void in_datetime::set(uint pos,Item *item)
3229
3192
Item **tmp_item= &item;
3231
3194
struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3233
buff->val= get_datetime_value(session, &tmp_item, 0, warn_item, &is_null);
3196
buff->val= get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
3234
3197
buff->unsigned_flag= 1L;
3237
unsigned char *in_datetime::get_value(Item *item)
3200
uchar *in_datetime::get_value(Item *item)
3240
3203
Item **tmp_item= lval_cache ? &lval_cache : &item;
3241
tmp.val= get_datetime_value(session, &tmp_item, &lval_cache, warn_item, &is_null);
3204
tmp.val= get_datetime_value(thd, &tmp_item, &lval_cache, warn_item, &is_null);
3242
3205
if (item->null_value)
3244
3207
tmp.unsigned_flag= 1L;
3245
return (unsigned char*) &tmp;
3208
return (uchar*) &tmp;
3248
in_double::in_double(uint32_t elements)
3211
in_double::in_double(uint elements)
3249
3212
:in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
3252
void in_double::set(uint32_t pos,Item *item)
3215
void in_double::set(uint pos,Item *item)
3254
3217
((double*) base)[pos]= item->val_real();
3257
unsigned char *in_double::get_value(Item *item)
3220
uchar *in_double::get_value(Item *item)
3259
3222
tmp= item->val_real();
3260
3223
if (item->null_value)
3262
return (unsigned char*) &tmp;
3224
return 0; /* purecov: inspected */
3225
return (uchar*) &tmp;
3266
in_decimal::in_decimal(uint32_t elements)
3267
:in_vector(elements, sizeof(type::Decimal),(qsort2_cmp) cmp_decimal, 0)
3229
in_decimal::in_decimal(uint elements)
3230
:in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3271
void in_decimal::set(uint32_t pos, Item *item)
3234
void in_decimal::set(uint pos, Item *item)
3273
/* as far as 'item' is constant, we can store reference on type::Decimal */
3274
type::Decimal *dec= ((type::Decimal *)base) + pos;
3236
/* as far as 'item' is constant, we can store reference on my_decimal */
3237
my_decimal *dec= ((my_decimal *)base) + pos;
3275
3238
dec->len= DECIMAL_BUFF_LENGTH;
3276
3239
dec->fix_buffer_pointer();
3277
type::Decimal *res= item->val_decimal(dec);
3278
/* if item->val_decimal() is evaluated to NULL then res == 0 */
3240
my_decimal *res= item->val_decimal(dec);
3241
/* if item->val_decimal() is evaluated to NULL then res == 0 */
3279
3242
if (!item->null_value && res != dec)
3280
class_decimal2decimal(res, dec);
3243
my_decimal2decimal(res, dec);
3284
unsigned char *in_decimal::get_value(Item *item)
3247
uchar *in_decimal::get_value(Item *item)
3286
type::Decimal *result= item->val_decimal(&val);
3249
my_decimal *result= item->val_decimal(&val);
3287
3250
if (item->null_value)
3289
return (unsigned char *)result;
3252
return (uchar *)result;
3293
3256
cmp_item* cmp_item::get_comparator(Item_result type,
3294
const CHARSET_INFO * const cs)
3296
3259
switch (type) {
3297
3260
case STRING_RESULT:
3298
3261
return new cmp_item_sort_string(cs);
3300
3262
case INT_RESULT:
3301
3263
return new cmp_item_int;
3303
3264
case REAL_RESULT:
3304
3265
return new cmp_item_real;
3306
3266
case ROW_RESULT:
3307
3267
return new cmp_item_row;
3309
3268
case DECIMAL_RESULT:
3310
3269
return new cmp_item_decimal;
3313
3274
return 0; // to satisfy compiler :)
3443
3404
void cmp_item_decimal::store_value(Item *item)
3445
type::Decimal *val= item->val_decimal(&value);
3406
my_decimal *val= item->val_decimal(&value);
3446
3407
/* val may be zero if item is nnull */
3447
3408
if (val && val != &value)
3448
class_decimal2decimal(val, &value);
3409
my_decimal2decimal(val, &value);
3452
3413
int cmp_item_decimal::cmp(Item *arg)
3454
type::Decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3415
my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3455
3416
if (arg->null_value)
3457
return class_decimal_cmp(&value, tmp);
3418
return my_decimal_cmp(&value, tmp);
3461
3422
int cmp_item_decimal::compare(cmp_item *arg)
3463
3424
cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3464
return class_decimal_cmp(&value, &l_cmp->value);
3425
return my_decimal_cmp(&value, &l_cmp->value);
3876
Item_cond::Item_cond(Session *session, Item_cond *item)
3877
:item::function::Boolean(session, item),
3832
int64_t Item_func_bit_or::val_int()
3835
uint64_t arg1= (uint64_t) args[0]->val_int();
3836
if (args[0]->null_value)
3838
null_value=1; /* purecov: inspected */
3839
return 0; /* purecov: inspected */
3841
uint64_t arg2= (uint64_t) args[1]->val_int();
3842
if (args[1]->null_value)
3848
return (int64_t) (arg1 | arg2);
3852
int64_t Item_func_bit_and::val_int()
3855
uint64_t arg1= (uint64_t) args[0]->val_int();
3856
if (args[0]->null_value)
3858
null_value=1; /* purecov: inspected */
3859
return 0; /* purecov: inspected */
3861
uint64_t arg2= (uint64_t) args[1]->val_int();
3862
if (args[1]->null_value)
3864
null_value=1; /* purecov: inspected */
3865
return 0; /* purecov: inspected */
3868
return (int64_t) (arg1 & arg2);
3871
Item_cond::Item_cond(THD *thd, Item_cond *item)
3872
:Item_bool_func(thd, item),
3878
3873
abort_on_null(item->abort_on_null),
3879
3874
and_tables_cache(item->and_tables_cache)
3887
void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
3882
void Item_cond::copy_andor_arguments(THD *thd, Item_cond *item)
3889
List<Item>::iterator li(item->list.begin());
3884
List_iterator_fast<Item> li(item->list);
3890
3885
while (Item *it= li++)
3891
list.push_back(it->copy_andor_structure(session));
3886
list.push_back(it->copy_andor_structure(thd));
3896
Item_cond::fix_fields(Session *session, Item **)
3891
Item_cond::fix_fields(THD *thd, Item **ref __attribute__((unused)))
3898
3893
assert(fixed == 0);
3899
List<Item>::iterator li(list.begin());
3894
List_iterator<Item> li(list);
3901
void *orig_session_marker= session->session_marker;
3902
unsigned char buff[sizeof(char*)]; // Max local vars in function
3896
void *orig_thd_marker= thd->thd_marker;
3897
uchar buff[sizeof(char*)]; // Max local vars in function
3903
3898
not_null_tables_cache= used_tables_cache= 0;
3904
const_item_cache= true;
3899
const_item_cache= 1;
3906
3901
if (functype() == COND_OR_FUNC)
3907
session->session_marker= 0;
3909
3904
and_table_cache is the value that Item_cond_or() returns for
3910
3905
not_null_tables()
3912
3907
and_tables_cache= ~(table_map) 0;
3914
if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
3909
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
3915
3910
return true; // Fatal error flag is set!
3917
3912
The following optimization reduces the depth of an AND-OR tree.
4075
4070
@param arg_t parameter to be passed to the transformer
4078
Item returned as the result of transformation of the root node
4073
Item returned as the result of transformation of the root node
4081
Item *Item_cond::compile(Item_analyzer analyzer, unsigned char **arg_p,
4082
Item_transformer transformer, unsigned char *arg_t)
4076
Item *Item_cond::compile(Item_analyzer analyzer, uchar **arg_p,
4077
Item_transformer transformer, uchar *arg_t)
4084
4079
if (!(this->*analyzer)(arg_p))
4087
List<Item>::iterator li(list.begin());
4082
List_iterator<Item> li(list);
4089
4084
while ((item= li++))
4092
4087
The same parameter value of arg_p must be passed
4093
4088
to analyze any argument of the condition formula.
4095
unsigned char *arg_v= *arg_p;
4090
uchar *arg_v= *arg_p;
4096
4091
Item *new_item= item->compile(analyzer, &arg_v, transformer, arg_t);
4097
4092
if (new_item && new_item != item)
4098
4093
li.replace(new_item);
4436
4429
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4440
4433
if (escape_item->const_item())
4443
4435
/* If we are on execution stage */
4444
4436
String *escape_str= escape_item->val_str(&tmp_value1);
4445
4437
if (escape_str)
4447
escape= (char *)memory::sql_alloc(escape_str->length());
4448
strcpy(escape, escape_str->ptr());
4439
if (escape_used_in_parsing && (
4440
(((thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
4441
escape_str->numchars() != 1) ||
4442
escape_str->numchars() > 1)))
4444
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4448
if (use_mb(cmp.cmp_collation.collation))
4450
CHARSET_INFO *cs= escape_str->charset();
4452
int rc= cs->cset->mb_wc(cs, &wc,
4453
(const uchar*) escape_str->ptr(),
4454
(const uchar*) escape_str->ptr() +
4455
escape_str->length());
4456
escape= (int) (rc > 0 ? wc : '\\');
4461
In the case of 8bit character set, we pass native
4462
code instead of Unicode code as "escape" argument.
4463
Convert to "cs" if charset of escape differs.
4465
CHARSET_INFO *cs= cmp.cmp_collation.collation;
4467
if (escape_str->needs_conversion(escape_str->length(),
4468
escape_str->charset(), cs, &unused))
4472
uint32_t cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
4473
escape_str->length(),
4474
escape_str->charset(), &errors);
4475
escape= cnvlen ? ch : '\\';
4478
escape= *(escape_str->ptr());
4452
escape= (char *)memory::sql_alloc(1);
4453
strcpy(escape, "\\");
4457
4485
We could also do boyer-more for non-const items, but as we would have to
4458
4486
recompute the tables for each row it's not worth it.
4460
if (args[1]->const_item() && !use_strnxfrm(collation.collation))
4488
if (args[1]->const_item() && !use_strnxfrm(collation.collation) &&
4489
!(specialflag & SPECIAL_NO_NEW_FUNC))
4462
4491
String* res2 = args[1]->val_str(&tmp_value2);
4464
4493
return false; // Null argument
4466
4495
const size_t len = res2->length();
4467
4496
const char* first = res2->ptr();
4468
4497
const char* last = first + len - 1;
4698
4722
while (j <= tlmpl)
4724
register int i = plm1;
4701
4725
while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
4704
if (i == plm1 - shift)
4728
if (i == plm1 - shift)
4711
const int v= plm1 - i;
4713
bcShift= bmBc[(uint32_t) likeconv(cs, text[i + j])] - plm1 + i;
4714
shift= (turboShift > bcShift) ? turboShift : bcShift;
4715
shift= max(shift, bmGs[i]);
4734
register const int v = plm1 - i;
4736
bcShift = bmBc[(uint) likeconv(cs, text[i + j])] - plm1 + i;
4737
shift = max(turboShift, bcShift);
4738
shift = max(shift, bmGs[i]);
4717
4739
if (shift == bmGs[i])
4718
u= (pattern_len - shift < v) ? pattern_len - shift : v;
4740
u = min(pattern_len - shift, v);
4721
if (turboShift < bcShift)
4722
shift= max(shift, u + 1);
4743
if (turboShift < bcShift)
4744
shift = max(shift, u + 1);
4911
4932
Item_equal::Item_equal(Item_field *f1, Item_field *f2)
4912
: item::function::Boolean(), const_item(0), eval_item(0), cond_false(0)
4933
: Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
4914
const_item_cache= false;
4935
const_item_cache= 0;
4915
4936
fields.push_back(f1);
4916
4937
fields.push_back(f2);
4919
4940
Item_equal::Item_equal(Item *c, Item_field *f)
4920
: item::function::Boolean(), eval_item(0), cond_false(0)
4941
: Item_bool_func(), eval_item(0), cond_false(0)
4922
const_item_cache= false;
4943
const_item_cache= 0;
4923
4944
fields.push_back(f);
4928
4949
Item_equal::Item_equal(Item_equal *item_equal)
4929
: item::function::Boolean(), eval_item(0), cond_false(0)
4950
: Item_bool_func(), eval_item(0), cond_false(0)
4931
const_item_cache= false;
4932
List<Item_field>::iterator li(item_equal->fields.begin());
4952
const_item_cache= 0;
4953
List_iterator_fast<Item_field> li(item_equal->fields);
4933
4954
Item_field *item;
4934
4955
while ((item= li++))