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>
24
#include <drizzled/server_includes.h>
36
25
#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 **);
27
static bool convert_constant_item(THD *, Item_field *, Item **);
59
29
static Item_result item_store_type(Item_result a, Item *item,
60
30
bool unsigned_flag)
235
204
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 ||
206
if ((left_result == ROW_RESULT ||
240
207
items[i]->result_type() == ROW_RESULT) &&
241
208
cmp_row_type(items[0], items[i]))
243
found_types|= 1<< (uint32_t)item_cmp_type(left_result,
210
found_types|= 1<< (uint)item_cmp_type(left_result,
244
211
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
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(),
256
226
Item_bool_func2* Eq_creator::create(Item *a, Item *b) const
262
const Eq_creator* Eq_creator::instance()
268
232
Item_bool_func2* Ne_creator::create(Item *a, Item *b) const
270
234
return new Item_func_ne(a, b);
274
const Ne_creator* Ne_creator::instance()
280
238
Item_bool_func2* Gt_creator::create(Item *a, Item *b) const
282
240
return new Item_func_gt(a, b);
286
const Gt_creator* Gt_creator::instance()
292
244
Item_bool_func2* Lt_creator::create(Item *a, Item *b) const
294
246
return new Item_func_lt(a, b);
298
const Lt_creator* Lt_creator::instance()
304
250
Item_bool_func2* Ge_creator::create(Item *a, Item *b) const
306
252
return new Item_func_ge(a, b);
310
const Ge_creator* Ge_creator::instance()
316
256
Item_bool_func2* Le_creator::create(Item *a, Item *b) const
318
258
return new Item_func_le(a, b);
321
const Le_creator* Le_creator::instance()
329
263
Most of these returns 0LL if false and 1LL if true and
444
378
1 Item was replaced with an integer version of the item
447
static bool convert_constant_item(Session *session, Item_field *field_item,
381
static bool convert_constant_item(THD *thd, Item_field *field_item,
450
384
Field *field= field_item->field;
453
field->setWriteSet();
455
387
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;
389
ulong orig_sql_mode= thd->variables.sql_mode;
390
enum_check_fields orig_count_cuted_fields= thd->count_cuted_fields;
459
391
uint64_t orig_field_val= 0; /* original field value if valid */
461
393
/* For comparison purposes allow invalid dates like 2000-01-32 */
462
session->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
394
thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
463
395
MODE_INVALID_DATES;
464
session->count_cuted_fields= CHECK_FIELD_IGNORE;
396
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
467
399
Store the value of the field if it references an outer field because
468
400
the call to save_in_field below overrides that value.
470
402
if (field_item->depended_from)
472
403
orig_field_val= field->val_int();
475
404
if (!(*item)->is_null() && !(*item)->save_in_field(field, 1))
477
406
Item *tmp= new Item_int_with_ref(field->val_int(), *item,
478
407
test(field->flags & UNSIGNED_FLAG));
480
session->change_item_tree(item, tmp);
409
thd->change_item_tree(item, tmp);
481
410
result= 1; // Item was replaced
484
412
/* Restore the original field value. */
485
413
if (field_item->depended_from)
487
result= field->store(orig_field_val, field->isUnsigned());
415
result= field->store(orig_field_val, true);
488
416
/* 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;
419
thd->variables.sql_mode= orig_sql_mode;
420
thd->count_cuted_fields= orig_count_cuted_fields;
575
Arg_comparator::Arg_comparator():
576
session(current_session),
581
Arg_comparator::Arg_comparator(Item **a1, Item **a2):
584
session(current_session),
589
505
int Arg_comparator::set_compare_func(Item_bool_func2 *item, Item_result type)
592
508
func= comparator_matrix[type]
593
[test(owner->functype() == Item_func::EQUAL_FUNC)];
509
[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));
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));
619
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)
622
We must set cmp_charset here as we may be called from for an automatic
623
generated item, like in natural join
548
We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
549
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;
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;
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:
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:
650
(*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
651
(*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);
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;
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;
673
587
case DECIMAL_RESULT:
675
589
case REAL_RESULT:
591
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;
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;
712
627
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,
631
get_date_from_str(THD *thd, String *str, enum enum_drizzle_timestamp_type warn_type,
717
632
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)
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)
731
647
Do not return yet, we may still want to throw a "trailing garbage"
734
650
*error_arg= false;
735
l_time.convert(value);
651
value= TIME_to_uint64_t_datetime(&l_time);
739
655
*error_arg= true;
740
error= type::CUT; /* force warning */
656
error= 1; /* force warning */
743
if (error != type::VALID)
745
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
660
make_truncated_value_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
746
661
str->ptr(), str->length(),
747
662
warn_type, warn_name);
823
734
(str_arg->type() != Item::FUNC_ITEM ||
824
735
((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. */
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);
849
744
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);
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;
872
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;
879
821
int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg,
880
822
Item **a1, Item **a2,
881
823
Item_result type)
883
enum_date_cmp_type cmp_type;
884
int64_t const_value= -1;
825
enum enum_date_cmp_type cmp_type;
826
uint64_t const_value= (uint64_t)-1;
888
830
if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
890
833
owner= owner_arg;
891
834
a_type= (*a)->field_type();
892
835
b_type= (*b)->field_type();
896
if (const_value != -1)
839
if (const_value != (uint64_t)-1)
898
841
Item_cache_int *cache= new Item_cache_int();
899
842
/* Mark the cache as non-const to prevent re-caching. */
2144
2109
else if (cmp_type == DECIMAL_RESULT)
2146
type::Decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2111
my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2147
2112
a_buf, *a_dec, b_buf, *b_dec;
2148
2113
if ((null_value=args[0]->null_value))
2114
return 0; /* purecov: inspected */
2150
2115
a_dec= args[1]->val_decimal(&a_buf);
2151
2116
b_dec= args[2]->val_decimal(&b_buf);
2152
2117
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);
2118
return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2119
my_decimal_cmp(dec, b_dec) <= 0) != negated);
2155
2120
if (args[1]->null_value && args[2]->null_value)
2157
2122
else if (args[1]->null_value)
2158
null_value= (class_decimal_cmp(dec, b_dec) <= 0);
2123
null_value= (my_decimal_cmp(dec, b_dec) <= 0);
2160
null_value= (class_decimal_cmp(dec, a_dec) >= 0);
2125
null_value= (my_decimal_cmp(dec, a_dec) >= 0);
2164
2129
double value= args[0]->val_real(),a,b;
2165
2130
if ((null_value=args[0]->null_value))
2131
return 0; /* purecov: inspected */
2167
2132
a= args[1]->val_real();
2168
2133
b= args[2]->val_real();
2169
2134
if (!args[1]->null_value && !args[2]->null_value)
2212
2177
int len1= args[1]->max_length - args[1]->decimals
2213
2178
- (args[1]->unsigned_flag ? 0 : 1);
2215
max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2180
max_length= cmax(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2219
max_length= max(args[0]->max_length, args[1]->max_length);
2183
max_length= cmax(args[0]->max_length, args[1]->max_length);
2222
switch (hybrid_type)
2185
switch (hybrid_type) {
2224
2186
case STRING_RESULT:
2225
2187
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2228
2189
case DECIMAL_RESULT:
2229
2190
case REAL_RESULT:
2232
2192
case INT_RESULT:
2236
2195
case ROW_RESULT:
2240
2199
cached_field_type= agg_field_type(args, 2);
2244
2203
uint32_t Item_func_ifnull::decimal_precision() const
2246
int max_int_part= max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2247
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);
2251
enum_field_types Item_func_ifnull::field_type() const
2210
enum_field_types Item_func_ifnull::field_type() const
2253
2212
return cached_field_type;
3045
3002
0 left argument is equal to the right argument.
3046
3003
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)
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)
3051
3009
if (a->unsigned_flag != b->unsigned_flag)
3054
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
3055
3013
positive signed range. Report no match.
3057
3015
if ((a->unsigned_flag && ((uint64_t) a->val) > (uint64_t) INT64_MAX) ||
3058
3016
(b->unsigned_flag && ((uint64_t) b->val) > (uint64_t) INT64_MAX))
3059
3017
return a->unsigned_flag ? 1 : -1;
3061
Although the signedness differs both args can fit into the signed
3019
Although the signedness differs both args can fit into the signed
3062
3020
positive range. Make them signed and compare as usual.
3064
3022
return cmp_longs (a->val, b->val);
3066
3024
if (a->unsigned_flag)
3259
3204
tmp= item->val_real();
3260
3205
if (item->null_value)
3206
return 0; /* purecov: inspected */
3262
3207
return (unsigned char*) &tmp;
3266
3211
in_decimal::in_decimal(uint32_t elements)
3267
:in_vector(elements, sizeof(type::Decimal),(qsort2_cmp) cmp_decimal, 0)
3212
:in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3271
3216
void in_decimal::set(uint32_t 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;
3218
/* as far as 'item' is constant, we can store reference on my_decimal */
3219
my_decimal *dec= ((my_decimal *)base) + pos;
3275
3220
dec->len= DECIMAL_BUFF_LENGTH;
3276
3221
dec->fix_buffer_pointer();
3277
type::Decimal *res= item->val_decimal(dec);
3278
/* 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 */
3279
3224
if (!item->null_value && res != dec)
3280
class_decimal2decimal(res, dec);
3225
my_decimal2decimal(res, dec);
3284
3229
unsigned char *in_decimal::get_value(Item *item)
3286
type::Decimal *result= item->val_decimal(&val);
3231
my_decimal *result= item->val_decimal(&val);
3287
3232
if (item->null_value)
3289
3234
return (unsigned char *)result;
3443
3386
void cmp_item_decimal::store_value(Item *item)
3445
type::Decimal *val= item->val_decimal(&value);
3388
my_decimal *val= item->val_decimal(&value);
3446
3389
/* val may be zero if item is nnull */
3447
3390
if (val && val != &value)
3448
class_decimal2decimal(val, &value);
3391
my_decimal2decimal(val, &value);
3452
3395
int cmp_item_decimal::cmp(Item *arg)
3454
type::Decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3397
my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3455
3398
if (arg->null_value)
3457
return class_decimal_cmp(&value, tmp);
3400
return my_decimal_cmp(&value, tmp);
3461
3404
int cmp_item_decimal::compare(cmp_item *arg)
3463
3406
cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3464
return class_decimal_cmp(&value, &l_cmp->value);
3407
return my_decimal_cmp(&value, &l_cmp->value);
3876
Item_cond::Item_cond(Session *session, Item_cond *item)
3877
: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),
3878
3855
abort_on_null(item->abort_on_null),
3879
3856
and_tables_cache(item->and_tables_cache)
3887
void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
3864
void Item_cond::copy_andor_arguments(THD *thd, Item_cond *item)
3889
List<Item>::iterator li(item->list.begin());
3866
List_iterator_fast<Item> li(item->list);
3890
3867
while (Item *it= li++)
3891
list.push_back(it->copy_andor_structure(session));
3868
list.push_back(it->copy_andor_structure(thd));
3896
Item_cond::fix_fields(Session *session, Item **)
3873
Item_cond::fix_fields(THD *thd, Item **ref __attribute__((unused)))
3898
3875
assert(fixed == 0);
3899
List<Item>::iterator li(list.begin());
3876
List_iterator<Item> li(list);
3901
void *orig_session_marker= session->session_marker;
3878
void *orig_thd_marker= thd->thd_marker;
3902
3879
unsigned char buff[sizeof(char*)]; // Max local vars in function
3903
3880
not_null_tables_cache= used_tables_cache= 0;
3904
const_item_cache= true;
3881
const_item_cache= 1;
3906
3883
if (functype() == COND_OR_FUNC)
3907
session->session_marker= 0;
3909
3886
and_table_cache is the value that Item_cond_or() returns for
3910
3887
not_null_tables()
3912
3889
and_tables_cache= ~(table_map) 0;
3914
if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
3891
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
3915
3892
return true; // Fatal error flag is set!
3917
3894
The following optimization reduces the depth of an AND-OR tree.
4436
4411
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4440
4415
if (escape_item->const_item())
4443
4417
/* If we are on execution stage */
4444
4418
String *escape_str= escape_item->val_str(&tmp_value1);
4445
4419
if (escape_str)
4447
escape= (char *)memory::sql_alloc(escape_str->length());
4448
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());
4452
escape= (char *)memory::sql_alloc(1);
4453
strcpy(escape, "\\");
4457
4464
We could also do boyer-more for non-const items, but as we would have to
4458
4465
recompute the tables for each row it's not worth it.
4460
if (args[1]->const_item() && !use_strnxfrm(collation.collation))
4467
if (args[1]->const_item() && !use_strnxfrm(collation.collation))
4462
4469
String* res2 = args[1]->val_str(&tmp_value2);
4464
4471
return false; // Null argument
4466
4473
const size_t len = res2->length();
4467
4474
const char* first = res2->ptr();
4468
4475
const char* last = first + len - 1;
4911
4910
Item_equal::Item_equal(Item_field *f1, Item_field *f2)
4912
: 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)
4914
const_item_cache= false;
4913
const_item_cache= 0;
4915
4914
fields.push_back(f1);
4916
4915
fields.push_back(f2);
4919
4918
Item_equal::Item_equal(Item *c, Item_field *f)
4920
: item::function::Boolean(), eval_item(0), cond_false(0)
4919
: Item_bool_func(), eval_item(0), cond_false(0)
4922
const_item_cache= false;
4921
const_item_cache= 0;
4923
4922
fields.push_back(f);
4928
4927
Item_equal::Item_equal(Item_equal *item_equal)
4929
: item::function::Boolean(), eval_item(0), cond_false(0)
4928
: Item_bool_func(), eval_item(0), cond_false(0)
4931
const_item_cache= false;
4932
List<Item_field>::iterator li(item_equal->fields.begin());
4930
const_item_cache= 0;
4931
List_iterator_fast<Item_field> li(item_equal->fields);
4933
4932
Item_field *item;
4934
4933
while ((item= li++))