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
#ifdef USE_PRAGMA_IMPLEMENTATION
25
#pragma implementation // gcc: Class implementation
28
#include "mysql_priv.h"
30
#include "sql_select.h"
32
static bool convert_constant_item(THD *, Item_field *, Item **);
54
34
static Item_result item_store_type(Item_result a, Item *item,
35
my_bool unsigned_flag)
57
37
Item_result b= item->result_type();
220
199
Bitmap of collected types - otherwise
223
static uint32_t collect_cmp_types(Item **items, uint32_t nitems, bool skip_nulls= false)
202
static uint collect_cmp_types(Item **items, uint nitems)
226
uint32_t found_types;
227
206
Item_result left_result= items[0]->result_type();
207
DBUG_ASSERT(nitems > 1);
230
209
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 ||
211
if ((left_result == ROW_RESULT ||
235
212
items[i]->result_type() == ROW_RESULT) &&
236
213
cmp_row_type(items[0], items[i]))
238
found_types|= 1<< (uint32_t)item_cmp_type(left_result,
215
found_types|= 1<< (uint)item_cmp_type(left_result,
239
216
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
218
return found_types;
221
static void my_coll_agg_error(DTCollation &c1, DTCollation &c2,
224
my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
225
c1.collation->name,c1.derivation_name(),
226
c2.collation->name,c2.derivation_name(),
251
231
Item_bool_func2* Eq_creator::create(Item *a, Item *b) const
257
const Eq_creator* Eq_creator::instance()
263
237
Item_bool_func2* Ne_creator::create(Item *a, Item *b) const
265
239
return new Item_func_ne(a, b);
269
const Ne_creator* Ne_creator::instance()
275
243
Item_bool_func2* Gt_creator::create(Item *a, Item *b) const
277
245
return new Item_func_gt(a, b);
281
const Gt_creator* Gt_creator::instance()
287
249
Item_bool_func2* Lt_creator::create(Item *a, Item *b) const
289
251
return new Item_func_lt(a, b);
293
const Lt_creator* Lt_creator::instance()
299
255
Item_bool_func2* Ge_creator::create(Item *a, Item *b) const
301
257
return new Item_func_ge(a, b);
305
const Ge_creator* Ge_creator::instance()
311
261
Item_bool_func2* Le_creator::create(Item *a, Item *b) const
313
263
return new Item_func_le(a, b);
316
const Le_creator* Le_creator::instance()
324
268
Most of these returns 0LL if false and 1LL if true and
325
269
NULL if some arg is NULL.
328
int64_t Item_func_not::val_int()
272
longlong Item_func_not::val_int()
274
DBUG_ASSERT(fixed == 1);
331
275
bool value= args[0]->val_bool();
332
276
null_value=args[0]->null_value;
333
277
return ((!null_value && value == 0) ? 1 : 0);
439
383
1 Item was replaced with an integer version of the item
442
static bool convert_constant_item(Session *session, Item_field *field_item,
386
static bool convert_constant_item(THD *thd, Item_field *field_item,
445
389
Field *field= field_item->field;
448
field->setWriteSet();
450
392
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;
454
uint64_t orig_field_val= 0; /* original field value if valid */
394
TABLE *table= field->table;
395
ulong orig_sql_mode= thd->variables.sql_mode;
396
enum_check_fields orig_count_cuted_fields= thd->count_cuted_fields;
397
my_bitmap_map *old_write_map;
398
my_bitmap_map *old_read_map;
399
ulonglong orig_field_val= 0; /* original field value if valid */
403
old_write_map= dbug_tmp_use_all_columns(table, table->write_set);
404
old_read_map= dbug_tmp_use_all_columns(table, table->read_set);
456
406
/* For comparison purposes allow invalid dates like 2000-01-32 */
457
session->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
407
thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
458
408
MODE_INVALID_DATES;
459
session->count_cuted_fields= CHECK_FIELD_IGNORE;
409
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
462
412
Store the value of the field if it references an outer field because
498
453
if (!args[0] || !args[1])
502
457
We allow to convert to Unicode character sets in some cases.
503
458
The conditions when conversion is possible are:
504
459
- arguments A and B have different charsets
505
460
- A wins according to coercibility rules
506
461
- character set of A is superset for character set of B
508
463
If all of the above is true, then it's possible to convert
509
464
B into the character set of A, and then compare according
510
465
to the collation of A.
514
469
DTCollation coll;
515
470
if (args[0]->result_type() == STRING_RESULT &&
516
471
args[1]->result_type() == STRING_RESULT &&
517
472
agg_arg_charsets(coll, args, 2, MY_COLL_CMP_CONV, 1))
520
475
args[0]->cmp_context= args[1]->cmp_context=
521
476
item_cmp_type(args[0]->result_type(), args[1]->result_type());
522
477
// Make a special case of compare with fields to get nicer DATE comparisons
530
session= current_session;
531
Item_field *field_item= NULL;
533
if (args[0]->real_item()->type() == FIELD_ITEM)
486
if (!thd->is_context_analysis_only())
535
field_item= static_cast<Item_field*>(args[0]->real_item());
536
if (field_item->field->can_be_compared_as_int64_t() &&
537
!(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
488
if (args[0]->real_item()->type() == FIELD_ITEM)
539
if (convert_constant_item(session, field_item, &args[1]))
490
Item_field *field_item= (Item_field*) (args[0]->real_item());
491
if (field_item->field->can_be_compared_as_longlong() &&
492
!(field_item->is_datetime() &&
493
args[1]->result_type() == STRING_RESULT))
541
cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
542
INT_RESULT); // Works for all types.
543
args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
495
if (convert_constant_item(thd, field_item, &args[1]))
497
cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
498
INT_RESULT); // Works for all types.
499
args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
548
504
if (args[1]->real_item()->type() == FIELD_ITEM)
550
field_item= static_cast<Item_field*>(args[1]->real_item());
551
if (field_item->field->can_be_compared_as_int64_t() &&
506
Item_field *field_item= (Item_field*) (args[1]->real_item());
507
if (field_item->field->can_be_compared_as_longlong() &&
552
508
!(field_item->is_datetime() &&
553
509
args[0]->result_type() == STRING_RESULT))
555
if (convert_constant_item(session, field_item, &args[0]))
511
if (convert_constant_item(thd, field_item, &args[0]))
557
513
cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
558
514
INT_RESULT); // Works for all types.
691
647
converted value. 0 on error and on zero-dates -- check 'failure'
695
get_date_from_str(Session *session, String *str, enum enum_drizzle_timestamp_type warn_type,
651
get_date_from_str(THD *thd, String *str, timestamp_type warn_type,
696
652
char *warn_name, bool *error_arg)
701
enum enum_drizzle_timestamp_type ret;
657
enum_mysql_timestamp_type ret;
703
659
ret= str_to_datetime(str->ptr(), str->length(), &l_time,
704
660
(TIME_FUZZY_DATE | MODE_INVALID_DATES |
705
(session->variables.sql_mode & MODE_NO_ZERO_DATE)),
661
(thd->variables.sql_mode &
662
(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE))),
708
if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
665
if (ret == MYSQL_TIMESTAMP_DATETIME || ret == MYSQL_TIMESTAMP_DATE)
711
668
Do not return yet, we may still want to throw a "trailing garbage"
714
671
*error_arg= false;
715
value= TIME_to_uint64_t_datetime(&l_time);
672
value= TIME_to_ulonglong_datetime(&l_time);
800
755
(str_arg->type() != Item::FUNC_ITEM ||
801
756
((Item_func*)str_arg)->functype() != Item_func::GUSERVAR_FUNC))
804
* OK, we are here if we've got a date field (or something which can be
805
* compared as a date field) on one side of the equation, and a constant
806
* string on the other side. In this case, we must verify that the constant
807
* string expression can indeed be evaluated as a datetime. If it cannot,
808
* we throw an error here and stop processsing. Bad data should ALWAYS
809
* produce an error, and no implicit conversion or truncation should take place.
811
* If the conversion to a DateTime temporal is successful, then we convert
812
* the Temporal instance to a uint64_t for the comparison operator, which
813
* compares date(times) using int64_t semantics.
817
* Does a uint64_t conversion really have to happen here? Fields return int64_t
818
* from val_int(), not uint64_t...
823
/* DateTime used to pick up as many string conversion possibilities as possible. */
758
THD *thd= current_thd;
761
String tmp, *str_val= 0;
762
timestamp_type t_type= (date_arg->field_type() == MYSQL_TYPE_DATE ?
763
MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME);
826
765
str_val= str_arg->val_str(&tmp);
830
* If we are here, it is most likely due to the comparison item
831
* being a NULL. Although this is incorrect (SQL demands that the term IS NULL
832
* be used, not = NULL since no item can be equal to NULL).
834
* So, return gracefully.
836
return CMP_DATE_DFLT;
838
if (! temporal.from_string(str_val->c_ptr(), str_val->length()))
840
/* Chuck an error. Bad datetime input. */
841
my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), str_val->c_ptr());
842
return CMP_DATE_DFLT; /* :( What else can I return... */
845
/* String conversion was good. Convert to an integer for comparison purposes. */
847
temporal.to_int64_t(&int_value);
848
value= (uint64_t) int_value;
766
if (str_arg->null_value)
767
return CMP_DATE_DFLT;
768
value= get_date_from_str(thd, str_val, t_type, date_arg->name, &error);
770
return CMP_DATE_DFLT;
851
772
*const_value= value;
780
Retrieves correct TIME value from the given item.
785
item_arg [in/out] item to retrieve TIME value from
786
cache_arg [in/out] pointer to place to store the cache item to
787
warn_item [in] unused
788
is_null [out] true <=> the item_arg is null
791
Retrieves the correct TIME value from given item for comparison by the
792
compare_datetime() function.
793
If item's result can be compared as longlong then its int value is used
794
and a value returned by get_time function is used otherwise.
795
If an item is a constant one then its value is cached and it isn't
796
get parsed again. An Item_cache_int object is used for for cached values.
797
It seamlessly substitutes the original item. The cache item is marked as
798
non-constant to prevent re-caching it again.
805
get_time_value(THD *thd __attribute__((__unused__)),
806
Item ***item_arg, Item **cache_arg,
807
Item *warn_item __attribute__((__unused__)),
811
Item *item= **item_arg;
814
if (item->result_as_longlong())
816
value= item->val_int();
817
*is_null= item->null_value;
821
*is_null= item->get_time(<ime);
822
value= !*is_null ? TIME_to_ulonglong_datetime(<ime) : 0;
825
Do not cache GET_USER_VAR() function as its const_item() may return true
826
for the current thread but it still may change during the execution.
828
if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
829
((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
831
Item_cache_int *cache= new Item_cache_int();
832
/* Mark the cache as non-const to prevent re-caching. */
833
cache->set_used_tables(1);
834
cache->store(item, value);
836
*item_arg= cache_arg;
858
842
int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg,
859
843
Item **a1, Item **a2,
860
844
Item_result type)
862
846
enum enum_date_cmp_type cmp_type;
863
uint64_t const_value= (uint64_t)-1;
847
ulonglong const_value= (ulonglong)-1;
867
851
if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
869
session= current_session;
870
854
owner= owner_arg;
871
855
a_type= (*a)->field_type();
872
856
b_type= (*b)->field_type();
876
if (const_value != (uint64_t)-1)
860
if (const_value != (ulonglong)-1)
878
862
Item_cache_int *cache= new Item_cache_int();
879
863
/* Mark the cache as non-const to prevent re-caching. */
951
get_datetime_value(Session *session, Item ***item_arg, Item **cache_arg,
948
get_datetime_value(THD *thd, Item ***item_arg, Item **cache_arg,
952
949
Item *warn_item, bool *is_null)
955
952
String buf, *str= 0;
956
953
Item *item= **item_arg;
958
if (item->result_as_int64_t())
955
if (item->result_as_longlong())
960
957
value= item->val_int();
961
958
*is_null= item->null_value;
962
959
enum_field_types f_type= item->field_type();
964
Item_date_add_interval may return DRIZZLE_TYPE_STRING as the result
961
Item_date_add_interval may return MYSQL_TYPE_STRING as the result
965
962
field type. To detect that the DATE value has been returned we
966
963
compare it with 100000000L - any DATE value should be less than it.
967
964
Don't shift cached DATETIME values up for the second time.
969
if (f_type == DRIZZLE_TYPE_DATE ||
970
(f_type != DRIZZLE_TYPE_DATETIME && value < 100000000L))
966
if (f_type == MYSQL_TYPE_DATE ||
967
(f_type != MYSQL_TYPE_DATETIME && value < 100000000L))
971
968
value*= 1000000L;
1645
1643
@retval NULL if an error occurred
1648
Item *Item_in_optimizer::transform(Item_transformer transformer, unsigned char *argument)
1646
Item *Item_in_optimizer::transform(Item_transformer transformer, uchar *argument)
1650
1648
Item *new_item;
1652
assert(arg_count == 2);
1650
DBUG_ASSERT(arg_count == 2);
1654
1652
/* Transform the left IN operand. */
1655
1653
new_item= (*args)->transform(transformer, argument);
1659
Session::change_item_tree() should be called only if the tree was
1657
THD::change_item_tree() should be called only if the tree was
1660
1658
really transformed, i.e. when a new item has been created.
1661
1659
Otherwise we'll be allocating a lot of unnecessary memory for
1662
1660
change records at each execution.
1664
1662
if ((*args) != new_item)
1665
current_session->change_item_tree(args, new_item);
1663
current_thd->change_item_tree(args, new_item);
1668
1666
Transform the right IN operand which should be an Item_in_subselect or a
1703
1701
maybe_null=null_value=0;
1706
int64_t Item_func_equal::val_int()
1704
longlong Item_func_equal::val_int()
1706
DBUG_ASSERT(fixed == 1);
1709
1707
return cmp.compare();
1712
int64_t Item_func_ne::val_int()
1710
longlong Item_func_ne::val_int()
1712
DBUG_ASSERT(fixed == 1);
1715
1713
int value= cmp.compare();
1716
1714
return value != 0 && !null_value ? 1 : 0;
1720
int64_t Item_func_ge::val_int()
1718
longlong Item_func_ge::val_int()
1720
DBUG_ASSERT(fixed == 1);
1723
1721
int value= cmp.compare();
1724
1722
return value >= 0 ? 1 : 0;
1728
int64_t Item_func_gt::val_int()
1726
longlong Item_func_gt::val_int()
1728
DBUG_ASSERT(fixed == 1);
1731
1729
int value= cmp.compare();
1732
1730
return value > 0 ? 1 : 0;
1735
int64_t Item_func_le::val_int()
1733
longlong Item_func_le::val_int()
1735
DBUG_ASSERT(fixed == 1);
1738
1736
int value= cmp.compare();
1739
1737
return value <= 0 && !null_value ? 1 : 0;
1743
int64_t Item_func_lt::val_int()
1741
longlong Item_func_lt::val_int()
1743
DBUG_ASSERT(fixed == 1);
1746
1744
int value= cmp.compare();
1747
1745
return value < 0 && !null_value ? 1 : 0;
1751
int64_t Item_func_strcmp::val_int()
1749
longlong Item_func_strcmp::val_int()
1751
DBUG_ASSERT(fixed == 1);
1754
1752
String *a=args[0]->val_str(&tmp_value1);
1755
1753
String *b=args[1]->val_str(&tmp_value2);
2034
2036
ge_cmp.set_datetime_cmp_func(args, args + 1);
2035
2037
le_cmp.set_datetime_cmp_func(args, args + 2);
2039
else if (time_items_found == 3)
2041
/* Compare TIME items as integers. */
2042
cmp_type= INT_RESULT;
2037
2044
else if (args[0]->real_item()->type() == FIELD_ITEM &&
2038
session->lex->sql_command != SQLCOM_SHOW_CREATE)
2045
thd->lex->sql_command != SQLCOM_SHOW_CREATE)
2040
2047
Item_field *field_item= (Item_field*) (args[0]->real_item());
2041
if (field_item->field->can_be_compared_as_int64_t())
2048
if (field_item->field->can_be_compared_as_longlong())
2044
2051
The following can't be recoded with || as convert_constant_item
2045
2052
changes the argument
2047
if (convert_constant_item(session, field_item, &args[1]))
2054
if (convert_constant_item(thd, field_item, &args[1]))
2048
2055
cmp_type=INT_RESULT; // Works for all types.
2049
if (convert_constant_item(session, field_item, &args[2]))
2056
if (convert_constant_item(thd, field_item, &args[2]))
2050
2057
cmp_type=INT_RESULT; // Works for all types.
2056
int64_t Item_func_between::val_int()
2063
longlong Item_func_between::val_int()
2057
2064
{ // ANSI BETWEEN
2065
DBUG_ASSERT(fixed == 1);
2059
2066
if (compare_as_dates)
2061
2068
int ge_res, le_res;
2126
2133
my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2127
2134
a_buf, *a_dec, b_buf, *b_dec;
2128
2135
if ((null_value=args[0]->null_value))
2136
return 0; /* purecov: inspected */
2130
2137
a_dec= args[1]->val_decimal(&a_buf);
2131
2138
b_dec= args[2]->val_decimal(&b_buf);
2132
2139
if (!args[1]->null_value && !args[2]->null_value)
2133
return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2140
return (longlong) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2134
2141
my_decimal_cmp(dec, b_dec) <= 0) != negated);
2135
2142
if (args[1]->null_value && args[2]->null_value)
2211
2217
case ROW_RESULT:
2215
2221
cached_field_type= agg_field_type(args, 2);
2219
uint32_t Item_func_ifnull::decimal_precision() const
2225
uint Item_func_ifnull::decimal_precision() const
2221
int max_int_part= max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2227
int max_int_part=max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2222
2228
return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2226
enum_field_types Item_func_ifnull::field_type() const
2232
enum_field_types Item_func_ifnull::field_type() const
2228
2234
return cached_field_type;
2231
Field *Item_func_ifnull::tmp_table_field(Table *table)
2237
Field *Item_func_ifnull::tmp_table_field(TABLE *table)
2233
2239
return tmp_table_field_from_field_type(table, 0);
2347
2353
Item_func_if::fix_length_and_dec()
2349
maybe_null= args[1]->maybe_null || args[2]->maybe_null;
2355
maybe_null=args[1]->maybe_null || args[2]->maybe_null;
2350
2356
decimals= max(args[1]->decimals, args[2]->decimals);
2351
unsigned_flag= args[1]->unsigned_flag && args[2]->unsigned_flag;
2357
unsigned_flag=args[1]->unsigned_flag && args[2]->unsigned_flag;
2353
enum Item_result arg1_type= args[1]->result_type();
2354
enum Item_result arg2_type= args[2]->result_type();
2355
bool null1= args[1]->const_item() && args[1]->null_value;
2356
bool null2= args[2]->const_item() && args[2]->null_value;
2359
enum Item_result arg1_type=args[1]->result_type();
2360
enum Item_result arg2_type=args[2]->result_type();
2361
bool null1=args[1]->const_item() && args[1]->null_value;
2362
bool null2=args[2]->const_item() && args[2]->null_value;
2587
2593
/* Compare every WHEN argument with it and return the first match */
2588
for (uint32_t i=0 ; i < ncases ; i+=2)
2594
for (uint i=0 ; i < ncases ; i+=2)
2590
2596
cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
2591
assert(cmp_type != ROW_RESULT);
2592
assert(cmp_items[(uint32_t)cmp_type]);
2593
if (!(value_added_map & (1<<(uint32_t)cmp_type)))
2597
DBUG_ASSERT(cmp_type != ROW_RESULT);
2598
DBUG_ASSERT(cmp_items[(uint)cmp_type]);
2599
if (!(value_added_map & (1<<(uint)cmp_type)))
2595
cmp_items[(uint32_t)cmp_type]->store_value(args[first_expr_num]);
2601
cmp_items[(uint)cmp_type]->store_value(args[first_expr_num]);
2596
2602
if ((null_value=args[first_expr_num]->null_value))
2597
2603
return else_expr_num != -1 ? args[else_expr_num] : 0;
2598
value_added_map|= 1<<(uint32_t)cmp_type;
2604
value_added_map|= 1<<(uint)cmp_type;
2600
if (!cmp_items[(uint32_t)cmp_type]->cmp(args[i]) && !args[i]->null_value)
2606
if (!cmp_items[(uint)cmp_type]->cmp(args[i]) && !args[i]->null_value)
2601
2607
return args[i + 1];
2684
bool Item_func_case::fix_fields(Session *session, Item **ref)
2690
bool Item_func_case::fix_fields(THD *thd, Item **ref)
2687
2693
buff should match stack usage from
2688
2694
Item_func_case::val_int() -> Item_func_case::find_item()
2690
unsigned char buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2
2691
+sizeof(double)*2+sizeof(int64_t)*2];
2692
bool res= Item_func::fix_fields(session, ref);
2696
uchar buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(longlong)*2];
2697
bool res= Item_func::fix_fields(thd, ref);
2694
2699
Call check_stack_overrun after fix_fields to be sure that stack variable
2695
2700
is not optimized away
2697
if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
2702
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2698
2703
return true; // Fatal error flag is set!
2711
2716
void Item_func_case::agg_num_lengths(Item *arg)
2713
uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2718
uint len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2714
2719
arg->unsigned_flag) - arg->decimals;
2715
set_if_bigger(max_length, len);
2720
set_if_bigger(max_length, len);
2716
2721
set_if_bigger(decimals, arg->decimals);
2717
unsigned_flag= unsigned_flag && arg->unsigned_flag;
2722
unsigned_flag= unsigned_flag && arg->unsigned_flag;
2721
2726
void Item_func_case::fix_length_and_dec()
2725
uint32_t found_types= 0;
2726
if (!(agg= (Item**) memory::sql_alloc(sizeof(Item*)*(ncases+1))))
2730
uint found_types= 0;
2731
if (!(agg= (Item**) sql_alloc(sizeof(Item*)*(ncases+1))))
2730
2735
Aggregate all THEN and ELSE expression types
2731
2736
and collations when string result
2734
2739
for (nagg= 0 ; nagg < ncases/2 ; nagg++)
2735
2740
agg[nagg]= args[nagg*2+1];
2737
2742
if (else_expr_num != -1)
2738
2743
agg[nagg++]= args[else_expr_num];
2740
2745
agg_result_type(&cached_result_type, agg, nagg);
2741
2746
if ((cached_result_type == STRING_RESULT) &&
2742
2747
agg_arg_charsets(collation, agg, nagg, MY_COLL_ALLOW_CONV, 1))
2745
2750
cached_field_type= agg_field_type(agg, nagg);
2747
2752
Aggregate first expression and all THEN expression types
3019
3025
0 left argument is equal to the right argument.
3020
3026
1 left argument is greater than the right argument.
3022
int cmp_int64_t(void *, in_int64_t::packed_int64_t *a,
3023
in_int64_t::packed_int64_t *b)
3028
int cmp_longlong(void *cmp_arg __attribute__((__unused__)),
3029
in_longlong::packed_longlong *a,
3030
in_longlong::packed_longlong *b)
3025
3032
if (a->unsigned_flag != b->unsigned_flag)
3028
One of the args is unsigned and is too big to fit into the
3035
One of the args is unsigned and is too big to fit into the
3029
3036
positive signed range. Report no match.
3031
if ((a->unsigned_flag && ((uint64_t) a->val) > (uint64_t) INT64_MAX) ||
3032
(b->unsigned_flag && ((uint64_t) b->val) > (uint64_t) INT64_MAX))
3038
if ((a->unsigned_flag && ((ulonglong) a->val) > (ulonglong) LONGLONG_MAX) ||
3039
(b->unsigned_flag && ((ulonglong) b->val) > (ulonglong) LONGLONG_MAX))
3033
3040
return a->unsigned_flag ? 1 : -1;
3035
Although the signedness differs both args can fit into the signed
3042
Although the signedness differs both args can fit into the signed
3036
3043
positive range. Make them signed and compare as usual.
3038
3045
return cmp_longs (a->val, b->val);
3040
3047
if (a->unsigned_flag)
3041
return cmp_ulongs ((uint64_t) a->val, (uint64_t) b->val);
3048
return cmp_ulongs ((ulonglong) a->val, (ulonglong) b->val);
3043
3050
return cmp_longs (a->val, b->val);
3046
static int cmp_double(void *, double *a, double *b)
3053
static int cmp_double(void *cmp_arg __attribute__((__unused__)), double *a,double *b)
3048
3055
return *a < *b ? -1 : *a == *b ? 0 : 1;
3051
static int cmp_row(void *, cmp_item_row *a, cmp_item_row *b)
3058
static int cmp_row(void *cmp_arg __attribute__((__unused__)), cmp_item_row *a, cmp_item_row *b)
3053
3060
return a->compare(b);
3057
static int cmp_decimal(void *, my_decimal *a, my_decimal *b)
3064
static int cmp_decimal(void *cmp_arg __attribute__((__unused__)), my_decimal *a, my_decimal *b)
3060
3067
We need call of fixing buffer pointer, because fast sort just copy
3156
3157
delete [] (cmp_item_row*) base;
3159
unsigned char *in_row::get_value(Item *item)
3160
uchar *in_row::get_value(Item *item)
3161
3162
tmp.store_value(item);
3162
3163
if (item->is_null())
3164
return (unsigned char *)&tmp;
3165
return (uchar *)&tmp;
3167
void in_row::set(uint32_t pos, Item *item)
3168
void in_row::set(uint pos, Item *item)
3170
DBUG_ENTER("in_row::set");
3171
DBUG_PRINT("enter", ("pos: %u item: 0x%lx", pos, (ulong) item));
3169
3172
((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3173
in_int64_t::in_int64_t(uint32_t elements)
3174
:in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3176
in_longlong::in_longlong(uint elements)
3177
:in_vector(elements,sizeof(packed_longlong),(qsort2_cmp) cmp_longlong, 0)
3177
void in_int64_t::set(uint32_t pos,Item *item)
3180
void in_longlong::set(uint pos,Item *item)
3179
struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3182
struct packed_longlong *buff= &((packed_longlong*) base)[pos];
3181
3184
buff->val= item->val_int();
3182
3185
buff->unsigned_flag= item->unsigned_flag;
3185
unsigned char *in_int64_t::get_value(Item *item)
3188
uchar *in_longlong::get_value(Item *item)
3187
3190
tmp.val= item->val_int();
3188
3191
if (item->null_value)
3190
3193
tmp.unsigned_flag= item->unsigned_flag;
3191
return (unsigned char*) &tmp;
3194
return (uchar*) &tmp;
3194
void in_datetime::set(uint32_t pos,Item *item)
3197
void in_datetime::set(uint pos,Item *item)
3196
3199
Item **tmp_item= &item;
3198
struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3201
struct packed_longlong *buff= &((packed_longlong*) base)[pos];
3200
buff->val= get_datetime_value(session, &tmp_item, 0, warn_item, &is_null);
3203
buff->val= get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
3201
3204
buff->unsigned_flag= 1L;
3204
unsigned char *in_datetime::get_value(Item *item)
3207
uchar *in_datetime::get_value(Item *item)
3207
3210
Item **tmp_item= lval_cache ? &lval_cache : &item;
3208
tmp.val= get_datetime_value(session, &tmp_item, &lval_cache, warn_item, &is_null);
3211
tmp.val= get_datetime_value(thd, &tmp_item, &lval_cache, warn_item, &is_null);
3209
3212
if (item->null_value)
3211
3214
tmp.unsigned_flag= 1L;
3212
return (unsigned char*) &tmp;
3215
return (uchar*) &tmp;
3215
in_double::in_double(uint32_t elements)
3218
in_double::in_double(uint elements)
3216
3219
:in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
3219
void in_double::set(uint32_t pos,Item *item)
3222
void in_double::set(uint pos,Item *item)
3221
3224
((double*) base)[pos]= item->val_real();
3224
unsigned char *in_double::get_value(Item *item)
3227
uchar *in_double::get_value(Item *item)
3226
3229
tmp= item->val_real();
3227
3230
if (item->null_value)
3229
return (unsigned char*) &tmp;
3231
return 0; /* purecov: inspected */
3232
return (uchar*) &tmp;
3233
in_decimal::in_decimal(uint32_t elements)
3236
in_decimal::in_decimal(uint elements)
3234
3237
:in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3238
void in_decimal::set(uint32_t pos, Item *item)
3241
void in_decimal::set(uint pos, Item *item)
3240
3243
/* as far as 'item' is constant, we can store reference on my_decimal */
3241
3244
my_decimal *dec= ((my_decimal *)base) + pos;
3242
3245
dec->len= DECIMAL_BUFF_LENGTH;
3243
3246
dec->fix_buffer_pointer();
3244
3247
my_decimal *res= item->val_decimal(dec);
3245
/* if item->val_decimal() is evaluated to NULL then res == 0 */
3248
/* if item->val_decimal() is evaluated to NULL then res == 0 */
3246
3249
if (!item->null_value && res != dec)
3247
3250
my_decimal2decimal(res, dec);
3251
unsigned char *in_decimal::get_value(Item *item)
3254
uchar *in_decimal::get_value(Item *item)
3253
3256
my_decimal *result= item->val_decimal(&val);
3254
3257
if (item->null_value)
3256
return (unsigned char *)result;
3259
return (uchar *)result;
3260
3263
cmp_item* cmp_item::get_comparator(Item_result type,
3261
const CHARSET_INFO * const cs)
3263
3266
switch (type) {
3264
3267
case STRING_RESULT:
3673
3679
IN must compare INT columns and constants as int values (the same
3674
3680
way as equality does).
3675
So we must check here if the column on the left and all the constant
3676
values on the right can be compared as integers and adjust the
3681
So we must check here if the column on the left and all the constant
3682
values on the right can be compared as integers and adjust the
3677
3683
comparison type accordingly.
3679
3685
if (args[0]->real_item()->type() == FIELD_ITEM &&
3680
session->lex->sql_command != SQLCOM_SHOW_CREATE &&
3686
thd->lex->sql_command != SQLCOM_SHOW_CREATE &&
3681
3687
cmp_type != INT_RESULT)
3683
3689
Item_field *field_item= (Item_field*) (args[0]->real_item());
3684
if (field_item->field->can_be_compared_as_int64_t())
3690
if (field_item->field->can_be_compared_as_longlong())
3686
3692
bool all_converted= true;
3687
3693
for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3689
if (!convert_constant_item (session, field_item, &arg[0]))
3695
if (!convert_constant_item (thd, field_item, &arg[0]))
3690
3696
all_converted= false;
3692
3698
if (all_converted)
3800
3804
Value of the function
3803
int64_t Item_func_in::val_int()
3807
longlong Item_func_in::val_int()
3805
3809
cmp_item *in_item;
3807
uint32_t value_added_map= 0;
3810
DBUG_ASSERT(fixed == 1);
3811
uint value_added_map= 0;
3810
3814
int tmp=array->find(args[0]);
3811
3815
null_value=args[0]->null_value || (!tmp && have_null);
3812
return (int64_t) (!null_value && tmp != negated);
3816
return (longlong) (!null_value && tmp != negated);
3815
for (uint32_t i= 1 ; i < arg_count ; i++)
3819
for (uint i= 1 ; i < arg_count ; i++)
3817
3821
Item_result cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
3818
in_item= cmp_items[(uint32_t)cmp_type];
3820
if (!(value_added_map & (1 << (uint32_t)cmp_type)))
3822
in_item= cmp_items[(uint)cmp_type];
3823
DBUG_ASSERT(in_item);
3824
if (!(value_added_map & (1 << (uint)cmp_type)))
3822
3826
in_item->store_value(args[0]);
3823
3827
if ((null_value=args[0]->null_value))
3826
value_added_map|= 1 << (uint32_t)cmp_type;
3830
value_added_map|= 1 << (uint)cmp_type;
3828
3832
if (!in_item->cmp(args[i]) && !args[i]->null_value)
3829
return (int64_t) (!negated);
3833
return (longlong) (!negated);
3830
3834
have_null|= args[i]->null_value;
3833
3837
null_value= have_null;
3834
return (int64_t) (!null_value && negated);
3838
Item_cond::Item_cond(Session *session, Item_cond *item)
3839
:Item_bool_func(session, item),
3838
return (longlong) (!null_value && negated);
3842
longlong Item_func_bit_or::val_int()
3844
DBUG_ASSERT(fixed == 1);
3845
ulonglong arg1= (ulonglong) args[0]->val_int();
3846
if (args[0]->null_value)
3848
null_value=1; /* purecov: inspected */
3849
return 0; /* purecov: inspected */
3851
ulonglong arg2= (ulonglong) args[1]->val_int();
3852
if (args[1]->null_value)
3858
return (longlong) (arg1 | arg2);
3862
longlong Item_func_bit_and::val_int()
3864
DBUG_ASSERT(fixed == 1);
3865
ulonglong arg1= (ulonglong) args[0]->val_int();
3866
if (args[0]->null_value)
3868
null_value=1; /* purecov: inspected */
3869
return 0; /* purecov: inspected */
3871
ulonglong arg2= (ulonglong) args[1]->val_int();
3872
if (args[1]->null_value)
3874
null_value=1; /* purecov: inspected */
3875
return 0; /* purecov: inspected */
3878
return (longlong) (arg1 & arg2);
3881
Item_cond::Item_cond(THD *thd, Item_cond *item)
3882
:Item_bool_func(thd, item),
3840
3883
abort_on_null(item->abort_on_null),
3841
3884
and_tables_cache(item->and_tables_cache)
3849
void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
3892
void Item_cond::copy_andor_arguments(THD *thd, Item_cond *item)
3851
3894
List_iterator_fast<Item> li(item->list);
3852
3895
while (Item *it= li++)
3853
list.push_back(it->copy_andor_structure(session));
3896
list.push_back(it->copy_andor_structure(thd));
3858
Item_cond::fix_fields(Session *session, Item **)
3901
Item_cond::fix_fields(THD *thd, Item **ref __attribute__((__unused__)))
3903
DBUG_ASSERT(fixed == 0);
3861
3904
List_iterator<Item> li(list);
3863
void *orig_session_marker= session->session_marker;
3864
unsigned char buff[sizeof(char*)]; // Max local vars in function
3906
void *orig_thd_marker= thd->thd_marker;
3907
uchar buff[sizeof(char*)]; // Max local vars in function
3865
3908
not_null_tables_cache= used_tables_cache= 0;
3866
3909
const_item_cache= 1;
3868
3911
if (functype() == COND_OR_FUNC)
3869
session->session_marker= 0;
3871
3914
and_table_cache is the value that Item_cond_or() returns for
3872
3915
not_null_tables()
3874
3917
and_tables_cache= ~(table_map) 0;
3876
if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
3919
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
3877
3920
return true; // Fatal error flag is set!
3879
3922
The following optimization reduces the depth of an AND-OR tree.
3979
4022
Transform an Item_cond object with a transformer callback function.
3981
4024
The function recursively applies the transform method to each
3982
4025
member item of the condition list.
3983
4026
If the call of the method for a member item returns a new item
3984
4027
the old item is substituted for a new one.
3985
4028
After this the transformer is applied to the root node
3986
of the Item_cond object.
4029
of the Item_cond object.
3988
4031
@param transformer the transformer callback function to be applied to
3989
4032
the nodes of the tree of the object
3990
4033
@param arg parameter to be passed to the transformer
3993
Item returned as the result of transformation of the root node
4036
Item returned as the result of transformation of the root node
3996
Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
4039
Item *Item_cond::transform(Item_transformer transformer, uchar *arg)
3998
4041
List_iterator<Item> li(list);
4037
4080
@param arg_t parameter to be passed to the transformer
4040
Item returned as the result of transformation of the root node
4083
Item returned as the result of transformation of the root node
4043
Item *Item_cond::compile(Item_analyzer analyzer, unsigned char **arg_p,
4044
Item_transformer transformer, unsigned char *arg_t)
4086
Item *Item_cond::compile(Item_analyzer analyzer, uchar **arg_p,
4087
Item_transformer transformer, uchar *arg_t)
4046
4089
if (!(this->*analyzer)(arg_p))
4049
4092
List_iterator<Item> li(list);
4051
4094
while ((item= li++))
4054
4097
The same parameter value of arg_p must be passed
4055
4098
to analyze any argument of the condition formula.
4057
unsigned char *arg_v= *arg_p;
4100
uchar *arg_v= *arg_p;
4058
4101
Item *new_item= item->compile(analyzer, &arg_v, transformer, arg_t);
4059
4102
if (new_item && new_item != item)
4060
4103
li.replace(new_item);
4398
4442
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4402
4446
if (escape_item->const_item())
4405
4448
/* If we are on execution stage */
4406
4449
String *escape_str= escape_item->val_str(&tmp_value1);
4407
4450
if (escape_str)
4409
escape= (char *)memory::sql_alloc(escape_str->length());
4410
strcpy(escape, escape_str->ptr());
4452
if (escape_used_in_parsing && (
4453
(((thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
4454
escape_str->numchars() != 1) ||
4455
escape_str->numchars() > 1)))
4457
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4461
if (use_mb(cmp.cmp_collation.collation))
4463
CHARSET_INFO *cs= escape_str->charset();
4465
int rc= cs->cset->mb_wc(cs, &wc,
4466
(const uchar*) escape_str->ptr(),
4467
(const uchar*) escape_str->ptr() +
4468
escape_str->length());
4469
escape= (int) (rc > 0 ? wc : '\\');
4474
In the case of 8bit character set, we pass native
4475
code instead of Unicode code as "escape" argument.
4476
Convert to "cs" if charset of escape differs.
4478
CHARSET_INFO *cs= cmp.cmp_collation.collation;
4480
if (escape_str->needs_conversion(escape_str->length(),
4481
escape_str->charset(), cs, &unused))
4485
uint32 cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
4486
escape_str->length(),
4487
escape_str->charset(), &errors);
4488
escape= cnvlen ? ch : '\\';
4491
escape= *(escape_str->ptr());
4414
escape= (char *)memory::sql_alloc(1);
4415
strcpy(escape, "\\");
4419
4498
We could also do boyer-more for non-const items, but as we would have to
4420
4499
recompute the tables for each row it's not worth it.
4422
if (args[1]->const_item() && !use_strnxfrm(collation.collation))
4501
if (args[1]->const_item() && !use_strnxfrm(collation.collation) &&
4502
!(specialflag & SPECIAL_NO_NEW_FUNC))
4424
4504
String* res2 = args[1]->val_str(&tmp_value2);
4426
4506
return false; // Null argument
4428
4508
const size_t len = res2->length();
4429
4509
const char* first = res2->ptr();
4430
4510
const char* last = first + len - 1;
4432
4512
len must be > 2 ('%pattern%')
4433
4513
heuristic: only do TurboBM for pattern_len > 2
4436
4516
if (len > MIN_TURBOBM_PATTERN_LEN + 2 &&
4437
*first == internal::wild_many &&
4438
*last == internal::wild_many)
4517
*first == wild_many &&
4440
4520
const char* tmp = first + 1;
4441
for (; *tmp != internal::wild_many && *tmp != internal::wild_one; tmp++)
4521
for (; *tmp != wild_many && *tmp != wild_one && *tmp != escape; tmp++) ;
4447
4522
canDoTurboBM = (tmp == last) && !use_mb(args[0]->collation.collation);
4449
4524
if (canDoTurboBM)
4451
4526
pattern = first + 1;
4452
4527
pattern_len = (int) len - 2;
4453
int *suff = (int*) session->alloc((int) (sizeof(int)*
4528
DBUG_PRINT("info", ("Initializing pattern: '%s'", first));
4529
int *suff = (int*) thd->alloc((int) (sizeof(int)*
4454
4530
((pattern_len + 1)*2+
4455
4531
alphabet_size)));
4456
4532
bmGs = suff + pattern_len + 1;
4457
4533
bmBc = bmGs + pattern_len + 1;
4458
4534
turboBM_compute_good_suffix_shifts(suff);
4459
4535
turboBM_compute_bad_character_shifts();
4536
DBUG_PRINT("info",("done"));
4488
4565
int *const splm1 = suff + plm1;
4489
const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
4566
CHARSET_INFO *cs= cmp.cmp_collation.collation;
4491
4568
*splm1 = pattern_len;
4493
4570
if (!cs->sort_order)
4495
for (int i = pattern_len - 2; i >= 0; i--)
4573
for (i = pattern_len - 2; i >= 0; i--)
4497
4575
int tmp = *(splm1 + i - f);
4498
4576
if (g < i && tmp < i - g)
4505
while (g >= 0 && pattern[g] == pattern[g + plm1 - f])
4581
g = i; // g = min(i, g)
4583
while (g >= 0 && pattern[g] == pattern[g + plm1 - f])
4513
for (int i = pattern_len - 2; 0 <= i; --i)
4592
for (i = pattern_len - 2; 0 <= i; --i)
4515
4594
int tmp = *(splm1 + i - f);
4516
4595
if (g < i && tmp < i - g)
4524
likeconv(cs, pattern[g]) == likeconv(cs, pattern[g + plm1 - f]))
4600
g = i; // g = min(i, g)
4603
likeconv(cs, pattern[g]) == likeconv(cs, pattern[g + plm1 - f]))
4639
4718
register const int v = plm1 - i;
4640
4719
turboShift = u - v;
4641
bcShift = bmBc[(uint32_t) (unsigned char) text[i + j]] - plm1 + i;
4642
shift = (turboShift > bcShift) ? turboShift : bcShift;
4643
shift = (shift > bmGs[i]) ? shift : bmGs[i];
4720
bcShift = bmBc[(uint) (uchar) text[i + j]] - plm1 + i;
4721
shift = max(turboShift, bcShift);
4722
shift = max(shift, bmGs[i]);
4644
4723
if (shift == bmGs[i])
4645
u = (pattern_len - shift < v) ? pattern_len - shift : v;
4724
u = min(pattern_len - shift, v);
4648
if (turboShift < bcShift)
4649
shift= max(shift, u + 1);
4727
if (turboShift < bcShift)
4728
shift = max(shift, u + 1);
4660
4739
register int i = plm1;
4661
4740
while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
4664
if (i == plm1 - shift)
4743
if (i == plm1 - shift)
4671
register const int v= plm1 - i;
4673
bcShift= bmBc[(uint32_t) likeconv(cs, text[i + j])] - plm1 + i;
4674
shift= (turboShift > bcShift) ? turboShift : bcShift;
4675
shift= max(shift, bmGs[i]);
4749
register const int v = plm1 - i;
4751
bcShift = bmBc[(uint) likeconv(cs, text[i + j])] - plm1 + i;
4752
shift = max(turboShift, bcShift);
4753
shift = max(shift, bmGs[i]);
4677
4754
if (shift == bmGs[i])
4678
u= (pattern_len - shift < v) ? pattern_len - shift : v;
4755
u = min(pattern_len - shift, v);
4681
if (turboShift < bcShift)
4682
shift= max(shift, u + 1);
4758
if (turboShift < bcShift)
4759
shift = max(shift, u + 1);
4778
4854
a IS NOT NULL -> a IS NULL.
4780
Item *Item_func_isnotnull::neg_transformer(Session *)
4856
Item *Item_func_isnotnull::neg_transformer(THD *thd __attribute__((__unused__)))
4782
4858
Item *item= new Item_func_isnull(args[0]);
4787
Item *Item_cond_and::neg_transformer(Session *session) /* NOT(a AND b AND ...) -> */
4863
Item *Item_cond_and::neg_transformer(THD *thd) /* NOT(a AND b AND ...) -> */
4788
4864
/* NOT a OR NOT b OR ... */
4790
neg_arguments(session);
4791
4867
Item *item= new Item_cond_or(list);
4796
Item *Item_cond_or::neg_transformer(Session *session) /* NOT(a OR b OR ...) -> */
4872
Item *Item_cond_or::neg_transformer(THD *thd) /* NOT(a OR b OR ...) -> */
4797
4873
/* NOT a AND NOT b AND ... */
4799
neg_arguments(session);
4800
4876
Item *item= new Item_cond_and(list);
4805
Item *Item_func_nop_all::neg_transformer(Session *)
4881
Item *Item_func_nop_all::neg_transformer(THD *thd __attribute__((__unused__)))
4807
4883
/* "NOT (e $cmp$ ANY (SELECT ...)) -> e $rev_cmp$" ALL (SELECT ...) */
4808
4884
Item_func_not_all *new_item= new Item_func_not_all(args[0]);