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"
43
extern const double log_10[309];
45
static Eq_creator eq_creator;
46
static Ne_creator ne_creator;
47
static Gt_creator gt_creator;
48
static Lt_creator lt_creator;
49
static Ge_creator ge_creator;
50
static Le_creator le_creator;
52
static bool convert_constant_item(Session *, Item_field *, Item **);
24
#include <drizzled/server_includes.h>
25
#include <drizzled/sql_select.h>
27
static bool convert_constant_item(THD *, Item_field *, Item **);
54
29
static Item_result item_store_type(Item_result a, Item *item,
55
30
bool unsigned_flag)
230
204
for (i= 1; i < nitems ; i++)
232
if (skip_nulls && items[i]->type() == Item::NULL_ITEM)
233
continue; // Skip NULL constant items
234
if ((left_result == ROW_RESULT ||
206
if ((left_result == ROW_RESULT ||
235
207
items[i]->result_type() == ROW_RESULT) &&
236
208
cmp_row_type(items[0], items[i]))
238
found_types|= 1<< (uint32_t)item_cmp_type(left_result,
210
found_types|= 1<< (uint)item_cmp_type(left_result,
239
211
items[i]->result_type());
242
Even if all right-hand items are NULLs and we are skipping them all, we need
243
at least one type bit in the found_type bitmask.
245
if (skip_nulls && !found_types)
246
found_types= 1 << (uint)left_result;
247
213
return found_types;
216
static void my_coll_agg_error(DTCollation &c1, DTCollation &c2,
219
my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
220
c1.collation->name,c1.derivation_name(),
221
c2.collation->name,c2.derivation_name(),
251
226
Item_bool_func2* Eq_creator::create(Item *a, Item *b) const
257
const Eq_creator* Eq_creator::instance()
263
232
Item_bool_func2* Ne_creator::create(Item *a, Item *b) const
265
234
return new Item_func_ne(a, b);
269
const Ne_creator* Ne_creator::instance()
275
238
Item_bool_func2* Gt_creator::create(Item *a, Item *b) const
277
240
return new Item_func_gt(a, b);
281
const Gt_creator* Gt_creator::instance()
287
244
Item_bool_func2* Lt_creator::create(Item *a, Item *b) const
289
246
return new Item_func_lt(a, b);
293
const Lt_creator* Lt_creator::instance()
299
250
Item_bool_func2* Ge_creator::create(Item *a, Item *b) const
301
252
return new Item_func_ge(a, b);
305
const Ge_creator* Ge_creator::instance()
311
256
Item_bool_func2* Le_creator::create(Item *a, Item *b) const
313
258
return new Item_func_le(a, b);
316
const Le_creator* Le_creator::instance()
324
263
Most of these returns 0LL if false and 1LL if true and
439
378
1 Item was replaced with an integer version of the item
442
static bool convert_constant_item(Session *session, Item_field *field_item,
381
static bool convert_constant_item(THD *thd, Item_field *field_item,
445
384
Field *field= field_item->field;
448
field->setWriteSet();
450
387
if (!(*item)->with_subselect && (*item)->const_item())
452
ulong orig_sql_mode= session->variables.sql_mode;
453
enum_check_fields orig_count_cuted_fields= session->count_cuted_fields;
389
ulong orig_sql_mode= thd->variables.sql_mode;
390
enum_check_fields orig_count_cuted_fields= thd->count_cuted_fields;
454
391
uint64_t orig_field_val= 0; /* original field value if valid */
456
393
/* For comparison purposes allow invalid dates like 2000-01-32 */
457
session->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
394
thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
458
395
MODE_INVALID_DATES;
459
session->count_cuted_fields= CHECK_FIELD_IGNORE;
396
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
462
399
Store the value of the field if it references an outer field because
463
400
the call to save_in_field below overrides that value.
465
402
if (field_item->depended_from)
467
403
orig_field_val= field->val_int();
470
404
if (!(*item)->is_null() && !(*item)->save_in_field(field, 1))
472
406
Item *tmp= new Item_int_with_ref(field->val_int(), *item,
473
407
test(field->flags & UNSIGNED_FLAG));
475
session->change_item_tree(item, tmp);
409
thd->change_item_tree(item, tmp);
476
410
result= 1; // Item was replaced
479
412
/* Restore the original field value. */
480
413
if (field_item->depended_from)
482
result= field->store(orig_field_val, field->isUnsigned());
415
result= field->store(orig_field_val, true);
483
416
/* orig_field_val must be a valid value that can be restored back. */
486
session->variables.sql_mode= orig_sql_mode;
487
session->count_cuted_fields= orig_count_cuted_fields;
419
thd->variables.sql_mode= orig_sql_mode;
420
thd->count_cuted_fields= orig_count_cuted_fields;
501
435
if (!args[0] || !args[1])
505
439
We allow to convert to Unicode character sets in some cases.
506
440
The conditions when conversion is possible are:
507
441
- arguments A and B have different charsets
508
442
- A wins according to coercibility rules
509
443
- character set of A is superset for character set of B
511
445
If all of the above is true, then it's possible to convert
512
446
B into the character set of A, and then compare according
513
447
to the collation of A.
517
451
DTCollation coll;
518
452
if (args[0]->result_type() == STRING_RESULT &&
519
453
args[1]->result_type() == STRING_RESULT &&
520
454
agg_arg_charsets(coll, args, 2, MY_COLL_CMP_CONV, 1))
523
457
args[0]->cmp_context= args[1]->cmp_context=
524
458
item_cmp_type(args[0]->result_type(), args[1]->result_type());
525
459
// Make a special case of compare with fields to get nicer DATE comparisons
574
508
func= comparator_matrix[type]
575
[test(owner->functype() == Item_func::EQUAL_FUNC)];
509
[test(owner->functype() == Item_func::EQUAL_FUNC)];
580
uint32_t n= (*a)->cols();
581
if (n != (*b)->cols())
583
my_error(ER_OPERAND_COLUMNS, MYF(0), n);
587
if (!(comparators= new Arg_comparator[n]))
589
for (uint32_t i=0; i < n; i++)
591
if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
593
my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
596
comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
513
uint32_t n= (*a)->cols();
514
if (n != (*b)->cols())
516
my_error(ER_OPERAND_COLUMNS, MYF(0), n);
520
if (!(comparators= new Arg_comparator[n]))
522
for (uint32_t i=0; i < n; i++)
524
if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
526
my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
529
comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
601
533
case STRING_RESULT:
536
We must set cmp_charset here as we may be called from for an automatic
537
generated item, like in natural join
539
if (cmp_collation.set((*a)->collation, (*b)->collation) ||
540
cmp_collation.derivation == DERIVATION_NONE)
542
my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
545
if (cmp_collation.collation == &my_charset_bin)
604
We must set cmp_charset here as we may be called from for an automatic
605
generated item, like in natural join
548
We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
549
without removing end space
607
if (cmp_collation.set((*a)->collation, (*b)->collation) ||
608
cmp_collation.derivation == DERIVATION_NONE)
610
my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
613
if (cmp_collation.collation == &my_charset_bin)
616
We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
617
without removing end space
619
if (func == &Arg_comparator::compare_string)
620
func= &Arg_comparator::compare_binary_string;
621
else if (func == &Arg_comparator::compare_e_string)
622
func= &Arg_comparator::compare_e_binary_string;
551
if (func == &Arg_comparator::compare_string)
552
func= &Arg_comparator::compare_binary_string;
553
else if (func == &Arg_comparator::compare_e_string)
554
func= &Arg_comparator::compare_e_binary_string;
625
As this is binary compassion, mark all fields that they can't be
626
transformed. Otherwise we would get into trouble with comparisons
628
WHERE col= 'j' AND col LIKE BINARY 'j'
629
which would be transformed to:
557
As this is binary compassion, mark all fields that they can't be
558
transformed. Otherwise we would get into trouble with comparisons
560
WHERE col= 'j' AND col LIKE BINARY 'j'
561
which would be transformed to:
632
(*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
633
(*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
564
(*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
565
(*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
639
if (func == &Arg_comparator::compare_int_signed)
641
if ((*a)->unsigned_flag)
642
func= (((*b)->unsigned_flag)?
643
&Arg_comparator::compare_int_unsigned :
644
&Arg_comparator::compare_int_unsigned_signed);
645
else if ((*b)->unsigned_flag)
646
func= &Arg_comparator::compare_int_signed_unsigned;
648
else if (func== &Arg_comparator::compare_e_int)
650
if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
651
func= &Arg_comparator::compare_e_int_diff_signedness;
571
if (func == &Arg_comparator::compare_int_signed)
573
if ((*a)->unsigned_flag)
574
func= (((*b)->unsigned_flag)?
575
&Arg_comparator::compare_int_unsigned :
576
&Arg_comparator::compare_int_unsigned_signed);
577
else if ((*b)->unsigned_flag)
578
func= &Arg_comparator::compare_int_signed_unsigned;
580
else if (func== &Arg_comparator::compare_e_int)
582
if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
583
func= &Arg_comparator::compare_e_int_diff_signedness;
655
587
case DECIMAL_RESULT:
657
589
case REAL_RESULT:
591
if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
659
if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
661
precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
662
if (func == &Arg_comparator::compare_real)
663
func= &Arg_comparator::compare_real_fixed;
664
else if (func == &Arg_comparator::compare_e_real)
665
func= &Arg_comparator::compare_e_real_fixed;
593
precision= 5 / log_10[cmax((*a)->decimals, (*b)->decimals) + 1];
594
if (func == &Arg_comparator::compare_real)
595
func= &Arg_comparator::compare_real_fixed;
596
else if (func == &Arg_comparator::compare_e_real)
597
func= &Arg_comparator::compare_e_real_fixed;
694
627
converted value. 0 on error and on zero-dates -- check 'failure'
698
get_date_from_str(Session *session, String *str, type::timestamp_t warn_type,
631
get_date_from_str(THD *thd, String *str, enum enum_drizzle_timestamp_type warn_type,
699
632
char *warn_name, bool *error_arg)
702
type::cut_t error= type::VALID;
704
type::timestamp_t ret;
706
ret= l_time.store(str->ptr(), str->length(),
707
(TIME_FUZZY_DATE | MODE_INVALID_DATES | (session->variables.sql_mode & MODE_NO_ZERO_DATE)),
710
if (ret == type::DRIZZLE_TIMESTAMP_DATETIME || ret == type::DRIZZLE_TIMESTAMP_DATE)
637
enum enum_drizzle_timestamp_type ret;
639
ret= str_to_datetime(str->ptr(), str->length(), &l_time,
640
(TIME_FUZZY_DATE | MODE_INVALID_DATES |
641
(thd->variables.sql_mode & MODE_NO_ZERO_DATE)),
644
if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
713
647
Do not return yet, we may still want to throw a "trailing garbage"
716
650
*error_arg= false;
717
l_time.convert(value);
651
value= TIME_to_uint64_t_datetime(&l_time);
721
655
*error_arg= true;
722
error= type::CUT; /* force warning */
656
error= 1; /* force warning */
725
if (error != type::VALID)
727
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
660
make_truncated_value_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
728
661
str->ptr(), str->length(),
729
662
warn_type, warn_name);
768
700
enum Arg_comparator::enum_date_cmp_type
769
Arg_comparator::can_compare_as_dates(Item *in_a, Item *in_b,
770
int64_t *const_value)
701
Arg_comparator::can_compare_as_dates(Item *a, Item *b, uint64_t *const_value)
772
703
enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
773
704
Item *str_arg= 0, *date_arg= 0;
775
if (in_a->type() == Item::ROW_ITEM || in_b->type() == Item::ROW_ITEM)
706
if (a->type() == Item::ROW_ITEM || b->type() == Item::ROW_ITEM)
776
707
return CMP_DATE_DFLT;
778
if (in_a->is_datetime())
709
if (a->is_datetime())
780
if (in_b->is_datetime())
711
if (b->is_datetime())
782
712
cmp_type= CMP_DATE_WITH_DATE;
784
else if (in_b->result_type() == STRING_RESULT)
713
else if (b->result_type() == STRING_RESULT)
786
715
cmp_type= CMP_DATE_WITH_STR;
791
else if (in_b->is_datetime() && in_a->result_type() == STRING_RESULT)
720
else if (b->is_datetime() && a->result_type() == STRING_RESULT)
793
722
cmp_type= CMP_STR_WITH_DATE;
798
727
if (cmp_type != CMP_DATE_DFLT)
805
734
(str_arg->type() != Item::FUNC_ITEM ||
806
735
((Item_func*)str_arg)->functype() != Item_func::GUSERVAR_FUNC))
809
* OK, we are here if we've got a date field (or something which can be
810
* compared as a date field) on one side of the equation, and a constant
811
* string on the other side. In this case, we must verify that the constant
812
* string expression can indeed be evaluated as a datetime. If it cannot,
813
* we throw an error here and stop processsing. Bad data should ALWAYS
814
* produce an error, and no implicit conversion or truncation should take place.
816
* If the conversion to a DateTime temporal is successful, then we convert
817
* the Temporal instance to a uint64_t for the comparison operator, which
818
* compares date(times) using int64_t semantics.
822
* Does a uint64_t conversion really have to happen here? Fields return int64_t
823
* from val_int(), not uint64_t...
828
/* DateTime used to pick up as many string conversion possibilities as possible. */
737
THD *thd= current_thd;
740
String tmp, *str_val= 0;
741
enum enum_drizzle_timestamp_type t_type= (date_arg->field_type() == DRIZZLE_TYPE_NEWDATE ?
742
DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME);
831
744
str_val= str_arg->val_str(&tmp);
835
* If we are here, it is most likely due to the comparison item
836
* being a NULL. Although this is incorrect (SQL demands that the term IS NULL
837
* be used, not = NULL since no item can be equal to NULL).
839
* So, return gracefully.
841
return CMP_DATE_DFLT;
843
if (! temporal.from_string(str_val->c_ptr(), str_val->length()))
845
/* Chuck an error. Bad datetime input. */
846
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), str_val->c_ptr());
847
return CMP_DATE_DFLT; /* :( What else can I return... */
850
/* String conversion was good. Convert to an integer for comparison purposes. */
851
temporal.to_int64_t(&value);
745
if (str_arg->null_value)
746
return CMP_DATE_DFLT;
747
value= get_date_from_str(thd, str_val, t_type, date_arg->name, &error);
749
return CMP_DATE_DFLT;
854
751
*const_value= value;
759
Retrieves correct TIME value from the given item.
764
item_arg [in/out] item to retrieve TIME value from
765
cache_arg [in/out] pointer to place to store the cache item to
766
warn_item [in] unused
767
is_null [out] true <=> the item_arg is null
770
Retrieves the correct TIME value from given item for comparison by the
771
compare_datetime() function.
772
If item's result can be compared as int64_t then its int value is used
773
and a value returned by get_time function is used otherwise.
774
If an item is a constant one then its value is cached and it isn't
775
get parsed again. An Item_cache_int object is used for for cached values.
776
It seamlessly substitutes the original item. The cache item is marked as
777
non-constant to prevent re-caching it again.
784
get_time_value(THD *thd __attribute__((unused)),
785
Item ***item_arg, Item **cache_arg,
786
Item *warn_item __attribute__((unused)),
790
Item *item= **item_arg;
793
if (item->result_as_int64_t())
795
value= item->val_int();
796
*is_null= item->null_value;
800
*is_null= item->get_time(<ime);
801
value= !*is_null ? TIME_to_uint64_t_datetime(<ime) : 0;
804
Do not cache GET_USER_VAR() function as its const_item() may return true
805
for the current thread but it still may change during the execution.
807
if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
808
((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
810
Item_cache_int *cache= new Item_cache_int();
811
/* Mark the cache as non-const to prevent re-caching. */
812
cache->set_used_tables(1);
813
cache->store(item, value);
815
*item_arg= cache_arg;
861
821
int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg,
862
822
Item **a1, Item **a2,
863
823
Item_result type)
865
enum_date_cmp_type cmp_type;
866
int64_t const_value= -1;
825
enum enum_date_cmp_type cmp_type;
826
uint64_t const_value= (uint64_t)-1;
870
830
if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
872
833
owner= owner_arg;
873
834
a_type= (*a)->field_type();
874
835
b_type= (*b)->field_type();
878
if (const_value != -1)
839
if (const_value != (uint64_t)-1)
880
841
Item_cache_int *cache= new Item_cache_int();
881
842
/* Mark the cache as non-const to prevent re-caching. */
2126
2109
else if (cmp_type == DECIMAL_RESULT)
2128
type::Decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2111
my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2129
2112
a_buf, *a_dec, b_buf, *b_dec;
2130
2113
if ((null_value=args[0]->null_value))
2114
return 0; /* purecov: inspected */
2132
2115
a_dec= args[1]->val_decimal(&a_buf);
2133
2116
b_dec= args[2]->val_decimal(&b_buf);
2134
2117
if (!args[1]->null_value && !args[2]->null_value)
2135
return (int64_t) ((class_decimal_cmp(dec, a_dec) >= 0 &&
2136
class_decimal_cmp(dec, b_dec) <= 0) != negated);
2118
return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2119
my_decimal_cmp(dec, b_dec) <= 0) != negated);
2137
2120
if (args[1]->null_value && args[2]->null_value)
2139
2122
else if (args[1]->null_value)
2140
null_value= (class_decimal_cmp(dec, b_dec) <= 0);
2123
null_value= (my_decimal_cmp(dec, b_dec) <= 0);
2142
null_value= (class_decimal_cmp(dec, a_dec) >= 0);
2125
null_value= (my_decimal_cmp(dec, a_dec) >= 0);
2146
2129
double value= args[0]->val_real(),a,b;
2147
2130
if ((null_value=args[0]->null_value))
2131
return 0; /* purecov: inspected */
2149
2132
a= args[1]->val_real();
2150
2133
b= args[2]->val_real();
2151
2134
if (!args[1]->null_value && !args[2]->null_value)
2194
2177
int len1= args[1]->max_length - args[1]->decimals
2195
2178
- (args[1]->unsigned_flag ? 0 : 1);
2197
max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2180
max_length= cmax(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2201
max_length= max(args[0]->max_length, args[1]->max_length);
2183
max_length= cmax(args[0]->max_length, args[1]->max_length);
2204
switch (hybrid_type)
2185
switch (hybrid_type) {
2206
2186
case STRING_RESULT:
2207
2187
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2210
2189
case DECIMAL_RESULT:
2211
2190
case REAL_RESULT:
2214
2192
case INT_RESULT:
2218
2195
case ROW_RESULT:
2222
2199
cached_field_type= agg_field_type(args, 2);
2226
2203
uint32_t Item_func_ifnull::decimal_precision() const
2228
int max_int_part= max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2229
return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2205
int max_int_part=cmax(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2206
return cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2233
enum_field_types Item_func_ifnull::field_type() const
2210
enum_field_types Item_func_ifnull::field_type() const
2235
2212
return cached_field_type;
2691
bool Item_func_case::fix_fields(Session *session, Item **ref)
2668
bool Item_func_case::fix_fields(THD *thd, Item **ref)
2694
2671
buff should match stack usage from
2695
2672
Item_func_case::val_int() -> Item_func_case::find_item()
2697
unsigned char buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2
2698
+sizeof(double)*2+sizeof(int64_t)*2];
2699
bool res= Item_func::fix_fields(session, ref);
2674
unsigned char buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(int64_t)*2];
2675
bool res= Item_func::fix_fields(thd, ref);
2701
2677
Call check_stack_overrun after fix_fields to be sure that stack variable
2702
2678
is not optimized away
2704
if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
2680
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2705
2681
return true; // Fatal error flag is set!
3027
3002
0 left argument is equal to the right argument.
3028
3003
1 left argument is greater than the right argument.
3030
int cmp_int64_t(void *, in_int64_t::packed_int64_t *a,
3031
in_int64_t::packed_int64_t *b)
3005
int cmp_int64_t(void *cmp_arg __attribute__((unused)),
3006
in_int64_t::packed_int64_t *a,
3007
in_int64_t::packed_int64_t *b)
3033
3009
if (a->unsigned_flag != b->unsigned_flag)
3036
One of the args is unsigned and is too big to fit into the
3012
One of the args is unsigned and is too big to fit into the
3037
3013
positive signed range. Report no match.
3039
3015
if ((a->unsigned_flag && ((uint64_t) a->val) > (uint64_t) INT64_MAX) ||
3040
3016
(b->unsigned_flag && ((uint64_t) b->val) > (uint64_t) INT64_MAX))
3041
3017
return a->unsigned_flag ? 1 : -1;
3043
Although the signedness differs both args can fit into the signed
3019
Although the signedness differs both args can fit into the signed
3044
3020
positive range. Make them signed and compare as usual.
3046
3022
return cmp_longs (a->val, b->val);
3048
3024
if (a->unsigned_flag)
3234
3204
tmp= item->val_real();
3235
3205
if (item->null_value)
3206
return 0; /* purecov: inspected */
3237
3207
return (unsigned char*) &tmp;
3241
3211
in_decimal::in_decimal(uint32_t elements)
3242
:in_vector(elements, sizeof(type::Decimal),(qsort2_cmp) cmp_decimal, 0)
3212
:in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3246
3216
void in_decimal::set(uint32_t pos, Item *item)
3248
/* as far as 'item' is constant, we can store reference on type::Decimal */
3249
type::Decimal *dec= ((type::Decimal *)base) + pos;
3218
/* as far as 'item' is constant, we can store reference on my_decimal */
3219
my_decimal *dec= ((my_decimal *)base) + pos;
3250
3220
dec->len= DECIMAL_BUFF_LENGTH;
3251
3221
dec->fix_buffer_pointer();
3252
type::Decimal *res= item->val_decimal(dec);
3253
/* if item->val_decimal() is evaluated to NULL then res == 0 */
3222
my_decimal *res= item->val_decimal(dec);
3223
/* if item->val_decimal() is evaluated to NULL then res == 0 */
3254
3224
if (!item->null_value && res != dec)
3255
class_decimal2decimal(res, dec);
3225
my_decimal2decimal(res, dec);
3259
3229
unsigned char *in_decimal::get_value(Item *item)
3261
type::Decimal *result= item->val_decimal(&val);
3231
my_decimal *result= item->val_decimal(&val);
3262
3232
if (item->null_value)
3264
3234
return (unsigned char *)result;
3418
3386
void cmp_item_decimal::store_value(Item *item)
3420
type::Decimal *val= item->val_decimal(&value);
3388
my_decimal *val= item->val_decimal(&value);
3421
3389
/* val may be zero if item is nnull */
3422
3390
if (val && val != &value)
3423
class_decimal2decimal(val, &value);
3391
my_decimal2decimal(val, &value);
3427
3395
int cmp_item_decimal::cmp(Item *arg)
3429
type::Decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3397
my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3430
3398
if (arg->null_value)
3432
return class_decimal_cmp(&value, tmp);
3400
return my_decimal_cmp(&value, tmp);
3436
3404
int cmp_item_decimal::compare(cmp_item *arg)
3438
3406
cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3439
return class_decimal_cmp(&value, &l_cmp->value);
3407
return my_decimal_cmp(&value, &l_cmp->value);
3851
Item_cond::Item_cond(Session *session, Item_cond *item)
3852
:item::function::Boolean(session, item),
3814
int64_t Item_func_bit_or::val_int()
3817
uint64_t arg1= (uint64_t) args[0]->val_int();
3818
if (args[0]->null_value)
3820
null_value=1; /* purecov: inspected */
3821
return 0; /* purecov: inspected */
3823
uint64_t arg2= (uint64_t) args[1]->val_int();
3824
if (args[1]->null_value)
3830
return (int64_t) (arg1 | arg2);
3834
int64_t Item_func_bit_and::val_int()
3837
uint64_t arg1= (uint64_t) args[0]->val_int();
3838
if (args[0]->null_value)
3840
null_value=1; /* purecov: inspected */
3841
return 0; /* purecov: inspected */
3843
uint64_t arg2= (uint64_t) args[1]->val_int();
3844
if (args[1]->null_value)
3846
null_value=1; /* purecov: inspected */
3847
return 0; /* purecov: inspected */
3850
return (int64_t) (arg1 & arg2);
3853
Item_cond::Item_cond(THD *thd, Item_cond *item)
3854
:Item_bool_func(thd, item),
3853
3855
abort_on_null(item->abort_on_null),
3854
3856
and_tables_cache(item->and_tables_cache)
3862
void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
3864
void Item_cond::copy_andor_arguments(THD *thd, Item_cond *item)
3864
3866
List_iterator_fast<Item> li(item->list);
3865
3867
while (Item *it= li++)
3866
list.push_back(it->copy_andor_structure(session));
3868
list.push_back(it->copy_andor_structure(thd));
3871
Item_cond::fix_fields(Session *session, Item **)
3873
Item_cond::fix_fields(THD *thd, Item **ref __attribute__((unused)))
3873
3875
assert(fixed == 0);
3874
3876
List_iterator<Item> li(list);
3876
void *orig_session_marker= session->session_marker;
3878
void *orig_thd_marker= thd->thd_marker;
3877
3879
unsigned char buff[sizeof(char*)]; // Max local vars in function
3878
3880
not_null_tables_cache= used_tables_cache= 0;
3879
const_item_cache= true;
3881
const_item_cache= 1;
3881
3883
if (functype() == COND_OR_FUNC)
3882
session->session_marker= 0;
3884
3886
and_table_cache is the value that Item_cond_or() returns for
3885
3887
not_null_tables()
3887
3889
and_tables_cache= ~(table_map) 0;
3889
if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
3891
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
3890
3892
return true; // Fatal error flag is set!
3892
3894
The following optimization reduces the depth of an AND-OR tree.
4411
4411
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4415
4415
if (escape_item->const_item())
4418
4417
/* If we are on execution stage */
4419
4418
String *escape_str= escape_item->val_str(&tmp_value1);
4420
4419
if (escape_str)
4422
escape= (char *)memory::sql_alloc(escape_str->length());
4423
strcpy(escape, escape_str->ptr());
4421
if (escape_used_in_parsing && escape_str->numchars() > 1)
4423
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4427
if (use_mb(cmp.cmp_collation.collation))
4429
const CHARSET_INFO * const cs= escape_str->charset();
4431
int rc= cs->cset->mb_wc(cs, &wc,
4432
(const unsigned char*) escape_str->ptr(),
4433
(const unsigned char*) escape_str->ptr() +
4434
escape_str->length());
4435
escape= (int) (rc > 0 ? wc : '\\');
4440
In the case of 8bit character set, we pass native
4441
code instead of Unicode code as "escape" argument.
4442
Convert to "cs" if charset of escape differs.
4444
const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
4446
if (escape_str->needs_conversion(escape_str->length(),
4447
escape_str->charset(), cs, &unused))
4451
uint32_t cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
4452
escape_str->length(),
4453
escape_str->charset(), &errors);
4454
escape= cnvlen ? ch : '\\';
4457
escape= *(escape_str->ptr());
4427
escape= (char *)memory::sql_alloc(1);
4428
strcpy(escape, "\\");
4432
4464
We could also do boyer-more for non-const items, but as we would have to
4433
4465
recompute the tables for each row it's not worth it.
4435
if (args[1]->const_item() && !use_strnxfrm(collation.collation))
4467
if (args[1]->const_item() && !use_strnxfrm(collation.collation))
4437
4469
String* res2 = args[1]->val_str(&tmp_value2);
4439
4471
return false; // Null argument
4441
4473
const size_t len = res2->length();
4442
4474
const char* first = res2->ptr();
4443
4475
const char* last = first + len - 1;
4675
4702
register int i = plm1;
4676
4703
while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
4679
if (i == plm1 - shift)
4706
if (i == plm1 - shift)
4686
register const int v= plm1 - i;
4688
bcShift= bmBc[(uint32_t) likeconv(cs, text[i + j])] - plm1 + i;
4689
shift= (turboShift > bcShift) ? turboShift : bcShift;
4690
shift= max(shift, bmGs[i]);
4712
register const int v = plm1 - i;
4714
bcShift = bmBc[(uint) likeconv(cs, text[i + j])] - plm1 + i;
4715
shift = (turboShift > bcShift) ? turboShift : bcShift;
4716
shift = cmax(shift, bmGs[i]);
4692
4717
if (shift == bmGs[i])
4693
u= (pattern_len - shift < v) ? pattern_len - shift : v;
4718
u = (pattern_len - shift < v) ? pattern_len - shift : v;
4696
if (turboShift < bcShift)
4697
shift= max(shift, u + 1);
4721
if (turboShift < bcShift)
4722
shift = cmax(shift, u + 1);
4793
4817
a IS NOT NULL -> a IS NULL.
4795
Item *Item_func_isnotnull::neg_transformer(Session *)
4819
Item *Item_func_isnotnull::neg_transformer(THD *thd __attribute__((unused)))
4797
4821
Item *item= new Item_func_isnull(args[0]);
4802
Item *Item_cond_and::neg_transformer(Session *session) /* NOT(a AND b AND ...) -> */
4826
Item *Item_cond_and::neg_transformer(THD *thd) /* NOT(a AND b AND ...) -> */
4803
4827
/* NOT a OR NOT b OR ... */
4805
neg_arguments(session);
4806
4830
Item *item= new Item_cond_or(list);
4811
Item *Item_cond_or::neg_transformer(Session *session) /* NOT(a OR b OR ...) -> */
4835
Item *Item_cond_or::neg_transformer(THD *thd) /* NOT(a OR b OR ...) -> */
4812
4836
/* NOT a AND NOT b AND ... */
4814
neg_arguments(session);
4815
4839
Item *item= new Item_cond_and(list);
4820
Item *Item_func_nop_all::neg_transformer(Session *)
4844
Item *Item_func_nop_all::neg_transformer(THD *thd __attribute__((unused)))
4822
4846
/* "NOT (e $cmp$ ANY (SELECT ...)) -> e $rev_cmp$" ALL (SELECT ...) */
4823
4847
Item_func_not_all *new_item= new Item_func_not_all(args[0]);
4886
4910
Item_equal::Item_equal(Item_field *f1, Item_field *f2)
4887
: item::function::Boolean(), const_item(0), eval_item(0), cond_false(0)
4911
: Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
4889
const_item_cache= false;
4913
const_item_cache= 0;
4890
4914
fields.push_back(f1);
4891
4915
fields.push_back(f2);
4894
4918
Item_equal::Item_equal(Item *c, Item_field *f)
4895
: item::function::Boolean(), eval_item(0), cond_false(0)
4919
: Item_bool_func(), eval_item(0), cond_false(0)
4897
const_item_cache= false;
4921
const_item_cache= 0;
4898
4922
fields.push_back(f);
4903
4927
Item_equal::Item_equal(Item_equal *item_equal)
4904
: item::function::Boolean(), eval_item(0), cond_false(0)
4928
: Item_bool_func(), eval_item(0), cond_false(0)
4906
const_item_cache= false;
4930
const_item_cache= 0;
4907
4931
List_iterator_fast<Item_field> li(item_equal->fields);
4908
4932
Item_field *item;
4909
4933
while ((item= li++))