21
21
This file defines all compare functions
24
#include <drizzled/server_includes.h>
25
#include <drizzled/sql_select.h>
24
#ifdef USE_PRAGMA_IMPLEMENTATION
25
#pragma implementation // gcc: Class implementation
28
#include "mysql_priv.h"
30
#include "sql_select.h"
27
32
static bool convert_constant_item(THD *, Item_field *, Item **);
29
34
static Item_result item_store_type(Item_result a, Item *item,
35
my_bool unsigned_flag)
32
37
Item_result b= item->result_type();
45
static void agg_result_type(Item_result *type, Item **items, uint32_t nitems)
50
static void agg_result_type(Item_result *type, Item **items, uint nitems)
47
52
Item **item, **item_end;
48
bool unsigned_flag= 0;
53
my_bool unsigned_flag= 0;
50
55
*type= STRING_RESULT;
51
56
/* Skip beginning NULL items */
91
96
static int cmp_row_type(Item* item1, Item* item2)
93
uint32_t n= item1->cols();
98
uint n= item1->cols();
94
99
if (item2->check_cols(n))
96
for (uint32_t i=0; i<n; i++)
101
for (uint i=0; i<n; i++)
98
103
if (item2->element_index(i)->check_cols(item1->element_index(i)->cols()) ||
99
104
(item1->element_index(i)->result_type() == ROW_RESULT &&
130
static int agg_cmp_type(Item_result *type, Item **items, uint32_t nitems)
135
static int agg_cmp_type(Item_result *type, Item **items, uint nitems)
133
138
type[0]= items[0]->result_type();
134
139
for (i= 1 ; i < nitems ; i++)
166
171
@return aggregated field type.
169
enum_field_types agg_field_type(Item **items, uint32_t nitems)
174
enum_field_types agg_field_type(Item **items, uint nitems)
172
177
if (!nitems || items[0]->result_type() == ROW_RESULT )
173
178
return (enum_field_types)-1;
174
179
enum_field_types res= items[0]->field_type();
194
199
Bitmap of collected types - otherwise
197
static uint32_t collect_cmp_types(Item **items, uint32_t nitems)
202
static uint collect_cmp_types(Item **items, uint nitems)
200
uint32_t found_types;
201
206
Item_result left_result= items[0]->result_type();
207
DBUG_ASSERT(nitems > 1);
204
209
for (i= 1; i < nitems ; i++)
264
269
NULL if some arg is NULL.
267
int64_t Item_func_not::val_int()
272
longlong Item_func_not::val_int()
274
DBUG_ASSERT(fixed == 1);
270
275
bool value= args[0]->val_bool();
271
276
null_value=args[0]->null_value;
272
277
return ((!null_value && value == 0) ? 1 : 0);
296
int64_t Item_func_not_all::val_int()
301
longlong Item_func_not_all::val_int()
303
DBUG_ASSERT(fixed == 1);
299
304
bool value= args[0]->val_bool();
302
return true if there was records in underlying select in max/min
307
return TRUE if there was records in underlying select in max/min
303
308
optimization (ALL subquery)
305
310
if (empty_underlying_subquery())
330
335
Item_func_not_all.
333
(return true if underlying subquery do not return rows) but if subquery
334
returns some rows it return same value as argument (true/false).
338
(return TRUE if underlying subquery do not return rows) but if subquery
339
returns some rows it return same value as argument (TRUE/FALSE).
337
int64_t Item_func_nop_all::val_int()
342
longlong Item_func_nop_all::val_int()
340
int64_t value= args[0]->val_int();
344
DBUG_ASSERT(fixed == 1);
345
longlong value= args[0]->val_int();
343
return false if there was records in underlying select in max/min
348
return FALSE if there was records in underlying select in max/min
344
349
optimization (SAME/ANY subquery)
346
351
if (empty_underlying_subquery())
387
392
if (!(*item)->with_subselect && (*item)->const_item())
394
TABLE *table= field->table;
389
395
ulong orig_sql_mode= thd->variables.sql_mode;
390
396
enum_check_fields orig_count_cuted_fields= thd->count_cuted_fields;
391
uint64_t orig_field_val= 0; /* original field value if valid */
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);
393
406
/* For comparison purposes allow invalid dates like 2000-01-32 */
394
407
thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
395
408
MODE_INVALID_DATES;
412
425
/* Restore the original field value. */
413
426
if (field_item->depended_from)
415
result= field->store(orig_field_val, true);
428
result= field->store(orig_field_val, TRUE);
416
429
/* orig_field_val must be a valid value that can be restored back. */
430
DBUG_ASSERT(!result);
419
432
thd->variables.sql_mode= orig_sql_mode;
420
433
thd->count_cuted_fields= orig_count_cuted_fields;
436
dbug_tmp_restore_column_map(table->write_set, old_write_map);
437
dbug_tmp_restore_column_map(table->read_set, old_read_map);
467
485
thd= current_thd;
469
if (args[0]->real_item()->type() == FIELD_ITEM)
486
if (!thd->is_context_analysis_only())
471
Item_field *field_item= (Item_field*) (args[0]->real_item());
472
if (field_item->field->can_be_compared_as_int64_t() &&
473
!(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
488
if (args[0]->real_item()->type() == FIELD_ITEM)
475
if (convert_constant_item(thd, 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))
477
cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
478
INT_RESULT); // Works for all types.
479
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;
484
504
if (args[1]->real_item()->type() == FIELD_ITEM)
486
506
Item_field *field_item= (Item_field*) (args[1]->real_item());
487
if (field_item->field->can_be_compared_as_int64_t() &&
507
if (field_item->field->can_be_compared_as_longlong() &&
488
508
!(field_item->is_datetime() &&
489
509
args[0]->result_type() == STRING_RESULT))
561
581
which would be transformed to:
564
(*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
565
(*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
584
(*a)->walk(&Item::set_no_const_sub, FALSE, (uchar*) 0);
585
(*b)->walk(&Item::set_no_const_sub, FALSE, (uchar*) 0);
591
611
if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
593
precision= 5 / log_10[cmax((*a)->decimals, (*b)->decimals) + 1];
613
precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
594
614
if (func == &Arg_comparator::compare_real)
595
615
func= &Arg_comparator::compare_real_fixed;
596
616
else if (func == &Arg_comparator::compare_e_real)
627
647
converted value. 0 on error and on zero-dates -- check 'failure'
631
get_date_from_str(THD *thd, String *str, enum enum_drizzle_timestamp_type warn_type,
651
get_date_from_str(THD *thd, String *str, timestamp_type warn_type,
632
652
char *warn_name, bool *error_arg)
637
enum enum_drizzle_timestamp_type ret;
657
enum_mysql_timestamp_type ret;
639
659
ret= str_to_datetime(str->ptr(), str->length(), &l_time,
640
660
(TIME_FUZZY_DATE | MODE_INVALID_DATES |
641
(thd->variables.sql_mode & MODE_NO_ZERO_DATE)),
661
(thd->variables.sql_mode &
662
(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE))),
644
if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
665
if (ret == MYSQL_TIMESTAMP_DATETIME || ret == MYSQL_TIMESTAMP_DATE)
647
668
Do not return yet, we may still want to throw a "trailing garbage"
651
value= TIME_to_uint64_t_datetime(&l_time);
672
value= TIME_to_ulonglong_datetime(&l_time);
656
677
error= 1; /* force warning */
660
make_truncated_value_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
681
make_truncated_value_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
661
682
str->ptr(), str->length(),
662
683
warn_type, warn_name);
700
721
enum Arg_comparator::enum_date_cmp_type
701
Arg_comparator::can_compare_as_dates(Item *a, Item *b, uint64_t *const_value)
722
Arg_comparator::can_compare_as_dates(Item *a, Item *b, ulonglong *const_value)
703
724
enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
704
725
Item *str_arg= 0, *date_arg= 0;
727
748
if (cmp_type != CMP_DATE_DFLT)
730
Do not cache GET_USER_VAR() function as its const_item() may return true
751
Do not cache GET_USER_VAR() function as its const_item() may return TRUE
731
752
for the current thread but it still may change during the execution.
733
754
if (cmp_type != CMP_DATE_WITH_DATE && str_arg->const_item() &&
735
756
((Item_func*)str_arg)->functype() != Item_func::GUSERVAR_FUNC))
737
758
THD *thd= current_thd;
740
761
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);
762
timestamp_type t_type= (date_arg->field_type() == MYSQL_TYPE_DATE ?
763
MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME);
744
765
str_val= str_arg->val_str(&tmp);
745
766
if (str_arg->null_value)
764
785
item_arg [in/out] item to retrieve TIME value from
765
786
cache_arg [in/out] pointer to place to store the cache item to
766
787
warn_item [in] unused
767
is_null [out] true <=> the item_arg is null
788
is_null [out] TRUE <=> the item_arg is null
770
791
Retrieves the correct TIME value from given item for comparison by the
771
792
compare_datetime() function.
772
If item's result can be compared as int64_t then its int value is used
793
If item's result can be compared as longlong then its int value is used
773
794
and a value returned by get_time function is used otherwise.
774
795
If an item is a constant one then its value is cached and it isn't
775
796
get parsed again. An Item_cache_int object is used for for cached values.
784
get_time_value(THD *thd __attribute__((unused)),
785
Item ***item_arg, Item **cache_arg,
786
Item *warn_item __attribute__((unused)),
805
get_time_value(THD *thd, Item ***item_arg, Item **cache_arg,
806
Item *warn_item, bool *is_null)
790
809
Item *item= **item_arg;
793
if (item->result_as_int64_t())
812
if (item->result_as_longlong())
795
814
value= item->val_int();
796
815
*is_null= item->null_value;
800
819
*is_null= item->get_time(<ime);
801
value= !*is_null ? TIME_to_uint64_t_datetime(<ime) : 0;
820
value= !*is_null ? TIME_to_ulonglong_datetime(<ime) : 0;
804
Do not cache GET_USER_VAR() function as its const_item() may return true
823
Do not cache GET_USER_VAR() function as its const_item() may return TRUE
805
824
for the current thread but it still may change during the execution.
807
826
if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
859
878
get_value_func= &get_datetime_value;
862
else if (type == STRING_RESULT && (*a)->field_type() == DRIZZLE_TYPE_TIME &&
863
(*b)->field_type() == DRIZZLE_TYPE_TIME)
881
else if (type == STRING_RESULT && (*a)->field_type() == MYSQL_TYPE_TIME &&
882
(*b)->field_type() == MYSQL_TYPE_TIME)
865
884
/* Compare TIME values as integers. */
866
885
thd= current_thd;
903
922
item_arg [in/out] item to retrieve DATETIME value from
904
923
cache_arg [in/out] pointer to place to store the caching item to
905
924
warn_item [in] item for issuing the conversion warning
906
is_null [out] true <=> the item_arg is null
925
is_null [out] TRUE <=> the item_arg is null
909
928
Retrieves the correct DATETIME value from given item for comparison by the
910
929
compare_datetime() function.
911
If item's result can be compared as int64_t then its int value is used
930
If item's result can be compared as longlong then its int value is used
912
931
and its string value is used otherwise. Strings are always parsed and
913
932
converted to int values by the get_date_from_str() function.
914
933
This allows us to compare correctly string dates with missed insignificant
927
946
get_datetime_value(THD *thd, Item ***item_arg, Item **cache_arg,
928
947
Item *warn_item, bool *is_null)
931
950
String buf, *str= 0;
932
951
Item *item= **item_arg;
934
if (item->result_as_int64_t())
953
if (item->result_as_longlong())
936
955
value= item->val_int();
937
956
*is_null= item->null_value;
938
957
enum_field_types f_type= item->field_type();
940
Item_date_add_interval may return DRIZZLE_TYPE_STRING as the result
959
Item_date_add_interval may return MYSQL_TYPE_STRING as the result
941
960
field type. To detect that the DATE value has been returned we
942
961
compare it with 100000000L - any DATE value should be less than it.
943
962
Don't shift cached DATETIME values up for the second time.
945
if (f_type == DRIZZLE_TYPE_NEWDATE ||
946
(f_type != DRIZZLE_TYPE_DATETIME && value < 100000000L))
964
if (f_type == MYSQL_TYPE_DATE ||
965
(f_type != MYSQL_TYPE_DATETIME && value < 100000000L))
947
966
value*= 1000000L;
965
984
enum_field_types f_type= warn_item->field_type();
966
enum enum_drizzle_timestamp_type t_type= f_type ==
967
DRIZZLE_TYPE_NEWDATE ? DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME;
985
timestamp_type t_type= f_type ==
986
MYSQL_TYPE_DATE ? MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME;
968
987
value= get_date_from_str(thd, str, t_type, warn_item->name, &error);
970
989
If str did not contain a valid date according to the current
977
Do not cache GET_USER_VAR() function as its const_item() may return true
996
Do not cache GET_USER_VAR() function as its const_item() may return TRUE
978
997
for the current thread but it still may change during the execution.
980
999
if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
981
1000
((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
983
Item_cache_int *cache= new Item_cache_int(DRIZZLE_TYPE_DATETIME);
1002
Item_cache_int *cache= new Item_cache_int(MYSQL_TYPE_DATETIME);
984
1003
/* Mark the cache as non-const to prevent re-caching. */
985
1004
cache->set_used_tables(1);
986
1005
cache->store(item, value);
1014
1033
int Arg_comparator::compare_datetime()
1016
bool is_null= false;
1017
uint64_t a_value, b_value;
1035
bool is_null= FALSE;
1036
ulonglong a_value, b_value;
1019
1038
/* Get DATE/DATETIME/TIME value of the 'a' item. */
1020
1039
a_value= (*get_value_func)(thd, &a, &a_cache, *b, &is_null);
1079
1098
if ((res2= (*b)->val_str(&owner->tmp_value2)))
1081
1100
owner->null_value= 0;
1082
uint32_t res1_length= res1->length();
1083
uint32_t res2_length= res2->length();
1084
int cmp= memcmp(res1->ptr(), res2->ptr(), cmin(res1_length,res2_length));
1101
uint res1_length= res1->length();
1102
uint res2_length= res2->length();
1103
int cmp= memcmp(res1->ptr(), res2->ptr(), min(res1_length,res2_length));
1085
1104
return cmp ? cmp : (int) (res1_length - res2_length);
1265
1284
int Arg_comparator::compare_int_signed_unsigned()
1267
int64_t sval1= (*a)->val_int();
1286
longlong sval1= (*a)->val_int();
1268
1287
if (!(*a)->null_value)
1270
uint64_t uval2= (uint64_t)(*b)->val_int();
1289
ulonglong uval2= (ulonglong)(*b)->val_int();
1271
1290
if (!(*b)->null_value)
1273
1292
owner->null_value= 0;
1274
if (sval1 < 0 || (uint64_t)sval1 < uval2)
1293
if (sval1 < 0 || (ulonglong)sval1 < uval2)
1276
if ((uint64_t)sval1 == uval2)
1295
if ((ulonglong)sval1 == uval2)
1290
1309
int Arg_comparator::compare_int_unsigned_signed()
1292
uint64_t uval1= (uint64_t)(*a)->val_int();
1311
ulonglong uval1= (ulonglong)(*a)->val_int();
1293
1312
if (!(*a)->null_value)
1295
int64_t sval2= (*b)->val_int();
1314
longlong sval2= (*b)->val_int();
1296
1315
if (!(*b)->null_value)
1298
1317
owner->null_value= 0;
1301
if (uval1 < (uint64_t)sval2)
1320
if (uval1 < (ulonglong)sval2)
1303
if (uval1 == (uint64_t)sval2)
1322
if (uval1 == (ulonglong)sval2)
1313
1332
int Arg_comparator::compare_e_int()
1315
int64_t val1= (*a)->val_int();
1316
int64_t val2= (*b)->val_int();
1334
longlong val1= (*a)->val_int();
1335
longlong val2= (*b)->val_int();
1317
1336
if ((*a)->null_value || (*b)->null_value)
1318
1337
return test((*a)->null_value && (*b)->null_value);
1319
1338
return test(val1 == val2);
1325
1344
int Arg_comparator::compare_e_int_diff_signedness()
1327
int64_t val1= (*a)->val_int();
1328
int64_t val2= (*b)->val_int();
1346
longlong val1= (*a)->val_int();
1347
longlong val2= (*b)->val_int();
1329
1348
if ((*a)->null_value || (*b)->null_value)
1330
1349
return test((*a)->null_value && (*b)->null_value);
1331
1350
return (val1 >= 0) && test(val1 == val2);
1420
1439
if (args[0]->null_value)
1423
NULL val IS {true, false} --> false
1424
NULL val IS NOT {true, false} --> true
1442
NULL val IS {TRUE, FALSE} --> FALSE
1443
NULL val IS NOT {TRUE, FALSE} --> TRUE
1426
1445
return (! affirmative);
1429
1448
if (affirmative)
1431
/* {true, false} val IS {true, false} value */
1450
/* {TRUE, FALSE} val IS {TRUE, FALSE} value */
1432
1451
return (val == value);
1435
/* {true, false} val IS NOT {true, false} value */
1454
/* {TRUE, FALSE} val IS NOT {TRUE, FALSE} value */
1436
1455
return (val != value);
1440
int64_t Item_func_truth::val_int()
1459
longlong Item_func_truth::val_int()
1442
1461
return (val_bool() ? 1 : 0);
1446
bool Item_in_optimizer::fix_left(THD *thd, Item **ref __attribute__((unused)))
1465
bool Item_in_optimizer::fix_left(THD *thd, Item **ref)
1448
1467
if ((!args[0]->fixed && args[0]->fix_fields(thd, args)) ||
1449
1468
(!cache && !(cache= Item_cache::get_cache(args[0]))))
1462
uint32_t n= cache->cols();
1463
for (uint32_t i= 0; i < n; i++)
1481
uint n= cache->cols();
1482
for (uint i= 0; i < n; i++)
1465
1484
if (args[0]->element_index(i)->used_tables())
1466
1485
((Item_cache *)cache->element_index(i))->set_used_tables(OUTER_REF_TABLE_BIT);
1480
1499
bool Item_in_optimizer::fix_fields(THD *thd, Item **ref)
1501
DBUG_ASSERT(fixed == 0);
1483
1502
if (fix_left(thd, ref))
1485
1504
if (args[0]->maybe_null)
1488
1507
if (!args[1]->fixed && args[1]->fix_fields(thd, args+1))
1490
1509
Item_in_subselect * sub= (Item_in_subselect *)args[1];
1491
1510
if (args[0]->cols() != sub->engine->cols())
1493
1512
my_error(ER_OPERAND_COLUMNS, MYF(0), args[0]->cols());
1496
1515
if (args[1]->maybe_null)
1534
1553
We're evaluating "NULL IN (SELECT ...)". The result is:
1535
false if SELECT produces an empty set, or
1554
FALSE if SELECT produces an empty set, or
1536
1555
NULL otherwise.
1537
1556
We disable the predicates we've pushed down into subselect, run the
1538
1557
subselect and see if it has produced any rows.
1540
1559
Item_in_subselect *item_subs=(Item_in_subselect*)args[1];
1541
1560
if (cache->cols() == 1)
1543
item_subs->set_cond_guard_var(0, false);
1562
item_subs->set_cond_guard_var(0, FALSE);
1544
1563
(void) args[1]->val_bool_result();
1545
1564
result_for_null_param= null_value= !item_subs->engine->no_rows();
1546
item_subs->set_cond_guard_var(0, true);
1565
item_subs->set_cond_guard_var(0, TRUE);
1551
uint32_t ncols= cache->cols();
1570
uint ncols= cache->cols();
1553
1572
Turn off the predicates that are based on column compares for
1554
1573
which the left part is currently NULL
1556
1575
for (i= 0; i < ncols; i++)
1558
1577
if (cache->element_index(i)->null_value)
1559
item_subs->set_cond_guard_var(i, false);
1578
item_subs->set_cond_guard_var(i, FALSE);
1562
1581
(void) args[1]->val_bool_result();
1621
1641
@retval NULL if an error occurred
1624
Item *Item_in_optimizer::transform(Item_transformer transformer, unsigned char *argument)
1644
Item *Item_in_optimizer::transform(Item_transformer transformer, uchar *argument)
1626
1646
Item *new_item;
1628
assert(arg_count == 2);
1648
DBUG_ASSERT(arg_count == 2);
1630
1650
/* Transform the left IN operand. */
1631
1651
new_item= (*args)->transform(transformer, argument);
1647
1667
transformation, we only make both operands the same.
1648
1668
TODO: is it the way it should be?
1650
assert((args[1])->type() == Item::SUBSELECT_ITEM &&
1670
DBUG_ASSERT((args[1])->type() == Item::SUBSELECT_ITEM &&
1651
1671
(((Item_subselect*)(args[1]))->substype() ==
1652
1672
Item_subselect::IN_SUBS ||
1653
1673
((Item_subselect*)(args[1]))->substype() ==
1679
1699
maybe_null=null_value=0;
1682
int64_t Item_func_equal::val_int()
1702
longlong Item_func_equal::val_int()
1704
DBUG_ASSERT(fixed == 1);
1685
1705
return cmp.compare();
1688
int64_t Item_func_ne::val_int()
1708
longlong Item_func_ne::val_int()
1710
DBUG_ASSERT(fixed == 1);
1691
1711
int value= cmp.compare();
1692
1712
return value != 0 && !null_value ? 1 : 0;
1696
int64_t Item_func_ge::val_int()
1716
longlong Item_func_ge::val_int()
1718
DBUG_ASSERT(fixed == 1);
1699
1719
int value= cmp.compare();
1700
1720
return value >= 0 ? 1 : 0;
1704
int64_t Item_func_gt::val_int()
1724
longlong Item_func_gt::val_int()
1726
DBUG_ASSERT(fixed == 1);
1707
1727
int value= cmp.compare();
1708
1728
return value > 0 ? 1 : 0;
1711
int64_t Item_func_le::val_int()
1731
longlong Item_func_le::val_int()
1733
DBUG_ASSERT(fixed == 1);
1714
1734
int value= cmp.compare();
1715
1735
return value <= 0 && !null_value ? 1 : 0;
1719
int64_t Item_func_lt::val_int()
1739
longlong Item_func_lt::val_int()
1741
DBUG_ASSERT(fixed == 1);
1722
1742
int value= cmp.compare();
1723
1743
return value < 0 && !null_value ? 1 : 0;
1727
int64_t Item_func_strcmp::val_int()
1747
longlong Item_func_strcmp::val_int()
1749
DBUG_ASSERT(fixed == 1);
1730
1750
String *a=args[0]->val_str(&tmp_value1);
1731
1751
String *b=args[1]->val_str(&tmp_value2);
1773
bool not_null_consts= true;
1793
bool not_null_consts= TRUE;
1775
for (uint32_t i= 1; not_null_consts && i < rows; i++)
1795
for (uint i= 1; not_null_consts && i < rows; i++)
1777
1797
Item *el= row->element_index(i);
1778
1798
not_null_consts&= el->const_item() & !el->is_null();
1839
1859
- arg_count if higher than biggest argument
1842
int64_t Item_func_interval::val_int()
1862
longlong Item_func_interval::val_int()
1864
DBUG_ASSERT(fixed == 1);
1846
1866
my_decimal dec_buf, *dec= NULL;
1849
1869
if (use_decimal_comparison)
1864
1884
{ // Use binary search to find interval
1867
1887
end= row->cols()-2;
1868
1888
while (start != end)
1870
uint32_t mid= (start + end + 1) / 2;
1890
uint mid= (start + end + 1) / 2;
1871
1891
interval_range *range= intervals + mid;
1874
1894
The values in the range intervall may have different types,
1875
1895
Only do a decimal comparision of the first argument is a decimal
1999
2019
if (args[i]->is_datetime())
2001
datetime_found= true;
2021
datetime_found= TRUE;
2004
if (args[i]->field_type() == DRIZZLE_TYPE_TIME &&
2005
args[i]->result_as_int64_t())
2024
if (args[i]->field_type() == MYSQL_TYPE_TIME &&
2025
args[i]->result_as_longlong())
2006
2026
time_items_found++;
2009
2029
if (!datetime_found)
2010
compare_as_dates= false;
2030
compare_as_dates= FALSE;
2012
2032
if (compare_as_dates)
2023
2043
thd->lex->sql_command != SQLCOM_SHOW_CREATE)
2025
2045
Item_field *field_item= (Item_field*) (args[0]->real_item());
2026
if (field_item->field->can_be_compared_as_int64_t())
2046
if (field_item->field->can_be_compared_as_longlong())
2029
2049
The following can't be recoded with || as convert_constant_item
2051
2071
le_res= le_cmp.compare();
2053
2073
if (!args[1]->null_value && !args[2]->null_value)
2054
return (int64_t) ((ge_res >= 0 && le_res <=0) != negated);
2074
return (longlong) ((ge_res >= 0 && le_res <=0) != negated);
2055
2075
else if (args[1]->null_value)
2057
2077
null_value= le_res > 0; // not null if false range.
2070
2090
a=args[1]->val_str(&value1);
2071
2091
b=args[2]->val_str(&value2);
2072
2092
if (!args[1]->null_value && !args[2]->null_value)
2073
return (int64_t) ((sortcmp(value,a,cmp_collation.collation) >= 0 &&
2093
return (longlong) ((sortcmp(value,a,cmp_collation.collation) >= 0 &&
2074
2094
sortcmp(value,b,cmp_collation.collation) <= 0) !=
2076
2096
if (args[1]->null_value && args[2]->null_value)
2089
2109
else if (cmp_type == INT_RESULT)
2091
int64_t value=args[0]->val_int(), a, b;
2111
longlong value=args[0]->val_int(), a, b;
2092
2112
if ((null_value=args[0]->null_value))
2093
2113
return 0; /* purecov: inspected */
2094
2114
a=args[1]->val_int();
2095
2115
b=args[2]->val_int();
2096
2116
if (!args[1]->null_value && !args[2]->null_value)
2097
return (int64_t) ((value >= a && value <= b) != negated);
2117
return (longlong) ((value >= a && value <= b) != negated);
2098
2118
if (args[1]->null_value && args[2]->null_value)
2100
2120
else if (args[1]->null_value)
2115
2135
a_dec= args[1]->val_decimal(&a_buf);
2116
2136
b_dec= args[2]->val_decimal(&b_buf);
2117
2137
if (!args[1]->null_value && !args[2]->null_value)
2118
return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2138
return (longlong) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2119
2139
my_decimal_cmp(dec, b_dec) <= 0) != negated);
2120
2140
if (args[1]->null_value && args[2]->null_value)
2132
2152
a= args[1]->val_real();
2133
2153
b= args[2]->val_real();
2134
2154
if (!args[1]->null_value && !args[2]->null_value)
2135
return (int64_t) ((value >= a && value <= b) != negated);
2155
return (longlong) ((value >= a && value <= b) != negated);
2136
2156
if (args[1]->null_value && args[2]->null_value)
2138
2158
else if (args[1]->null_value)
2167
2187
agg_result_type(&hybrid_type, args, 2);
2168
2188
maybe_null=args[1]->maybe_null;
2169
decimals= cmax(args[0]->decimals, args[1]->decimals);
2189
decimals= max(args[0]->decimals, args[1]->decimals);
2170
2190
unsigned_flag= args[0]->unsigned_flag && args[1]->unsigned_flag;
2172
2192
if (hybrid_type == DECIMAL_RESULT || hybrid_type == INT_RESULT)
2177
2197
int len1= args[1]->max_length - args[1]->decimals
2178
2198
- (args[1]->unsigned_flag ? 0 : 1);
2180
max_length= cmax(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2200
max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2183
max_length= cmax(args[0]->max_length, args[1]->max_length);
2203
max_length= max(args[0]->max_length, args[1]->max_length);
2185
2205
switch (hybrid_type) {
2186
2206
case STRING_RESULT:
2195
2215
case ROW_RESULT:
2199
2219
cached_field_type= agg_field_type(args, 2);
2203
uint32_t Item_func_ifnull::decimal_precision() const
2223
uint Item_func_ifnull::decimal_precision() const
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);
2225
int max_int_part=max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2226
return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2331
2351
Item_func_if::fix_length_and_dec()
2333
2353
maybe_null=args[1]->maybe_null || args[2]->maybe_null;
2334
decimals= cmax(args[1]->decimals, args[2]->decimals);
2354
decimals= max(args[1]->decimals, args[2]->decimals);
2335
2355
unsigned_flag=args[1]->unsigned_flag && args[2]->unsigned_flag;
2337
2357
enum Item_result arg1_type=args[1]->result_type();
2375
2395
int len2= args[2]->max_length - args[2]->decimals
2376
2396
- (args[2]->unsigned_flag ? 0 : 1);
2378
max_length=cmax(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
2398
max_length=max(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
2381
max_length= cmax(args[1]->max_length, args[2]->max_length);
2401
max_length= max(args[1]->max_length, args[2]->max_length);
2385
uint32_t Item_func_if::decimal_precision() const
2405
uint Item_func_if::decimal_precision() const
2387
int precision=(cmax(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
2407
int precision=(max(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
2389
return cmin(precision, DECIMAL_MAX_PRECISION);
2409
return min(precision, DECIMAL_MAX_PRECISION);
2394
2414
Item_func_if::val_real()
2416
DBUG_ASSERT(fixed == 1);
2397
2417
Item *arg= args[0]->val_bool() ? args[1] : args[2];
2398
2418
double value= arg->val_real();
2399
2419
null_value=arg->null_value;
2404
2424
Item_func_if::val_int()
2426
DBUG_ASSERT(fixed == 1);
2407
2427
Item *arg= args[0]->val_bool() ? args[1] : args[2];
2408
int64_t value=arg->val_int();
2428
longlong value=arg->val_int();
2409
2429
null_value=arg->null_value;
2555
Item *Item_func_case::find_item(String *str __attribute__((unused)))
2575
Item *Item_func_case::find_item(String *str)
2557
uint32_t value_added_map= 0;
2577
uint value_added_map= 0;
2559
2579
if (first_expr_num == -1)
2561
for (uint32_t i=0 ; i < ncases ; i+=2)
2581
for (uint i=0 ; i < ncases ; i+=2)
2563
2583
// No expression between CASE and the first WHEN
2564
2584
if (args[i]->val_bool())
2571
2591
/* Compare every WHEN argument with it and return the first match */
2572
for (uint32_t i=0 ; i < ncases ; i+=2)
2592
for (uint i=0 ; i < ncases ; i+=2)
2574
2594
cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
2575
assert(cmp_type != ROW_RESULT);
2576
assert(cmp_items[(uint)cmp_type]);
2595
DBUG_ASSERT(cmp_type != ROW_RESULT);
2596
DBUG_ASSERT(cmp_items[(uint)cmp_type]);
2577
2597
if (!(value_added_map & (1<<(uint)cmp_type)))
2579
2599
cmp_items[(uint)cmp_type]->store_value(args[first_expr_num]);
2611
int64_t Item_func_case::val_int()
2631
longlong Item_func_case::val_int()
2633
DBUG_ASSERT(fixed == 1);
2614
2634
char buff[MAX_FIELD_WIDTH];
2615
2635
String dummy_str(buff,sizeof(buff),default_charset());
2616
2636
Item *item=find_item(&dummy_str);
2648
2668
my_decimal *Item_func_case::val_decimal(my_decimal *decimal_value)
2670
DBUG_ASSERT(fixed == 1);
2651
2671
char buff[MAX_FIELD_WIDTH];
2652
2672
String dummy_str(buff, sizeof(buff), default_charset());
2653
2673
Item *item= find_item(&dummy_str);
2671
2691
buff should match stack usage from
2672
2692
Item_func_case::val_int() -> Item_func_case::find_item()
2674
unsigned char buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(int64_t)*2];
2694
uchar buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(longlong)*2];
2675
2695
bool res= Item_func::fix_fields(thd, ref);
2677
2697
Call check_stack_overrun after fix_fields to be sure that stack variable
2678
2698
is not optimized away
2680
2700
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2681
return true; // Fatal error flag is set!
2701
return TRUE; // Fatal error flag is set!
2694
2714
void Item_func_case::agg_num_lengths(Item *arg)
2696
uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2716
uint len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2697
2717
arg->unsigned_flag) - arg->decimals;
2698
2718
set_if_bigger(max_length, len);
2699
2719
set_if_bigger(decimals, arg->decimals);
2747
2767
if (found_types & (1 << i) && !cmp_items[i])
2749
assert((Item_result)i != ROW_RESULT);
2769
DBUG_ASSERT((Item_result)i != ROW_RESULT);
2750
2770
if ((Item_result)i == STRING_RESULT &&
2751
2771
agg_arg_charsets(cmp_collation, agg, nagg, MY_COLL_CMP_CONV, 1))
2766
unsigned_flag= true;
2786
unsigned_flag= TRUE;
2767
2787
if (cached_result_type == STRING_RESULT)
2769
for (uint32_t i= 0; i < ncases; i+= 2)
2789
for (uint i= 0; i < ncases; i+= 2)
2770
2790
agg_str_lengths(args[i + 1]);
2771
2791
if (else_expr_num != -1)
2772
2792
agg_str_lengths(args[else_expr_num]);
2776
for (uint32_t i= 0; i < ncases; i+= 2)
2796
for (uint i= 0; i < ncases; i+= 2)
2777
2797
agg_num_lengths(args[i + 1]);
2778
2798
if (else_expr_num != -1)
2779
2799
agg_num_lengths(args[else_expr_num]);
2786
uint32_t Item_func_case::decimal_precision() const
2806
uint Item_func_case::decimal_precision() const
2788
2808
int max_int_part=0;
2789
for (uint32_t i=0 ; i < ncases ; i+=2)
2809
for (uint i=0 ; i < ncases ; i+=2)
2790
2810
set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
2792
2812
if (else_expr_num != -1)
2793
2813
set_if_bigger(max_int_part, args[else_expr_num]->decimal_int_part());
2794
return cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2814
return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2808
2828
args[first_expr_num]->print(str, query_type);
2809
2829
str->append(' ');
2811
for (uint32_t i=0 ; i < ncases ; i+=2)
2831
for (uint i=0 ; i < ncases ; i+=2)
2813
2833
str->append(STRING_WITH_LEN("when "));
2814
2834
args[i]->print(str, query_type);
2860
int64_t Item_func_coalesce::int_op()
2881
longlong Item_func_coalesce::int_op()
2883
DBUG_ASSERT(fixed == 1);
2864
for (uint32_t i=0 ; i < arg_count ; i++)
2885
for (uint i=0 ; i < arg_count ; i++)
2866
int64_t res=args[i]->val_int();
2887
longlong res=args[i]->val_int();
2867
2888
if (!args[i]->null_value)
2889
2910
my_decimal *Item_func_coalesce::decimal_op(my_decimal *decimal_value)
2912
DBUG_ASSERT(fixed == 1);
2893
for (uint32_t i= 0; i < arg_count; i++)
2914
for (uint i= 0; i < arg_count; i++)
2895
2916
my_decimal *res= args[i]->val_decimal(decimal_value);
2896
2917
if (!args[i]->null_value)
2940
2961
b_val right argument
2943
This function will compare two signed int64_t arguments
2964
This function will compare two signed longlong arguments
2944
2965
and will return -1, 0, or 1 if left argument is smaller than,
2945
2966
equal to or greater than the right argument.
2949
2970
0 left argument is equal to the right argument.
2950
2971
1 left argument is greater than the right argument.
2952
static inline int cmp_longs (int64_t a_val, int64_t b_val)
2973
static inline int cmp_longs (longlong a_val, longlong b_val)
2954
2975
return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
2959
Determine which of the unsigned int64_t arguments is bigger
2980
Determine which of the unsigned longlong arguments is bigger
2964
2985
b_val right argument
2967
This function will compare two unsigned int64_t arguments
2988
This function will compare two unsigned longlong arguments
2968
2989
and will return -1, 0, or 1 if left argument is smaller than,
2969
2990
equal to or greater than the right argument.
2973
2994
0 left argument is equal to the right argument.
2974
2995
1 left argument is greater than the right argument.
2976
static inline int cmp_ulongs (uint64_t a_val, uint64_t b_val)
2997
static inline int cmp_ulongs (ulonglong a_val, ulonglong b_val)
2978
2999
return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
2983
Compare two integers in IN value list format (packed_int64_t)
3004
Compare two integers in IN value list format (packed_longlong)
2987
3008
cmp_arg an argument passed to the calling function (my_qsort2)
2988
3009
a left argument
2989
3010
b right argument
2993
3014
format and will return -1, 0, or 1 if left argument is smaller than,
2994
3015
equal to or greater than the right argument.
2995
3016
It's used in sorting the IN values list and finding an element in it.
2996
Depending on the signedness of the arguments cmp_int64_t() will
3017
Depending on the signedness of the arguments cmp_longlong() will
2997
3018
compare them as either signed (using cmp_longs()) or unsigned (using
3002
3023
0 left argument is equal to the right argument.
3003
3024
1 left argument is greater than the right argument.
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)
3026
int cmp_longlong(void *cmp_arg,
3027
in_longlong::packed_longlong *a,
3028
in_longlong::packed_longlong *b)
3009
3030
if (a->unsigned_flag != b->unsigned_flag)
3012
3033
One of the args is unsigned and is too big to fit into the
3013
3034
positive signed range. Report no match.
3015
if ((a->unsigned_flag && ((uint64_t) a->val) > (uint64_t) INT64_MAX) ||
3016
(b->unsigned_flag && ((uint64_t) b->val) > (uint64_t) INT64_MAX))
3036
if ((a->unsigned_flag && ((ulonglong) a->val) > (ulonglong) LONGLONG_MAX) ||
3037
(b->unsigned_flag && ((ulonglong) b->val) > (ulonglong) LONGLONG_MAX))
3017
3038
return a->unsigned_flag ? 1 : -1;
3019
3040
Although the signedness differs both args can fit into the signed
3022
3043
return cmp_longs (a->val, b->val);
3024
3045
if (a->unsigned_flag)
3025
return cmp_ulongs ((uint64_t) a->val, (uint64_t) b->val);
3046
return cmp_ulongs ((ulonglong) a->val, (ulonglong) b->val);
3027
3048
return cmp_longs (a->val, b->val);
3030
static int cmp_double(void *cmp_arg __attribute__((unused)), double *a,double *b)
3051
static int cmp_double(void *cmp_arg, double *a,double *b)
3032
3053
return *a < *b ? -1 : *a == *b ? 0 : 1;
3035
static int cmp_row(void *cmp_arg __attribute__((unused)), cmp_item_row *a, cmp_item_row *b)
3056
static int cmp_row(void *cmp_arg, cmp_item_row *a, cmp_item_row *b)
3037
3058
return a->compare(b);
3041
static int cmp_decimal(void *cmp_arg __attribute__((unused)), my_decimal *a, my_decimal *b)
3062
static int cmp_decimal(void *cmp_arg, my_decimal *a, my_decimal *b)
3044
3065
We need call of fixing buffer pointer, because fast sort just copy
3053
3074
int in_vector::find(Item *item)
3055
unsigned char *result=get_value(item);
3076
uchar *result=get_value(item);
3056
3077
if (!result || !used_count)
3057
3078
return 0; // Null value
3060
3081
start=0; end=used_count-1;
3061
3082
while (start != end)
3063
uint32_t mid=(start+end+1)/2;
3084
uint mid=(start+end+1)/2;
3065
3086
if ((res=(*compare)(collation, base+mid*size, result)) == 0)
3072
3093
return (int) ((*compare)(collation, base+start*size, result) == 0);
3075
in_string::in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs)
3096
in_string::in_string(uint elements,qsort2_cmp cmp_func, CHARSET_INFO *cs)
3076
3097
:in_vector(elements, sizeof(String), cmp_func, cs),
3077
3098
tmp(buff, sizeof(buff), &my_charset_bin)
3084
3105
// base was allocated with help of sql_alloc => following is OK
3085
for (uint32_t i=0 ; i < count ; i++)
3106
for (uint i=0 ; i < count ; i++)
3086
3107
((String*) base)[i].free();
3090
void in_string::set(uint32_t pos,Item *item)
3111
void in_string::set(uint pos,Item *item)
3092
3113
String *str=((String*) base)+pos;
3093
3114
String *res=item->val_str(str);
3113
unsigned char *in_string::get_value(Item *item)
3134
uchar *in_string::get_value(Item *item)
3115
return (unsigned char*) item->val_str(&tmp);
3136
return (uchar*) item->val_str(&tmp);
3118
in_row::in_row(uint32_t elements, Item * item __attribute__((unused)))
3139
in_row::in_row(uint elements, Item * item)
3120
3141
base= (char*) new cmp_item_row[count= elements];
3121
3142
size= sizeof(cmp_item_row);
3134
3155
delete [] (cmp_item_row*) base;
3137
unsigned char *in_row::get_value(Item *item)
3158
uchar *in_row::get_value(Item *item)
3139
3160
tmp.store_value(item);
3140
3161
if (item->is_null())
3142
return (unsigned char *)&tmp;
3163
return (uchar *)&tmp;
3145
void in_row::set(uint32_t pos, Item *item)
3166
void in_row::set(uint pos, Item *item)
3168
DBUG_ENTER("in_row::set");
3169
DBUG_PRINT("enter", ("pos: %u item: 0x%lx", pos, (ulong) item));
3147
3170
((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3151
in_int64_t::in_int64_t(uint32_t elements)
3152
:in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3174
in_longlong::in_longlong(uint elements)
3175
:in_vector(elements,sizeof(packed_longlong),(qsort2_cmp) cmp_longlong, 0)
3155
void in_int64_t::set(uint32_t pos,Item *item)
3178
void in_longlong::set(uint pos,Item *item)
3157
struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3180
struct packed_longlong *buff= &((packed_longlong*) base)[pos];
3159
3182
buff->val= item->val_int();
3160
3183
buff->unsigned_flag= item->unsigned_flag;
3163
unsigned char *in_int64_t::get_value(Item *item)
3186
uchar *in_longlong::get_value(Item *item)
3165
3188
tmp.val= item->val_int();
3166
3189
if (item->null_value)
3168
3191
tmp.unsigned_flag= item->unsigned_flag;
3169
return (unsigned char*) &tmp;
3192
return (uchar*) &tmp;
3172
void in_datetime::set(uint32_t pos,Item *item)
3195
void in_datetime::set(uint pos,Item *item)
3174
3197
Item **tmp_item= &item;
3176
struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3199
struct packed_longlong *buff= &((packed_longlong*) base)[pos];
3178
3201
buff->val= get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
3179
3202
buff->unsigned_flag= 1L;
3182
unsigned char *in_datetime::get_value(Item *item)
3205
uchar *in_datetime::get_value(Item *item)
3185
3208
Item **tmp_item= lval_cache ? &lval_cache : &item;
3187
3210
if (item->null_value)
3189
3212
tmp.unsigned_flag= 1L;
3190
return (unsigned char*) &tmp;
3213
return (uchar*) &tmp;
3193
in_double::in_double(uint32_t elements)
3216
in_double::in_double(uint elements)
3194
3217
:in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
3197
void in_double::set(uint32_t pos,Item *item)
3220
void in_double::set(uint pos,Item *item)
3199
3222
((double*) base)[pos]= item->val_real();
3202
unsigned char *in_double::get_value(Item *item)
3225
uchar *in_double::get_value(Item *item)
3204
3227
tmp= item->val_real();
3205
3228
if (item->null_value)
3206
3229
return 0; /* purecov: inspected */
3207
return (unsigned char*) &tmp;
3230
return (uchar*) &tmp;
3211
in_decimal::in_decimal(uint32_t elements)
3234
in_decimal::in_decimal(uint elements)
3212
3235
:in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3216
void in_decimal::set(uint32_t pos, Item *item)
3239
void in_decimal::set(uint pos, Item *item)
3218
3241
/* as far as 'item' is constant, we can store reference on my_decimal */
3219
3242
my_decimal *dec= ((my_decimal *)base) + pos;
3229
unsigned char *in_decimal::get_value(Item *item)
3252
uchar *in_decimal::get_value(Item *item)
3231
3254
my_decimal *result= item->val_decimal(&val);
3232
3255
if (item->null_value)
3234
return (unsigned char *)result;
3257
return (uchar *)result;
3238
3261
cmp_item* cmp_item::get_comparator(Item_result type,
3239
const CHARSET_INFO * const cs)
3241
3264
switch (type) {
3242
3265
case STRING_RESULT:
3281
3304
cmp_item_row::~cmp_item_row()
3306
DBUG_ENTER("~cmp_item_row");
3307
DBUG_PRINT("enter",("this: 0x%lx", (long) this));
3283
3308
if (comparators)
3285
for (uint32_t i= 0; i < n; i++)
3310
for (uint i= 0; i < n; i++)
3287
3312
if (comparators[i])
3288
3313
delete comparators[i];
3373
3399
int cmp_item_row::compare(cmp_item *c)
3375
3401
cmp_item_row *l_cmp= (cmp_item_row *) c;
3376
for (uint32_t i=0; i < n; i++)
3402
for (uint i=0; i < n; i++)
3379
3405
if ((res= comparators[i]->compare(l_cmp->comparators[i])))
3508
static int srtcmp_in(const CHARSET_INFO * const cs, const String *x,const String *y)
3534
static int srtcmp_in(CHARSET_INFO *cs, const String *x,const String *y)
3510
3536
return cs->coll->strnncollsp(cs,
3511
(unsigned char *) x->ptr(),x->length(),
3512
(unsigned char *) y->ptr(),y->length(), 0);
3537
(uchar *) x->ptr(),x->length(),
3538
(uchar *) y->ptr(),y->length(), 0);
3518
3544
Item **arg, **arg_end;
3519
3545
bool const_itm= 1;
3520
3546
THD *thd= current_thd;
3521
bool datetime_found= false;
3522
/* true <=> arguments values will be compared as DATETIMEs. */
3523
bool compare_as_datetime= false;
3547
bool datetime_found= FALSE;
3548
/* TRUE <=> arguments values will be compared as DATETIMEs. */
3549
bool compare_as_datetime= FALSE;
3524
3550
Item *date_arg= 0;
3525
uint32_t found_types= 0;
3526
uint32_t type_cnt= 0, i;
3551
uint found_types= 0;
3552
uint type_cnt= 0, i;
3527
3553
Item_result cmp_type= STRING_RESULT;
3528
3554
left_result_type= args[0]->result_type();
3529
3555
if (!(found_types= collect_cmp_types(args, arg_count)))
3551
3577
if (cmp_type == STRING_RESULT &&
3552
3578
agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1))
3554
arg_types_compatible= true;
3580
arg_types_compatible= TRUE;
3556
3582
if (type_cnt == 1)
3579
3605
/* All DATE/DATETIME fields/functions has the STRING result type. */
3580
3606
if (cmp_type == STRING_RESULT || cmp_type == ROW_RESULT)
3582
uint32_t col, cols= args[0]->cols();
3608
uint col, cols= args[0]->cols();
3584
3610
for (col= 0; col < cols; col++)
3586
bool skip_column= false;
3612
bool skip_column= FALSE;
3588
3614
Check that all items to be compared has the STRING result type and at
3589
3615
least one of them is a DATE/DATETIME item.
3594
3620
arg[0]->element_index(col));
3595
3621
if (itm->result_type() != STRING_RESULT)
3600
3626
else if (itm->is_datetime())
3602
datetime_found= true;
3628
datetime_found= TRUE;
3604
3630
Internally all DATE/DATETIME values are converted to the DATETIME
3605
3631
type. So try to find a DATETIME item to issue correct warnings.
3609
else if (itm->field_type() == DRIZZLE_TYPE_DATETIME)
3635
else if (itm->field_type() == MYSQL_TYPE_DATETIME)
3612
3638
/* All arguments are already checked to have the STRING result. */
3629
3655
*cmp= new cmp_item_datetime(date_arg);
3630
3656
/* Reset variables for the next column. */
3632
datetime_found= false;
3658
datetime_found= FALSE;
3635
compare_as_datetime= true;
3661
compare_as_datetime= TRUE;
3641
Row item with NULLs inside can return NULL or false =>
3667
Row item with NULLs inside can return NULL or FALSE =>
3642
3668
they can't be processed as static
3644
3670
if (type_cnt == 1 && const_itm && !nulls_in_row())
3659
3685
cmp_type != INT_RESULT)
3661
3687
Item_field *field_item= (Item_field*) (args[0]->real_item());
3662
if (field_item->field->can_be_compared_as_int64_t())
3688
if (field_item->field->can_be_compared_as_longlong())
3664
bool all_converted= true;
3690
bool all_converted= TRUE;
3665
3691
for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3667
3693
if (!convert_constant_item (thd, field_item, &arg[0]))
3668
all_converted= false;
3694
all_converted= FALSE;
3670
3696
if (all_converted)
3671
3697
cmp_type= INT_RESULT;
3694
3720
array= new in_decimal(arg_count - 1);
3701
3727
if (array && !(thd->is_fatal_error)) // If not EOM
3704
for (uint32_t i=1 ; i < arg_count ; i++)
3730
for (uint i=1 ; i < arg_count ; i++)
3706
3732
array->set(j,args[i]);
3707
3733
if (!args[i]->null_value) // Skip NULL values
3776
3802
Value of the function
3779
int64_t Item_func_in::val_int()
3805
longlong Item_func_in::val_int()
3781
3807
cmp_item *in_item;
3783
uint32_t value_added_map= 0;
3808
DBUG_ASSERT(fixed == 1);
3809
uint value_added_map= 0;
3786
3812
int tmp=array->find(args[0]);
3787
3813
null_value=args[0]->null_value || (!tmp && have_null);
3788
return (int64_t) (!null_value && tmp != negated);
3814
return (longlong) (!null_value && tmp != negated);
3791
for (uint32_t i= 1 ; i < arg_count ; i++)
3817
for (uint i= 1 ; i < arg_count ; i++)
3793
3819
Item_result cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
3794
3820
in_item= cmp_items[(uint)cmp_type];
3821
DBUG_ASSERT(in_item);
3796
3822
if (!(value_added_map & (1 << (uint)cmp_type)))
3798
3824
in_item->store_value(args[0]);
3802
3828
value_added_map|= 1 << (uint)cmp_type;
3804
3830
if (!in_item->cmp(args[i]) && !args[i]->null_value)
3805
return (int64_t) (!negated);
3831
return (longlong) (!negated);
3806
3832
have_null|= args[i]->null_value;
3809
3835
null_value= have_null;
3810
return (int64_t) (!null_value && negated);
3836
return (longlong) (!null_value && negated);
3814
int64_t Item_func_bit_or::val_int()
3840
longlong Item_func_bit_or::val_int()
3817
uint64_t arg1= (uint64_t) args[0]->val_int();
3842
DBUG_ASSERT(fixed == 1);
3843
ulonglong arg1= (ulonglong) args[0]->val_int();
3818
3844
if (args[0]->null_value)
3820
3846
null_value=1; /* purecov: inspected */
3821
3847
return 0; /* purecov: inspected */
3823
uint64_t arg2= (uint64_t) args[1]->val_int();
3849
ulonglong arg2= (ulonglong) args[1]->val_int();
3824
3850
if (args[1]->null_value)
3830
return (int64_t) (arg1 | arg2);
3856
return (longlong) (arg1 | arg2);
3834
int64_t Item_func_bit_and::val_int()
3860
longlong Item_func_bit_and::val_int()
3837
uint64_t arg1= (uint64_t) args[0]->val_int();
3862
DBUG_ASSERT(fixed == 1);
3863
ulonglong arg1= (ulonglong) args[0]->val_int();
3838
3864
if (args[0]->null_value)
3840
3866
null_value=1; /* purecov: inspected */
3841
3867
return 0; /* purecov: inspected */
3843
uint64_t arg2= (uint64_t) args[1]->val_int();
3869
ulonglong arg2= (ulonglong) args[1]->val_int();
3844
3870
if (args[1]->null_value)
3846
3872
null_value=1; /* purecov: inspected */
3847
3873
return 0; /* purecov: inspected */
3850
return (int64_t) (arg1 & arg2);
3876
return (longlong) (arg1 & arg2);
3853
3879
Item_cond::Item_cond(THD *thd, Item_cond *item)
3873
Item_cond::fix_fields(THD *thd, Item **ref __attribute__((unused)))
3899
Item_cond::fix_fields(THD *thd, Item **ref)
3901
DBUG_ASSERT(fixed == 0);
3876
3902
List_iterator<Item> li(list);
3878
3904
void *orig_thd_marker= thd->thd_marker;
3879
unsigned char buff[sizeof(char*)]; // Max local vars in function
3905
uchar buff[sizeof(char*)]; // Max local vars in function
3880
3906
not_null_tables_cache= used_tables_cache= 0;
3881
3907
const_item_cache= 1;
3889
3915
and_tables_cache= ~(table_map) 0;
3891
3917
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
3892
return true; // Fatal error flag is set!
3918
return TRUE; // Fatal error flag is set!
3894
3920
The following optimization reduces the depth of an AND-OR tree.
3895
3921
E.g. a WHERE clause like
3923
3949
if ((!item->fixed &&
3924
3950
item->fix_fields(thd, li.ref())) ||
3925
3951
(item= *li.ref())->check_cols(1))
3926
return true; /* purecov: inspected */
3952
return TRUE; /* purecov: inspected */
3927
3953
used_tables_cache|= item->used_tables();
3928
3954
if (item->const_item())
3929
3955
and_tables_cache= (table_map) 0;
3932
3958
tmp_table_map= item->not_null_tables();
3933
3959
not_null_tables_cache|= tmp_table_map;
3934
3960
and_tables_cache&= tmp_table_map;
3935
const_item_cache= false;
3961
const_item_cache= FALSE;
3937
3963
with_sum_func= with_sum_func || item->with_sum_func;
3938
3964
with_subselect|= item->with_subselect;
3943
3969
thd->thd_marker= orig_thd_marker;
3944
3970
fix_length_and_dec();
3950
void Item_cond::fix_after_pullout(st_select_lex *new_parent, Item **ref __attribute__((unused)))
3976
void Item_cond::fix_after_pullout(st_select_lex *new_parent, Item **ref)
3952
3978
List_iterator<Item> li(list);
3973
3999
tmp_table_map= item->not_null_tables();
3974
4000
not_null_tables_cache|= tmp_table_map;
3975
4001
and_tables_cache&= tmp_table_map;
3976
const_item_cache= false;
4002
const_item_cache= FALSE;
3982
bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
4008
bool Item_cond::walk(Item_processor processor, bool walk_subquery, uchar *arg)
3984
4010
List_iterator_fast<Item> li(list);
4008
4034
Item returned as the result of transformation of the root node
4011
Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
4037
Item *Item_cond::transform(Item_transformer transformer, uchar *arg)
4013
4039
List_iterator<Item> li(list);
4036
4062
callback functions.
4038
4064
First the function applies the analyzer to the root node of
4039
the Item_func object. Then if the analyzer succeeeds (returns true)
4065
the Item_func object. Then if the analyzer succeeeds (returns TRUE)
4040
4066
the function recursively applies the compile method to member
4041
4067
item of the condition list.
4042
4068
If the call of the method for a member item returns a new item
4055
4081
Item returned as the result of transformation of the root node
4058
Item *Item_cond::compile(Item_analyzer analyzer, unsigned char **arg_p,
4059
Item_transformer transformer, unsigned char *arg_t)
4084
Item *Item_cond::compile(Item_analyzer analyzer, uchar **arg_p,
4085
Item_transformer transformer, uchar *arg_t)
4061
4087
if (!(this->*analyzer)(arg_p))
4069
4095
The same parameter value of arg_p must be passed
4070
4096
to analyze any argument of the condition formula.
4072
unsigned char *arg_v= *arg_p;
4098
uchar *arg_v= *arg_p;
4073
4099
Item *new_item= item->compile(analyzer, &arg_v, transformer, arg_t);
4074
4100
if (new_item && new_item != item)
4075
4101
li.replace(new_item);
4124
4150
List_iterator<Item> li(list);
4126
4152
while ((item= li++))
4127
item->split_sum_func2(thd, ref_pointer_array, fields, li.ref(), true);
4153
item->split_sum_func2(thd, ref_pointer_array, fields, li.ref(), TRUE);
4217
4243
if (!item->val_bool())
4219
4245
if (abort_on_null || !(null_value= item->null_value))
4220
return 0; // return false
4246
return 0; // return FALSE
4223
4249
return null_value ? 0 : 1;
4227
int64_t Item_cond_or::val_int()
4253
longlong Item_cond_or::val_int()
4255
DBUG_ASSERT(fixed == 1);
4230
4256
List_iterator_fast<Item> li(list);
4297
4323
return args[0]->is_null() ? 1: 0;
4300
int64_t Item_is_not_null_test::val_int()
4326
longlong Item_is_not_null_test::val_int()
4328
DBUG_ASSERT(fixed == 1);
4329
DBUG_ENTER("Item_is_not_null_test::val_int");
4303
4330
if (!used_tables_cache && !with_subselect)
4305
4332
owner->was_null|= (!cached_value);
4306
return(cached_value);
4333
DBUG_PRINT("info", ("cached: %ld", (long) cached_value));
4334
DBUG_RETURN(cached_value);
4308
4336
if (args[0]->is_null())
4338
DBUG_PRINT("info", ("null"));
4310
4339
owner->was_null|= 1;
4330
4359
if (!(used_tables_cache=args[0]->used_tables()) && !with_subselect)
4332
4361
/* Remember if the value is always NULL or never NULL */
4333
cached_value= (int64_t) !args[0]->is_null();
4362
cached_value= (longlong) !args[0]->is_null();
4339
int64_t Item_func_isnotnull::val_int()
4368
longlong Item_func_isnotnull::val_int()
4370
DBUG_ASSERT(fixed == 1);
4342
4371
return args[0]->is_null() ? 0 : 1;
4402
4431
bool Item_func_like::fix_fields(THD *thd, Item **ref)
4433
DBUG_ASSERT(fixed == 0);
4405
4434
if (Item_bool_func2::fix_fields(thd, ref) ||
4406
4435
escape_item->fix_fields(thd, &escape_item))
4409
4438
if (!escape_item->const_during_execution())
4411
4440
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4415
4444
if (escape_item->const_item())
4418
4447
String *escape_str= escape_item->val_str(&tmp_value1);
4419
4448
if (escape_str)
4421
if (escape_used_in_parsing && escape_str->numchars() > 1)
4450
if (escape_used_in_parsing && (
4451
(((thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
4452
escape_str->numchars() != 1) ||
4453
escape_str->numchars() > 1)))
4423
4455
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4427
4459
if (use_mb(cmp.cmp_collation.collation))
4429
const CHARSET_INFO * const cs= escape_str->charset();
4461
CHARSET_INFO *cs= escape_str->charset();
4431
4463
int rc= cs->cset->mb_wc(cs, &wc,
4432
(const unsigned char*) escape_str->ptr(),
4433
(const unsigned char*) escape_str->ptr() +
4464
(const uchar*) escape_str->ptr(),
4465
(const uchar*) escape_str->ptr() +
4434
4466
escape_str->length());
4435
4467
escape= (int) (rc > 0 ? wc : '\\');
4441
4473
code instead of Unicode code as "escape" argument.
4442
4474
Convert to "cs" if charset of escape differs.
4444
const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
4476
CHARSET_INFO *cs= cmp.cmp_collation.collation;
4446
4478
if (escape_str->needs_conversion(escape_str->length(),
4447
4479
escape_str->charset(), cs, &unused))
4451
uint32_t cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
4483
uint32 cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
4452
4484
escape_str->length(),
4453
4485
escape_str->charset(), &errors);
4454
4486
escape= cnvlen ? ch : '\\';
4464
4496
We could also do boyer-more for non-const items, but as we would have to
4465
4497
recompute the tables for each row it's not worth it.
4467
if (args[1]->const_item() && !use_strnxfrm(collation.collation))
4499
if (args[1]->const_item() && !use_strnxfrm(collation.collation) &&
4500
!(specialflag & SPECIAL_NO_NEW_FUNC))
4469
4502
String* res2 = args[1]->val_str(&tmp_value2);
4471
return false; // Null argument
4504
return FALSE; // Null argument
4473
4506
const size_t len = res2->length();
4474
4507
const char* first = res2->ptr();
4497
4531
bmBc = bmGs + pattern_len + 1;
4498
4532
turboBM_compute_good_suffix_shifts(suff);
4499
4533
turboBM_compute_bad_character_shifts();
4534
DBUG_PRINT("info",("done"));
4506
4541
void Item_func_like::cleanup()
4508
canDoTurboBM= false;
4543
canDoTurboBM= FALSE;
4509
4544
Item_bool_func2::cleanup();
4512
4547
#ifdef LIKE_CMP_TOUPPER
4513
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
4548
#define likeconv(cs,A) (uchar) (cs)->toupper(A)
4515
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
4550
#define likeconv(cs,A) (uchar) (cs)->sort_order[(uchar) (A)]
4626
4661
int *end = bmBc + alphabet_size;
4628
4663
const int plm1 = pattern_len - 1;
4629
const CHARSET_INFO *const cs= cmp.cmp_collation.collation;
4664
CHARSET_INFO *cs= cmp.cmp_collation.collation;
4631
4666
for (i = bmBc; i < end; i++)
4632
4667
*i = pattern_len;
4681
4716
register const int v = plm1 - i;
4682
4717
turboShift = u - v;
4683
bcShift = bmBc[(uint) (unsigned char) text[i + j]] - plm1 + i;
4684
shift = (turboShift > bcShift) ? turboShift : bcShift;
4685
shift = (shift > bmGs[i]) ? shift : bmGs[i];
4718
bcShift = bmBc[(uint) (uchar) text[i + j]] - plm1 + i;
4719
shift = max(turboShift, bcShift);
4720
shift = max(shift, bmGs[i]);
4686
4721
if (shift == bmGs[i])
4687
u = (pattern_len - shift < v) ? pattern_len - shift : v;
4722
u = min(pattern_len - shift, v);
4690
4725
if (turboShift < bcShift)
4691
shift = cmax(shift, u + 1);
4726
shift = max(shift, u + 1);
4712
4747
register const int v = plm1 - i;
4713
4748
turboShift = u - v;
4714
4749
bcShift = bmBc[(uint) likeconv(cs, text[i + j])] - plm1 + i;
4715
shift = (turboShift > bcShift) ? turboShift : bcShift;
4716
shift = cmax(shift, bmGs[i]);
4750
shift = max(turboShift, bcShift);
4751
shift = max(shift, bmGs[i]);
4717
4752
if (shift == bmGs[i])
4718
u = (pattern_len - shift < v) ? pattern_len - shift : v;
4753
u = min(pattern_len - shift, v);
4721
4756
if (turboShift < bcShift)
4722
shift = cmax(shift, u + 1);
4757
shift = max(shift, u + 1);
4790
4825
NULL if we cannot apply NOT transformation (see Item::neg_transformer()).
4793
Item *Item_func_not::neg_transformer(THD *thd __attribute__((unused))) /* NOT(x) -> x */
4828
Item *Item_func_not::neg_transformer(THD *thd) /* NOT(x) -> x */
4795
4830
return args[0];
4799
Item *Item_bool_rowready_func2::neg_transformer(THD *thd __attribute__((unused)))
4834
Item *Item_bool_rowready_func2::neg_transformer(THD *thd)
4801
4836
Item *item= negated_item();
4844
Item *Item_func_nop_all::neg_transformer(THD *thd __attribute__((unused)))
4879
Item *Item_func_nop_all::neg_transformer(THD *thd)
4846
4881
/* "NOT (e $cmp$ ANY (SELECT ...)) -> e $rev_cmp$" ALL (SELECT ...) */
4847
4882
Item_func_not_all *new_item= new Item_func_not_all(args[0]);
4848
4883
Item_allany_subselect *allany= (Item_allany_subselect*)args[0];
4849
allany->func= allany->func_creator(false);
4884
allany->func= allany->func_creator(FALSE);
4850
4885
allany->all= !allany->all;
4851
4886
allany->upper_item= new_item;
4852
4887
return new_item;
4855
Item *Item_func_not_all::neg_transformer(THD *thd __attribute__((unused)))
4890
Item *Item_func_not_all::neg_transformer(THD *thd)
4857
4892
/* "NOT (e $cmp$ ALL (SELECT ...)) -> e $rev_cmp$" ANY (SELECT ...) */
4858
4893
Item_func_nop_all *new_item= new Item_func_nop_all(args[0]);
4859
4894
Item_allany_subselect *allany= (Item_allany_subselect*)args[0];
4860
4895
allany->all= !allany->all;
4861
allany->func= allany->func_creator(true);
4896
allany->func= allany->func_creator(TRUE);
4862
4897
allany->upper_item= new_item;
4863
4898
return new_item;
5156
5191
item->collation.collation);
5159
bool Item_equal::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
5194
bool Item_equal::walk(Item_processor processor, bool walk_subquery, uchar *arg)
5161
5196
List_iterator_fast<Item_field> it(fields);
5168
5203
return Item_func::walk(processor, walk_subquery, arg);
5171
Item *Item_equal::transform(Item_transformer transformer, unsigned char *arg)
5206
Item *Item_equal::transform(Item_transformer transformer, uchar *arg)
5173
5208
List_iterator<Item_field> it(fields);