1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21
This file defines all compare functions
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 **);
34
static Item_result item_store_type(Item_result a, Item *item,
35
my_bool unsigned_flag)
37
Item_result b= item->result_type();
39
if (a == STRING_RESULT || b == STRING_RESULT)
41
else if (a == REAL_RESULT || b == REAL_RESULT)
43
else if (a == DECIMAL_RESULT || b == DECIMAL_RESULT ||
44
unsigned_flag != item->unsigned_flag)
45
return DECIMAL_RESULT;
50
static void agg_result_type(Item_result *type, Item **items, uint nitems)
52
Item **item, **item_end;
53
my_bool unsigned_flag= 0;
56
/* Skip beginning NULL items */
57
for (item= items, item_end= item + nitems; item < item_end; item++)
59
if ((*item)->type() != Item::NULL_ITEM)
61
*type= (*item)->result_type();
62
unsigned_flag= (*item)->unsigned_flag;
67
/* Combine result types. Note: NULL items don't affect the result */
68
for (; item < item_end; item++)
70
if ((*item)->type() != Item::NULL_ITEM)
71
*type= item_store_type(*type, *item, unsigned_flag);
77
Compare row signature of two expressions
81
item1 the first expression
82
item2 the second expression
85
The function checks that two expressions have compatible row signatures
86
i.e. that the number of columns they return are the same and that if they
87
are both row expressions then each component from the first expression has
88
a row signature compatible with the signature of the corresponding component
89
of the second expression.
92
1 type incompatibility has been detected
96
static int cmp_row_type(Item* item1, Item* item2)
98
uint n= item1->cols();
99
if (item2->check_cols(n))
101
for (uint i=0; i<n; i++)
103
if (item2->element_index(i)->check_cols(item1->element_index(i)->cols()) ||
104
(item1->element_index(i)->result_type() == ROW_RESULT &&
105
cmp_row_type(item1->element_index(i), item2->element_index(i))))
113
Aggregates result types from the array of items.
117
type [out] the aggregated type
118
items array of items to aggregate the type from
119
nitems number of items in the array
122
This function aggregates result types from the array of items. Found type
123
supposed to be used later for comparison of values of these items.
124
Aggregation itself is performed by the item_cmp_type() function.
125
@param[out] type the aggregated type
126
@param items array of items to aggregate the type from
127
@param nitems number of items in the array
130
1 type incompatibility has been detected
135
static int agg_cmp_type(Item_result *type, Item **items, uint nitems)
138
type[0]= items[0]->result_type();
139
for (i= 1 ; i < nitems ; i++)
141
type[0]= item_cmp_type(type[0], items[i]->result_type());
143
When aggregating types of two row expressions we have to check
144
that they have the same cardinality and that each component
145
of the first row expression has a compatible row signature with
146
the signature of the corresponding component of the second row
149
if (type[0] == ROW_RESULT && cmp_row_type(items[0], items[i]))
150
return 1; // error found: invalid usage of rows
157
@brief Aggregates field types from the array of items.
159
@param[in] items array of items to aggregate the type from
160
@paran[in] nitems number of items in the array
162
@details This function aggregates field types from the array of items.
163
Found type is supposed to be used later as the result field type
164
of a multi-argument function.
165
Aggregation itself is performed by the Field::field_type_merge()
168
@note The term "aggregation" is used here in the sense of inferring the
169
result type of a function from its argument types.
171
@return aggregated field type.
174
enum_field_types agg_field_type(Item **items, uint nitems)
177
if (!nitems || items[0]->result_type() == ROW_RESULT )
178
return (enum_field_types)-1;
179
enum_field_types res= items[0]->field_type();
180
for (i= 1 ; i < nitems ; i++)
181
res= Field::field_type_merge(res, items[i]->field_type());
186
Collects different types for comparison of first item with each other items
190
items Array of items to collect types from
191
nitems Number of items in the array
194
This function collects different result types for comparison of the first
195
item in the list with each of the remaining items in the 'items' array.
198
0 - if row type incompatibility has been detected (see cmp_row_type)
199
Bitmap of collected types - otherwise
202
static uint collect_cmp_types(Item **items, uint nitems)
206
Item_result left_result= items[0]->result_type();
207
DBUG_ASSERT(nitems > 1);
209
for (i= 1; i < nitems ; i++)
211
if ((left_result == ROW_RESULT ||
212
items[i]->result_type() == ROW_RESULT) &&
213
cmp_row_type(items[0], items[i]))
215
found_types|= 1<< (uint)item_cmp_type(left_result,
216
items[i]->result_type());
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(),
231
Item_bool_func2* Eq_creator::create(Item *a, Item *b) const
233
return new Item_func_eq(a, b);
237
Item_bool_func2* Ne_creator::create(Item *a, Item *b) const
239
return new Item_func_ne(a, b);
243
Item_bool_func2* Gt_creator::create(Item *a, Item *b) const
245
return new Item_func_gt(a, b);
249
Item_bool_func2* Lt_creator::create(Item *a, Item *b) const
251
return new Item_func_lt(a, b);
255
Item_bool_func2* Ge_creator::create(Item *a, Item *b) const
257
return new Item_func_ge(a, b);
261
Item_bool_func2* Le_creator::create(Item *a, Item *b) const
263
return new Item_func_le(a, b);
268
Most of these returns 0LL if false and 1LL if true and
269
NULL if some arg is NULL.
272
longlong Item_func_not::val_int()
274
DBUG_ASSERT(fixed == 1);
275
bool value= args[0]->val_bool();
276
null_value=args[0]->null_value;
277
return ((!null_value && value == 0) ? 1 : 0);
281
We put any NOT expression into parenthesis to avoid
282
possible problems with internal view representations where
283
any '!' is converted to NOT. It may cause a problem if
284
'!' is used in an expression together with other operators
285
whose precedence is lower than the precedence of '!' yet
286
higher than the precedence of NOT.
289
void Item_func_not::print(String *str, enum_query_type query_type)
292
Item_func::print(str, query_type);
297
special NOT for ALL subquery.
301
longlong Item_func_not_all::val_int()
303
DBUG_ASSERT(fixed == 1);
304
bool value= args[0]->val_bool();
307
return TRUE if there was records in underlying select in max/min
308
optimization (ALL subquery)
310
if (empty_underlying_subquery())
313
null_value= args[0]->null_value;
314
return ((!null_value && value == 0) ? 1 : 0);
318
bool Item_func_not_all::empty_underlying_subquery()
320
return ((test_sum_item && !test_sum_item->any_value()) ||
321
(test_sub_item && !test_sub_item->any_value()));
324
void Item_func_not_all::print(String *str, enum_query_type query_type)
327
Item_func::print(str, query_type);
329
args[0]->print(str, query_type);
334
Special NOP (No OPeration) for ALL subquery. It is like
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).
342
longlong Item_func_nop_all::val_int()
344
DBUG_ASSERT(fixed == 1);
345
longlong value= args[0]->val_int();
348
return FALSE if there was records in underlying select in max/min
349
optimization (SAME/ANY subquery)
351
if (empty_underlying_subquery())
354
null_value= args[0]->null_value;
355
return (null_value || value == 0) ? 0 : 1;
360
Convert a constant item to an int and replace the original item.
362
The function converts a constant expression or string to an integer.
363
On successful conversion the original item is substituted for the
364
result of the item evaluation.
365
This is done when comparing DATE/TIME of different formats and
366
also when comparing bigint to strings (in which case strings
367
are converted to bigints).
369
@param thd thread handle
370
@param field_item item will be converted using the type of this field
371
@param[in,out] item reference to the item to convert
374
This function is called only at prepare stage.
375
As all derived tables are filled only after all derived tables
376
are prepared we do not evaluate items with subselects here because
377
they can contain derived tables and thus we may attempt to use a
378
table that has not been populated yet.
383
1 Item was replaced with an integer version of the item
386
static bool convert_constant_item(THD *thd, Item_field *field_item,
389
Field *field= field_item->field;
392
if (!(*item)->with_subselect && (*item)->const_item())
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);
406
/* For comparison purposes allow invalid dates like 2000-01-32 */
407
thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
409
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
412
Store the value of the field if it references an outer field because
413
the call to save_in_field below overrides that value.
415
if (field_item->depended_from)
416
orig_field_val= field->val_int();
417
if (!(*item)->is_null() && !(*item)->save_in_field(field, 1))
419
Item *tmp= new Item_int_with_ref(field->val_int(), *item,
420
test(field->flags & UNSIGNED_FLAG));
422
thd->change_item_tree(item, tmp);
423
result= 1; // Item was replaced
425
/* Restore the original field value. */
426
if (field_item->depended_from)
428
result= field->store(orig_field_val, TRUE);
429
/* orig_field_val must be a valid value that can be restored back. */
430
DBUG_ASSERT(!result);
432
thd->variables.sql_mode= orig_sql_mode;
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);
444
void Item_bool_func2::fix_length_and_dec()
446
max_length= 1; // Function returns 0 or 1
450
As some compare functions are generated after sql_yacc,
451
we have to check for out of memory conditions here
453
if (!args[0] || !args[1])
457
We allow to convert to Unicode character sets in some cases.
458
The conditions when conversion is possible are:
459
- arguments A and B have different charsets
460
- A wins according to coercibility rules
461
- character set of A is superset for character set of B
463
If all of the above is true, then it's possible to convert
464
B into the character set of A, and then compare according
465
to the collation of A.
470
if (args[0]->result_type() == STRING_RESULT &&
471
args[1]->result_type() == STRING_RESULT &&
472
agg_arg_charsets(coll, args, 2, MY_COLL_CMP_CONV, 1))
475
args[0]->cmp_context= args[1]->cmp_context=
476
item_cmp_type(args[0]->result_type(), args[1]->result_type());
477
// Make a special case of compare with fields to get nicer DATE comparisons
479
if (functype() == LIKE_FUNC) // Disable conversion in case of LIKE function.
486
if (!thd->is_context_analysis_only())
488
if (args[0]->real_item()->type() == FIELD_ITEM)
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))
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;
504
if (args[1]->real_item()->type() == FIELD_ITEM)
506
Item_field *field_item= (Item_field*) (args[1]->real_item());
507
if (field_item->field->can_be_compared_as_longlong() &&
508
!(field_item->is_datetime() &&
509
args[0]->result_type() == STRING_RESULT))
511
if (convert_constant_item(thd, field_item, &args[0]))
513
cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
514
INT_RESULT); // Works for all types.
515
args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
525
int Arg_comparator::set_compare_func(Item_bool_func2 *item, Item_result type)
528
func= comparator_matrix[type]
529
[test(owner->functype() == Item_func::EQUAL_FUNC)];
533
uint n= (*a)->cols();
534
if (n != (*b)->cols())
536
my_error(ER_OPERAND_COLUMNS, MYF(0), n);
540
if (!(comparators= new Arg_comparator[n]))
542
for (uint i=0; i < n; i++)
544
if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
546
my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
549
comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
556
We must set cmp_charset here as we may be called from for an automatic
557
generated item, like in natural join
559
if (cmp_collation.set((*a)->collation, (*b)->collation) ||
560
cmp_collation.derivation == DERIVATION_NONE)
562
my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
565
if (cmp_collation.collation == &my_charset_bin)
568
We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
569
without removing end space
571
if (func == &Arg_comparator::compare_string)
572
func= &Arg_comparator::compare_binary_string;
573
else if (func == &Arg_comparator::compare_e_string)
574
func= &Arg_comparator::compare_e_binary_string;
577
As this is binary compassion, mark all fields that they can't be
578
transformed. Otherwise we would get into trouble with comparisons
580
WHERE col= 'j' AND col LIKE BINARY 'j'
581
which would be transformed to:
584
(*a)->walk(&Item::set_no_const_sub, FALSE, (uchar*) 0);
585
(*b)->walk(&Item::set_no_const_sub, FALSE, (uchar*) 0);
591
if (func == &Arg_comparator::compare_int_signed)
593
if ((*a)->unsigned_flag)
594
func= (((*b)->unsigned_flag)?
595
&Arg_comparator::compare_int_unsigned :
596
&Arg_comparator::compare_int_unsigned_signed);
597
else if ((*b)->unsigned_flag)
598
func= &Arg_comparator::compare_int_signed_unsigned;
600
else if (func== &Arg_comparator::compare_e_int)
602
if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
603
func= &Arg_comparator::compare_e_int_diff_signedness;
611
if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
613
precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
614
if (func == &Arg_comparator::compare_real)
615
func= &Arg_comparator::compare_real_fixed;
616
else if (func == &Arg_comparator::compare_e_real)
617
func= &Arg_comparator::compare_e_real_fixed;
629
@brief Convert date provided in a string to the int representation.
631
@param[in] thd thread handle
632
@param[in] str a string to convert
633
@param[in] warn_type type of the timestamp for issuing the warning
634
@param[in] warn_name field name for issuing the warning
635
@param[out] error_arg could not extract a DATE or DATETIME
637
@details Convert date provided in the string str to the int
638
representation. If the string contains wrong date or doesn't
639
contain it at all then a warning is issued. The warn_type and
640
the warn_name arguments are used as the name and the type of the
641
field when issuing the warning. If any input was discarded
642
(trailing or non-timestampy characters), was_cut will be non-zero.
643
was_type will return the type str_to_datetime() could correctly
647
converted value. 0 on error and on zero-dates -- check 'failure'
651
get_date_from_str(THD *thd, String *str, timestamp_type warn_type,
652
char *warn_name, bool *error_arg)
657
enum_mysql_timestamp_type ret;
659
ret= str_to_datetime(str->ptr(), str->length(), &l_time,
660
(TIME_FUZZY_DATE | MODE_INVALID_DATES |
661
(thd->variables.sql_mode &
662
(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE))),
665
if (ret == MYSQL_TIMESTAMP_DATETIME || ret == MYSQL_TIMESTAMP_DATE)
668
Do not return yet, we may still want to throw a "trailing garbage"
672
value= TIME_to_ulonglong_datetime(&l_time);
677
error= 1; /* force warning */
681
make_truncated_value_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
682
str->ptr(), str->length(),
683
warn_type, warn_name);
690
Check whether compare_datetime() can be used to compare items.
693
Arg_comparator::can_compare_as_dates()
694
a, b [in] items to be compared
695
const_value [out] converted value of the string constant, if any
698
Check several cases when the DATE/DATETIME comparator should be used.
699
The following cases are checked:
700
1. Both a and b is a DATE/DATETIME field/function returning string or
702
2. Only a or b is a DATE/DATETIME field/function returning string or
703
int result and the other item (b or a) is an item with string result.
704
If the second item is a constant one then it's checked to be
705
convertible to the DATE/DATETIME type. If the constant can't be
706
converted to a DATE/DATETIME then the compare_datetime() comparator
707
isn't used and the warning about wrong DATE/DATETIME value is issued.
708
In all other cases (date-[int|real|decimal]/[int|real|decimal]-date)
709
the comparison is handled by other comparators.
710
If the datetime comparator can be used and one the operands of the
711
comparison is a string constant that was successfully converted to a
712
DATE/DATETIME type then the result of the conversion is returned in the
713
const_value if it is provided. If there is no constant or
714
compare_datetime() isn't applicable then the *const_value remains
718
the found type of date comparison
721
enum Arg_comparator::enum_date_cmp_type
722
Arg_comparator::can_compare_as_dates(Item *a, Item *b, ulonglong *const_value)
724
enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
725
Item *str_arg= 0, *date_arg= 0;
727
if (a->type() == Item::ROW_ITEM || b->type() == Item::ROW_ITEM)
728
return CMP_DATE_DFLT;
730
if (a->is_datetime())
732
if (b->is_datetime())
733
cmp_type= CMP_DATE_WITH_DATE;
734
else if (b->result_type() == STRING_RESULT)
736
cmp_type= CMP_DATE_WITH_STR;
741
else if (b->is_datetime() && a->result_type() == STRING_RESULT)
743
cmp_type= CMP_STR_WITH_DATE;
748
if (cmp_type != CMP_DATE_DFLT)
751
Do not cache GET_USER_VAR() function as its const_item() may return TRUE
752
for the current thread but it still may change during the execution.
754
if (cmp_type != CMP_DATE_WITH_DATE && str_arg->const_item() &&
755
(str_arg->type() != Item::FUNC_ITEM ||
756
((Item_func*)str_arg)->functype() != Item_func::GUSERVAR_FUNC))
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);
765
str_val= str_arg->val_str(&tmp);
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;
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, Item ***item_arg, Item **cache_arg,
806
Item *warn_item, bool *is_null)
809
Item *item= **item_arg;
812
if (item->result_as_longlong())
814
value= item->val_int();
815
*is_null= item->null_value;
819
*is_null= item->get_time(<ime);
820
value= !*is_null ? TIME_to_ulonglong_datetime(<ime) : 0;
823
Do not cache GET_USER_VAR() function as its const_item() may return TRUE
824
for the current thread but it still may change during the execution.
826
if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
827
((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
829
Item_cache_int *cache= new Item_cache_int();
830
/* Mark the cache as non-const to prevent re-caching. */
831
cache->set_used_tables(1);
832
cache->store(item, value);
834
*item_arg= cache_arg;
840
int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg,
841
Item **a1, Item **a2,
844
enum enum_date_cmp_type cmp_type;
845
ulonglong const_value= (ulonglong)-1;
849
if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
853
a_type= (*a)->field_type();
854
b_type= (*b)->field_type();
858
if (const_value != (ulonglong)-1)
860
Item_cache_int *cache= new Item_cache_int();
861
/* Mark the cache as non-const to prevent re-caching. */
862
cache->set_used_tables(1);
863
if (!(*a)->is_datetime())
865
cache->store((*a), const_value);
867
a= (Item **)&a_cache;
871
cache->store((*b), const_value);
873
b= (Item **)&b_cache;
876
is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC);
877
func= &Arg_comparator::compare_datetime;
878
get_value_func= &get_datetime_value;
881
else if (type == STRING_RESULT && (*a)->field_type() == MYSQL_TYPE_TIME &&
882
(*b)->field_type() == MYSQL_TYPE_TIME)
884
/* Compare TIME values as integers. */
889
is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC);
890
func= &Arg_comparator::compare_datetime;
891
get_value_func= &get_time_value;
895
return set_compare_func(owner_arg, type);
899
void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
902
/* A caller will handle null values by itself. */
906
a_type= (*a)->field_type();
907
b_type= (*b)->field_type();
911
func= &Arg_comparator::compare_datetime;
912
get_value_func= &get_datetime_value;
917
Retrieves correct DATETIME value from given item.
922
item_arg [in/out] item to retrieve DATETIME value from
923
cache_arg [in/out] pointer to place to store the caching item to
924
warn_item [in] item for issuing the conversion warning
925
is_null [out] TRUE <=> the item_arg is null
928
Retrieves the correct DATETIME value from given item for comparison by the
929
compare_datetime() function.
930
If item's result can be compared as longlong then its int value is used
931
and its string value is used otherwise. Strings are always parsed and
932
converted to int values by the get_date_from_str() function.
933
This allows us to compare correctly string dates with missed insignificant
934
zeros. If an item is a constant one then its value is cached and it isn't
935
get parsed again. An Item_cache_int object is used for caching values. It
936
seamlessly substitutes the original item. The cache item is marked as
937
non-constant to prevent re-caching it again. In order to compare
938
correctly DATE and DATETIME items the result of the former are treated as
939
a DATETIME with zero time (00:00:00).
946
get_datetime_value(THD *thd, Item ***item_arg, Item **cache_arg,
947
Item *warn_item, bool *is_null)
951
Item *item= **item_arg;
953
if (item->result_as_longlong())
955
value= item->val_int();
956
*is_null= item->null_value;
957
enum_field_types f_type= item->field_type();
959
Item_date_add_interval may return MYSQL_TYPE_STRING as the result
960
field type. To detect that the DATE value has been returned we
961
compare it with 100000000L - any DATE value should be less than it.
962
Don't shift cached DATETIME values up for the second time.
964
if (f_type == MYSQL_TYPE_DATE ||
965
(f_type != MYSQL_TYPE_DATETIME && value < 100000000L))
970
str= item->val_str(&buf);
971
*is_null= item->null_value;
974
return ~(ulonglong) 0;
976
Convert strings to the integer DATE/DATETIME representation.
977
Even if both dates provided in strings we can't compare them directly as
978
strings as there is no warranty that they are correct and do not miss
979
some insignificant zeros.
984
enum_field_types f_type= warn_item->field_type();
985
timestamp_type t_type= f_type ==
986
MYSQL_TYPE_DATE ? MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME;
987
value= get_date_from_str(thd, str, t_type, warn_item->name, &error);
989
If str did not contain a valid date according to the current
990
SQL_MODE, get_date_from_str() has already thrown a warning,
991
and we don't want to throw NULL on invalid date (see 5.2.6
992
"SQL modes" in the manual), so we're done here.
996
Do not cache GET_USER_VAR() function as its const_item() may return TRUE
997
for the current thread but it still may change during the execution.
999
if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
1000
((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
1002
Item_cache_int *cache= new Item_cache_int(MYSQL_TYPE_DATETIME);
1003
/* Mark the cache as non-const to prevent re-caching. */
1004
cache->set_used_tables(1);
1005
cache->store(item, value);
1007
*item_arg= cache_arg;
1013
Compare items values as dates.
1016
Arg_comparator::compare_datetime()
1019
Compare items values as DATE/DATETIME for both EQUAL_FUNC and from other
1020
comparison functions. The correct DATETIME values are obtained
1021
with help of the get_datetime_value() function.
1024
If is_nulls_eq is TRUE:
1025
1 if items are equal or both are null
1027
If is_nulls_eq is FALSE:
1028
-1 a < b or one of items is null
1033
int Arg_comparator::compare_datetime()
1035
bool is_null= FALSE;
1036
ulonglong a_value, b_value;
1038
/* Get DATE/DATETIME/TIME value of the 'a' item. */
1039
a_value= (*get_value_func)(thd, &a, &a_cache, *b, &is_null);
1040
if (!is_nulls_eq && is_null)
1043
owner->null_value= 1;
1047
/* Get DATE/DATETIME/TIME value of the 'b' item. */
1048
b_value= (*get_value_func)(thd, &b, &b_cache, *a, &is_null);
1052
owner->null_value= is_nulls_eq ? 0 : 1;
1053
return is_nulls_eq ? 1 : -1;
1057
owner->null_value= 0;
1059
/* Compare values. */
1061
return (a_value == b_value);
1062
return a_value < b_value ? -1 : (a_value > b_value ? 1 : 0);
1066
int Arg_comparator::compare_string()
1069
if ((res1= (*a)->val_str(&owner->tmp_value1)))
1071
if ((res2= (*b)->val_str(&owner->tmp_value2)))
1073
owner->null_value= 0;
1074
return sortcmp(res1,res2,cmp_collation.collation);
1077
owner->null_value= 1;
1083
Compare strings byte by byte. End spaces are also compared.
1093
int Arg_comparator::compare_binary_string()
1096
if ((res1= (*a)->val_str(&owner->tmp_value1)))
1098
if ((res2= (*b)->val_str(&owner->tmp_value2)))
1100
owner->null_value= 0;
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));
1104
return cmp ? cmp : (int) (res1_length - res2_length);
1107
owner->null_value= 1;
1113
Compare strings, but take into account that NULL == NULL.
1117
int Arg_comparator::compare_e_string()
1120
res1= (*a)->val_str(&owner->tmp_value1);
1121
res2= (*b)->val_str(&owner->tmp_value2);
1123
return test(res1 == res2);
1124
return test(sortcmp(res1, res2, cmp_collation.collation) == 0);
1128
int Arg_comparator::compare_e_binary_string()
1131
res1= (*a)->val_str(&owner->tmp_value1);
1132
res2= (*b)->val_str(&owner->tmp_value2);
1134
return test(res1 == res2);
1135
return test(stringcmp(res1, res2) == 0);
1139
int Arg_comparator::compare_real()
1142
Fix yet another manifestation of Bug#2338. 'Volatile' will instruct
1143
gcc to flush double values out of 80-bit Intel FPU registers before
1144
performing the comparison.
1146
volatile double val1, val2;
1147
val1= (*a)->val_real();
1148
if (!(*a)->null_value)
1150
val2= (*b)->val_real();
1151
if (!(*b)->null_value)
1153
owner->null_value= 0;
1154
if (val1 < val2) return -1;
1155
if (val1 == val2) return 0;
1159
owner->null_value= 1;
1163
int Arg_comparator::compare_decimal()
1166
my_decimal *val1= (*a)->val_decimal(&value1);
1167
if (!(*a)->null_value)
1170
my_decimal *val2= (*b)->val_decimal(&value2);
1171
if (!(*b)->null_value)
1173
owner->null_value= 0;
1174
return my_decimal_cmp(val1, val2);
1177
owner->null_value= 1;
1181
int Arg_comparator::compare_e_real()
1183
double val1= (*a)->val_real();
1184
double val2= (*b)->val_real();
1185
if ((*a)->null_value || (*b)->null_value)
1186
return test((*a)->null_value && (*b)->null_value);
1187
return test(val1 == val2);
1190
int Arg_comparator::compare_e_decimal()
1192
my_decimal value1, value2;
1193
my_decimal *val1= (*a)->val_decimal(&value1);
1194
my_decimal *val2= (*b)->val_decimal(&value2);
1195
if ((*a)->null_value || (*b)->null_value)
1196
return test((*a)->null_value && (*b)->null_value);
1197
return test(my_decimal_cmp(val1, val2) == 0);
1201
int Arg_comparator::compare_real_fixed()
1204
Fix yet another manifestation of Bug#2338. 'Volatile' will instruct
1205
gcc to flush double values out of 80-bit Intel FPU registers before
1206
performing the comparison.
1208
volatile double val1, val2;
1209
val1= (*a)->val_real();
1210
if (!(*a)->null_value)
1212
val2= (*b)->val_real();
1213
if (!(*b)->null_value)
1215
owner->null_value= 0;
1216
if (val1 == val2 || fabs(val1 - val2) < precision)
1223
owner->null_value= 1;
1228
int Arg_comparator::compare_e_real_fixed()
1230
double val1= (*a)->val_real();
1231
double val2= (*b)->val_real();
1232
if ((*a)->null_value || (*b)->null_value)
1233
return test((*a)->null_value && (*b)->null_value);
1234
return test(val1 == val2 || fabs(val1 - val2) < precision);
1238
int Arg_comparator::compare_int_signed()
1240
longlong val1= (*a)->val_int();
1241
if (!(*a)->null_value)
1243
longlong val2= (*b)->val_int();
1244
if (!(*b)->null_value)
1246
owner->null_value= 0;
1247
if (val1 < val2) return -1;
1248
if (val1 == val2) return 0;
1252
owner->null_value= 1;
1258
Compare values as BIGINT UNSIGNED.
1261
int Arg_comparator::compare_int_unsigned()
1263
ulonglong val1= (*a)->val_int();
1264
if (!(*a)->null_value)
1266
ulonglong val2= (*b)->val_int();
1267
if (!(*b)->null_value)
1269
owner->null_value= 0;
1270
if (val1 < val2) return -1;
1271
if (val1 == val2) return 0;
1275
owner->null_value= 1;
1281
Compare signed (*a) with unsigned (*B)
1284
int Arg_comparator::compare_int_signed_unsigned()
1286
longlong sval1= (*a)->val_int();
1287
if (!(*a)->null_value)
1289
ulonglong uval2= (ulonglong)(*b)->val_int();
1290
if (!(*b)->null_value)
1292
owner->null_value= 0;
1293
if (sval1 < 0 || (ulonglong)sval1 < uval2)
1295
if ((ulonglong)sval1 == uval2)
1300
owner->null_value= 1;
1306
Compare unsigned (*a) with signed (*B)
1309
int Arg_comparator::compare_int_unsigned_signed()
1311
ulonglong uval1= (ulonglong)(*a)->val_int();
1312
if (!(*a)->null_value)
1314
longlong sval2= (*b)->val_int();
1315
if (!(*b)->null_value)
1317
owner->null_value= 0;
1320
if (uval1 < (ulonglong)sval2)
1322
if (uval1 == (ulonglong)sval2)
1327
owner->null_value= 1;
1332
int Arg_comparator::compare_e_int()
1334
longlong val1= (*a)->val_int();
1335
longlong val2= (*b)->val_int();
1336
if ((*a)->null_value || (*b)->null_value)
1337
return test((*a)->null_value && (*b)->null_value);
1338
return test(val1 == val2);
1342
Compare unsigned *a with signed *b or signed *a with unsigned *b.
1344
int Arg_comparator::compare_e_int_diff_signedness()
1346
longlong val1= (*a)->val_int();
1347
longlong val2= (*b)->val_int();
1348
if ((*a)->null_value || (*b)->null_value)
1349
return test((*a)->null_value && (*b)->null_value);
1350
return (val1 >= 0) && test(val1 == val2);
1353
int Arg_comparator::compare_row()
1357
(*a)->bring_value();
1358
(*b)->bring_value();
1359
uint n= (*a)->cols();
1360
for (uint i= 0; i<n; i++)
1362
res= comparators[i].compare();
1363
if (owner->null_value)
1365
// NULL was compared
1366
switch (owner->functype()) {
1367
case Item_func::NE_FUNC:
1368
break; // NE never aborts on NULL even if abort_on_null is set
1369
case Item_func::LT_FUNC:
1370
case Item_func::LE_FUNC:
1371
case Item_func::GT_FUNC:
1372
case Item_func::GE_FUNC:
1373
return -1; // <, <=, > and >= always fail on NULL
1375
if (owner->abort_on_null)
1376
return -1; // We do not need correct NULL returning
1379
owner->null_value= 0;
1380
res= 0; // continue comparison (maybe we will meet explicit difference)
1388
There was NULL(s) in comparison in some parts, but there was no
1389
explicit difference in other parts, so we have to return NULL.
1391
owner->null_value= 1;
1398
int Arg_comparator::compare_e_row()
1400
(*a)->bring_value();
1401
(*b)->bring_value();
1402
uint n= (*a)->cols();
1403
for (uint i= 0; i<n; i++)
1405
if (!comparators[i].compare())
1412
void Item_func_truth::fix_length_and_dec()
1421
void Item_func_truth::print(String *str, enum_query_type query_type)
1424
args[0]->print(str, query_type);
1425
str->append(STRING_WITH_LEN(" is "));
1427
str->append(STRING_WITH_LEN("not "));
1429
str->append(STRING_WITH_LEN("true"));
1431
str->append(STRING_WITH_LEN("false"));
1436
bool Item_func_truth::val_bool()
1438
bool val= args[0]->val_bool();
1439
if (args[0]->null_value)
1442
NULL val IS {TRUE, FALSE} --> FALSE
1443
NULL val IS NOT {TRUE, FALSE} --> TRUE
1445
return (! affirmative);
1450
/* {TRUE, FALSE} val IS {TRUE, FALSE} value */
1451
return (val == value);
1454
/* {TRUE, FALSE} val IS NOT {TRUE, FALSE} value */
1455
return (val != value);
1459
longlong Item_func_truth::val_int()
1461
return (val_bool() ? 1 : 0);
1465
bool Item_in_optimizer::fix_left(THD *thd, Item **ref)
1467
if ((!args[0]->fixed && args[0]->fix_fields(thd, args)) ||
1468
(!cache && !(cache= Item_cache::get_cache(args[0]))))
1471
cache->setup(args[0]);
1472
if (cache->cols() == 1)
1474
if ((used_tables_cache= args[0]->used_tables()))
1475
cache->set_used_tables(OUTER_REF_TABLE_BIT);
1477
cache->set_used_tables(0);
1481
uint n= cache->cols();
1482
for (uint i= 0; i < n; i++)
1484
if (args[0]->element_index(i)->used_tables())
1485
((Item_cache *)cache->element_index(i))->set_used_tables(OUTER_REF_TABLE_BIT);
1487
((Item_cache *)cache->element_index(i))->set_used_tables(0);
1489
used_tables_cache= args[0]->used_tables();
1491
not_null_tables_cache= args[0]->not_null_tables();
1492
with_sum_func= args[0]->with_sum_func;
1493
if ((const_item_cache= args[0]->const_item()))
1494
cache->store(args[0]);
1499
bool Item_in_optimizer::fix_fields(THD *thd, Item **ref)
1501
DBUG_ASSERT(fixed == 0);
1502
if (fix_left(thd, ref))
1504
if (args[0]->maybe_null)
1507
if (!args[1]->fixed && args[1]->fix_fields(thd, args+1))
1509
Item_in_subselect * sub= (Item_in_subselect *)args[1];
1510
if (args[0]->cols() != sub->engine->cols())
1512
my_error(ER_OPERAND_COLUMNS, MYF(0), args[0]->cols());
1515
if (args[1]->maybe_null)
1517
with_sum_func= with_sum_func || args[1]->with_sum_func;
1518
used_tables_cache|= args[1]->used_tables();
1519
not_null_tables_cache|= args[1]->not_null_tables();
1520
const_item_cache&= args[1]->const_item();
1526
longlong Item_in_optimizer::val_int()
1529
DBUG_ASSERT(fixed == 1);
1530
cache->store(args[0]);
1532
if (cache->null_value)
1534
if (((Item_in_subselect*)args[1])->is_top_level_item())
1537
We're evaluating "NULL IN (SELECT ...)". The result can be NULL or
1538
FALSE, and we can return one instead of another. Just return NULL.
1544
if (!((Item_in_subselect*)args[1])->is_correlated &&
1545
result_for_null_param != UNKNOWN)
1547
/* Use cached value from previous execution */
1548
null_value= result_for_null_param;
1553
We're evaluating "NULL IN (SELECT ...)". The result is:
1554
FALSE if SELECT produces an empty set, or
1556
We disable the predicates we've pushed down into subselect, run the
1557
subselect and see if it has produced any rows.
1559
Item_in_subselect *item_subs=(Item_in_subselect*)args[1];
1560
if (cache->cols() == 1)
1562
item_subs->set_cond_guard_var(0, FALSE);
1563
(void) args[1]->val_bool_result();
1564
result_for_null_param= null_value= !item_subs->engine->no_rows();
1565
item_subs->set_cond_guard_var(0, TRUE);
1570
uint ncols= cache->cols();
1572
Turn off the predicates that are based on column compares for
1573
which the left part is currently NULL
1575
for (i= 0; i < ncols; i++)
1577
if (cache->element_index(i)->null_value)
1578
item_subs->set_cond_guard_var(i, FALSE);
1581
(void) args[1]->val_bool_result();
1582
result_for_null_param= null_value= !item_subs->engine->no_rows();
1584
/* Turn all predicates back on */
1585
for (i= 0; i < ncols; i++)
1586
item_subs->set_cond_guard_var(i, TRUE);
1592
tmp= args[1]->val_bool_result();
1593
null_value= args[1]->null_value;
1598
void Item_in_optimizer::keep_top_level_cache()
1600
cache->keep_array();
1605
void Item_in_optimizer::cleanup()
1607
DBUG_ENTER("Item_in_optimizer::cleanup");
1608
Item_bool_func::cleanup();
1615
bool Item_in_optimizer::is_null()
1617
cache->store(args[0]);
1618
return (null_value= (cache->null_value || args[1]->is_null()));
1623
Transform an Item_in_optimizer and its arguments with a callback function.
1625
@param transformer the transformer callback function to be applied to the
1626
nodes of the tree of the object
1627
@param parameter to be passed to the transformer
1630
Recursively transform the left and the right operand of this Item. The
1631
Right operand is an Item_in_subselect or its subclass. To avoid the
1632
creation of new Items, we use the fact the the left operand of the
1633
Item_in_subselect is the same as the one of 'this', so instead of
1634
transforming its operand, we just assign the left operand of the
1635
Item_in_subselect to be equal to the left operand of 'this'.
1636
The transformation is not applied further to the subquery operand
1637
if the IN predicate.
1640
@retval pointer to the transformed item
1641
@retval NULL if an error occurred
1644
Item *Item_in_optimizer::transform(Item_transformer transformer, uchar *argument)
1648
DBUG_ASSERT(arg_count == 2);
1650
/* Transform the left IN operand. */
1651
new_item= (*args)->transform(transformer, argument);
1655
THD::change_item_tree() should be called only if the tree was
1656
really transformed, i.e. when a new item has been created.
1657
Otherwise we'll be allocating a lot of unnecessary memory for
1658
change records at each execution.
1660
if ((*args) != new_item)
1661
current_thd->change_item_tree(args, new_item);
1664
Transform the right IN operand which should be an Item_in_subselect or a
1665
subclass of it. The left operand of the IN must be the same as the left
1666
operand of this Item_in_optimizer, so in this case there is no further
1667
transformation, we only make both operands the same.
1668
TODO: is it the way it should be?
1670
DBUG_ASSERT((args[1])->type() == Item::SUBSELECT_ITEM &&
1671
(((Item_subselect*)(args[1]))->substype() ==
1672
Item_subselect::IN_SUBS ||
1673
((Item_subselect*)(args[1]))->substype() ==
1674
Item_subselect::ALL_SUBS ||
1675
((Item_subselect*)(args[1]))->substype() ==
1676
Item_subselect::ANY_SUBS));
1678
Item_in_subselect *in_arg= (Item_in_subselect*)args[1];
1679
in_arg->left_expr= args[0];
1681
return (this->*transformer)(argument);
1686
longlong Item_func_eq::val_int()
1688
DBUG_ASSERT(fixed == 1);
1689
int value= cmp.compare();
1690
return value == 0 ? 1 : 0;
1694
/** Same as Item_func_eq, but NULL = NULL. */
1696
void Item_func_equal::fix_length_and_dec()
1698
Item_bool_func2::fix_length_and_dec();
1699
maybe_null=null_value=0;
1702
longlong Item_func_equal::val_int()
1704
DBUG_ASSERT(fixed == 1);
1705
return cmp.compare();
1708
longlong Item_func_ne::val_int()
1710
DBUG_ASSERT(fixed == 1);
1711
int value= cmp.compare();
1712
return value != 0 && !null_value ? 1 : 0;
1716
longlong Item_func_ge::val_int()
1718
DBUG_ASSERT(fixed == 1);
1719
int value= cmp.compare();
1720
return value >= 0 ? 1 : 0;
1724
longlong Item_func_gt::val_int()
1726
DBUG_ASSERT(fixed == 1);
1727
int value= cmp.compare();
1728
return value > 0 ? 1 : 0;
1731
longlong Item_func_le::val_int()
1733
DBUG_ASSERT(fixed == 1);
1734
int value= cmp.compare();
1735
return value <= 0 && !null_value ? 1 : 0;
1739
longlong Item_func_lt::val_int()
1741
DBUG_ASSERT(fixed == 1);
1742
int value= cmp.compare();
1743
return value < 0 && !null_value ? 1 : 0;
1747
longlong Item_func_strcmp::val_int()
1749
DBUG_ASSERT(fixed == 1);
1750
String *a=args[0]->val_str(&tmp_value1);
1751
String *b=args[1]->val_str(&tmp_value2);
1757
int value= sortcmp(a,b,cmp.cmp_collation.collation);
1759
return !value ? 0 : (value < 0 ? (longlong) -1 : (longlong) 1);
1763
bool Item_func_opt_neg::eq(const Item *item, bool binary_cmp) const
1765
/* Assume we don't have rtti */
1768
if (item->type() != FUNC_ITEM)
1770
Item_func *item_func=(Item_func*) item;
1771
if (arg_count != item_func->arg_count ||
1772
functype() != item_func->functype())
1774
if (negated != ((Item_func_opt_neg *) item_func)->negated)
1776
for (uint i=0; i < arg_count ; i++)
1777
if (!args[i]->eq(item_func->arguments()[i], binary_cmp))
1783
void Item_func_interval::fix_length_and_dec()
1785
uint rows= row->cols();
1787
use_decimal_comparison= ((row->element_index(0)->result_type() ==
1789
(row->element_index(0)->result_type() ==
1793
bool not_null_consts= TRUE;
1795
for (uint i= 1; not_null_consts && i < rows; i++)
1797
Item *el= row->element_index(i);
1798
not_null_consts&= el->const_item() & !el->is_null();
1801
if (not_null_consts &&
1803
(interval_range*) sql_alloc(sizeof(interval_range) * (rows - 1))))
1805
if (use_decimal_comparison)
1807
for (uint i= 1; i < rows; i++)
1809
Item *el= row->element_index(i);
1810
interval_range *range= intervals + (i-1);
1811
if ((el->result_type() == DECIMAL_RESULT) ||
1812
(el->result_type() == INT_RESULT))
1814
range->type= DECIMAL_RESULT;
1816
my_decimal *dec= el->val_decimal(&range->dec);
1817
if (dec != &range->dec)
1820
range->dec.fix_buffer_pointer();
1825
range->type= REAL_RESULT;
1826
range->dbl= el->val_real();
1832
for (uint i= 1; i < rows; i++)
1834
intervals[i-1].dbl= row->element_index(i)->val_real();
1841
used_tables_cache|= row->used_tables();
1842
not_null_tables_cache= row->not_null_tables();
1843
with_sum_func= with_sum_func || row->with_sum_func;
1844
const_item_cache&= row->const_item();
1849
Execute Item_func_interval().
1852
If we are doing a decimal comparison, we are evaluating the first
1857
- 0 if lower than lowest
1858
- 1 - arg_count-1 if between args[n] and args[n+1]
1859
- arg_count if higher than biggest argument
1862
longlong Item_func_interval::val_int()
1864
DBUG_ASSERT(fixed == 1);
1866
my_decimal dec_buf, *dec= NULL;
1869
if (use_decimal_comparison)
1871
dec= row->element_index(0)->val_decimal(&dec_buf);
1872
if (row->element_index(0)->null_value)
1874
my_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
1878
value= row->element_index(0)->val_real();
1879
if (row->element_index(0)->null_value)
1884
{ // Use binary search to find interval
1888
while (start != end)
1890
uint mid= (start + end + 1) / 2;
1891
interval_range *range= intervals + mid;
1894
The values in the range intervall may have different types,
1895
Only do a decimal comparision of the first argument is a decimal
1896
and we are comparing against a decimal
1898
if (dec && range->type == DECIMAL_RESULT)
1899
cmp_result= my_decimal_cmp(&range->dec, dec) <= 0;
1901
cmp_result= (range->dbl <= value);
1907
interval_range *range= intervals+start;
1908
return ((dec && range->type == DECIMAL_RESULT) ?
1909
my_decimal_cmp(dec, &range->dec) < 0 :
1910
value < range->dbl) ? 0 : start + 1;
1913
for (i=1 ; i < row->cols() ; i++)
1915
Item *el= row->element_index(i);
1916
if (use_decimal_comparison &&
1917
((el->result_type() == DECIMAL_RESULT) ||
1918
(el->result_type() == INT_RESULT)))
1920
my_decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
1921
/* Skip NULL ranges. */
1924
if (my_decimal_cmp(e_dec, dec) > 0)
1929
double val= el->val_real();
1930
/* Skip NULL ranges. */
1942
Perform context analysis of a BETWEEN item tree.
1944
This function performs context analysis (name resolution) and calculates
1945
various attributes of the item tree with Item_func_between as its root.
1946
The function saves in ref the pointer to the item or to a newly created
1947
item that is considered as a replacement for the original one.
1949
@param thd reference to the global context of the query thread
1950
@param ref pointer to Item* variable where pointer to resulting "fixed"
1951
item is to be assigned
1954
Let T0(e)/T1(e) be the value of not_null_tables(e) when e is used on
1955
a predicate/function level. Then it's easy to show that:
1957
T0(e BETWEEN e1 AND e2) = union(T1(e),T1(e1),T1(e2))
1958
T1(e BETWEEN e1 AND e2) = union(T1(e),intersection(T1(e1),T1(e2)))
1959
T0(e NOT BETWEEN e1 AND e2) = union(T1(e),intersection(T1(e1),T1(e2)))
1960
T1(e NOT BETWEEN e1 AND e2) = union(T1(e),intersection(T1(e1),T1(e2)))
1969
bool Item_func_between::fix_fields(THD *thd, Item **ref)
1971
if (Item_func_opt_neg::fix_fields(thd, ref))
1974
thd->lex->current_select->between_count++;
1976
/* not_null_tables_cache == union(T1(e),T1(e1),T1(e2)) */
1977
if (pred_level && !negated)
1980
/* not_null_tables_cache == union(T1(e), intersection(T1(e1),T1(e2))) */
1981
not_null_tables_cache= (args[0]->not_null_tables() |
1982
(args[1]->not_null_tables() &
1983
args[2]->not_null_tables()));
1989
void Item_func_between::fix_length_and_dec()
1993
bool datetime_found= FALSE;
1994
int time_items_found= 0;
1995
compare_as_dates= TRUE;
1996
THD *thd= current_thd;
1999
As some compare functions are generated after sql_yacc,
2000
we have to check for out of memory conditions here
2002
if (!args[0] || !args[1] || !args[2])
2004
if ( agg_cmp_type(&cmp_type, args, 3))
2006
if (cmp_type == STRING_RESULT &&
2007
agg_arg_charsets(cmp_collation, args, 3, MY_COLL_CMP_CONV, 1))
2011
Detect the comparison of DATE/DATETIME items.
2012
At least one of items should be a DATE/DATETIME item and other items
2013
should return the STRING result.
2015
if (cmp_type == STRING_RESULT)
2017
for (i= 0; i < 3; i++)
2019
if (args[i]->is_datetime())
2021
datetime_found= TRUE;
2024
if (args[i]->field_type() == MYSQL_TYPE_TIME &&
2025
args[i]->result_as_longlong())
2029
if (!datetime_found)
2030
compare_as_dates= FALSE;
2032
if (compare_as_dates)
2034
ge_cmp.set_datetime_cmp_func(args, args + 1);
2035
le_cmp.set_datetime_cmp_func(args, args + 2);
2037
else if (time_items_found == 3)
2039
/* Compare TIME items as integers. */
2040
cmp_type= INT_RESULT;
2042
else if (args[0]->real_item()->type() == FIELD_ITEM &&
2043
thd->lex->sql_command != SQLCOM_SHOW_CREATE)
2045
Item_field *field_item= (Item_field*) (args[0]->real_item());
2046
if (field_item->field->can_be_compared_as_longlong())
2049
The following can't be recoded with || as convert_constant_item
2050
changes the argument
2052
if (convert_constant_item(thd, field_item, &args[1]))
2053
cmp_type=INT_RESULT; // Works for all types.
2054
if (convert_constant_item(thd, field_item, &args[2]))
2055
cmp_type=INT_RESULT; // Works for all types.
2061
longlong Item_func_between::val_int()
2063
DBUG_ASSERT(fixed == 1);
2064
if (compare_as_dates)
2068
ge_res= ge_cmp.compare();
2069
if ((null_value= args[0]->null_value))
2071
le_res= le_cmp.compare();
2073
if (!args[1]->null_value && !args[2]->null_value)
2074
return (longlong) ((ge_res >= 0 && le_res <=0) != negated);
2075
else if (args[1]->null_value)
2077
null_value= le_res > 0; // not null if false range.
2081
null_value= ge_res < 0;
2084
else if (cmp_type == STRING_RESULT)
2086
String *value,*a,*b;
2087
value=args[0]->val_str(&value0);
2088
if ((null_value=args[0]->null_value))
2090
a=args[1]->val_str(&value1);
2091
b=args[2]->val_str(&value2);
2092
if (!args[1]->null_value && !args[2]->null_value)
2093
return (longlong) ((sortcmp(value,a,cmp_collation.collation) >= 0 &&
2094
sortcmp(value,b,cmp_collation.collation) <= 0) !=
2096
if (args[1]->null_value && args[2]->null_value)
2098
else if (args[1]->null_value)
2100
// Set to not null if false range.
2101
null_value= sortcmp(value,b,cmp_collation.collation) <= 0;
2105
// Set to not null if false range.
2106
null_value= sortcmp(value,a,cmp_collation.collation) >= 0;
2109
else if (cmp_type == INT_RESULT)
2111
longlong value=args[0]->val_int(), a, b;
2112
if ((null_value=args[0]->null_value))
2113
return 0; /* purecov: inspected */
2114
a=args[1]->val_int();
2115
b=args[2]->val_int();
2116
if (!args[1]->null_value && !args[2]->null_value)
2117
return (longlong) ((value >= a && value <= b) != negated);
2118
if (args[1]->null_value && args[2]->null_value)
2120
else if (args[1]->null_value)
2122
null_value= value <= b; // not null if false range.
2126
null_value= value >= a;
2129
else if (cmp_type == DECIMAL_RESULT)
2131
my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2132
a_buf, *a_dec, b_buf, *b_dec;
2133
if ((null_value=args[0]->null_value))
2134
return 0; /* purecov: inspected */
2135
a_dec= args[1]->val_decimal(&a_buf);
2136
b_dec= args[2]->val_decimal(&b_buf);
2137
if (!args[1]->null_value && !args[2]->null_value)
2138
return (longlong) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2139
my_decimal_cmp(dec, b_dec) <= 0) != negated);
2140
if (args[1]->null_value && args[2]->null_value)
2142
else if (args[1]->null_value)
2143
null_value= (my_decimal_cmp(dec, b_dec) <= 0);
2145
null_value= (my_decimal_cmp(dec, a_dec) >= 0);
2149
double value= args[0]->val_real(),a,b;
2150
if ((null_value=args[0]->null_value))
2151
return 0; /* purecov: inspected */
2152
a= args[1]->val_real();
2153
b= args[2]->val_real();
2154
if (!args[1]->null_value && !args[2]->null_value)
2155
return (longlong) ((value >= a && value <= b) != negated);
2156
if (args[1]->null_value && args[2]->null_value)
2158
else if (args[1]->null_value)
2160
null_value= value <= b; // not null if false range.
2164
null_value= value >= a;
2167
return (longlong) (!null_value && negated);
2171
void Item_func_between::print(String *str, enum_query_type query_type)
2174
args[0]->print(str, query_type);
2176
str->append(STRING_WITH_LEN(" not"));
2177
str->append(STRING_WITH_LEN(" between "));
2178
args[1]->print(str, query_type);
2179
str->append(STRING_WITH_LEN(" and "));
2180
args[2]->print(str, query_type);
2185
Item_func_ifnull::fix_length_and_dec()
2187
agg_result_type(&hybrid_type, args, 2);
2188
maybe_null=args[1]->maybe_null;
2189
decimals= max(args[0]->decimals, args[1]->decimals);
2190
unsigned_flag= args[0]->unsigned_flag && args[1]->unsigned_flag;
2192
if (hybrid_type == DECIMAL_RESULT || hybrid_type == INT_RESULT)
2194
int len0= args[0]->max_length - args[0]->decimals
2195
- (args[0]->unsigned_flag ? 0 : 1);
2197
int len1= args[1]->max_length - args[1]->decimals
2198
- (args[1]->unsigned_flag ? 0 : 1);
2200
max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2203
max_length= max(args[0]->max_length, args[1]->max_length);
2205
switch (hybrid_type) {
2207
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2209
case DECIMAL_RESULT:
2219
cached_field_type= agg_field_type(args, 2);
2223
uint Item_func_ifnull::decimal_precision() const
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);
2230
enum_field_types Item_func_ifnull::field_type() const
2232
return cached_field_type;
2235
Field *Item_func_ifnull::tmp_table_field(TABLE *table)
2237
return tmp_table_field_from_field_type(table, 0);
2241
Item_func_ifnull::real_op()
2243
DBUG_ASSERT(fixed == 1);
2244
double value= args[0]->val_real();
2245
if (!args[0]->null_value)
2250
value= args[1]->val_real();
2251
if ((null_value=args[1]->null_value))
2257
Item_func_ifnull::int_op()
2259
DBUG_ASSERT(fixed == 1);
2260
longlong value=args[0]->val_int();
2261
if (!args[0]->null_value)
2266
value=args[1]->val_int();
2267
if ((null_value=args[1]->null_value))
2273
my_decimal *Item_func_ifnull::decimal_op(my_decimal *decimal_value)
2275
DBUG_ASSERT(fixed == 1);
2276
my_decimal *value= args[0]->val_decimal(decimal_value);
2277
if (!args[0]->null_value)
2282
value= args[1]->val_decimal(decimal_value);
2283
if ((null_value= args[1]->null_value))
2290
Item_func_ifnull::str_op(String *str)
2292
DBUG_ASSERT(fixed == 1);
2293
String *res =args[0]->val_str(str);
2294
if (!args[0]->null_value)
2297
res->set_charset(collation.collation);
2300
res=args[1]->val_str(str);
2301
if ((null_value=args[1]->null_value))
2303
res->set_charset(collation.collation);
2309
Perform context analysis of an IF item tree.
2311
This function performs context analysis (name resolution) and calculates
2312
various attributes of the item tree with Item_func_if as its root.
2313
The function saves in ref the pointer to the item or to a newly created
2314
item that is considered as a replacement for the original one.
2316
@param thd reference to the global context of the query thread
2317
@param ref pointer to Item* variable where pointer to resulting "fixed"
2318
item is to be assigned
2321
Let T0(e)/T1(e) be the value of not_null_tables(e) when e is used on
2322
a predicate/function level. Then it's easy to show that:
2324
T0(IF(e,e1,e2) = T1(IF(e,e1,e2))
2325
T1(IF(e,e1,e2)) = intersection(T1(e1),T1(e2))
2335
Item_func_if::fix_fields(THD *thd, Item **ref)
2337
DBUG_ASSERT(fixed == 0);
2338
args[0]->top_level_item();
2340
if (Item_func::fix_fields(thd, ref))
2343
not_null_tables_cache= (args[1]->not_null_tables() &
2344
args[2]->not_null_tables());
2351
Item_func_if::fix_length_and_dec()
2353
maybe_null=args[1]->maybe_null || args[2]->maybe_null;
2354
decimals= max(args[1]->decimals, args[2]->decimals);
2355
unsigned_flag=args[1]->unsigned_flag && args[2]->unsigned_flag;
2357
enum Item_result arg1_type=args[1]->result_type();
2358
enum Item_result arg2_type=args[2]->result_type();
2359
bool null1=args[1]->const_item() && args[1]->null_value;
2360
bool null2=args[2]->const_item() && args[2]->null_value;
2364
cached_result_type= arg2_type;
2365
collation.set(args[2]->collation.collation);
2366
cached_field_type= args[2]->field_type();
2370
cached_result_type= arg1_type;
2371
collation.set(args[1]->collation.collation);
2372
cached_field_type= args[1]->field_type();
2376
agg_result_type(&cached_result_type, args+1, 2);
2377
if (cached_result_type == STRING_RESULT)
2379
if (agg_arg_charsets(collation, args+1, 2, MY_COLL_ALLOW_CONV, 1))
2384
collation.set(&my_charset_bin); // Number
2386
cached_field_type= agg_field_type(args + 1, 2);
2389
if ((cached_result_type == DECIMAL_RESULT )
2390
|| (cached_result_type == INT_RESULT))
2392
int len1= args[1]->max_length - args[1]->decimals
2393
- (args[1]->unsigned_flag ? 0 : 1);
2395
int len2= args[2]->max_length - args[2]->decimals
2396
- (args[2]->unsigned_flag ? 0 : 1);
2398
max_length=max(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
2401
max_length= max(args[1]->max_length, args[2]->max_length);
2405
uint Item_func_if::decimal_precision() const
2407
int precision=(max(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
2409
return min(precision, DECIMAL_MAX_PRECISION);
2414
Item_func_if::val_real()
2416
DBUG_ASSERT(fixed == 1);
2417
Item *arg= args[0]->val_bool() ? args[1] : args[2];
2418
double value= arg->val_real();
2419
null_value=arg->null_value;
2424
Item_func_if::val_int()
2426
DBUG_ASSERT(fixed == 1);
2427
Item *arg= args[0]->val_bool() ? args[1] : args[2];
2428
longlong value=arg->val_int();
2429
null_value=arg->null_value;
2434
Item_func_if::val_str(String *str)
2436
DBUG_ASSERT(fixed == 1);
2437
Item *arg= args[0]->val_bool() ? args[1] : args[2];
2438
String *res=arg->val_str(str);
2440
res->set_charset(collation.collation);
2441
null_value=arg->null_value;
2447
Item_func_if::val_decimal(my_decimal *decimal_value)
2449
DBUG_ASSERT(fixed == 1);
2450
Item *arg= args[0]->val_bool() ? args[1] : args[2];
2451
my_decimal *value= arg->val_decimal(decimal_value);
2452
null_value= arg->null_value;
2458
Item_func_nullif::fix_length_and_dec()
2460
Item_bool_func2::fix_length_and_dec();
2462
if (args[0]) // Only false if EOM
2464
max_length=args[0]->max_length;
2465
decimals=args[0]->decimals;
2466
unsigned_flag= args[0]->unsigned_flag;
2467
cached_result_type= args[0]->result_type();
2468
if (cached_result_type == STRING_RESULT &&
2469
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1))
2477
Note that we have to evaluate the first argument twice as the compare
2478
may have been done with a different type than return value
2480
NULL if arguments are equal
2482
the first argument if not equal
2486
Item_func_nullif::val_real()
2488
DBUG_ASSERT(fixed == 1);
2495
value= args[0]->val_real();
2496
null_value=args[0]->null_value;
2501
Item_func_nullif::val_int()
2503
DBUG_ASSERT(fixed == 1);
2510
value=args[0]->val_int();
2511
null_value=args[0]->null_value;
2516
Item_func_nullif::val_str(String *str)
2518
DBUG_ASSERT(fixed == 1);
2525
res=args[0]->val_str(str);
2526
null_value=args[0]->null_value;
2532
Item_func_nullif::val_decimal(my_decimal * decimal_value)
2534
DBUG_ASSERT(fixed == 1);
2541
res= args[0]->val_decimal(decimal_value);
2542
null_value= args[0]->null_value;
2548
Item_func_nullif::is_null()
2550
return (null_value= (!cmp.compare() ? 1 : args[0]->null_value));
2555
Find and return matching items for CASE or ELSE item if all compares
2556
are failed or NULL if ELSE item isn't defined.
2559
In order to do correct comparisons of the CASE expression (the expression
2560
between CASE and the first WHEN) with each WHEN expression several
2561
comparators are used. One for each result type. CASE expression can be
2562
evaluated up to # of different result types are used. To check whether
2563
the CASE expression already was evaluated for a particular result type
2564
a bit mapped variable value_added_map is used. Result types are mapped
2565
to it according to their int values i.e. STRING_RESULT is mapped to bit
2566
0, REAL_RESULT to bit 1, so on.
2569
NULL Nothing found and there is no ELSE expression defined
2571
item Found item or ELSE item if defined and all comparisons are
2575
Item *Item_func_case::find_item(String *str)
2577
uint value_added_map= 0;
2579
if (first_expr_num == -1)
2581
for (uint i=0 ; i < ncases ; i+=2)
2583
// No expression between CASE and the first WHEN
2584
if (args[i]->val_bool())
2591
/* Compare every WHEN argument with it and return the first match */
2592
for (uint i=0 ; i < ncases ; i+=2)
2594
cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
2595
DBUG_ASSERT(cmp_type != ROW_RESULT);
2596
DBUG_ASSERT(cmp_items[(uint)cmp_type]);
2597
if (!(value_added_map & (1<<(uint)cmp_type)))
2599
cmp_items[(uint)cmp_type]->store_value(args[first_expr_num]);
2600
if ((null_value=args[first_expr_num]->null_value))
2601
return else_expr_num != -1 ? args[else_expr_num] : 0;
2602
value_added_map|= 1<<(uint)cmp_type;
2604
if (!cmp_items[(uint)cmp_type]->cmp(args[i]) && !args[i]->null_value)
2608
// No, WHEN clauses all missed, return ELSE expression
2609
return else_expr_num != -1 ? args[else_expr_num] : 0;
2613
String *Item_func_case::val_str(String *str)
2615
DBUG_ASSERT(fixed == 1);
2617
Item *item=find_item(str);
2625
if (!(res=item->val_str(str)))
2631
longlong Item_func_case::val_int()
2633
DBUG_ASSERT(fixed == 1);
2634
char buff[MAX_FIELD_WIDTH];
2635
String dummy_str(buff,sizeof(buff),default_charset());
2636
Item *item=find_item(&dummy_str);
2644
res=item->val_int();
2645
null_value=item->null_value;
2649
double Item_func_case::val_real()
2651
DBUG_ASSERT(fixed == 1);
2652
char buff[MAX_FIELD_WIDTH];
2653
String dummy_str(buff,sizeof(buff),default_charset());
2654
Item *item=find_item(&dummy_str);
2662
res= item->val_real();
2663
null_value=item->null_value;
2668
my_decimal *Item_func_case::val_decimal(my_decimal *decimal_value)
2670
DBUG_ASSERT(fixed == 1);
2671
char buff[MAX_FIELD_WIDTH];
2672
String dummy_str(buff, sizeof(buff), default_charset());
2673
Item *item= find_item(&dummy_str);
2682
res= item->val_decimal(decimal_value);
2683
null_value= item->null_value;
2688
bool Item_func_case::fix_fields(THD *thd, Item **ref)
2691
buff should match stack usage from
2692
Item_func_case::val_int() -> Item_func_case::find_item()
2694
uchar buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(longlong)*2];
2695
bool res= Item_func::fix_fields(thd, ref);
2697
Call check_stack_overrun after fix_fields to be sure that stack variable
2698
is not optimized away
2700
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2701
return TRUE; // Fatal error flag is set!
2706
void Item_func_case::agg_str_lengths(Item* arg)
2708
set_if_bigger(max_length, arg->max_length);
2709
set_if_bigger(decimals, arg->decimals);
2710
unsigned_flag= unsigned_flag && arg->unsigned_flag;
2714
void Item_func_case::agg_num_lengths(Item *arg)
2716
uint len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2717
arg->unsigned_flag) - arg->decimals;
2718
set_if_bigger(max_length, len);
2719
set_if_bigger(decimals, arg->decimals);
2720
unsigned_flag= unsigned_flag && arg->unsigned_flag;
2724
void Item_func_case::fix_length_and_dec()
2728
uint found_types= 0;
2729
if (!(agg= (Item**) sql_alloc(sizeof(Item*)*(ncases+1))))
2733
Aggregate all THEN and ELSE expression types
2734
and collations when string result
2737
for (nagg= 0 ; nagg < ncases/2 ; nagg++)
2738
agg[nagg]= args[nagg*2+1];
2740
if (else_expr_num != -1)
2741
agg[nagg++]= args[else_expr_num];
2743
agg_result_type(&cached_result_type, agg, nagg);
2744
if ((cached_result_type == STRING_RESULT) &&
2745
agg_arg_charsets(collation, agg, nagg, MY_COLL_ALLOW_CONV, 1))
2748
cached_field_type= agg_field_type(agg, nagg);
2750
Aggregate first expression and all THEN expression types
2751
and collations when string comparison
2753
if (first_expr_num != -1)
2756
agg[0]= args[first_expr_num];
2757
left_result_type= agg[0]->result_type();
2759
for (nagg= 0; nagg < ncases/2 ; nagg++)
2760
agg[nagg+1]= args[nagg*2];
2762
if (!(found_types= collect_cmp_types(agg, nagg)))
2765
for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
2767
if (found_types & (1 << i) && !cmp_items[i])
2769
DBUG_ASSERT((Item_result)i != ROW_RESULT);
2770
if ((Item_result)i == STRING_RESULT &&
2771
agg_arg_charsets(cmp_collation, agg, nagg, MY_COLL_CMP_CONV, 1))
2774
cmp_item::get_comparator((Item_result)i,
2775
cmp_collation.collation)))
2781
if (else_expr_num == -1 || args[else_expr_num]->maybe_null)
2786
unsigned_flag= TRUE;
2787
if (cached_result_type == STRING_RESULT)
2789
for (uint i= 0; i < ncases; i+= 2)
2790
agg_str_lengths(args[i + 1]);
2791
if (else_expr_num != -1)
2792
agg_str_lengths(args[else_expr_num]);
2796
for (uint i= 0; i < ncases; i+= 2)
2797
agg_num_lengths(args[i + 1]);
2798
if (else_expr_num != -1)
2799
agg_num_lengths(args[else_expr_num]);
2800
max_length= my_decimal_precision_to_length(max_length + decimals, decimals,
2806
uint Item_func_case::decimal_precision() const
2809
for (uint i=0 ; i < ncases ; i+=2)
2810
set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
2812
if (else_expr_num != -1)
2813
set_if_bigger(max_int_part, args[else_expr_num]->decimal_int_part());
2814
return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2820
Fix this so that it prints the whole CASE expression
2823
void Item_func_case::print(String *str, enum_query_type query_type)
2825
str->append(STRING_WITH_LEN("(case "));
2826
if (first_expr_num != -1)
2828
args[first_expr_num]->print(str, query_type);
2831
for (uint i=0 ; i < ncases ; i+=2)
2833
str->append(STRING_WITH_LEN("when "));
2834
args[i]->print(str, query_type);
2835
str->append(STRING_WITH_LEN(" then "));
2836
args[i+1]->print(str, query_type);
2839
if (else_expr_num != -1)
2841
str->append(STRING_WITH_LEN("else "));
2842
args[else_expr_num]->print(str, query_type);
2845
str->append(STRING_WITH_LEN("end)"));
2849
void Item_func_case::cleanup()
2852
DBUG_ENTER("Item_func_case::cleanup");
2853
Item_func::cleanup();
2854
for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
2856
delete cmp_items[i];
2864
Coalesce - return first not NULL argument.
2867
String *Item_func_coalesce::str_op(String *str)
2869
DBUG_ASSERT(fixed == 1);
2871
for (uint i=0 ; i < arg_count ; i++)
2874
if ((res=args[i]->val_str(str)))
2881
longlong Item_func_coalesce::int_op()
2883
DBUG_ASSERT(fixed == 1);
2885
for (uint i=0 ; i < arg_count ; i++)
2887
longlong res=args[i]->val_int();
2888
if (!args[i]->null_value)
2895
double Item_func_coalesce::real_op()
2897
DBUG_ASSERT(fixed == 1);
2899
for (uint i=0 ; i < arg_count ; i++)
2901
double res= args[i]->val_real();
2902
if (!args[i]->null_value)
2910
my_decimal *Item_func_coalesce::decimal_op(my_decimal *decimal_value)
2912
DBUG_ASSERT(fixed == 1);
2914
for (uint i= 0; i < arg_count; i++)
2916
my_decimal *res= args[i]->val_decimal(decimal_value);
2917
if (!args[i]->null_value)
2925
void Item_func_coalesce::fix_length_and_dec()
2927
cached_field_type= agg_field_type(args, arg_count);
2928
agg_result_type(&hybrid_type, args, arg_count);
2929
switch (hybrid_type) {
2931
count_only_length();
2932
decimals= NOT_FIXED_DEC;
2933
agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1);
2935
case DECIMAL_RESULT:
2936
count_decimal_length();
2939
count_real_length();
2942
count_only_length();
2951
/****************************************************************************
2952
Classes and function for the IN operator
2953
****************************************************************************/
2956
Determine which of the signed longlong arguments is bigger
2961
b_val right argument
2964
This function will compare two signed longlong arguments
2965
and will return -1, 0, or 1 if left argument is smaller than,
2966
equal to or greater than the right argument.
2969
-1 left argument is smaller than the right argument.
2970
0 left argument is equal to the right argument.
2971
1 left argument is greater than the right argument.
2973
static inline int cmp_longs (longlong a_val, longlong b_val)
2975
return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
2980
Determine which of the unsigned longlong arguments is bigger
2985
b_val right argument
2988
This function will compare two unsigned longlong arguments
2989
and will return -1, 0, or 1 if left argument is smaller than,
2990
equal to or greater than the right argument.
2993
-1 left argument is smaller than the right argument.
2994
0 left argument is equal to the right argument.
2995
1 left argument is greater than the right argument.
2997
static inline int cmp_ulongs (ulonglong a_val, ulonglong b_val)
2999
return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
3004
Compare two integers in IN value list format (packed_longlong)
3008
cmp_arg an argument passed to the calling function (my_qsort2)
3013
This function will compare two integer arguments in the IN value list
3014
format and will return -1, 0, or 1 if left argument is smaller than,
3015
equal to or greater than the right argument.
3016
It's used in sorting the IN values list and finding an element in it.
3017
Depending on the signedness of the arguments cmp_longlong() will
3018
compare them as either signed (using cmp_longs()) or unsigned (using
3022
-1 left argument is smaller than the right argument.
3023
0 left argument is equal to the right argument.
3024
1 left argument is greater than the right argument.
3026
int cmp_longlong(void *cmp_arg,
3027
in_longlong::packed_longlong *a,
3028
in_longlong::packed_longlong *b)
3030
if (a->unsigned_flag != b->unsigned_flag)
3033
One of the args is unsigned and is too big to fit into the
3034
positive signed range. Report no match.
3036
if ((a->unsigned_flag && ((ulonglong) a->val) > (ulonglong) LONGLONG_MAX) ||
3037
(b->unsigned_flag && ((ulonglong) b->val) > (ulonglong) LONGLONG_MAX))
3038
return a->unsigned_flag ? 1 : -1;
3040
Although the signedness differs both args can fit into the signed
3041
positive range. Make them signed and compare as usual.
3043
return cmp_longs (a->val, b->val);
3045
if (a->unsigned_flag)
3046
return cmp_ulongs ((ulonglong) a->val, (ulonglong) b->val);
3048
return cmp_longs (a->val, b->val);
3051
static int cmp_double(void *cmp_arg, double *a,double *b)
3053
return *a < *b ? -1 : *a == *b ? 0 : 1;
3056
static int cmp_row(void *cmp_arg, cmp_item_row *a, cmp_item_row *b)
3058
return a->compare(b);
3062
static int cmp_decimal(void *cmp_arg, my_decimal *a, my_decimal *b)
3065
We need call of fixing buffer pointer, because fast sort just copy
3066
decimal buffers in memory and pointers left pointing on old buffer place
3068
a->fix_buffer_pointer();
3069
b->fix_buffer_pointer();
3070
return my_decimal_cmp(a, b);
3074
int in_vector::find(Item *item)
3076
uchar *result=get_value(item);
3077
if (!result || !used_count)
3078
return 0; // Null value
3081
start=0; end=used_count-1;
3082
while (start != end)
3084
uint mid=(start+end+1)/2;
3086
if ((res=(*compare)(collation, base+mid*size, result)) == 0)
3093
return (int) ((*compare)(collation, base+start*size, result) == 0);
3096
in_string::in_string(uint elements,qsort2_cmp cmp_func, CHARSET_INFO *cs)
3097
:in_vector(elements, sizeof(String), cmp_func, cs),
3098
tmp(buff, sizeof(buff), &my_charset_bin)
3101
in_string::~in_string()
3105
// base was allocated with help of sql_alloc => following is OK
3106
for (uint i=0 ; i < count ; i++)
3107
((String*) base)[i].free();
3111
void in_string::set(uint pos,Item *item)
3113
String *str=((String*) base)+pos;
3114
String *res=item->val_str(str);
3115
if (res && res != str)
3117
if (res->uses_buffer_owned_by(str))
3119
if (item->type() == Item::FUNC_ITEM)
3124
if (!str->charset())
3127
if (!(cs= item->collation.collation))
3128
cs= &my_charset_bin; // Should never happen for STR items
3129
str->set_charset(cs);
3134
uchar *in_string::get_value(Item *item)
3136
return (uchar*) item->val_str(&tmp);
3139
in_row::in_row(uint elements, Item * item)
3141
base= (char*) new cmp_item_row[count= elements];
3142
size= sizeof(cmp_item_row);
3143
compare= (qsort2_cmp) cmp_row;
3145
We need to reset these as otherwise we will call sort() with
3146
uninitialized (even if not used) elements
3148
used_count= elements;
3155
delete [] (cmp_item_row*) base;
3158
uchar *in_row::get_value(Item *item)
3160
tmp.store_value(item);
3161
if (item->is_null())
3163
return (uchar *)&tmp;
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));
3170
((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3174
in_longlong::in_longlong(uint elements)
3175
:in_vector(elements,sizeof(packed_longlong),(qsort2_cmp) cmp_longlong, 0)
3178
void in_longlong::set(uint pos,Item *item)
3180
struct packed_longlong *buff= &((packed_longlong*) base)[pos];
3182
buff->val= item->val_int();
3183
buff->unsigned_flag= item->unsigned_flag;
3186
uchar *in_longlong::get_value(Item *item)
3188
tmp.val= item->val_int();
3189
if (item->null_value)
3191
tmp.unsigned_flag= item->unsigned_flag;
3192
return (uchar*) &tmp;
3195
void in_datetime::set(uint pos,Item *item)
3197
Item **tmp_item= &item;
3199
struct packed_longlong *buff= &((packed_longlong*) base)[pos];
3201
buff->val= get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
3202
buff->unsigned_flag= 1L;
3205
uchar *in_datetime::get_value(Item *item)
3208
Item **tmp_item= lval_cache ? &lval_cache : &item;
3209
tmp.val= get_datetime_value(thd, &tmp_item, &lval_cache, warn_item, &is_null);
3210
if (item->null_value)
3212
tmp.unsigned_flag= 1L;
3213
return (uchar*) &tmp;
3216
in_double::in_double(uint elements)
3217
:in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
3220
void in_double::set(uint pos,Item *item)
3222
((double*) base)[pos]= item->val_real();
3225
uchar *in_double::get_value(Item *item)
3227
tmp= item->val_real();
3228
if (item->null_value)
3229
return 0; /* purecov: inspected */
3230
return (uchar*) &tmp;
3234
in_decimal::in_decimal(uint elements)
3235
:in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3239
void in_decimal::set(uint pos, Item *item)
3241
/* as far as 'item' is constant, we can store reference on my_decimal */
3242
my_decimal *dec= ((my_decimal *)base) + pos;
3243
dec->len= DECIMAL_BUFF_LENGTH;
3244
dec->fix_buffer_pointer();
3245
my_decimal *res= item->val_decimal(dec);
3246
/* if item->val_decimal() is evaluated to NULL then res == 0 */
3247
if (!item->null_value && res != dec)
3248
my_decimal2decimal(res, dec);
3252
uchar *in_decimal::get_value(Item *item)
3254
my_decimal *result= item->val_decimal(&val);
3255
if (item->null_value)
3257
return (uchar *)result;
3261
cmp_item* cmp_item::get_comparator(Item_result type,
3266
return new cmp_item_sort_string(cs);
3268
return new cmp_item_int;
3270
return new cmp_item_real;
3272
return new cmp_item_row;
3273
case DECIMAL_RESULT:
3274
return new cmp_item_decimal;
3279
return 0; // to satisfy compiler :)
3283
cmp_item* cmp_item_sort_string::make_same()
3285
return new cmp_item_sort_string_in_static(cmp_charset);
3288
cmp_item* cmp_item_int::make_same()
3290
return new cmp_item_int();
3293
cmp_item* cmp_item_real::make_same()
3295
return new cmp_item_real();
3298
cmp_item* cmp_item_row::make_same()
3300
return new cmp_item_row();
3304
cmp_item_row::~cmp_item_row()
3306
DBUG_ENTER("~cmp_item_row");
3307
DBUG_PRINT("enter",("this: 0x%lx", (long) this));
3310
for (uint i= 0; i < n; i++)
3313
delete comparators[i];
3320
void cmp_item_row::alloc_comparators()
3323
comparators= (cmp_item **) current_thd->calloc(sizeof(cmp_item *)*n);
3327
void cmp_item_row::store_value(Item *item)
3329
DBUG_ENTER("cmp_item_row::store_value");
3331
alloc_comparators();
3334
item->bring_value();
3335
item->null_value= 0;
3336
for (uint i=0; i < n; i++)
3338
if (!comparators[i])
3339
if (!(comparators[i]=
3340
cmp_item::get_comparator(item->element_index(i)->result_type(),
3341
item->element_index(i)->collation.collation)))
3342
break; // new failed
3343
comparators[i]->store_value(item->element_index(i));
3344
item->null_value|= item->element_index(i)->null_value;
3351
void cmp_item_row::store_value_by_template(cmp_item *t, Item *item)
3353
cmp_item_row *tmpl= (cmp_item_row*) t;
3354
if (tmpl->n != item->cols())
3356
my_error(ER_OPERAND_COLUMNS, MYF(0), tmpl->n);
3360
if ((comparators= (cmp_item **) sql_alloc(sizeof(cmp_item *)*n)))
3362
item->bring_value();
3363
item->null_value= 0;
3364
for (uint i=0; i < n; i++)
3366
if (!(comparators[i]= tmpl->comparators[i]->make_same()))
3367
break; // new failed
3368
comparators[i]->store_value_by_template(tmpl->comparators[i],
3369
item->element_index(i));
3370
item->null_value|= item->element_index(i)->null_value;
3376
int cmp_item_row::cmp(Item *arg)
3379
if (arg->cols() != n)
3381
my_error(ER_OPERAND_COLUMNS, MYF(0), n);
3386
for (uint i=0; i < n; i++)
3388
if (comparators[i]->cmp(arg->element_index(i)))
3390
if (!arg->element_index(i)->null_value)
3395
return (arg->null_value= was_null);
3399
int cmp_item_row::compare(cmp_item *c)
3401
cmp_item_row *l_cmp= (cmp_item_row *) c;
3402
for (uint i=0; i < n; i++)
3405
if ((res= comparators[i]->compare(l_cmp->comparators[i])))
3412
void cmp_item_decimal::store_value(Item *item)
3414
my_decimal *val= item->val_decimal(&value);
3415
/* val may be zero if item is nnull */
3416
if (val && val != &value)
3417
my_decimal2decimal(val, &value);
3421
int cmp_item_decimal::cmp(Item *arg)
3423
my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3424
if (arg->null_value)
3426
return my_decimal_cmp(&value, tmp);
3430
int cmp_item_decimal::compare(cmp_item *arg)
3432
cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3433
return my_decimal_cmp(&value, &l_cmp->value);
3437
cmp_item* cmp_item_decimal::make_same()
3439
return new cmp_item_decimal();
3443
void cmp_item_datetime::store_value(Item *item)
3446
Item **tmp_item= lval_cache ? &lval_cache : &item;
3447
value= get_datetime_value(thd, &tmp_item, &lval_cache, warn_item, &is_null);
3451
int cmp_item_datetime::cmp(Item *arg)
3454
Item **tmp_item= &arg;
3456
get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
3460
int cmp_item_datetime::compare(cmp_item *ci)
3462
cmp_item_datetime *l_cmp= (cmp_item_datetime *)ci;
3463
return (value < l_cmp->value) ? -1 : ((value == l_cmp->value) ? 0 : 1);
3467
cmp_item *cmp_item_datetime::make_same()
3469
return new cmp_item_datetime(warn_item);
3473
bool Item_func_in::nulls_in_row()
3475
Item **arg,**arg_end;
3476
for (arg= args+1, arg_end= args+arg_count; arg != arg_end ; arg++)
3478
if ((*arg)->null_inside())
3486
Perform context analysis of an IN item tree.
3488
This function performs context analysis (name resolution) and calculates
3489
various attributes of the item tree with Item_func_in as its root.
3490
The function saves in ref the pointer to the item or to a newly created
3491
item that is considered as a replacement for the original one.
3493
@param thd reference to the global context of the query thread
3494
@param ref pointer to Item* variable where pointer to resulting "fixed"
3495
item is to be assigned
3498
Let T0(e)/T1(e) be the value of not_null_tables(e) when e is used on
3499
a predicate/function level. Then it's easy to show that:
3501
T0(e IN(e1,...,en)) = union(T1(e),intersection(T1(ei)))
3502
T1(e IN(e1,...,en)) = union(T1(e),intersection(T1(ei)))
3503
T0(e NOT IN(e1,...,en)) = union(T1(e),union(T1(ei)))
3504
T1(e NOT IN(e1,...,en)) = union(T1(e),intersection(T1(ei)))
3514
Item_func_in::fix_fields(THD *thd, Item **ref)
3516
Item **arg, **arg_end;
3518
if (Item_func_opt_neg::fix_fields(thd, ref))
3521
/* not_null_tables_cache == union(T1(e),union(T1(ei))) */
3522
if (pred_level && negated)
3525
/* not_null_tables_cache = union(T1(e),intersection(T1(ei))) */
3526
not_null_tables_cache= ~(table_map) 0;
3527
for (arg= args + 1, arg_end= args + arg_count; arg != arg_end; arg++)
3528
not_null_tables_cache&= (*arg)->not_null_tables();
3529
not_null_tables_cache|= (*args)->not_null_tables();
3534
static int srtcmp_in(CHARSET_INFO *cs, const String *x,const String *y)
3536
return cs->coll->strnncollsp(cs,
3537
(uchar *) x->ptr(),x->length(),
3538
(uchar *) y->ptr(),y->length(), 0);
3542
void Item_func_in::fix_length_and_dec()
3544
Item **arg, **arg_end;
3546
THD *thd= current_thd;
3547
bool datetime_found= FALSE;
3548
/* TRUE <=> arguments values will be compared as DATETIMEs. */
3549
bool compare_as_datetime= FALSE;
3551
uint found_types= 0;
3552
uint type_cnt= 0, i;
3553
Item_result cmp_type= STRING_RESULT;
3554
left_result_type= args[0]->result_type();
3555
if (!(found_types= collect_cmp_types(args, arg_count)))
3558
for (arg= args + 1, arg_end= args + arg_count; arg != arg_end ; arg++)
3560
if (!arg[0]->const_item())
3566
for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
3568
if (found_types & 1 << i)
3571
cmp_type= (Item_result) i;
3577
if (cmp_type == STRING_RESULT &&
3578
agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1))
3580
arg_types_compatible= TRUE;
3585
When comparing rows create the row comparator object beforehand to ease
3586
the DATETIME comparison detection procedure.
3588
if (cmp_type == ROW_RESULT)
3590
cmp_item_row *cmp= 0;
3591
if (const_itm && !nulls_in_row())
3593
array= new in_row(arg_count-1, 0);
3594
cmp= &((in_row*)array)->tmp;
3598
if (!(cmp= new cmp_item_row))
3600
cmp_items[ROW_RESULT]= cmp;
3602
cmp->n= args[0]->cols();
3603
cmp->alloc_comparators();
3605
/* All DATE/DATETIME fields/functions has the STRING result type. */
3606
if (cmp_type == STRING_RESULT || cmp_type == ROW_RESULT)
3608
uint col, cols= args[0]->cols();
3610
for (col= 0; col < cols; col++)
3612
bool skip_column= FALSE;
3614
Check that all items to be compared has the STRING result type and at
3615
least one of them is a DATE/DATETIME item.
3617
for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
3619
Item *itm= ((cmp_type == STRING_RESULT) ? arg[0] :
3620
arg[0]->element_index(col));
3621
if (itm->result_type() != STRING_RESULT)
3626
else if (itm->is_datetime())
3628
datetime_found= TRUE;
3630
Internally all DATE/DATETIME values are converted to the DATETIME
3631
type. So try to find a DATETIME item to issue correct warnings.
3635
else if (itm->field_type() == MYSQL_TYPE_DATETIME)
3638
/* All arguments are already checked to have the STRING result. */
3639
if (cmp_type == STRING_RESULT)
3648
if (cmp_type == ROW_RESULT)
3652
cmp= ((in_row*)array)->tmp.comparators + col;
3654
cmp= ((cmp_item_row*)cmp_items[ROW_RESULT])->comparators + col;
3655
*cmp= new cmp_item_datetime(date_arg);
3656
/* Reset variables for the next column. */
3658
datetime_found= FALSE;
3661
compare_as_datetime= TRUE;
3667
Row item with NULLs inside can return NULL or FALSE =>
3668
they can't be processed as static
3670
if (type_cnt == 1 && const_itm && !nulls_in_row())
3672
if (compare_as_datetime)
3673
array= new in_datetime(date_arg, arg_count - 1);
3677
IN must compare INT columns and constants as int values (the same
3678
way as equality does).
3679
So we must check here if the column on the left and all the constant
3680
values on the right can be compared as integers and adjust the
3681
comparison type accordingly.
3683
if (args[0]->real_item()->type() == FIELD_ITEM &&
3684
thd->lex->sql_command != SQLCOM_SHOW_CREATE &&
3685
cmp_type != INT_RESULT)
3687
Item_field *field_item= (Item_field*) (args[0]->real_item());
3688
if (field_item->field->can_be_compared_as_longlong())
3690
bool all_converted= TRUE;
3691
for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3693
if (!convert_constant_item (thd, field_item, &arg[0]))
3694
all_converted= FALSE;
3697
cmp_type= INT_RESULT;
3702
array=new in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
3703
cmp_collation.collation);
3706
array= new in_longlong(arg_count-1);
3709
array= new in_double(arg_count-1);
3713
The row comparator was created at the beginning but only DATETIME
3714
items comparators were initialized. Call store_value() to setup
3717
((in_row*)array)->tmp.store_value(args[0]);
3719
case DECIMAL_RESULT:
3720
array= new in_decimal(arg_count - 1);
3727
if (array && !(thd->is_fatal_error)) // If not EOM
3730
for (uint i=1 ; i < arg_count ; i++)
3732
array->set(j,args[i]);
3733
if (!args[i]->null_value) // Skip NULL values
3738
if ((array->used_count= j))
3744
if (compare_as_datetime)
3745
cmp_items[STRING_RESULT]= new cmp_item_datetime(date_arg);
3748
for (i= 0; i <= (uint) DECIMAL_RESULT; i++)
3750
if (found_types & (1 << i) && !cmp_items[i])
3752
if ((Item_result)i == STRING_RESULT &&
3753
agg_arg_charsets(cmp_collation, args, arg_count,
3754
MY_COLL_CMP_CONV, 1))
3756
if (!cmp_items[i] && !(cmp_items[i]=
3757
cmp_item::get_comparator((Item_result)i,
3758
cmp_collation.collation)))
3768
void Item_func_in::print(String *str, enum_query_type query_type)
3771
args[0]->print(str, query_type);
3773
str->append(STRING_WITH_LEN(" not"));
3774
str->append(STRING_WITH_LEN(" in ("));
3775
print_args(str, 1, query_type);
3776
str->append(STRING_WITH_LEN("))"));
3781
Evaluate the function and return its value.
3787
Evaluate the function and return its value.
3790
If the array object is defined then the value of the function is
3791
calculated by means of this array.
3792
Otherwise several cmp_item objects are used in order to do correct
3793
comparison of left expression and an expression from the values list.
3794
One cmp_item object correspond to one used comparison type. Left
3795
expression can be evaluated up to number of different used comparison
3796
types. A bit mapped variable value_added_map is used to check whether
3797
the left expression already was evaluated for a particular result type.
3798
Result types are mapped to it according to their integer values i.e.
3799
STRING_RESULT is mapped to bit 0, REAL_RESULT to bit 1, so on.
3802
Value of the function
3805
longlong Item_func_in::val_int()
3808
DBUG_ASSERT(fixed == 1);
3809
uint value_added_map= 0;
3812
int tmp=array->find(args[0]);
3813
null_value=args[0]->null_value || (!tmp && have_null);
3814
return (longlong) (!null_value && tmp != negated);
3817
for (uint i= 1 ; i < arg_count ; i++)
3819
Item_result cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
3820
in_item= cmp_items[(uint)cmp_type];
3821
DBUG_ASSERT(in_item);
3822
if (!(value_added_map & (1 << (uint)cmp_type)))
3824
in_item->store_value(args[0]);
3825
if ((null_value=args[0]->null_value))
3828
value_added_map|= 1 << (uint)cmp_type;
3830
if (!in_item->cmp(args[i]) && !args[i]->null_value)
3831
return (longlong) (!negated);
3832
have_null|= args[i]->null_value;
3835
null_value= have_null;
3836
return (longlong) (!null_value && negated);
3840
longlong Item_func_bit_or::val_int()
3842
DBUG_ASSERT(fixed == 1);
3843
ulonglong arg1= (ulonglong) args[0]->val_int();
3844
if (args[0]->null_value)
3846
null_value=1; /* purecov: inspected */
3847
return 0; /* purecov: inspected */
3849
ulonglong arg2= (ulonglong) args[1]->val_int();
3850
if (args[1]->null_value)
3856
return (longlong) (arg1 | arg2);
3860
longlong Item_func_bit_and::val_int()
3862
DBUG_ASSERT(fixed == 1);
3863
ulonglong arg1= (ulonglong) args[0]->val_int();
3864
if (args[0]->null_value)
3866
null_value=1; /* purecov: inspected */
3867
return 0; /* purecov: inspected */
3869
ulonglong arg2= (ulonglong) args[1]->val_int();
3870
if (args[1]->null_value)
3872
null_value=1; /* purecov: inspected */
3873
return 0; /* purecov: inspected */
3876
return (longlong) (arg1 & arg2);
3879
Item_cond::Item_cond(THD *thd, Item_cond *item)
3880
:Item_bool_func(thd, item),
3881
abort_on_null(item->abort_on_null),
3882
and_tables_cache(item->and_tables_cache)
3885
item->list will be copied by copy_andor_arguments() call
3890
void Item_cond::copy_andor_arguments(THD *thd, Item_cond *item)
3892
List_iterator_fast<Item> li(item->list);
3893
while (Item *it= li++)
3894
list.push_back(it->copy_andor_structure(thd));
3899
Item_cond::fix_fields(THD *thd, Item **ref)
3901
DBUG_ASSERT(fixed == 0);
3902
List_iterator<Item> li(list);
3904
void *orig_thd_marker= thd->thd_marker;
3905
uchar buff[sizeof(char*)]; // Max local vars in function
3906
not_null_tables_cache= used_tables_cache= 0;
3907
const_item_cache= 1;
3909
if (functype() == COND_OR_FUNC)
3912
and_table_cache is the value that Item_cond_or() returns for
3915
and_tables_cache= ~(table_map) 0;
3917
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
3918
return TRUE; // Fatal error flag is set!
3920
The following optimization reduces the depth of an AND-OR tree.
3921
E.g. a WHERE clause like
3922
F1 AND (F2 AND (F2 AND F4))
3923
is parsed into a tree with the same nested structure as defined
3924
by braces. This optimization will transform such tree into
3925
AND (F1, F2, F3, F4).
3926
Trees of OR items are flattened as well:
3927
((F1 OR F2) OR (F3 OR F4)) => OR (F1, F2, F3, F4)
3928
Items for removed AND/OR levels will dangle until the death of the
3930
The optimization is currently prepared statements and stored procedures
3931
friendly as it doesn't allocate any memory and its effects are durable
3932
(i.e. do not depend on PS/SP arguments).
3936
table_map tmp_table_map;
3937
while (item->type() == Item::COND_ITEM &&
3938
((Item_cond*) item)->functype() == functype() &&
3939
!((Item_cond*) item)->list.is_empty())
3940
{ // Identical function
3941
li.replace(((Item_cond*) item)->list);
3942
((Item_cond*) item)->list.empty();
3943
item= *li.ref(); // new current item
3946
item->top_level_item();
3948
// item can be substituted in fix_fields
3949
if ((!item->fixed &&
3950
item->fix_fields(thd, li.ref())) ||
3951
(item= *li.ref())->check_cols(1))
3952
return TRUE; /* purecov: inspected */
3953
used_tables_cache|= item->used_tables();
3954
if (item->const_item())
3955
and_tables_cache= (table_map) 0;
3958
tmp_table_map= item->not_null_tables();
3959
not_null_tables_cache|= tmp_table_map;
3960
and_tables_cache&= tmp_table_map;
3961
const_item_cache= FALSE;
3963
with_sum_func= with_sum_func || item->with_sum_func;
3964
with_subselect|= item->with_subselect;
3965
if (item->maybe_null)
3968
thd->lex->current_select->cond_count+= list.elements;
3969
thd->thd_marker= orig_thd_marker;
3970
fix_length_and_dec();
3976
void Item_cond::fix_after_pullout(st_select_lex *new_parent, Item **ref)
3978
List_iterator<Item> li(list);
3981
used_tables_cache=0;
3984
and_tables_cache= ~(table_map) 0; // Here and below we do as fix_fields does
3985
not_null_tables_cache= 0;
3989
table_map tmp_table_map;
3990
item->fix_after_pullout(new_parent, li.ref());
3992
used_tables_cache|= item->used_tables();
3993
const_item_cache&= item->const_item();
3995
if (item->const_item())
3996
and_tables_cache= (table_map) 0;
3999
tmp_table_map= item->not_null_tables();
4000
not_null_tables_cache|= tmp_table_map;
4001
and_tables_cache&= tmp_table_map;
4002
const_item_cache= FALSE;
4008
bool Item_cond::walk(Item_processor processor, bool walk_subquery, uchar *arg)
4010
List_iterator_fast<Item> li(list);
4012
while ((item= li++))
4013
if (item->walk(processor, walk_subquery, arg))
4015
return Item_func::walk(processor, walk_subquery, arg);
4020
Transform an Item_cond object with a transformer callback function.
4022
The function recursively applies the transform method to each
4023
member item of the condition list.
4024
If the call of the method for a member item returns a new item
4025
the old item is substituted for a new one.
4026
After this the transformer is applied to the root node
4027
of the Item_cond object.
4029
@param transformer the transformer callback function to be applied to
4030
the nodes of the tree of the object
4031
@param arg parameter to be passed to the transformer
4034
Item returned as the result of transformation of the root node
4037
Item *Item_cond::transform(Item_transformer transformer, uchar *arg)
4039
List_iterator<Item> li(list);
4041
while ((item= li++))
4043
Item *new_item= item->transform(transformer, arg);
4048
THD::change_item_tree() should be called only if the tree was
4049
really transformed, i.e. when a new item has been created.
4050
Otherwise we'll be allocating a lot of unnecessary memory for
4051
change records at each execution.
4053
if (new_item != item)
4054
current_thd->change_item_tree(li.ref(), new_item);
4056
return Item_func::transform(transformer, arg);
4061
Compile Item_cond object with a processor and a transformer
4064
First the function applies the analyzer to the root node of
4065
the Item_func object. Then if the analyzer succeeeds (returns TRUE)
4066
the function recursively applies the compile method to member
4067
item of the condition list.
4068
If the call of the method for a member item returns a new item
4069
the old item is substituted for a new one.
4070
After this the transformer is applied to the root node
4071
of the Item_cond object.
4073
@param analyzer the analyzer callback function to be applied to the
4074
nodes of the tree of the object
4075
@param[in,out] arg_p parameter to be passed to the analyzer
4076
@param transformer the transformer callback function to be applied to the
4077
nodes of the tree of the object
4078
@param arg_t parameter to be passed to the transformer
4081
Item returned as the result of transformation of the root node
4084
Item *Item_cond::compile(Item_analyzer analyzer, uchar **arg_p,
4085
Item_transformer transformer, uchar *arg_t)
4087
if (!(this->*analyzer)(arg_p))
4090
List_iterator<Item> li(list);
4092
while ((item= li++))
4095
The same parameter value of arg_p must be passed
4096
to analyze any argument of the condition formula.
4098
uchar *arg_v= *arg_p;
4099
Item *new_item= item->compile(analyzer, &arg_v, transformer, arg_t);
4100
if (new_item && new_item != item)
4101
li.replace(new_item);
4103
return Item_func::transform(transformer, arg_t);
4106
void Item_cond::traverse_cond(Cond_traverser traverser,
4107
void *arg, traverse_order order)
4109
List_iterator<Item> li(list);
4114
(*traverser)(this, arg);
4115
while ((item= li++))
4117
item->traverse_cond(traverser, arg, order);
4119
(*traverser)(NULL, arg);
4122
while ((item= li++))
4124
item->traverse_cond(traverser, arg, order);
4126
(*traverser)(this, arg);
4131
Move SUM items out from item tree and replace with reference.
4133
The split is done to get an unique item for each SUM function
4134
so that we can easily find and calculate them.
4135
(Calculation done by update_sum_func() and copy_sum_funcs() in
4138
@param thd Thread handler
4139
@param ref_pointer_array Pointer to array of reference fields
4140
@param fields All fields in select
4143
This function is run on all expression (SELECT list, WHERE, HAVING etc)
4144
that have or refer (HAVING) to a SUM expression.
4147
void Item_cond::split_sum_func(THD *thd, Item **ref_pointer_array,
4150
List_iterator<Item> li(list);
4152
while ((item= li++))
4153
item->split_sum_func2(thd, ref_pointer_array, fields, li.ref(), TRUE);
4158
Item_cond::used_tables() const
4159
{ // This caches used_tables
4160
return used_tables_cache;
4164
void Item_cond::update_used_tables()
4166
List_iterator_fast<Item> li(list);
4169
used_tables_cache=0;
4173
item->update_used_tables();
4174
used_tables_cache|= item->used_tables();
4175
const_item_cache&= item->const_item();
4180
void Item_cond::print(String *str, enum_query_type query_type)
4183
List_iterator_fast<Item> li(list);
4186
item->print(str, query_type);
4190
str->append(func_name());
4192
item->print(str, query_type);
4198
void Item_cond::neg_arguments(THD *thd)
4200
List_iterator<Item> li(list);
4202
while ((item= li++)) /* Apply not transformation to the arguments */
4204
Item *new_item= item->neg_transformer(thd);
4207
if (!(new_item= new Item_func_not(item)))
4208
return; // Fatal OEM error
4210
VOID(li.replace(new_item));
4216
Evaluation of AND(expr, expr, expr ...).
4219
abort_if_null is set for AND expressions for which we don't care if the
4220
result is NULL or 0. This is set for:
4226
1 If all expressions are true
4228
0 If all expressions are false or if we find a NULL expression and
4229
'abort_on_null' is set.
4231
NULL if all expression are either 1 or NULL
4235
longlong Item_cond_and::val_int()
4237
DBUG_ASSERT(fixed == 1);
4238
List_iterator_fast<Item> li(list);
4243
if (!item->val_bool())
4245
if (abort_on_null || !(null_value= item->null_value))
4246
return 0; // return FALSE
4249
return null_value ? 0 : 1;
4253
longlong Item_cond_or::val_int()
4255
DBUG_ASSERT(fixed == 1);
4256
List_iterator_fast<Item> li(list);
4261
if (item->val_bool())
4266
if (item->null_value)
4273
Create an AND expression from two expressions.
4275
@param a expression or NULL
4276
@param b expression.
4277
@param org_item Don't modify a if a == *org_item.
4278
If a == NULL, org_item is set to point at b,
4279
to ensure that future calls will not modify b.
4282
This will not modify item pointed to by org_item or b
4283
The idea is that one can call this in a loop and create and
4284
'and' over all items without modifying any of the original items.
4292
Item *and_expressions(Item *a, Item *b, Item **org_item)
4295
return (*org_item= (Item*) b);
4299
if ((res= new Item_cond_and(a, (Item*) b)))
4301
res->used_tables_cache= a->used_tables() | b->used_tables();
4302
res->not_null_tables_cache= a->not_null_tables() | b->not_null_tables();
4306
if (((Item_cond_and*) a)->add((Item*) b))
4308
((Item_cond_and*) a)->used_tables_cache|= b->used_tables();
4309
((Item_cond_and*) a)->not_null_tables_cache|= b->not_null_tables();
4314
longlong Item_func_isnull::val_int()
4316
DBUG_ASSERT(fixed == 1);
4318
Handle optimization if the argument can't be null
4319
This has to be here because of the test in update_used_tables().
4321
if (!used_tables_cache && !with_subselect)
4322
return cached_value;
4323
return args[0]->is_null() ? 1: 0;
4326
longlong Item_is_not_null_test::val_int()
4328
DBUG_ASSERT(fixed == 1);
4329
DBUG_ENTER("Item_is_not_null_test::val_int");
4330
if (!used_tables_cache && !with_subselect)
4332
owner->was_null|= (!cached_value);
4333
DBUG_PRINT("info", ("cached: %ld", (long) cached_value));
4334
DBUG_RETURN(cached_value);
4336
if (args[0]->is_null())
4338
DBUG_PRINT("info", ("null"));
4339
owner->was_null|= 1;
4347
Optimize case of not_null_column IS NULL.
4349
void Item_is_not_null_test::update_used_tables()
4351
if (!args[0]->maybe_null)
4353
used_tables_cache= 0; /* is always true */
4354
cached_value= (longlong) 1;
4358
args[0]->update_used_tables();
4359
if (!(used_tables_cache=args[0]->used_tables()) && !with_subselect)
4361
/* Remember if the value is always NULL or never NULL */
4362
cached_value= (longlong) !args[0]->is_null();
4368
longlong Item_func_isnotnull::val_int()
4370
DBUG_ASSERT(fixed == 1);
4371
return args[0]->is_null() ? 0 : 1;
4375
void Item_func_isnotnull::print(String *str, enum_query_type query_type)
4378
args[0]->print(str, query_type);
4379
str->append(STRING_WITH_LEN(" is not null)"));
4383
longlong Item_func_like::val_int()
4385
DBUG_ASSERT(fixed == 1);
4386
String* res = args[0]->val_str(&tmp_value1);
4387
if (args[0]->null_value)
4392
String* res2 = args[1]->val_str(&tmp_value2);
4393
if (args[1]->null_value)
4400
return turboBM_matches(res->ptr(), res->length()) ? 1 : 0;
4401
return my_wildcmp(cmp.cmp_collation.collation,
4402
res->ptr(),res->ptr()+res->length(),
4403
res2->ptr(),res2->ptr()+res2->length(),
4404
escape,wild_one,wild_many) ? 0 : 1;
4409
We can optimize a where if first character isn't a wildcard
4412
Item_func::optimize_type Item_func_like::select_optimize() const
4414
if (args[1]->const_item())
4416
String* res2= args[1]->val_str((String *)&tmp_value2);
4419
return OPTIMIZE_NONE;
4421
if (*res2->ptr() != wild_many)
4423
if (args[0]->result_type() != STRING_RESULT || *res2->ptr() != wild_one)
4427
return OPTIMIZE_NONE;
4431
bool Item_func_like::fix_fields(THD *thd, Item **ref)
4433
DBUG_ASSERT(fixed == 0);
4434
if (Item_bool_func2::fix_fields(thd, ref) ||
4435
escape_item->fix_fields(thd, &escape_item))
4438
if (!escape_item->const_during_execution())
4440
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4444
if (escape_item->const_item())
4446
/* If we are on execution stage */
4447
String *escape_str= escape_item->val_str(&tmp_value1);
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)))
4455
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4459
if (use_mb(cmp.cmp_collation.collation))
4461
CHARSET_INFO *cs= escape_str->charset();
4463
int rc= cs->cset->mb_wc(cs, &wc,
4464
(const uchar*) escape_str->ptr(),
4465
(const uchar*) escape_str->ptr() +
4466
escape_str->length());
4467
escape= (int) (rc > 0 ? wc : '\\');
4472
In the case of 8bit character set, we pass native
4473
code instead of Unicode code as "escape" argument.
4474
Convert to "cs" if charset of escape differs.
4476
CHARSET_INFO *cs= cmp.cmp_collation.collation;
4478
if (escape_str->needs_conversion(escape_str->length(),
4479
escape_str->charset(), cs, &unused))
4483
uint32 cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
4484
escape_str->length(),
4485
escape_str->charset(), &errors);
4486
escape= cnvlen ? ch : '\\';
4489
escape= *(escape_str->ptr());
4496
We could also do boyer-more for non-const items, but as we would have to
4497
recompute the tables for each row it's not worth it.
4499
if (args[1]->const_item() && !use_strnxfrm(collation.collation) &&
4500
!(specialflag & SPECIAL_NO_NEW_FUNC))
4502
String* res2 = args[1]->val_str(&tmp_value2);
4504
return FALSE; // Null argument
4506
const size_t len = res2->length();
4507
const char* first = res2->ptr();
4508
const char* last = first + len - 1;
4510
len must be > 2 ('%pattern%')
4511
heuristic: only do TurboBM for pattern_len > 2
4514
if (len > MIN_TURBOBM_PATTERN_LEN + 2 &&
4515
*first == wild_many &&
4518
const char* tmp = first + 1;
4519
for (; *tmp != wild_many && *tmp != wild_one && *tmp != escape; tmp++) ;
4520
canDoTurboBM = (tmp == last) && !use_mb(args[0]->collation.collation);
4524
pattern = first + 1;
4525
pattern_len = (int) len - 2;
4526
DBUG_PRINT("info", ("Initializing pattern: '%s'", first));
4527
int *suff = (int*) thd->alloc((int) (sizeof(int)*
4528
((pattern_len + 1)*2+
4530
bmGs = suff + pattern_len + 1;
4531
bmBc = bmGs + pattern_len + 1;
4532
turboBM_compute_good_suffix_shifts(suff);
4533
turboBM_compute_bad_character_shifts();
4534
DBUG_PRINT("info",("done"));
4541
void Item_func_like::cleanup()
4543
canDoTurboBM= FALSE;
4544
Item_bool_func2::cleanup();
4547
#ifdef LIKE_CMP_TOUPPER
4548
#define likeconv(cs,A) (uchar) (cs)->toupper(A)
4550
#define likeconv(cs,A) (uchar) (cs)->sort_order[(uchar) (A)]
4555
Precomputation dependent only on pattern_len.
4558
void Item_func_like::turboBM_compute_suffixes(int *suff)
4560
const int plm1 = pattern_len - 1;
4563
int *const splm1 = suff + plm1;
4564
CHARSET_INFO *cs= cmp.cmp_collation.collation;
4566
*splm1 = pattern_len;
4568
if (!cs->sort_order)
4571
for (i = pattern_len - 2; i >= 0; i--)
4573
int tmp = *(splm1 + i - f);
4574
if (g < i && tmp < i - g)
4579
g = i; // g = min(i, g)
4581
while (g >= 0 && pattern[g] == pattern[g + plm1 - f])
4590
for (i = pattern_len - 2; 0 <= i; --i)
4592
int tmp = *(splm1 + i - f);
4593
if (g < i && tmp < i - g)
4598
g = i; // g = min(i, g)
4601
likeconv(cs, pattern[g]) == likeconv(cs, pattern[g + plm1 - f]))
4611
Precomputation dependent only on pattern_len.
4614
void Item_func_like::turboBM_compute_good_suffix_shifts(int *suff)
4616
turboBM_compute_suffixes(suff);
4618
int *end = bmGs + pattern_len;
4620
for (k = bmGs; k < end; k++)
4626
const int plm1 = pattern_len - 1;
4627
for (i = plm1; i > -1; i--)
4629
if (suff[i] == i + 1)
4631
for (tmp = plm1 - i; j < tmp; j++)
4633
int *tmp2 = bmGs + j;
4634
if (*tmp2 == pattern_len)
4641
for (tmp = plm1 - i; j < tmp; j++)
4644
if (*tmp2 == pattern_len)
4649
for (i = 0; i <= pattern_len - 2; i++)
4650
*(tmp2 - suff[i]) = plm1 - i;
4655
Precomputation dependent on pattern_len.
4658
void Item_func_like::turboBM_compute_bad_character_shifts()
4661
int *end = bmBc + alphabet_size;
4663
const int plm1 = pattern_len - 1;
4664
CHARSET_INFO *cs= cmp.cmp_collation.collation;
4666
for (i = bmBc; i < end; i++)
4669
if (!cs->sort_order)
4671
for (j = 0; j < plm1; j++)
4672
bmBc[(uint) (uchar) pattern[j]] = plm1 - j;
4676
for (j = 0; j < plm1; j++)
4677
bmBc[(uint) likeconv(cs,pattern[j])] = plm1 - j;
4683
Search for pattern in text.
4686
returns true/false for match/no match
4689
bool Item_func_like::turboBM_matches(const char* text, int text_len) const
4691
register int bcShift;
4692
register int turboShift;
4693
int shift = pattern_len;
4696
CHARSET_INFO *cs= cmp.cmp_collation.collation;
4698
const int plm1= pattern_len - 1;
4699
const int tlmpl= text_len - pattern_len;
4702
if (!cs->sort_order)
4706
register int i= plm1;
4707
while (i >= 0 && pattern[i] == text[i + j])
4710
if (i == plm1 - shift)
4716
register const int v = plm1 - i;
4718
bcShift = bmBc[(uint) (uchar) text[i + j]] - plm1 + i;
4719
shift = max(turboShift, bcShift);
4720
shift = max(shift, bmGs[i]);
4721
if (shift == bmGs[i])
4722
u = min(pattern_len - shift, v);
4725
if (turboShift < bcShift)
4726
shift = max(shift, u + 1);
4737
register int i = plm1;
4738
while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
4741
if (i == plm1 - shift)
4747
register const int v = plm1 - i;
4749
bcShift = bmBc[(uint) likeconv(cs, text[i + j])] - plm1 + i;
4750
shift = max(turboShift, bcShift);
4751
shift = max(shift, bmGs[i]);
4752
if (shift == bmGs[i])
4753
u = min(pattern_len - shift, v);
4756
if (turboShift < bcShift)
4757
shift = max(shift, u + 1);
4768
Make a logical XOR of the arguments.
4770
If either operator is NULL, return NULL.
4773
(low priority) Change this to be optimized as: @n
4774
A XOR B -> (A) == 1 AND (B) <> 1) OR (A <> 1 AND (B) == 1) @n
4775
To be able to do this, we would however first have to extend the MySQL
4776
range optimizer to handle OR better.
4779
As we don't do any index optimization on XOR this is not going to be
4783
longlong Item_cond_xor::val_int()
4785
DBUG_ASSERT(fixed == 1);
4786
List_iterator<Item> li(list);
4792
result^= (item->val_int() != 0);
4793
if (item->null_value)
4799
return (longlong) result;
4803
Apply NOT transformation to the item and return a new one.
4806
Transform the item using next rules:
4808
a AND b AND ... -> NOT(a) OR NOT(b) OR ...
4809
a OR b OR ... -> NOT(a) AND NOT(b) AND ...
4817
IS NULL(a) -> IS NOT NULL(a)
4818
IS NOT NULL(a) -> IS NULL(a)
4821
@param thd thread handler
4825
NULL if we cannot apply NOT transformation (see Item::neg_transformer()).
4828
Item *Item_func_not::neg_transformer(THD *thd) /* NOT(x) -> x */
4834
Item *Item_bool_rowready_func2::neg_transformer(THD *thd)
4836
Item *item= negated_item();
4842
a IS NULL -> a IS NOT NULL.
4844
Item *Item_func_isnull::neg_transformer(THD *thd)
4846
Item *item= new Item_func_isnotnull(args[0]);
4852
a IS NOT NULL -> a IS NULL.
4854
Item *Item_func_isnotnull::neg_transformer(THD *thd)
4856
Item *item= new Item_func_isnull(args[0]);
4861
Item *Item_cond_and::neg_transformer(THD *thd) /* NOT(a AND b AND ...) -> */
4862
/* NOT a OR NOT b OR ... */
4865
Item *item= new Item_cond_or(list);
4870
Item *Item_cond_or::neg_transformer(THD *thd) /* NOT(a OR b OR ...) -> */
4871
/* NOT a AND NOT b AND ... */
4874
Item *item= new Item_cond_and(list);
4879
Item *Item_func_nop_all::neg_transformer(THD *thd)
4881
/* "NOT (e $cmp$ ANY (SELECT ...)) -> e $rev_cmp$" ALL (SELECT ...) */
4882
Item_func_not_all *new_item= new Item_func_not_all(args[0]);
4883
Item_allany_subselect *allany= (Item_allany_subselect*)args[0];
4884
allany->func= allany->func_creator(FALSE);
4885
allany->all= !allany->all;
4886
allany->upper_item= new_item;
4890
Item *Item_func_not_all::neg_transformer(THD *thd)
4892
/* "NOT (e $cmp$ ALL (SELECT ...)) -> e $rev_cmp$" ANY (SELECT ...) */
4893
Item_func_nop_all *new_item= new Item_func_nop_all(args[0]);
4894
Item_allany_subselect *allany= (Item_allany_subselect*)args[0];
4895
allany->all= !allany->all;
4896
allany->func= allany->func_creator(TRUE);
4897
allany->upper_item= new_item;
4901
Item *Item_func_eq::negated_item() /* a = b -> a != b */
4903
return new Item_func_ne(args[0], args[1]);
4907
Item *Item_func_ne::negated_item() /* a != b -> a = b */
4909
return new Item_func_eq(args[0], args[1]);
4913
Item *Item_func_lt::negated_item() /* a < b -> a >= b */
4915
return new Item_func_ge(args[0], args[1]);
4919
Item *Item_func_ge::negated_item() /* a >= b -> a < b */
4921
return new Item_func_lt(args[0], args[1]);
4925
Item *Item_func_gt::negated_item() /* a > b -> a <= b */
4927
return new Item_func_le(args[0], args[1]);
4931
Item *Item_func_le::negated_item() /* a <= b -> a > b */
4933
return new Item_func_gt(args[0], args[1]);
4937
just fake method, should never be called.
4939
Item *Item_bool_rowready_func2::negated_item()
4945
Item_equal::Item_equal(Item_field *f1, Item_field *f2)
4946
: Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
4948
const_item_cache= 0;
4949
fields.push_back(f1);
4950
fields.push_back(f2);
4953
Item_equal::Item_equal(Item *c, Item_field *f)
4954
: Item_bool_func(), eval_item(0), cond_false(0)
4956
const_item_cache= 0;
4957
fields.push_back(f);
4962
Item_equal::Item_equal(Item_equal *item_equal)
4963
: Item_bool_func(), eval_item(0), cond_false(0)
4965
const_item_cache= 0;
4966
List_iterator_fast<Item_field> li(item_equal->fields);
4968
while ((item= li++))
4970
fields.push_back(item);
4972
const_item= item_equal->const_item;
4973
cond_false= item_equal->cond_false;
4976
void Item_equal::add(Item *c)
4985
Item_func_eq *func= new Item_func_eq(c, const_item);
4986
func->set_cmp_func();
4987
func->quick_fix_field();
4988
if ((cond_false= !func->val_int()))
4989
const_item_cache= 1;
4992
void Item_equal::add(Item_field *f)
4994
fields.push_back(f);
4997
uint Item_equal::members()
4999
return fields.elements;
5004
Check whether a field is referred in the multiple equality.
5006
The function checks whether field is occurred in the Item_equal object .
5008
@param field field whose occurrence is to be checked
5011
1 if nultiple equality contains a reference to field
5016
bool Item_equal::contains(Field *field)
5018
List_iterator_fast<Item_field> it(fields);
5020
while ((item= it++))
5022
if (field->eq(item->field))
5030
Join members of another Item_equal object.
5032
The function actually merges two multiple equalities.
5033
After this operation the Item_equal object additionally contains
5034
the field items of another item of the type Item_equal.
5035
If the optional constant items are not equal the cond_false flag is
5037
@param item multiple equality whose members are to be joined
5040
void Item_equal::merge(Item_equal *item)
5042
fields.concat(&item->fields);
5043
Item *c= item->const_item;
5047
The flag cond_false will be set to 1 after this, if
5048
the multiple equality already contains a constant and its
5049
value is not equal to the value of c.
5053
cond_false|= item->cond_false;
5058
Order field items in multiple equality according to a sorting criteria.
5060
The function perform ordering of the field items in the Item_equal
5061
object according to the criteria determined by the cmp callback parameter.
5062
If cmp(item_field1,item_field2,arg)<0 than item_field1 must be
5063
placed after item_fiel2.
5065
The function sorts field items by the exchange sort algorithm.
5066
The list of field items is looked through and whenever two neighboring
5067
members follow in a wrong order they are swapped. This is performed
5068
again and again until we get all members in a right order.
5070
@param cmp function to compare field item
5071
@param arg context extra parameter for the cmp function
5074
void Item_equal::sort(Item_field_cmpfunc cmp, void *arg)
5077
List_iterator<Item_field> it(fields);
5080
Item_field *item1= it++;
5081
Item_field **ref1= it.ref();
5085
while ((item2= it++))
5087
Item_field **ref2= it.ref();
5088
if (cmp(item1, item2, arg) < 0)
5090
Item_field *item= *ref1;
5107
Check appearance of new constant items in the multiple equality object.
5109
The function checks appearance of new constant items among
5110
the members of multiple equalities. Each new constant item is
5111
compared with the designated constant item if there is any in the
5112
multiple equality. If there is none the first new constant item
5116
void Item_equal::update_const()
5118
List_iterator<Item_field> it(fields);
5120
while ((item= it++))
5122
if (item->const_item())
5130
bool Item_equal::fix_fields(THD *thd, Item **ref)
5132
List_iterator_fast<Item_field> li(fields);
5134
not_null_tables_cache= used_tables_cache= 0;
5135
const_item_cache= 0;
5136
while ((item= li++))
5138
table_map tmp_table_map;
5139
used_tables_cache|= item->used_tables();
5140
tmp_table_map= item->not_null_tables();
5141
not_null_tables_cache|= tmp_table_map;
5142
if (item->maybe_null)
5145
fix_length_and_dec();
5150
void Item_equal::update_used_tables()
5152
List_iterator_fast<Item_field> li(fields);
5154
not_null_tables_cache= used_tables_cache= 0;
5155
if ((const_item_cache= cond_false))
5159
item->update_used_tables();
5160
used_tables_cache|= item->used_tables();
5161
const_item_cache&= item->const_item();
5165
longlong Item_equal::val_int()
5167
Item_field *item_field;
5170
List_iterator_fast<Item_field> it(fields);
5171
Item *item= const_item ? const_item : it++;
5172
if ((null_value= item->null_value))
5174
eval_item->store_value(item);
5175
while ((item_field= it++))
5177
/* Skip fields of non-const tables. They haven't been read yet */
5178
if (item_field->field->table->const_table)
5180
if ((null_value= item_field->null_value) || eval_item->cmp(item_field))
5187
void Item_equal::fix_length_and_dec()
5189
Item *item= get_first();
5190
eval_item= cmp_item::get_comparator(item->result_type(),
5191
item->collation.collation);
5194
bool Item_equal::walk(Item_processor processor, bool walk_subquery, uchar *arg)
5196
List_iterator_fast<Item_field> it(fields);
5198
while ((item= it++))
5200
if (item->walk(processor, walk_subquery, arg))
5203
return Item_func::walk(processor, walk_subquery, arg);
5206
Item *Item_equal::transform(Item_transformer transformer, uchar *arg)
5208
List_iterator<Item_field> it(fields);
5210
while ((item= it++))
5212
Item *new_item= item->transform(transformer, arg);
5217
THD::change_item_tree() should be called only if the tree was
5218
really transformed, i.e. when a new item has been created.
5219
Otherwise we'll be allocating a lot of unnecessary memory for
5220
change records at each execution.
5222
if (new_item != item)
5223
current_thd->change_item_tree((Item **) it.ref(), new_item);
5225
return Item_func::transform(transformer, arg);
5228
void Item_equal::print(String *str, enum_query_type query_type)
5230
str->append(func_name());
5232
List_iterator_fast<Item_field> it(fields);
5235
const_item->print(str, query_type);
5239
item->print(str, query_type);
5241
while ((item= it++))
5245
item->print(str, query_type);