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
#include <drizzled/server_includes.h>
25
#include <drizzled/sql_select.h>
27
static bool convert_constant_item(THD *, Item_field *, Item **);
29
static Item_result item_store_type(Item_result a, Item *item,
32
Item_result b= item->result_type();
34
if (a == STRING_RESULT || b == STRING_RESULT)
36
else if (a == REAL_RESULT || b == REAL_RESULT)
38
else if (a == DECIMAL_RESULT || b == DECIMAL_RESULT ||
39
unsigned_flag != item->unsigned_flag)
40
return DECIMAL_RESULT;
45
static void agg_result_type(Item_result *type, Item **items, uint32_t nitems)
47
Item **item, **item_end;
48
bool unsigned_flag= 0;
51
/* Skip beginning NULL items */
52
for (item= items, item_end= item + nitems; item < item_end; item++)
54
if ((*item)->type() != Item::NULL_ITEM)
56
*type= (*item)->result_type();
57
unsigned_flag= (*item)->unsigned_flag;
62
/* Combine result types. Note: NULL items don't affect the result */
63
for (; item < item_end; item++)
65
if ((*item)->type() != Item::NULL_ITEM)
66
*type= item_store_type(*type, *item, unsigned_flag);
72
Compare row signature of two expressions
76
item1 the first expression
77
item2 the second expression
80
The function checks that two expressions have compatible row signatures
81
i.e. that the number of columns they return are the same and that if they
82
are both row expressions then each component from the first expression has
83
a row signature compatible with the signature of the corresponding component
84
of the second expression.
87
1 type incompatibility has been detected
91
static int cmp_row_type(Item* item1, Item* item2)
93
uint32_t n= item1->cols();
94
if (item2->check_cols(n))
96
for (uint32_t i=0; i<n; i++)
98
if (item2->element_index(i)->check_cols(item1->element_index(i)->cols()) ||
99
(item1->element_index(i)->result_type() == ROW_RESULT &&
100
cmp_row_type(item1->element_index(i), item2->element_index(i))))
108
Aggregates result types from the array of items.
112
type [out] the aggregated type
113
items array of items to aggregate the type from
114
nitems number of items in the array
117
This function aggregates result types from the array of items. Found type
118
supposed to be used later for comparison of values of these items.
119
Aggregation itself is performed by the item_cmp_type() function.
120
@param[out] type the aggregated type
121
@param items array of items to aggregate the type from
122
@param nitems number of items in the array
125
1 type incompatibility has been detected
130
static int agg_cmp_type(Item_result *type, Item **items, uint32_t nitems)
133
type[0]= items[0]->result_type();
134
for (i= 1 ; i < nitems ; i++)
136
type[0]= item_cmp_type(type[0], items[i]->result_type());
138
When aggregating types of two row expressions we have to check
139
that they have the same cardinality and that each component
140
of the first row expression has a compatible row signature with
141
the signature of the corresponding component of the second row
144
if (type[0] == ROW_RESULT && cmp_row_type(items[0], items[i]))
145
return 1; // error found: invalid usage of rows
152
@brief Aggregates field types from the array of items.
154
@param[in] items array of items to aggregate the type from
155
@paran[in] nitems number of items in the array
157
@details This function aggregates field types from the array of items.
158
Found type is supposed to be used later as the result field type
159
of a multi-argument function.
160
Aggregation itself is performed by the Field::field_type_merge()
163
@note The term "aggregation" is used here in the sense of inferring the
164
result type of a function from its argument types.
166
@return aggregated field type.
169
enum_field_types agg_field_type(Item **items, uint32_t nitems)
172
if (!nitems || items[0]->result_type() == ROW_RESULT )
173
return (enum_field_types)-1;
174
enum_field_types res= items[0]->field_type();
175
for (i= 1 ; i < nitems ; i++)
176
res= Field::field_type_merge(res, items[i]->field_type());
181
Collects different types for comparison of first item with each other items
185
items Array of items to collect types from
186
nitems Number of items in the array
189
This function collects different result types for comparison of the first
190
item in the list with each of the remaining items in the 'items' array.
193
0 - if row type incompatibility has been detected (see cmp_row_type)
194
Bitmap of collected types - otherwise
197
static uint32_t collect_cmp_types(Item **items, uint32_t nitems)
200
uint32_t found_types;
201
Item_result left_result= items[0]->result_type();
204
for (i= 1; i < nitems ; i++)
206
if ((left_result == ROW_RESULT ||
207
items[i]->result_type() == ROW_RESULT) &&
208
cmp_row_type(items[0], items[i]))
210
found_types|= 1<< (uint)item_cmp_type(left_result,
211
items[i]->result_type());
216
static void my_coll_agg_error(DTCollation &c1, DTCollation &c2,
219
my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
220
c1.collation->name,c1.derivation_name(),
221
c2.collation->name,c2.derivation_name(),
226
Item_bool_func2* Eq_creator::create(Item *a, Item *b) const
228
return new Item_func_eq(a, b);
232
Item_bool_func2* Ne_creator::create(Item *a, Item *b) const
234
return new Item_func_ne(a, b);
238
Item_bool_func2* Gt_creator::create(Item *a, Item *b) const
240
return new Item_func_gt(a, b);
244
Item_bool_func2* Lt_creator::create(Item *a, Item *b) const
246
return new Item_func_lt(a, b);
250
Item_bool_func2* Ge_creator::create(Item *a, Item *b) const
252
return new Item_func_ge(a, b);
256
Item_bool_func2* Le_creator::create(Item *a, Item *b) const
258
return new Item_func_le(a, b);
263
Most of these returns 0LL if false and 1LL if true and
264
NULL if some arg is NULL.
267
int64_t Item_func_not::val_int()
270
bool value= args[0]->val_bool();
271
null_value=args[0]->null_value;
272
return ((!null_value && value == 0) ? 1 : 0);
276
We put any NOT expression into parenthesis to avoid
277
possible problems with internal view representations where
278
any '!' is converted to NOT. It may cause a problem if
279
'!' is used in an expression together with other operators
280
whose precedence is lower than the precedence of '!' yet
281
higher than the precedence of NOT.
284
void Item_func_not::print(String *str, enum_query_type query_type)
287
Item_func::print(str, query_type);
292
special NOT for ALL subquery.
296
int64_t Item_func_not_all::val_int()
299
bool value= args[0]->val_bool();
302
return true if there was records in underlying select in max/min
303
optimization (ALL subquery)
305
if (empty_underlying_subquery())
308
null_value= args[0]->null_value;
309
return ((!null_value && value == 0) ? 1 : 0);
313
bool Item_func_not_all::empty_underlying_subquery()
315
return ((test_sum_item && !test_sum_item->any_value()) ||
316
(test_sub_item && !test_sub_item->any_value()));
319
void Item_func_not_all::print(String *str, enum_query_type query_type)
322
Item_func::print(str, query_type);
324
args[0]->print(str, query_type);
329
Special NOP (No OPeration) for ALL subquery. It is like
333
(return true if underlying subquery do not return rows) but if subquery
334
returns some rows it return same value as argument (true/false).
337
int64_t Item_func_nop_all::val_int()
340
int64_t value= args[0]->val_int();
343
return false if there was records in underlying select in max/min
344
optimization (SAME/ANY subquery)
346
if (empty_underlying_subquery())
349
null_value= args[0]->null_value;
350
return (null_value || value == 0) ? 0 : 1;
355
Convert a constant item to an int and replace the original item.
357
The function converts a constant expression or string to an integer.
358
On successful conversion the original item is substituted for the
359
result of the item evaluation.
360
This is done when comparing DATE/TIME of different formats and
361
also when comparing bigint to strings (in which case strings
362
are converted to bigints).
364
@param thd thread handle
365
@param field_item item will be converted using the type of this field
366
@param[in,out] item reference to the item to convert
369
This function is called only at prepare stage.
370
As all derived tables are filled only after all derived tables
371
are prepared we do not evaluate items with subselects here because
372
they can contain derived tables and thus we may attempt to use a
373
table that has not been populated yet.
378
1 Item was replaced with an integer version of the item
381
static bool convert_constant_item(THD *thd, Item_field *field_item,
384
Field *field= field_item->field;
387
if (!(*item)->with_subselect && (*item)->const_item())
389
ulong orig_sql_mode= thd->variables.sql_mode;
390
enum_check_fields orig_count_cuted_fields= thd->count_cuted_fields;
391
uint64_t orig_field_val= 0; /* original field value if valid */
393
/* For comparison purposes allow invalid dates like 2000-01-32 */
394
thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
396
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
399
Store the value of the field if it references an outer field because
400
the call to save_in_field below overrides that value.
402
if (field_item->depended_from)
403
orig_field_val= field->val_int();
404
if (!(*item)->is_null() && !(*item)->save_in_field(field, 1))
406
Item *tmp= new Item_int_with_ref(field->val_int(), *item,
407
test(field->flags & UNSIGNED_FLAG));
409
thd->change_item_tree(item, tmp);
410
result= 1; // Item was replaced
412
/* Restore the original field value. */
413
if (field_item->depended_from)
415
result= field->store(orig_field_val, true);
416
/* orig_field_val must be a valid value that can be restored back. */
419
thd->variables.sql_mode= orig_sql_mode;
420
thd->count_cuted_fields= orig_count_cuted_fields;
426
void Item_bool_func2::fix_length_and_dec()
428
max_length= 1; // Function returns 0 or 1
432
As some compare functions are generated after sql_yacc,
433
we have to check for out of memory conditions here
435
if (!args[0] || !args[1])
439
We allow to convert to Unicode character sets in some cases.
440
The conditions when conversion is possible are:
441
- arguments A and B have different charsets
442
- A wins according to coercibility rules
443
- character set of A is superset for character set of B
445
If all of the above is true, then it's possible to convert
446
B into the character set of A, and then compare according
447
to the collation of A.
452
if (args[0]->result_type() == STRING_RESULT &&
453
args[1]->result_type() == STRING_RESULT &&
454
agg_arg_charsets(coll, args, 2, MY_COLL_CMP_CONV, 1))
457
args[0]->cmp_context= args[1]->cmp_context=
458
item_cmp_type(args[0]->result_type(), args[1]->result_type());
459
// Make a special case of compare with fields to get nicer DATE comparisons
461
if (functype() == LIKE_FUNC) // Disable conversion in case of LIKE function.
469
if (args[0]->real_item()->type() == FIELD_ITEM)
471
Item_field *field_item= (Item_field*) (args[0]->real_item());
472
if (field_item->field->can_be_compared_as_int64_t() &&
473
!(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
475
if (convert_constant_item(thd, field_item, &args[1]))
477
cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
478
INT_RESULT); // Works for all types.
479
args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
484
if (args[1]->real_item()->type() == FIELD_ITEM)
486
Item_field *field_item= (Item_field*) (args[1]->real_item());
487
if (field_item->field->can_be_compared_as_int64_t() &&
488
!(field_item->is_datetime() &&
489
args[0]->result_type() == STRING_RESULT))
491
if (convert_constant_item(thd, field_item, &args[0]))
493
cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
494
INT_RESULT); // Works for all types.
495
args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
505
int Arg_comparator::set_compare_func(Item_bool_func2 *item, Item_result type)
508
func= comparator_matrix[type]
509
[test(owner->functype() == Item_func::EQUAL_FUNC)];
513
uint32_t n= (*a)->cols();
514
if (n != (*b)->cols())
516
my_error(ER_OPERAND_COLUMNS, MYF(0), n);
520
if (!(comparators= new Arg_comparator[n]))
522
for (uint32_t i=0; i < n; i++)
524
if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
526
my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
529
comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
536
We must set cmp_charset here as we may be called from for an automatic
537
generated item, like in natural join
539
if (cmp_collation.set((*a)->collation, (*b)->collation) ||
540
cmp_collation.derivation == DERIVATION_NONE)
542
my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
545
if (cmp_collation.collation == &my_charset_bin)
548
We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
549
without removing end space
551
if (func == &Arg_comparator::compare_string)
552
func= &Arg_comparator::compare_binary_string;
553
else if (func == &Arg_comparator::compare_e_string)
554
func= &Arg_comparator::compare_e_binary_string;
557
As this is binary compassion, mark all fields that they can't be
558
transformed. Otherwise we would get into trouble with comparisons
560
WHERE col= 'j' AND col LIKE BINARY 'j'
561
which would be transformed to:
564
(*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
565
(*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
571
if (func == &Arg_comparator::compare_int_signed)
573
if ((*a)->unsigned_flag)
574
func= (((*b)->unsigned_flag)?
575
&Arg_comparator::compare_int_unsigned :
576
&Arg_comparator::compare_int_unsigned_signed);
577
else if ((*b)->unsigned_flag)
578
func= &Arg_comparator::compare_int_signed_unsigned;
580
else if (func== &Arg_comparator::compare_e_int)
582
if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
583
func= &Arg_comparator::compare_e_int_diff_signedness;
591
if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
593
precision= 5 / log_10[cmax((*a)->decimals, (*b)->decimals) + 1];
594
if (func == &Arg_comparator::compare_real)
595
func= &Arg_comparator::compare_real_fixed;
596
else if (func == &Arg_comparator::compare_e_real)
597
func= &Arg_comparator::compare_e_real_fixed;
609
@brief Convert date provided in a string to the int representation.
611
@param[in] thd thread handle
612
@param[in] str a string to convert
613
@param[in] warn_type type of the timestamp for issuing the warning
614
@param[in] warn_name field name for issuing the warning
615
@param[out] error_arg could not extract a DATE or DATETIME
617
@details Convert date provided in the string str to the int
618
representation. If the string contains wrong date or doesn't
619
contain it at all then a warning is issued. The warn_type and
620
the warn_name arguments are used as the name and the type of the
621
field when issuing the warning. If any input was discarded
622
(trailing or non-timestampy characters), was_cut will be non-zero.
623
was_type will return the type str_to_datetime() could correctly
627
converted value. 0 on error and on zero-dates -- check 'failure'
631
get_date_from_str(THD *thd, String *str, enum enum_drizzle_timestamp_type warn_type,
632
char *warn_name, bool *error_arg)
637
enum enum_drizzle_timestamp_type ret;
639
ret= str_to_datetime(str->ptr(), str->length(), &l_time,
640
(TIME_FUZZY_DATE | MODE_INVALID_DATES |
641
(thd->variables.sql_mode & MODE_NO_ZERO_DATE)),
644
if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
647
Do not return yet, we may still want to throw a "trailing garbage"
651
value= TIME_to_uint64_t_datetime(&l_time);
656
error= 1; /* force warning */
660
make_truncated_value_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
661
str->ptr(), str->length(),
662
warn_type, warn_name);
669
Check whether compare_datetime() can be used to compare items.
672
Arg_comparator::can_compare_as_dates()
673
a, b [in] items to be compared
674
const_value [out] converted value of the string constant, if any
677
Check several cases when the DATE/DATETIME comparator should be used.
678
The following cases are checked:
679
1. Both a and b is a DATE/DATETIME field/function returning string or
681
2. Only a or b is a DATE/DATETIME field/function returning string or
682
int result and the other item (b or a) is an item with string result.
683
If the second item is a constant one then it's checked to be
684
convertible to the DATE/DATETIME type. If the constant can't be
685
converted to a DATE/DATETIME then the compare_datetime() comparator
686
isn't used and the warning about wrong DATE/DATETIME value is issued.
687
In all other cases (date-[int|real|decimal]/[int|real|decimal]-date)
688
the comparison is handled by other comparators.
689
If the datetime comparator can be used and one the operands of the
690
comparison is a string constant that was successfully converted to a
691
DATE/DATETIME type then the result of the conversion is returned in the
692
const_value if it is provided. If there is no constant or
693
compare_datetime() isn't applicable then the *const_value remains
697
the found type of date comparison
700
enum Arg_comparator::enum_date_cmp_type
701
Arg_comparator::can_compare_as_dates(Item *a, Item *b, uint64_t *const_value)
703
enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
704
Item *str_arg= 0, *date_arg= 0;
706
if (a->type() == Item::ROW_ITEM || b->type() == Item::ROW_ITEM)
707
return CMP_DATE_DFLT;
709
if (a->is_datetime())
711
if (b->is_datetime())
712
cmp_type= CMP_DATE_WITH_DATE;
713
else if (b->result_type() == STRING_RESULT)
715
cmp_type= CMP_DATE_WITH_STR;
720
else if (b->is_datetime() && a->result_type() == STRING_RESULT)
722
cmp_type= CMP_STR_WITH_DATE;
727
if (cmp_type != CMP_DATE_DFLT)
730
Do not cache GET_USER_VAR() function as its const_item() may return true
731
for the current thread but it still may change during the execution.
733
if (cmp_type != CMP_DATE_WITH_DATE && str_arg->const_item() &&
734
(str_arg->type() != Item::FUNC_ITEM ||
735
((Item_func*)str_arg)->functype() != Item_func::GUSERVAR_FUNC))
737
THD *thd= current_thd;
740
String tmp, *str_val= 0;
741
enum enum_drizzle_timestamp_type t_type= (date_arg->field_type() == DRIZZLE_TYPE_NEWDATE ?
742
DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME);
744
str_val= str_arg->val_str(&tmp);
745
if (str_arg->null_value)
746
return CMP_DATE_DFLT;
747
value= get_date_from_str(thd, str_val, t_type, date_arg->name, &error);
749
return CMP_DATE_DFLT;
759
Retrieves correct TIME value from the given item.
764
item_arg [in/out] item to retrieve TIME value from
765
cache_arg [in/out] pointer to place to store the cache item to
766
warn_item [in] unused
767
is_null [out] true <=> the item_arg is null
770
Retrieves the correct TIME value from given item for comparison by the
771
compare_datetime() function.
772
If item's result can be compared as int64_t then its int value is used
773
and a value returned by get_time function is used otherwise.
774
If an item is a constant one then its value is cached and it isn't
775
get parsed again. An Item_cache_int object is used for for cached values.
776
It seamlessly substitutes the original item. The cache item is marked as
777
non-constant to prevent re-caching it again.
784
get_time_value(THD *thd __attribute__((unused)),
785
Item ***item_arg, Item **cache_arg,
786
Item *warn_item __attribute__((unused)),
790
Item *item= **item_arg;
793
if (item->result_as_int64_t())
795
value= item->val_int();
796
*is_null= item->null_value;
800
*is_null= item->get_time(<ime);
801
value= !*is_null ? TIME_to_uint64_t_datetime(<ime) : 0;
804
Do not cache GET_USER_VAR() function as its const_item() may return true
805
for the current thread but it still may change during the execution.
807
if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
808
((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
810
Item_cache_int *cache= new Item_cache_int();
811
/* Mark the cache as non-const to prevent re-caching. */
812
cache->set_used_tables(1);
813
cache->store(item, value);
815
*item_arg= cache_arg;
821
int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg,
822
Item **a1, Item **a2,
825
enum enum_date_cmp_type cmp_type;
826
uint64_t const_value= (uint64_t)-1;
830
if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
834
a_type= (*a)->field_type();
835
b_type= (*b)->field_type();
839
if (const_value != (uint64_t)-1)
841
Item_cache_int *cache= new Item_cache_int();
842
/* Mark the cache as non-const to prevent re-caching. */
843
cache->set_used_tables(1);
844
if (!(*a)->is_datetime())
846
cache->store((*a), const_value);
848
a= (Item **)&a_cache;
852
cache->store((*b), const_value);
854
b= (Item **)&b_cache;
857
is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC);
858
func= &Arg_comparator::compare_datetime;
859
get_value_func= &get_datetime_value;
862
else if (type == STRING_RESULT && (*a)->field_type() == DRIZZLE_TYPE_TIME &&
863
(*b)->field_type() == DRIZZLE_TYPE_TIME)
865
/* Compare TIME values as integers. */
870
is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC);
871
func= &Arg_comparator::compare_datetime;
872
get_value_func= &get_time_value;
876
return set_compare_func(owner_arg, type);
880
void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
883
/* A caller will handle null values by itself. */
887
a_type= (*a)->field_type();
888
b_type= (*b)->field_type();
892
func= &Arg_comparator::compare_datetime;
893
get_value_func= &get_datetime_value;
898
Retrieves correct DATETIME value from given item.
903
item_arg [in/out] item to retrieve DATETIME value from
904
cache_arg [in/out] pointer to place to store the caching item to
905
warn_item [in] item for issuing the conversion warning
906
is_null [out] true <=> the item_arg is null
909
Retrieves the correct DATETIME value from given item for comparison by the
910
compare_datetime() function.
911
If item's result can be compared as int64_t then its int value is used
912
and its string value is used otherwise. Strings are always parsed and
913
converted to int values by the get_date_from_str() function.
914
This allows us to compare correctly string dates with missed insignificant
915
zeros. If an item is a constant one then its value is cached and it isn't
916
get parsed again. An Item_cache_int object is used for caching values. It
917
seamlessly substitutes the original item. The cache item is marked as
918
non-constant to prevent re-caching it again. In order to compare
919
correctly DATE and DATETIME items the result of the former are treated as
920
a DATETIME with zero time (00:00:00).
927
get_datetime_value(THD *thd, Item ***item_arg, Item **cache_arg,
928
Item *warn_item, bool *is_null)
932
Item *item= **item_arg;
934
if (item->result_as_int64_t())
936
value= item->val_int();
937
*is_null= item->null_value;
938
enum_field_types f_type= item->field_type();
940
Item_date_add_interval may return DRIZZLE_TYPE_STRING as the result
941
field type. To detect that the DATE value has been returned we
942
compare it with 100000000L - any DATE value should be less than it.
943
Don't shift cached DATETIME values up for the second time.
945
if (f_type == DRIZZLE_TYPE_NEWDATE ||
946
(f_type != DRIZZLE_TYPE_DATETIME && value < 100000000L))
951
str= item->val_str(&buf);
952
*is_null= item->null_value;
955
return ~(uint64_t) 0;
957
Convert strings to the integer DATE/DATETIME representation.
958
Even if both dates provided in strings we can't compare them directly as
959
strings as there is no warranty that they are correct and do not miss
960
some insignificant zeros.
965
enum_field_types f_type= warn_item->field_type();
966
enum enum_drizzle_timestamp_type t_type= f_type ==
967
DRIZZLE_TYPE_NEWDATE ? DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME;
968
value= get_date_from_str(thd, str, t_type, warn_item->name, &error);
970
If str did not contain a valid date according to the current
971
SQL_MODE, get_date_from_str() has already thrown a warning,
972
and we don't want to throw NULL on invalid date (see 5.2.6
973
"SQL modes" in the manual), so we're done here.
977
Do not cache GET_USER_VAR() function as its const_item() may return true
978
for the current thread but it still may change during the execution.
980
if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
981
((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
983
Item_cache_int *cache= new Item_cache_int(DRIZZLE_TYPE_DATETIME);
984
/* Mark the cache as non-const to prevent re-caching. */
985
cache->set_used_tables(1);
986
cache->store(item, value);
988
*item_arg= cache_arg;
994
Compare items values as dates.
997
Arg_comparator::compare_datetime()
1000
Compare items values as DATE/DATETIME for both EQUAL_FUNC and from other
1001
comparison functions. The correct DATETIME values are obtained
1002
with help of the get_datetime_value() function.
1005
If is_nulls_eq is true:
1006
1 if items are equal or both are null
1008
If is_nulls_eq is false:
1009
-1 a < b or one of items is null
1014
int Arg_comparator::compare_datetime()
1016
bool is_null= false;
1017
uint64_t a_value, b_value;
1019
/* Get DATE/DATETIME/TIME value of the 'a' item. */
1020
a_value= (*get_value_func)(thd, &a, &a_cache, *b, &is_null);
1021
if (!is_nulls_eq && is_null)
1024
owner->null_value= 1;
1028
/* Get DATE/DATETIME/TIME value of the 'b' item. */
1029
b_value= (*get_value_func)(thd, &b, &b_cache, *a, &is_null);
1033
owner->null_value= is_nulls_eq ? 0 : 1;
1034
return is_nulls_eq ? 1 : -1;
1038
owner->null_value= 0;
1040
/* Compare values. */
1042
return (a_value == b_value);
1043
return a_value < b_value ? -1 : (a_value > b_value ? 1 : 0);
1047
int Arg_comparator::compare_string()
1050
if ((res1= (*a)->val_str(&owner->tmp_value1)))
1052
if ((res2= (*b)->val_str(&owner->tmp_value2)))
1054
owner->null_value= 0;
1055
return sortcmp(res1,res2,cmp_collation.collation);
1058
owner->null_value= 1;
1064
Compare strings byte by byte. End spaces are also compared.
1074
int Arg_comparator::compare_binary_string()
1077
if ((res1= (*a)->val_str(&owner->tmp_value1)))
1079
if ((res2= (*b)->val_str(&owner->tmp_value2)))
1081
owner->null_value= 0;
1082
uint32_t res1_length= res1->length();
1083
uint32_t res2_length= res2->length();
1084
int cmp= memcmp(res1->ptr(), res2->ptr(), cmin(res1_length,res2_length));
1085
return cmp ? cmp : (int) (res1_length - res2_length);
1088
owner->null_value= 1;
1094
Compare strings, but take into account that NULL == NULL.
1098
int Arg_comparator::compare_e_string()
1101
res1= (*a)->val_str(&owner->tmp_value1);
1102
res2= (*b)->val_str(&owner->tmp_value2);
1104
return test(res1 == res2);
1105
return test(sortcmp(res1, res2, cmp_collation.collation) == 0);
1109
int Arg_comparator::compare_e_binary_string()
1112
res1= (*a)->val_str(&owner->tmp_value1);
1113
res2= (*b)->val_str(&owner->tmp_value2);
1115
return test(res1 == res2);
1116
return test(stringcmp(res1, res2) == 0);
1120
int Arg_comparator::compare_real()
1123
Fix yet another manifestation of Bug#2338. 'Volatile' will instruct
1124
gcc to flush double values out of 80-bit Intel FPU registers before
1125
performing the comparison.
1127
volatile double val1, val2;
1128
val1= (*a)->val_real();
1129
if (!(*a)->null_value)
1131
val2= (*b)->val_real();
1132
if (!(*b)->null_value)
1134
owner->null_value= 0;
1135
if (val1 < val2) return -1;
1136
if (val1 == val2) return 0;
1140
owner->null_value= 1;
1144
int Arg_comparator::compare_decimal()
1147
my_decimal *val1= (*a)->val_decimal(&value1);
1148
if (!(*a)->null_value)
1151
my_decimal *val2= (*b)->val_decimal(&value2);
1152
if (!(*b)->null_value)
1154
owner->null_value= 0;
1155
return my_decimal_cmp(val1, val2);
1158
owner->null_value= 1;
1162
int Arg_comparator::compare_e_real()
1164
double val1= (*a)->val_real();
1165
double val2= (*b)->val_real();
1166
if ((*a)->null_value || (*b)->null_value)
1167
return test((*a)->null_value && (*b)->null_value);
1168
return test(val1 == val2);
1171
int Arg_comparator::compare_e_decimal()
1173
my_decimal value1, value2;
1174
my_decimal *val1= (*a)->val_decimal(&value1);
1175
my_decimal *val2= (*b)->val_decimal(&value2);
1176
if ((*a)->null_value || (*b)->null_value)
1177
return test((*a)->null_value && (*b)->null_value);
1178
return test(my_decimal_cmp(val1, val2) == 0);
1182
int Arg_comparator::compare_real_fixed()
1185
Fix yet another manifestation of Bug#2338. 'Volatile' will instruct
1186
gcc to flush double values out of 80-bit Intel FPU registers before
1187
performing the comparison.
1189
volatile double val1, val2;
1190
val1= (*a)->val_real();
1191
if (!(*a)->null_value)
1193
val2= (*b)->val_real();
1194
if (!(*b)->null_value)
1196
owner->null_value= 0;
1197
if (val1 == val2 || fabs(val1 - val2) < precision)
1204
owner->null_value= 1;
1209
int Arg_comparator::compare_e_real_fixed()
1211
double val1= (*a)->val_real();
1212
double val2= (*b)->val_real();
1213
if ((*a)->null_value || (*b)->null_value)
1214
return test((*a)->null_value && (*b)->null_value);
1215
return test(val1 == val2 || fabs(val1 - val2) < precision);
1219
int Arg_comparator::compare_int_signed()
1221
int64_t val1= (*a)->val_int();
1222
if (!(*a)->null_value)
1224
int64_t val2= (*b)->val_int();
1225
if (!(*b)->null_value)
1227
owner->null_value= 0;
1228
if (val1 < val2) return -1;
1229
if (val1 == val2) return 0;
1233
owner->null_value= 1;
1239
Compare values as BIGINT UNSIGNED.
1242
int Arg_comparator::compare_int_unsigned()
1244
uint64_t val1= (*a)->val_int();
1245
if (!(*a)->null_value)
1247
uint64_t val2= (*b)->val_int();
1248
if (!(*b)->null_value)
1250
owner->null_value= 0;
1251
if (val1 < val2) return -1;
1252
if (val1 == val2) return 0;
1256
owner->null_value= 1;
1262
Compare signed (*a) with unsigned (*B)
1265
int Arg_comparator::compare_int_signed_unsigned()
1267
int64_t sval1= (*a)->val_int();
1268
if (!(*a)->null_value)
1270
uint64_t uval2= (uint64_t)(*b)->val_int();
1271
if (!(*b)->null_value)
1273
owner->null_value= 0;
1274
if (sval1 < 0 || (uint64_t)sval1 < uval2)
1276
if ((uint64_t)sval1 == uval2)
1281
owner->null_value= 1;
1287
Compare unsigned (*a) with signed (*B)
1290
int Arg_comparator::compare_int_unsigned_signed()
1292
uint64_t uval1= (uint64_t)(*a)->val_int();
1293
if (!(*a)->null_value)
1295
int64_t sval2= (*b)->val_int();
1296
if (!(*b)->null_value)
1298
owner->null_value= 0;
1301
if (uval1 < (uint64_t)sval2)
1303
if (uval1 == (uint64_t)sval2)
1308
owner->null_value= 1;
1313
int Arg_comparator::compare_e_int()
1315
int64_t val1= (*a)->val_int();
1316
int64_t val2= (*b)->val_int();
1317
if ((*a)->null_value || (*b)->null_value)
1318
return test((*a)->null_value && (*b)->null_value);
1319
return test(val1 == val2);
1323
Compare unsigned *a with signed *b or signed *a with unsigned *b.
1325
int Arg_comparator::compare_e_int_diff_signedness()
1327
int64_t val1= (*a)->val_int();
1328
int64_t val2= (*b)->val_int();
1329
if ((*a)->null_value || (*b)->null_value)
1330
return test((*a)->null_value && (*b)->null_value);
1331
return (val1 >= 0) && test(val1 == val2);
1334
int Arg_comparator::compare_row()
1338
(*a)->bring_value();
1339
(*b)->bring_value();
1340
uint32_t n= (*a)->cols();
1341
for (uint32_t i= 0; i<n; i++)
1343
res= comparators[i].compare();
1344
if (owner->null_value)
1346
// NULL was compared
1347
switch (owner->functype()) {
1348
case Item_func::NE_FUNC:
1349
break; // NE never aborts on NULL even if abort_on_null is set
1350
case Item_func::LT_FUNC:
1351
case Item_func::LE_FUNC:
1352
case Item_func::GT_FUNC:
1353
case Item_func::GE_FUNC:
1354
return -1; // <, <=, > and >= always fail on NULL
1356
if (owner->abort_on_null)
1357
return -1; // We do not need correct NULL returning
1360
owner->null_value= 0;
1361
res= 0; // continue comparison (maybe we will meet explicit difference)
1369
There was NULL(s) in comparison in some parts, but there was no
1370
explicit difference in other parts, so we have to return NULL.
1372
owner->null_value= 1;
1379
int Arg_comparator::compare_e_row()
1381
(*a)->bring_value();
1382
(*b)->bring_value();
1383
uint32_t n= (*a)->cols();
1384
for (uint32_t i= 0; i<n; i++)
1386
if (!comparators[i].compare())
1393
void Item_func_truth::fix_length_and_dec()
1402
void Item_func_truth::print(String *str, enum_query_type query_type)
1405
args[0]->print(str, query_type);
1406
str->append(STRING_WITH_LEN(" is "));
1408
str->append(STRING_WITH_LEN("not "));
1410
str->append(STRING_WITH_LEN("true"));
1412
str->append(STRING_WITH_LEN("false"));
1417
bool Item_func_truth::val_bool()
1419
bool val= args[0]->val_bool();
1420
if (args[0]->null_value)
1423
NULL val IS {true, false} --> false
1424
NULL val IS NOT {true, false} --> true
1426
return (! affirmative);
1431
/* {true, false} val IS {true, false} value */
1432
return (val == value);
1435
/* {true, false} val IS NOT {true, false} value */
1436
return (val != value);
1440
int64_t Item_func_truth::val_int()
1442
return (val_bool() ? 1 : 0);
1446
bool Item_in_optimizer::fix_left(THD *thd, Item **ref __attribute__((unused)))
1448
if ((!args[0]->fixed && args[0]->fix_fields(thd, args)) ||
1449
(!cache && !(cache= Item_cache::get_cache(args[0]))))
1452
cache->setup(args[0]);
1453
if (cache->cols() == 1)
1455
if ((used_tables_cache= args[0]->used_tables()))
1456
cache->set_used_tables(OUTER_REF_TABLE_BIT);
1458
cache->set_used_tables(0);
1462
uint32_t n= cache->cols();
1463
for (uint32_t i= 0; i < n; i++)
1465
if (args[0]->element_index(i)->used_tables())
1466
((Item_cache *)cache->element_index(i))->set_used_tables(OUTER_REF_TABLE_BIT);
1468
((Item_cache *)cache->element_index(i))->set_used_tables(0);
1470
used_tables_cache= args[0]->used_tables();
1472
not_null_tables_cache= args[0]->not_null_tables();
1473
with_sum_func= args[0]->with_sum_func;
1474
if ((const_item_cache= args[0]->const_item()))
1475
cache->store(args[0]);
1480
bool Item_in_optimizer::fix_fields(THD *thd, Item **ref)
1483
if (fix_left(thd, ref))
1485
if (args[0]->maybe_null)
1488
if (!args[1]->fixed && args[1]->fix_fields(thd, args+1))
1490
Item_in_subselect * sub= (Item_in_subselect *)args[1];
1491
if (args[0]->cols() != sub->engine->cols())
1493
my_error(ER_OPERAND_COLUMNS, MYF(0), args[0]->cols());
1496
if (args[1]->maybe_null)
1498
with_sum_func= with_sum_func || args[1]->with_sum_func;
1499
used_tables_cache|= args[1]->used_tables();
1500
not_null_tables_cache|= args[1]->not_null_tables();
1501
const_item_cache&= args[1]->const_item();
1507
int64_t Item_in_optimizer::val_int()
1511
cache->store(args[0]);
1513
if (cache->null_value)
1515
if (((Item_in_subselect*)args[1])->is_top_level_item())
1518
We're evaluating "NULL IN (SELECT ...)". The result can be NULL or
1519
false, and we can return one instead of another. Just return NULL.
1525
if (!((Item_in_subselect*)args[1])->is_correlated &&
1526
result_for_null_param != UNKNOWN)
1528
/* Use cached value from previous execution */
1529
null_value= result_for_null_param;
1534
We're evaluating "NULL IN (SELECT ...)". The result is:
1535
false if SELECT produces an empty set, or
1537
We disable the predicates we've pushed down into subselect, run the
1538
subselect and see if it has produced any rows.
1540
Item_in_subselect *item_subs=(Item_in_subselect*)args[1];
1541
if (cache->cols() == 1)
1543
item_subs->set_cond_guard_var(0, false);
1544
(void) args[1]->val_bool_result();
1545
result_for_null_param= null_value= !item_subs->engine->no_rows();
1546
item_subs->set_cond_guard_var(0, true);
1551
uint32_t ncols= cache->cols();
1553
Turn off the predicates that are based on column compares for
1554
which the left part is currently NULL
1556
for (i= 0; i < ncols; i++)
1558
if (cache->element_index(i)->null_value)
1559
item_subs->set_cond_guard_var(i, false);
1562
(void) args[1]->val_bool_result();
1563
result_for_null_param= null_value= !item_subs->engine->no_rows();
1565
/* Turn all predicates back on */
1566
for (i= 0; i < ncols; i++)
1567
item_subs->set_cond_guard_var(i, true);
1573
tmp= args[1]->val_bool_result();
1574
null_value= args[1]->null_value;
1579
void Item_in_optimizer::keep_top_level_cache()
1581
cache->keep_array();
1586
void Item_in_optimizer::cleanup()
1588
Item_bool_func::cleanup();
1595
bool Item_in_optimizer::is_null()
1597
cache->store(args[0]);
1598
return (null_value= (cache->null_value || args[1]->is_null()));
1603
Transform an Item_in_optimizer and its arguments with a callback function.
1605
@param transformer the transformer callback function to be applied to the
1606
nodes of the tree of the object
1607
@param parameter to be passed to the transformer
1610
Recursively transform the left and the right operand of this Item. The
1611
Right operand is an Item_in_subselect or its subclass. To avoid the
1612
creation of new Items, we use the fact the the left operand of the
1613
Item_in_subselect is the same as the one of 'this', so instead of
1614
transforming its operand, we just assign the left operand of the
1615
Item_in_subselect to be equal to the left operand of 'this'.
1616
The transformation is not applied further to the subquery operand
1617
if the IN predicate.
1620
@retval pointer to the transformed item
1621
@retval NULL if an error occurred
1624
Item *Item_in_optimizer::transform(Item_transformer transformer, unsigned char *argument)
1628
assert(arg_count == 2);
1630
/* Transform the left IN operand. */
1631
new_item= (*args)->transform(transformer, argument);
1635
THD::change_item_tree() should be called only if the tree was
1636
really transformed, i.e. when a new item has been created.
1637
Otherwise we'll be allocating a lot of unnecessary memory for
1638
change records at each execution.
1640
if ((*args) != new_item)
1641
current_thd->change_item_tree(args, new_item);
1644
Transform the right IN operand which should be an Item_in_subselect or a
1645
subclass of it. The left operand of the IN must be the same as the left
1646
operand of this Item_in_optimizer, so in this case there is no further
1647
transformation, we only make both operands the same.
1648
TODO: is it the way it should be?
1650
assert((args[1])->type() == Item::SUBSELECT_ITEM &&
1651
(((Item_subselect*)(args[1]))->substype() ==
1652
Item_subselect::IN_SUBS ||
1653
((Item_subselect*)(args[1]))->substype() ==
1654
Item_subselect::ALL_SUBS ||
1655
((Item_subselect*)(args[1]))->substype() ==
1656
Item_subselect::ANY_SUBS));
1658
Item_in_subselect *in_arg= (Item_in_subselect*)args[1];
1659
in_arg->left_expr= args[0];
1661
return (this->*transformer)(argument);
1666
int64_t Item_func_eq::val_int()
1669
int value= cmp.compare();
1670
return value == 0 ? 1 : 0;
1674
/** Same as Item_func_eq, but NULL = NULL. */
1676
void Item_func_equal::fix_length_and_dec()
1678
Item_bool_func2::fix_length_and_dec();
1679
maybe_null=null_value=0;
1682
int64_t Item_func_equal::val_int()
1685
return cmp.compare();
1688
int64_t Item_func_ne::val_int()
1691
int value= cmp.compare();
1692
return value != 0 && !null_value ? 1 : 0;
1696
int64_t Item_func_ge::val_int()
1699
int value= cmp.compare();
1700
return value >= 0 ? 1 : 0;
1704
int64_t Item_func_gt::val_int()
1707
int value= cmp.compare();
1708
return value > 0 ? 1 : 0;
1711
int64_t Item_func_le::val_int()
1714
int value= cmp.compare();
1715
return value <= 0 && !null_value ? 1 : 0;
1719
int64_t Item_func_lt::val_int()
1722
int value= cmp.compare();
1723
return value < 0 && !null_value ? 1 : 0;
1727
int64_t Item_func_strcmp::val_int()
1730
String *a=args[0]->val_str(&tmp_value1);
1731
String *b=args[1]->val_str(&tmp_value2);
1737
int value= sortcmp(a,b,cmp.cmp_collation.collation);
1739
return !value ? 0 : (value < 0 ? (int64_t) -1 : (int64_t) 1);
1743
bool Item_func_opt_neg::eq(const Item *item, bool binary_cmp) const
1745
/* Assume we don't have rtti */
1748
if (item->type() != FUNC_ITEM)
1750
Item_func *item_func=(Item_func*) item;
1751
if (arg_count != item_func->arg_count ||
1752
functype() != item_func->functype())
1754
if (negated != ((Item_func_opt_neg *) item_func)->negated)
1756
for (uint32_t i=0; i < arg_count ; i++)
1757
if (!args[i]->eq(item_func->arguments()[i], binary_cmp))
1763
void Item_func_interval::fix_length_and_dec()
1765
uint32_t rows= row->cols();
1767
use_decimal_comparison= ((row->element_index(0)->result_type() ==
1769
(row->element_index(0)->result_type() ==
1773
bool not_null_consts= true;
1775
for (uint32_t i= 1; not_null_consts && i < rows; i++)
1777
Item *el= row->element_index(i);
1778
not_null_consts&= el->const_item() & !el->is_null();
1781
if (not_null_consts &&
1783
(interval_range*) sql_alloc(sizeof(interval_range) * (rows - 1))))
1785
if (use_decimal_comparison)
1787
for (uint32_t i= 1; i < rows; i++)
1789
Item *el= row->element_index(i);
1790
interval_range *range= intervals + (i-1);
1791
if ((el->result_type() == DECIMAL_RESULT) ||
1792
(el->result_type() == INT_RESULT))
1794
range->type= DECIMAL_RESULT;
1796
my_decimal *dec= el->val_decimal(&range->dec);
1797
if (dec != &range->dec)
1800
range->dec.fix_buffer_pointer();
1805
range->type= REAL_RESULT;
1806
range->dbl= el->val_real();
1812
for (uint32_t i= 1; i < rows; i++)
1814
intervals[i-1].dbl= row->element_index(i)->val_real();
1821
used_tables_cache|= row->used_tables();
1822
not_null_tables_cache= row->not_null_tables();
1823
with_sum_func= with_sum_func || row->with_sum_func;
1824
const_item_cache&= row->const_item();
1829
Execute Item_func_interval().
1832
If we are doing a decimal comparison, we are evaluating the first
1837
- 0 if lower than lowest
1838
- 1 - arg_count-1 if between args[n] and args[n+1]
1839
- arg_count if higher than biggest argument
1842
int64_t Item_func_interval::val_int()
1846
my_decimal dec_buf, *dec= NULL;
1849
if (use_decimal_comparison)
1851
dec= row->element_index(0)->val_decimal(&dec_buf);
1852
if (row->element_index(0)->null_value)
1854
my_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
1858
value= row->element_index(0)->val_real();
1859
if (row->element_index(0)->null_value)
1864
{ // Use binary search to find interval
1868
while (start != end)
1870
uint32_t mid= (start + end + 1) / 2;
1871
interval_range *range= intervals + mid;
1874
The values in the range intervall may have different types,
1875
Only do a decimal comparision of the first argument is a decimal
1876
and we are comparing against a decimal
1878
if (dec && range->type == DECIMAL_RESULT)
1879
cmp_result= my_decimal_cmp(&range->dec, dec) <= 0;
1881
cmp_result= (range->dbl <= value);
1887
interval_range *range= intervals+start;
1888
return ((dec && range->type == DECIMAL_RESULT) ?
1889
my_decimal_cmp(dec, &range->dec) < 0 :
1890
value < range->dbl) ? 0 : start + 1;
1893
for (i=1 ; i < row->cols() ; i++)
1895
Item *el= row->element_index(i);
1896
if (use_decimal_comparison &&
1897
((el->result_type() == DECIMAL_RESULT) ||
1898
(el->result_type() == INT_RESULT)))
1900
my_decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
1901
/* Skip NULL ranges. */
1904
if (my_decimal_cmp(e_dec, dec) > 0)
1909
double val= el->val_real();
1910
/* Skip NULL ranges. */
1922
Perform context analysis of a BETWEEN item tree.
1924
This function performs context analysis (name resolution) and calculates
1925
various attributes of the item tree with Item_func_between as its root.
1926
The function saves in ref the pointer to the item or to a newly created
1927
item that is considered as a replacement for the original one.
1929
@param thd reference to the global context of the query thread
1930
@param ref pointer to Item* variable where pointer to resulting "fixed"
1931
item is to be assigned
1934
Let T0(e)/T1(e) be the value of not_null_tables(e) when e is used on
1935
a predicate/function level. Then it's easy to show that:
1937
T0(e BETWEEN e1 AND e2) = union(T1(e),T1(e1),T1(e2))
1938
T1(e BETWEEN e1 AND e2) = union(T1(e),intersection(T1(e1),T1(e2)))
1939
T0(e NOT BETWEEN e1 AND e2) = union(T1(e),intersection(T1(e1),T1(e2)))
1940
T1(e NOT BETWEEN e1 AND e2) = union(T1(e),intersection(T1(e1),T1(e2)))
1949
bool Item_func_between::fix_fields(THD *thd, Item **ref)
1951
if (Item_func_opt_neg::fix_fields(thd, ref))
1954
thd->lex->current_select->between_count++;
1956
/* not_null_tables_cache == union(T1(e),T1(e1),T1(e2)) */
1957
if (pred_level && !negated)
1960
/* not_null_tables_cache == union(T1(e), intersection(T1(e1),T1(e2))) */
1961
not_null_tables_cache= (args[0]->not_null_tables() |
1962
(args[1]->not_null_tables() &
1963
args[2]->not_null_tables()));
1969
void Item_func_between::fix_length_and_dec()
1973
bool datetime_found= false;
1974
int time_items_found= 0;
1975
compare_as_dates= true;
1976
THD *thd= current_thd;
1979
As some compare functions are generated after sql_yacc,
1980
we have to check for out of memory conditions here
1982
if (!args[0] || !args[1] || !args[2])
1984
if ( agg_cmp_type(&cmp_type, args, 3))
1986
if (cmp_type == STRING_RESULT &&
1987
agg_arg_charsets(cmp_collation, args, 3, MY_COLL_CMP_CONV, 1))
1991
Detect the comparison of DATE/DATETIME items.
1992
At least one of items should be a DATE/DATETIME item and other items
1993
should return the STRING result.
1995
if (cmp_type == STRING_RESULT)
1997
for (i= 0; i < 3; i++)
1999
if (args[i]->is_datetime())
2001
datetime_found= true;
2004
if (args[i]->field_type() == DRIZZLE_TYPE_TIME &&
2005
args[i]->result_as_int64_t())
2009
if (!datetime_found)
2010
compare_as_dates= false;
2012
if (compare_as_dates)
2014
ge_cmp.set_datetime_cmp_func(args, args + 1);
2015
le_cmp.set_datetime_cmp_func(args, args + 2);
2017
else if (time_items_found == 3)
2019
/* Compare TIME items as integers. */
2020
cmp_type= INT_RESULT;
2022
else if (args[0]->real_item()->type() == FIELD_ITEM &&
2023
thd->lex->sql_command != SQLCOM_SHOW_CREATE)
2025
Item_field *field_item= (Item_field*) (args[0]->real_item());
2026
if (field_item->field->can_be_compared_as_int64_t())
2029
The following can't be recoded with || as convert_constant_item
2030
changes the argument
2032
if (convert_constant_item(thd, field_item, &args[1]))
2033
cmp_type=INT_RESULT; // Works for all types.
2034
if (convert_constant_item(thd, field_item, &args[2]))
2035
cmp_type=INT_RESULT; // Works for all types.
2041
int64_t Item_func_between::val_int()
2044
if (compare_as_dates)
2048
ge_res= ge_cmp.compare();
2049
if ((null_value= args[0]->null_value))
2051
le_res= le_cmp.compare();
2053
if (!args[1]->null_value && !args[2]->null_value)
2054
return (int64_t) ((ge_res >= 0 && le_res <=0) != negated);
2055
else if (args[1]->null_value)
2057
null_value= le_res > 0; // not null if false range.
2061
null_value= ge_res < 0;
2064
else if (cmp_type == STRING_RESULT)
2066
String *value,*a,*b;
2067
value=args[0]->val_str(&value0);
2068
if ((null_value=args[0]->null_value))
2070
a=args[1]->val_str(&value1);
2071
b=args[2]->val_str(&value2);
2072
if (!args[1]->null_value && !args[2]->null_value)
2073
return (int64_t) ((sortcmp(value,a,cmp_collation.collation) >= 0 &&
2074
sortcmp(value,b,cmp_collation.collation) <= 0) !=
2076
if (args[1]->null_value && args[2]->null_value)
2078
else if (args[1]->null_value)
2080
// Set to not null if false range.
2081
null_value= sortcmp(value,b,cmp_collation.collation) <= 0;
2085
// Set to not null if false range.
2086
null_value= sortcmp(value,a,cmp_collation.collation) >= 0;
2089
else if (cmp_type == INT_RESULT)
2091
int64_t value=args[0]->val_int(), a, b;
2092
if ((null_value=args[0]->null_value))
2093
return 0; /* purecov: inspected */
2094
a=args[1]->val_int();
2095
b=args[2]->val_int();
2096
if (!args[1]->null_value && !args[2]->null_value)
2097
return (int64_t) ((value >= a && value <= b) != negated);
2098
if (args[1]->null_value && args[2]->null_value)
2100
else if (args[1]->null_value)
2102
null_value= value <= b; // not null if false range.
2106
null_value= value >= a;
2109
else if (cmp_type == DECIMAL_RESULT)
2111
my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2112
a_buf, *a_dec, b_buf, *b_dec;
2113
if ((null_value=args[0]->null_value))
2114
return 0; /* purecov: inspected */
2115
a_dec= args[1]->val_decimal(&a_buf);
2116
b_dec= args[2]->val_decimal(&b_buf);
2117
if (!args[1]->null_value && !args[2]->null_value)
2118
return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2119
my_decimal_cmp(dec, b_dec) <= 0) != negated);
2120
if (args[1]->null_value && args[2]->null_value)
2122
else if (args[1]->null_value)
2123
null_value= (my_decimal_cmp(dec, b_dec) <= 0);
2125
null_value= (my_decimal_cmp(dec, a_dec) >= 0);
2129
double value= args[0]->val_real(),a,b;
2130
if ((null_value=args[0]->null_value))
2131
return 0; /* purecov: inspected */
2132
a= args[1]->val_real();
2133
b= args[2]->val_real();
2134
if (!args[1]->null_value && !args[2]->null_value)
2135
return (int64_t) ((value >= a && value <= b) != negated);
2136
if (args[1]->null_value && args[2]->null_value)
2138
else if (args[1]->null_value)
2140
null_value= value <= b; // not null if false range.
2144
null_value= value >= a;
2147
return (int64_t) (!null_value && negated);
2151
void Item_func_between::print(String *str, enum_query_type query_type)
2154
args[0]->print(str, query_type);
2156
str->append(STRING_WITH_LEN(" not"));
2157
str->append(STRING_WITH_LEN(" between "));
2158
args[1]->print(str, query_type);
2159
str->append(STRING_WITH_LEN(" and "));
2160
args[2]->print(str, query_type);
2165
Item_func_ifnull::fix_length_and_dec()
2167
agg_result_type(&hybrid_type, args, 2);
2168
maybe_null=args[1]->maybe_null;
2169
decimals= cmax(args[0]->decimals, args[1]->decimals);
2170
unsigned_flag= args[0]->unsigned_flag && args[1]->unsigned_flag;
2172
if (hybrid_type == DECIMAL_RESULT || hybrid_type == INT_RESULT)
2174
int len0= args[0]->max_length - args[0]->decimals
2175
- (args[0]->unsigned_flag ? 0 : 1);
2177
int len1= args[1]->max_length - args[1]->decimals
2178
- (args[1]->unsigned_flag ? 0 : 1);
2180
max_length= cmax(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2183
max_length= cmax(args[0]->max_length, args[1]->max_length);
2185
switch (hybrid_type) {
2187
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2189
case DECIMAL_RESULT:
2199
cached_field_type= agg_field_type(args, 2);
2203
uint32_t Item_func_ifnull::decimal_precision() const
2205
int max_int_part=cmax(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2206
return cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2210
enum_field_types Item_func_ifnull::field_type() const
2212
return cached_field_type;
2215
Field *Item_func_ifnull::tmp_table_field(Table *table)
2217
return tmp_table_field_from_field_type(table, 0);
2221
Item_func_ifnull::real_op()
2224
double value= args[0]->val_real();
2225
if (!args[0]->null_value)
2230
value= args[1]->val_real();
2231
if ((null_value=args[1]->null_value))
2237
Item_func_ifnull::int_op()
2240
int64_t value=args[0]->val_int();
2241
if (!args[0]->null_value)
2246
value=args[1]->val_int();
2247
if ((null_value=args[1]->null_value))
2253
my_decimal *Item_func_ifnull::decimal_op(my_decimal *decimal_value)
2256
my_decimal *value= args[0]->val_decimal(decimal_value);
2257
if (!args[0]->null_value)
2262
value= args[1]->val_decimal(decimal_value);
2263
if ((null_value= args[1]->null_value))
2270
Item_func_ifnull::str_op(String *str)
2273
String *res =args[0]->val_str(str);
2274
if (!args[0]->null_value)
2277
res->set_charset(collation.collation);
2280
res=args[1]->val_str(str);
2281
if ((null_value=args[1]->null_value))
2283
res->set_charset(collation.collation);
2289
Perform context analysis of an IF item tree.
2291
This function performs context analysis (name resolution) and calculates
2292
various attributes of the item tree with Item_func_if as its root.
2293
The function saves in ref the pointer to the item or to a newly created
2294
item that is considered as a replacement for the original one.
2296
@param thd reference to the global context of the query thread
2297
@param ref pointer to Item* variable where pointer to resulting "fixed"
2298
item is to be assigned
2301
Let T0(e)/T1(e) be the value of not_null_tables(e) when e is used on
2302
a predicate/function level. Then it's easy to show that:
2304
T0(IF(e,e1,e2) = T1(IF(e,e1,e2))
2305
T1(IF(e,e1,e2)) = intersection(T1(e1),T1(e2))
2315
Item_func_if::fix_fields(THD *thd, Item **ref)
2318
args[0]->top_level_item();
2320
if (Item_func::fix_fields(thd, ref))
2323
not_null_tables_cache= (args[1]->not_null_tables() &
2324
args[2]->not_null_tables());
2331
Item_func_if::fix_length_and_dec()
2333
maybe_null=args[1]->maybe_null || args[2]->maybe_null;
2334
decimals= cmax(args[1]->decimals, args[2]->decimals);
2335
unsigned_flag=args[1]->unsigned_flag && args[2]->unsigned_flag;
2337
enum Item_result arg1_type=args[1]->result_type();
2338
enum Item_result arg2_type=args[2]->result_type();
2339
bool null1=args[1]->const_item() && args[1]->null_value;
2340
bool null2=args[2]->const_item() && args[2]->null_value;
2344
cached_result_type= arg2_type;
2345
collation.set(args[2]->collation.collation);
2346
cached_field_type= args[2]->field_type();
2350
cached_result_type= arg1_type;
2351
collation.set(args[1]->collation.collation);
2352
cached_field_type= args[1]->field_type();
2356
agg_result_type(&cached_result_type, args+1, 2);
2357
if (cached_result_type == STRING_RESULT)
2359
if (agg_arg_charsets(collation, args+1, 2, MY_COLL_ALLOW_CONV, 1))
2364
collation.set(&my_charset_bin); // Number
2366
cached_field_type= agg_field_type(args + 1, 2);
2369
if ((cached_result_type == DECIMAL_RESULT )
2370
|| (cached_result_type == INT_RESULT))
2372
int len1= args[1]->max_length - args[1]->decimals
2373
- (args[1]->unsigned_flag ? 0 : 1);
2375
int len2= args[2]->max_length - args[2]->decimals
2376
- (args[2]->unsigned_flag ? 0 : 1);
2378
max_length=cmax(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
2381
max_length= cmax(args[1]->max_length, args[2]->max_length);
2385
uint32_t Item_func_if::decimal_precision() const
2387
int precision=(cmax(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
2389
return cmin(precision, DECIMAL_MAX_PRECISION);
2394
Item_func_if::val_real()
2397
Item *arg= args[0]->val_bool() ? args[1] : args[2];
2398
double value= arg->val_real();
2399
null_value=arg->null_value;
2404
Item_func_if::val_int()
2407
Item *arg= args[0]->val_bool() ? args[1] : args[2];
2408
int64_t value=arg->val_int();
2409
null_value=arg->null_value;
2414
Item_func_if::val_str(String *str)
2417
Item *arg= args[0]->val_bool() ? args[1] : args[2];
2418
String *res=arg->val_str(str);
2420
res->set_charset(collation.collation);
2421
null_value=arg->null_value;
2427
Item_func_if::val_decimal(my_decimal *decimal_value)
2430
Item *arg= args[0]->val_bool() ? args[1] : args[2];
2431
my_decimal *value= arg->val_decimal(decimal_value);
2432
null_value= arg->null_value;
2438
Item_func_nullif::fix_length_and_dec()
2440
Item_bool_func2::fix_length_and_dec();
2442
if (args[0]) // Only false if EOM
2444
max_length=args[0]->max_length;
2445
decimals=args[0]->decimals;
2446
unsigned_flag= args[0]->unsigned_flag;
2447
cached_result_type= args[0]->result_type();
2448
if (cached_result_type == STRING_RESULT &&
2449
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1))
2457
Note that we have to evaluate the first argument twice as the compare
2458
may have been done with a different type than return value
2460
NULL if arguments are equal
2462
the first argument if not equal
2466
Item_func_nullif::val_real()
2475
value= args[0]->val_real();
2476
null_value=args[0]->null_value;
2481
Item_func_nullif::val_int()
2490
value=args[0]->val_int();
2491
null_value=args[0]->null_value;
2496
Item_func_nullif::val_str(String *str)
2505
res=args[0]->val_str(str);
2506
null_value=args[0]->null_value;
2512
Item_func_nullif::val_decimal(my_decimal * decimal_value)
2521
res= args[0]->val_decimal(decimal_value);
2522
null_value= args[0]->null_value;
2528
Item_func_nullif::is_null()
2530
return (null_value= (!cmp.compare() ? 1 : args[0]->null_value));
2535
Find and return matching items for CASE or ELSE item if all compares
2536
are failed or NULL if ELSE item isn't defined.
2539
In order to do correct comparisons of the CASE expression (the expression
2540
between CASE and the first WHEN) with each WHEN expression several
2541
comparators are used. One for each result type. CASE expression can be
2542
evaluated up to # of different result types are used. To check whether
2543
the CASE expression already was evaluated for a particular result type
2544
a bit mapped variable value_added_map is used. Result types are mapped
2545
to it according to their int values i.e. STRING_RESULT is mapped to bit
2546
0, REAL_RESULT to bit 1, so on.
2549
NULL Nothing found and there is no ELSE expression defined
2551
item Found item or ELSE item if defined and all comparisons are
2555
Item *Item_func_case::find_item(String *str __attribute__((unused)))
2557
uint32_t value_added_map= 0;
2559
if (first_expr_num == -1)
2561
for (uint32_t i=0 ; i < ncases ; i+=2)
2563
// No expression between CASE and the first WHEN
2564
if (args[i]->val_bool())
2571
/* Compare every WHEN argument with it and return the first match */
2572
for (uint32_t i=0 ; i < ncases ; i+=2)
2574
cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
2575
assert(cmp_type != ROW_RESULT);
2576
assert(cmp_items[(uint)cmp_type]);
2577
if (!(value_added_map & (1<<(uint)cmp_type)))
2579
cmp_items[(uint)cmp_type]->store_value(args[first_expr_num]);
2580
if ((null_value=args[first_expr_num]->null_value))
2581
return else_expr_num != -1 ? args[else_expr_num] : 0;
2582
value_added_map|= 1<<(uint)cmp_type;
2584
if (!cmp_items[(uint)cmp_type]->cmp(args[i]) && !args[i]->null_value)
2588
// No, WHEN clauses all missed, return ELSE expression
2589
return else_expr_num != -1 ? args[else_expr_num] : 0;
2593
String *Item_func_case::val_str(String *str)
2597
Item *item=find_item(str);
2605
if (!(res=item->val_str(str)))
2611
int64_t Item_func_case::val_int()
2614
char buff[MAX_FIELD_WIDTH];
2615
String dummy_str(buff,sizeof(buff),default_charset());
2616
Item *item=find_item(&dummy_str);
2624
res=item->val_int();
2625
null_value=item->null_value;
2629
double Item_func_case::val_real()
2632
char buff[MAX_FIELD_WIDTH];
2633
String dummy_str(buff,sizeof(buff),default_charset());
2634
Item *item=find_item(&dummy_str);
2642
res= item->val_real();
2643
null_value=item->null_value;
2648
my_decimal *Item_func_case::val_decimal(my_decimal *decimal_value)
2651
char buff[MAX_FIELD_WIDTH];
2652
String dummy_str(buff, sizeof(buff), default_charset());
2653
Item *item= find_item(&dummy_str);
2662
res= item->val_decimal(decimal_value);
2663
null_value= item->null_value;
2668
bool Item_func_case::fix_fields(THD *thd, Item **ref)
2671
buff should match stack usage from
2672
Item_func_case::val_int() -> Item_func_case::find_item()
2674
unsigned char buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(int64_t)*2];
2675
bool res= Item_func::fix_fields(thd, ref);
2677
Call check_stack_overrun after fix_fields to be sure that stack variable
2678
is not optimized away
2680
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2681
return true; // Fatal error flag is set!
2686
void Item_func_case::agg_str_lengths(Item* arg)
2688
set_if_bigger(max_length, arg->max_length);
2689
set_if_bigger(decimals, arg->decimals);
2690
unsigned_flag= unsigned_flag && arg->unsigned_flag;
2694
void Item_func_case::agg_num_lengths(Item *arg)
2696
uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2697
arg->unsigned_flag) - arg->decimals;
2698
set_if_bigger(max_length, len);
2699
set_if_bigger(decimals, arg->decimals);
2700
unsigned_flag= unsigned_flag && arg->unsigned_flag;
2704
void Item_func_case::fix_length_and_dec()
2708
uint32_t found_types= 0;
2709
if (!(agg= (Item**) sql_alloc(sizeof(Item*)*(ncases+1))))
2713
Aggregate all THEN and ELSE expression types
2714
and collations when string result
2717
for (nagg= 0 ; nagg < ncases/2 ; nagg++)
2718
agg[nagg]= args[nagg*2+1];
2720
if (else_expr_num != -1)
2721
agg[nagg++]= args[else_expr_num];
2723
agg_result_type(&cached_result_type, agg, nagg);
2724
if ((cached_result_type == STRING_RESULT) &&
2725
agg_arg_charsets(collation, agg, nagg, MY_COLL_ALLOW_CONV, 1))
2728
cached_field_type= agg_field_type(agg, nagg);
2730
Aggregate first expression and all THEN expression types
2731
and collations when string comparison
2733
if (first_expr_num != -1)
2736
agg[0]= args[first_expr_num];
2737
left_result_type= agg[0]->result_type();
2739
for (nagg= 0; nagg < ncases/2 ; nagg++)
2740
agg[nagg+1]= args[nagg*2];
2742
if (!(found_types= collect_cmp_types(agg, nagg)))
2745
for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
2747
if (found_types & (1 << i) && !cmp_items[i])
2749
assert((Item_result)i != ROW_RESULT);
2750
if ((Item_result)i == STRING_RESULT &&
2751
agg_arg_charsets(cmp_collation, agg, nagg, MY_COLL_CMP_CONV, 1))
2754
cmp_item::get_comparator((Item_result)i,
2755
cmp_collation.collation)))
2761
if (else_expr_num == -1 || args[else_expr_num]->maybe_null)
2766
unsigned_flag= true;
2767
if (cached_result_type == STRING_RESULT)
2769
for (uint32_t i= 0; i < ncases; i+= 2)
2770
agg_str_lengths(args[i + 1]);
2771
if (else_expr_num != -1)
2772
agg_str_lengths(args[else_expr_num]);
2776
for (uint32_t i= 0; i < ncases; i+= 2)
2777
agg_num_lengths(args[i + 1]);
2778
if (else_expr_num != -1)
2779
agg_num_lengths(args[else_expr_num]);
2780
max_length= my_decimal_precision_to_length(max_length + decimals, decimals,
2786
uint32_t Item_func_case::decimal_precision() const
2789
for (uint32_t i=0 ; i < ncases ; i+=2)
2790
set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
2792
if (else_expr_num != -1)
2793
set_if_bigger(max_int_part, args[else_expr_num]->decimal_int_part());
2794
return cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2800
Fix this so that it prints the whole CASE expression
2803
void Item_func_case::print(String *str, enum_query_type query_type)
2805
str->append(STRING_WITH_LEN("(case "));
2806
if (first_expr_num != -1)
2808
args[first_expr_num]->print(str, query_type);
2811
for (uint32_t i=0 ; i < ncases ; i+=2)
2813
str->append(STRING_WITH_LEN("when "));
2814
args[i]->print(str, query_type);
2815
str->append(STRING_WITH_LEN(" then "));
2816
args[i+1]->print(str, query_type);
2819
if (else_expr_num != -1)
2821
str->append(STRING_WITH_LEN("else "));
2822
args[else_expr_num]->print(str, query_type);
2825
str->append(STRING_WITH_LEN("end)"));
2829
void Item_func_case::cleanup()
2832
Item_func::cleanup();
2833
for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
2835
delete cmp_items[i];
2843
Coalesce - return first not NULL argument.
2846
String *Item_func_coalesce::str_op(String *str)
2850
for (uint32_t i=0 ; i < arg_count ; i++)
2853
if ((res=args[i]->val_str(str)))
2860
int64_t Item_func_coalesce::int_op()
2864
for (uint32_t i=0 ; i < arg_count ; i++)
2866
int64_t res=args[i]->val_int();
2867
if (!args[i]->null_value)
2874
double Item_func_coalesce::real_op()
2878
for (uint32_t i=0 ; i < arg_count ; i++)
2880
double res= args[i]->val_real();
2881
if (!args[i]->null_value)
2889
my_decimal *Item_func_coalesce::decimal_op(my_decimal *decimal_value)
2893
for (uint32_t i= 0; i < arg_count; i++)
2895
my_decimal *res= args[i]->val_decimal(decimal_value);
2896
if (!args[i]->null_value)
2904
void Item_func_coalesce::fix_length_and_dec()
2906
cached_field_type= agg_field_type(args, arg_count);
2907
agg_result_type(&hybrid_type, args, arg_count);
2908
switch (hybrid_type) {
2910
count_only_length();
2911
decimals= NOT_FIXED_DEC;
2912
agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1);
2914
case DECIMAL_RESULT:
2915
count_decimal_length();
2918
count_real_length();
2921
count_only_length();
2930
/****************************************************************************
2931
Classes and function for the IN operator
2932
****************************************************************************/
2935
Determine which of the signed int64_t arguments is bigger
2940
b_val right argument
2943
This function will compare two signed int64_t arguments
2944
and will return -1, 0, or 1 if left argument is smaller than,
2945
equal to or greater than the right argument.
2948
-1 left argument is smaller than the right argument.
2949
0 left argument is equal to the right argument.
2950
1 left argument is greater than the right argument.
2952
static inline int cmp_longs (int64_t a_val, int64_t b_val)
2954
return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
2959
Determine which of the unsigned int64_t arguments is bigger
2964
b_val right argument
2967
This function will compare two unsigned int64_t arguments
2968
and will return -1, 0, or 1 if left argument is smaller than,
2969
equal to or greater than the right argument.
2972
-1 left argument is smaller than the right argument.
2973
0 left argument is equal to the right argument.
2974
1 left argument is greater than the right argument.
2976
static inline int cmp_ulongs (uint64_t a_val, uint64_t b_val)
2978
return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
2983
Compare two integers in IN value list format (packed_int64_t)
2987
cmp_arg an argument passed to the calling function (my_qsort2)
2992
This function will compare two integer arguments in the IN value list
2993
format and will return -1, 0, or 1 if left argument is smaller than,
2994
equal to or greater than the right argument.
2995
It's used in sorting the IN values list and finding an element in it.
2996
Depending on the signedness of the arguments cmp_int64_t() will
2997
compare them as either signed (using cmp_longs()) or unsigned (using
3001
-1 left argument is smaller than the right argument.
3002
0 left argument is equal to the right argument.
3003
1 left argument is greater than the right argument.
3005
int cmp_int64_t(void *cmp_arg __attribute__((unused)),
3006
in_int64_t::packed_int64_t *a,
3007
in_int64_t::packed_int64_t *b)
3009
if (a->unsigned_flag != b->unsigned_flag)
3012
One of the args is unsigned and is too big to fit into the
3013
positive signed range. Report no match.
3015
if ((a->unsigned_flag && ((uint64_t) a->val) > (uint64_t) INT64_MAX) ||
3016
(b->unsigned_flag && ((uint64_t) b->val) > (uint64_t) INT64_MAX))
3017
return a->unsigned_flag ? 1 : -1;
3019
Although the signedness differs both args can fit into the signed
3020
positive range. Make them signed and compare as usual.
3022
return cmp_longs (a->val, b->val);
3024
if (a->unsigned_flag)
3025
return cmp_ulongs ((uint64_t) a->val, (uint64_t) b->val);
3027
return cmp_longs (a->val, b->val);
3030
static int cmp_double(void *cmp_arg __attribute__((unused)), double *a,double *b)
3032
return *a < *b ? -1 : *a == *b ? 0 : 1;
3035
static int cmp_row(void *cmp_arg __attribute__((unused)), cmp_item_row *a, cmp_item_row *b)
3037
return a->compare(b);
3041
static int cmp_decimal(void *cmp_arg __attribute__((unused)), my_decimal *a, my_decimal *b)
3044
We need call of fixing buffer pointer, because fast sort just copy
3045
decimal buffers in memory and pointers left pointing on old buffer place
3047
a->fix_buffer_pointer();
3048
b->fix_buffer_pointer();
3049
return my_decimal_cmp(a, b);
3053
int in_vector::find(Item *item)
3055
unsigned char *result=get_value(item);
3056
if (!result || !used_count)
3057
return 0; // Null value
3060
start=0; end=used_count-1;
3061
while (start != end)
3063
uint32_t mid=(start+end+1)/2;
3065
if ((res=(*compare)(collation, base+mid*size, result)) == 0)
3072
return (int) ((*compare)(collation, base+start*size, result) == 0);
3075
in_string::in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs)
3076
:in_vector(elements, sizeof(String), cmp_func, cs),
3077
tmp(buff, sizeof(buff), &my_charset_bin)
3080
in_string::~in_string()
3084
// base was allocated with help of sql_alloc => following is OK
3085
for (uint32_t i=0 ; i < count ; i++)
3086
((String*) base)[i].free();
3090
void in_string::set(uint32_t pos,Item *item)
3092
String *str=((String*) base)+pos;
3093
String *res=item->val_str(str);
3094
if (res && res != str)
3096
if (res->uses_buffer_owned_by(str))
3098
if (item->type() == Item::FUNC_ITEM)
3103
if (!str->charset())
3105
const CHARSET_INFO *cs;
3106
if (!(cs= item->collation.collation))
3107
cs= &my_charset_bin; // Should never happen for STR items
3108
str->set_charset(cs);
3113
unsigned char *in_string::get_value(Item *item)
3115
return (unsigned char*) item->val_str(&tmp);
3118
in_row::in_row(uint32_t elements, Item * item __attribute__((unused)))
3120
base= (char*) new cmp_item_row[count= elements];
3121
size= sizeof(cmp_item_row);
3122
compare= (qsort2_cmp) cmp_row;
3124
We need to reset these as otherwise we will call sort() with
3125
uninitialized (even if not used) elements
3127
used_count= elements;
3134
delete [] (cmp_item_row*) base;
3137
unsigned char *in_row::get_value(Item *item)
3139
tmp.store_value(item);
3140
if (item->is_null())
3142
return (unsigned char *)&tmp;
3145
void in_row::set(uint32_t pos, Item *item)
3147
((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3151
in_int64_t::in_int64_t(uint32_t elements)
3152
:in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3155
void in_int64_t::set(uint32_t pos,Item *item)
3157
struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3159
buff->val= item->val_int();
3160
buff->unsigned_flag= item->unsigned_flag;
3163
unsigned char *in_int64_t::get_value(Item *item)
3165
tmp.val= item->val_int();
3166
if (item->null_value)
3168
tmp.unsigned_flag= item->unsigned_flag;
3169
return (unsigned char*) &tmp;
3172
void in_datetime::set(uint32_t pos,Item *item)
3174
Item **tmp_item= &item;
3176
struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3178
buff->val= get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
3179
buff->unsigned_flag= 1L;
3182
unsigned char *in_datetime::get_value(Item *item)
3185
Item **tmp_item= lval_cache ? &lval_cache : &item;
3186
tmp.val= get_datetime_value(thd, &tmp_item, &lval_cache, warn_item, &is_null);
3187
if (item->null_value)
3189
tmp.unsigned_flag= 1L;
3190
return (unsigned char*) &tmp;
3193
in_double::in_double(uint32_t elements)
3194
:in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
3197
void in_double::set(uint32_t pos,Item *item)
3199
((double*) base)[pos]= item->val_real();
3202
unsigned char *in_double::get_value(Item *item)
3204
tmp= item->val_real();
3205
if (item->null_value)
3206
return 0; /* purecov: inspected */
3207
return (unsigned char*) &tmp;
3211
in_decimal::in_decimal(uint32_t elements)
3212
:in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3216
void in_decimal::set(uint32_t pos, Item *item)
3218
/* as far as 'item' is constant, we can store reference on my_decimal */
3219
my_decimal *dec= ((my_decimal *)base) + pos;
3220
dec->len= DECIMAL_BUFF_LENGTH;
3221
dec->fix_buffer_pointer();
3222
my_decimal *res= item->val_decimal(dec);
3223
/* if item->val_decimal() is evaluated to NULL then res == 0 */
3224
if (!item->null_value && res != dec)
3225
my_decimal2decimal(res, dec);
3229
unsigned char *in_decimal::get_value(Item *item)
3231
my_decimal *result= item->val_decimal(&val);
3232
if (item->null_value)
3234
return (unsigned char *)result;
3238
cmp_item* cmp_item::get_comparator(Item_result type,
3239
const CHARSET_INFO * const cs)
3243
return new cmp_item_sort_string(cs);
3245
return new cmp_item_int;
3247
return new cmp_item_real;
3249
return new cmp_item_row;
3250
case DECIMAL_RESULT:
3251
return new cmp_item_decimal;
3256
return 0; // to satisfy compiler :)
3260
cmp_item* cmp_item_sort_string::make_same()
3262
return new cmp_item_sort_string_in_static(cmp_charset);
3265
cmp_item* cmp_item_int::make_same()
3267
return new cmp_item_int();
3270
cmp_item* cmp_item_real::make_same()
3272
return new cmp_item_real();
3275
cmp_item* cmp_item_row::make_same()
3277
return new cmp_item_row();
3281
cmp_item_row::~cmp_item_row()
3285
for (uint32_t i= 0; i < n; i++)
3288
delete comparators[i];
3295
void cmp_item_row::alloc_comparators()
3298
comparators= (cmp_item **) current_thd->calloc(sizeof(cmp_item *)*n);
3302
void cmp_item_row::store_value(Item *item)
3305
alloc_comparators();
3308
item->bring_value();
3309
item->null_value= 0;
3310
for (uint32_t i=0; i < n; i++)
3312
if (!comparators[i])
3313
if (!(comparators[i]=
3314
cmp_item::get_comparator(item->element_index(i)->result_type(),
3315
item->element_index(i)->collation.collation)))
3316
break; // new failed
3317
comparators[i]->store_value(item->element_index(i));
3318
item->null_value|= item->element_index(i)->null_value;
3325
void cmp_item_row::store_value_by_template(cmp_item *t, Item *item)
3327
cmp_item_row *tmpl= (cmp_item_row*) t;
3328
if (tmpl->n != item->cols())
3330
my_error(ER_OPERAND_COLUMNS, MYF(0), tmpl->n);
3334
if ((comparators= (cmp_item **) sql_alloc(sizeof(cmp_item *)*n)))
3336
item->bring_value();
3337
item->null_value= 0;
3338
for (uint32_t i=0; i < n; i++)
3340
if (!(comparators[i]= tmpl->comparators[i]->make_same()))
3341
break; // new failed
3342
comparators[i]->store_value_by_template(tmpl->comparators[i],
3343
item->element_index(i));
3344
item->null_value|= item->element_index(i)->null_value;
3350
int cmp_item_row::cmp(Item *arg)
3353
if (arg->cols() != n)
3355
my_error(ER_OPERAND_COLUMNS, MYF(0), n);
3360
for (uint32_t i=0; i < n; i++)
3362
if (comparators[i]->cmp(arg->element_index(i)))
3364
if (!arg->element_index(i)->null_value)
3369
return (arg->null_value= was_null);
3373
int cmp_item_row::compare(cmp_item *c)
3375
cmp_item_row *l_cmp= (cmp_item_row *) c;
3376
for (uint32_t i=0; i < n; i++)
3379
if ((res= comparators[i]->compare(l_cmp->comparators[i])))
3386
void cmp_item_decimal::store_value(Item *item)
3388
my_decimal *val= item->val_decimal(&value);
3389
/* val may be zero if item is nnull */
3390
if (val && val != &value)
3391
my_decimal2decimal(val, &value);
3395
int cmp_item_decimal::cmp(Item *arg)
3397
my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3398
if (arg->null_value)
3400
return my_decimal_cmp(&value, tmp);
3404
int cmp_item_decimal::compare(cmp_item *arg)
3406
cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3407
return my_decimal_cmp(&value, &l_cmp->value);
3411
cmp_item* cmp_item_decimal::make_same()
3413
return new cmp_item_decimal();
3417
void cmp_item_datetime::store_value(Item *item)
3420
Item **tmp_item= lval_cache ? &lval_cache : &item;
3421
value= get_datetime_value(thd, &tmp_item, &lval_cache, warn_item, &is_null);
3425
int cmp_item_datetime::cmp(Item *arg)
3428
Item **tmp_item= &arg;
3430
get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
3434
int cmp_item_datetime::compare(cmp_item *ci)
3436
cmp_item_datetime *l_cmp= (cmp_item_datetime *)ci;
3437
return (value < l_cmp->value) ? -1 : ((value == l_cmp->value) ? 0 : 1);
3441
cmp_item *cmp_item_datetime::make_same()
3443
return new cmp_item_datetime(warn_item);
3447
bool Item_func_in::nulls_in_row()
3449
Item **arg,**arg_end;
3450
for (arg= args+1, arg_end= args+arg_count; arg != arg_end ; arg++)
3452
if ((*arg)->null_inside())
3460
Perform context analysis of an IN item tree.
3462
This function performs context analysis (name resolution) and calculates
3463
various attributes of the item tree with Item_func_in as its root.
3464
The function saves in ref the pointer to the item or to a newly created
3465
item that is considered as a replacement for the original one.
3467
@param thd reference to the global context of the query thread
3468
@param ref pointer to Item* variable where pointer to resulting "fixed"
3469
item is to be assigned
3472
Let T0(e)/T1(e) be the value of not_null_tables(e) when e is used on
3473
a predicate/function level. Then it's easy to show that:
3475
T0(e IN(e1,...,en)) = union(T1(e),intersection(T1(ei)))
3476
T1(e IN(e1,...,en)) = union(T1(e),intersection(T1(ei)))
3477
T0(e NOT IN(e1,...,en)) = union(T1(e),union(T1(ei)))
3478
T1(e NOT IN(e1,...,en)) = union(T1(e),intersection(T1(ei)))
3488
Item_func_in::fix_fields(THD *thd, Item **ref)
3490
Item **arg, **arg_end;
3492
if (Item_func_opt_neg::fix_fields(thd, ref))
3495
/* not_null_tables_cache == union(T1(e),union(T1(ei))) */
3496
if (pred_level && negated)
3499
/* not_null_tables_cache = union(T1(e),intersection(T1(ei))) */
3500
not_null_tables_cache= ~(table_map) 0;
3501
for (arg= args + 1, arg_end= args + arg_count; arg != arg_end; arg++)
3502
not_null_tables_cache&= (*arg)->not_null_tables();
3503
not_null_tables_cache|= (*args)->not_null_tables();
3508
static int srtcmp_in(const CHARSET_INFO * const cs, const String *x,const String *y)
3510
return cs->coll->strnncollsp(cs,
3511
(unsigned char *) x->ptr(),x->length(),
3512
(unsigned char *) y->ptr(),y->length(), 0);
3516
void Item_func_in::fix_length_and_dec()
3518
Item **arg, **arg_end;
3520
THD *thd= current_thd;
3521
bool datetime_found= false;
3522
/* true <=> arguments values will be compared as DATETIMEs. */
3523
bool compare_as_datetime= false;
3525
uint32_t found_types= 0;
3526
uint32_t type_cnt= 0, i;
3527
Item_result cmp_type= STRING_RESULT;
3528
left_result_type= args[0]->result_type();
3529
if (!(found_types= collect_cmp_types(args, arg_count)))
3532
for (arg= args + 1, arg_end= args + arg_count; arg != arg_end ; arg++)
3534
if (!arg[0]->const_item())
3540
for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
3542
if (found_types & 1 << i)
3545
cmp_type= (Item_result) i;
3551
if (cmp_type == STRING_RESULT &&
3552
agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1))
3554
arg_types_compatible= true;
3559
When comparing rows create the row comparator object beforehand to ease
3560
the DATETIME comparison detection procedure.
3562
if (cmp_type == ROW_RESULT)
3564
cmp_item_row *cmp= 0;
3565
if (const_itm && !nulls_in_row())
3567
array= new in_row(arg_count-1, 0);
3568
cmp= &((in_row*)array)->tmp;
3572
if (!(cmp= new cmp_item_row))
3574
cmp_items[ROW_RESULT]= cmp;
3576
cmp->n= args[0]->cols();
3577
cmp->alloc_comparators();
3579
/* All DATE/DATETIME fields/functions has the STRING result type. */
3580
if (cmp_type == STRING_RESULT || cmp_type == ROW_RESULT)
3582
uint32_t col, cols= args[0]->cols();
3584
for (col= 0; col < cols; col++)
3586
bool skip_column= false;
3588
Check that all items to be compared has the STRING result type and at
3589
least one of them is a DATE/DATETIME item.
3591
for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
3593
Item *itm= ((cmp_type == STRING_RESULT) ? arg[0] :
3594
arg[0]->element_index(col));
3595
if (itm->result_type() != STRING_RESULT)
3600
else if (itm->is_datetime())
3602
datetime_found= true;
3604
Internally all DATE/DATETIME values are converted to the DATETIME
3605
type. So try to find a DATETIME item to issue correct warnings.
3609
else if (itm->field_type() == DRIZZLE_TYPE_DATETIME)
3612
/* All arguments are already checked to have the STRING result. */
3613
if (cmp_type == STRING_RESULT)
3622
if (cmp_type == ROW_RESULT)
3626
cmp= ((in_row*)array)->tmp.comparators + col;
3628
cmp= ((cmp_item_row*)cmp_items[ROW_RESULT])->comparators + col;
3629
*cmp= new cmp_item_datetime(date_arg);
3630
/* Reset variables for the next column. */
3632
datetime_found= false;
3635
compare_as_datetime= true;
3641
Row item with NULLs inside can return NULL or false =>
3642
they can't be processed as static
3644
if (type_cnt == 1 && const_itm && !nulls_in_row())
3646
if (compare_as_datetime)
3647
array= new in_datetime(date_arg, arg_count - 1);
3651
IN must compare INT columns and constants as int values (the same
3652
way as equality does).
3653
So we must check here if the column on the left and all the constant
3654
values on the right can be compared as integers and adjust the
3655
comparison type accordingly.
3657
if (args[0]->real_item()->type() == FIELD_ITEM &&
3658
thd->lex->sql_command != SQLCOM_SHOW_CREATE &&
3659
cmp_type != INT_RESULT)
3661
Item_field *field_item= (Item_field*) (args[0]->real_item());
3662
if (field_item->field->can_be_compared_as_int64_t())
3664
bool all_converted= true;
3665
for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3667
if (!convert_constant_item (thd, field_item, &arg[0]))
3668
all_converted= false;
3671
cmp_type= INT_RESULT;
3676
array=new in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
3677
cmp_collation.collation);
3680
array= new in_int64_t(arg_count-1);
3683
array= new in_double(arg_count-1);
3687
The row comparator was created at the beginning but only DATETIME
3688
items comparators were initialized. Call store_value() to setup
3691
((in_row*)array)->tmp.store_value(args[0]);
3693
case DECIMAL_RESULT:
3694
array= new in_decimal(arg_count - 1);
3701
if (array && !(thd->is_fatal_error)) // If not EOM
3704
for (uint32_t i=1 ; i < arg_count ; i++)
3706
array->set(j,args[i]);
3707
if (!args[i]->null_value) // Skip NULL values
3712
if ((array->used_count= j))
3718
if (compare_as_datetime)
3719
cmp_items[STRING_RESULT]= new cmp_item_datetime(date_arg);
3722
for (i= 0; i <= (uint) DECIMAL_RESULT; i++)
3724
if (found_types & (1 << i) && !cmp_items[i])
3726
if ((Item_result)i == STRING_RESULT &&
3727
agg_arg_charsets(cmp_collation, args, arg_count,
3728
MY_COLL_CMP_CONV, 1))
3730
if (!cmp_items[i] && !(cmp_items[i]=
3731
cmp_item::get_comparator((Item_result)i,
3732
cmp_collation.collation)))
3742
void Item_func_in::print(String *str, enum_query_type query_type)
3745
args[0]->print(str, query_type);
3747
str->append(STRING_WITH_LEN(" not"));
3748
str->append(STRING_WITH_LEN(" in ("));
3749
print_args(str, 1, query_type);
3750
str->append(STRING_WITH_LEN("))"));
3755
Evaluate the function and return its value.
3761
Evaluate the function and return its value.
3764
If the array object is defined then the value of the function is
3765
calculated by means of this array.
3766
Otherwise several cmp_item objects are used in order to do correct
3767
comparison of left expression and an expression from the values list.
3768
One cmp_item object correspond to one used comparison type. Left
3769
expression can be evaluated up to number of different used comparison
3770
types. A bit mapped variable value_added_map is used to check whether
3771
the left expression already was evaluated for a particular result type.
3772
Result types are mapped to it according to their integer values i.e.
3773
STRING_RESULT is mapped to bit 0, REAL_RESULT to bit 1, so on.
3776
Value of the function
3779
int64_t Item_func_in::val_int()
3783
uint32_t value_added_map= 0;
3786
int tmp=array->find(args[0]);
3787
null_value=args[0]->null_value || (!tmp && have_null);
3788
return (int64_t) (!null_value && tmp != negated);
3791
for (uint32_t i= 1 ; i < arg_count ; i++)
3793
Item_result cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
3794
in_item= cmp_items[(uint)cmp_type];
3796
if (!(value_added_map & (1 << (uint)cmp_type)))
3798
in_item->store_value(args[0]);
3799
if ((null_value=args[0]->null_value))
3802
value_added_map|= 1 << (uint)cmp_type;
3804
if (!in_item->cmp(args[i]) && !args[i]->null_value)
3805
return (int64_t) (!negated);
3806
have_null|= args[i]->null_value;
3809
null_value= have_null;
3810
return (int64_t) (!null_value && negated);
3814
int64_t Item_func_bit_or::val_int()
3817
uint64_t arg1= (uint64_t) args[0]->val_int();
3818
if (args[0]->null_value)
3820
null_value=1; /* purecov: inspected */
3821
return 0; /* purecov: inspected */
3823
uint64_t arg2= (uint64_t) args[1]->val_int();
3824
if (args[1]->null_value)
3830
return (int64_t) (arg1 | arg2);
3834
int64_t Item_func_bit_and::val_int()
3837
uint64_t arg1= (uint64_t) args[0]->val_int();
3838
if (args[0]->null_value)
3840
null_value=1; /* purecov: inspected */
3841
return 0; /* purecov: inspected */
3843
uint64_t arg2= (uint64_t) args[1]->val_int();
3844
if (args[1]->null_value)
3846
null_value=1; /* purecov: inspected */
3847
return 0; /* purecov: inspected */
3850
return (int64_t) (arg1 & arg2);
3853
Item_cond::Item_cond(THD *thd, Item_cond *item)
3854
:Item_bool_func(thd, item),
3855
abort_on_null(item->abort_on_null),
3856
and_tables_cache(item->and_tables_cache)
3859
item->list will be copied by copy_andor_arguments() call
3864
void Item_cond::copy_andor_arguments(THD *thd, Item_cond *item)
3866
List_iterator_fast<Item> li(item->list);
3867
while (Item *it= li++)
3868
list.push_back(it->copy_andor_structure(thd));
3873
Item_cond::fix_fields(THD *thd, Item **ref __attribute__((unused)))
3876
List_iterator<Item> li(list);
3878
void *orig_thd_marker= thd->thd_marker;
3879
unsigned char buff[sizeof(char*)]; // Max local vars in function
3880
not_null_tables_cache= used_tables_cache= 0;
3881
const_item_cache= 1;
3883
if (functype() == COND_OR_FUNC)
3886
and_table_cache is the value that Item_cond_or() returns for
3889
and_tables_cache= ~(table_map) 0;
3891
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
3892
return true; // Fatal error flag is set!
3894
The following optimization reduces the depth of an AND-OR tree.
3895
E.g. a WHERE clause like
3896
F1 AND (F2 AND (F2 AND F4))
3897
is parsed into a tree with the same nested structure as defined
3898
by braces. This optimization will transform such tree into
3899
AND (F1, F2, F3, F4).
3900
Trees of OR items are flattened as well:
3901
((F1 OR F2) OR (F3 OR F4)) => OR (F1, F2, F3, F4)
3902
Items for removed AND/OR levels will dangle until the death of the
3904
The optimization is currently prepared statements and stored procedures
3905
friendly as it doesn't allocate any memory and its effects are durable
3906
(i.e. do not depend on PS/SP arguments).
3910
table_map tmp_table_map;
3911
while (item->type() == Item::COND_ITEM &&
3912
((Item_cond*) item)->functype() == functype() &&
3913
!((Item_cond*) item)->list.is_empty())
3914
{ // Identical function
3915
li.replace(((Item_cond*) item)->list);
3916
((Item_cond*) item)->list.empty();
3917
item= *li.ref(); // new current item
3920
item->top_level_item();
3922
// item can be substituted in fix_fields
3923
if ((!item->fixed &&
3924
item->fix_fields(thd, li.ref())) ||
3925
(item= *li.ref())->check_cols(1))
3926
return true; /* purecov: inspected */
3927
used_tables_cache|= item->used_tables();
3928
if (item->const_item())
3929
and_tables_cache= (table_map) 0;
3932
tmp_table_map= item->not_null_tables();
3933
not_null_tables_cache|= tmp_table_map;
3934
and_tables_cache&= tmp_table_map;
3935
const_item_cache= false;
3937
with_sum_func= with_sum_func || item->with_sum_func;
3938
with_subselect|= item->with_subselect;
3939
if (item->maybe_null)
3942
thd->lex->current_select->cond_count+= list.elements;
3943
thd->thd_marker= orig_thd_marker;
3944
fix_length_and_dec();
3950
void Item_cond::fix_after_pullout(st_select_lex *new_parent, Item **ref __attribute__((unused)))
3952
List_iterator<Item> li(list);
3955
used_tables_cache=0;
3958
and_tables_cache= ~(table_map) 0; // Here and below we do as fix_fields does
3959
not_null_tables_cache= 0;
3963
table_map tmp_table_map;
3964
item->fix_after_pullout(new_parent, li.ref());
3966
used_tables_cache|= item->used_tables();
3967
const_item_cache&= item->const_item();
3969
if (item->const_item())
3970
and_tables_cache= (table_map) 0;
3973
tmp_table_map= item->not_null_tables();
3974
not_null_tables_cache|= tmp_table_map;
3975
and_tables_cache&= tmp_table_map;
3976
const_item_cache= false;
3982
bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
3984
List_iterator_fast<Item> li(list);
3986
while ((item= li++))
3987
if (item->walk(processor, walk_subquery, arg))
3989
return Item_func::walk(processor, walk_subquery, arg);
3994
Transform an Item_cond object with a transformer callback function.
3996
The function recursively applies the transform method to each
3997
member item of the condition list.
3998
If the call of the method for a member item returns a new item
3999
the old item is substituted for a new one.
4000
After this the transformer is applied to the root node
4001
of the Item_cond object.
4003
@param transformer the transformer callback function to be applied to
4004
the nodes of the tree of the object
4005
@param arg parameter to be passed to the transformer
4008
Item returned as the result of transformation of the root node
4011
Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
4013
List_iterator<Item> li(list);
4015
while ((item= li++))
4017
Item *new_item= item->transform(transformer, arg);
4022
THD::change_item_tree() should be called only if the tree was
4023
really transformed, i.e. when a new item has been created.
4024
Otherwise we'll be allocating a lot of unnecessary memory for
4025
change records at each execution.
4027
if (new_item != item)
4028
current_thd->change_item_tree(li.ref(), new_item);
4030
return Item_func::transform(transformer, arg);
4035
Compile Item_cond object with a processor and a transformer
4038
First the function applies the analyzer to the root node of
4039
the Item_func object. Then if the analyzer succeeeds (returns true)
4040
the function recursively applies the compile method to member
4041
item of the condition list.
4042
If the call of the method for a member item returns a new item
4043
the old item is substituted for a new one.
4044
After this the transformer is applied to the root node
4045
of the Item_cond object.
4047
@param analyzer the analyzer callback function to be applied to the
4048
nodes of the tree of the object
4049
@param[in,out] arg_p parameter to be passed to the analyzer
4050
@param transformer the transformer callback function to be applied to the
4051
nodes of the tree of the object
4052
@param arg_t parameter to be passed to the transformer
4055
Item returned as the result of transformation of the root node
4058
Item *Item_cond::compile(Item_analyzer analyzer, unsigned char **arg_p,
4059
Item_transformer transformer, unsigned char *arg_t)
4061
if (!(this->*analyzer)(arg_p))
4064
List_iterator<Item> li(list);
4066
while ((item= li++))
4069
The same parameter value of arg_p must be passed
4070
to analyze any argument of the condition formula.
4072
unsigned char *arg_v= *arg_p;
4073
Item *new_item= item->compile(analyzer, &arg_v, transformer, arg_t);
4074
if (new_item && new_item != item)
4075
li.replace(new_item);
4077
return Item_func::transform(transformer, arg_t);
4080
void Item_cond::traverse_cond(Cond_traverser traverser,
4081
void *arg, traverse_order order)
4083
List_iterator<Item> li(list);
4088
(*traverser)(this, arg);
4089
while ((item= li++))
4091
item->traverse_cond(traverser, arg, order);
4093
(*traverser)(NULL, arg);
4096
while ((item= li++))
4098
item->traverse_cond(traverser, arg, order);
4100
(*traverser)(this, arg);
4105
Move SUM items out from item tree and replace with reference.
4107
The split is done to get an unique item for each SUM function
4108
so that we can easily find and calculate them.
4109
(Calculation done by update_sum_func() and copy_sum_funcs() in
4112
@param thd Thread handler
4113
@param ref_pointer_array Pointer to array of reference fields
4114
@param fields All fields in select
4117
This function is run on all expression (SELECT list, WHERE, HAVING etc)
4118
that have or refer (HAVING) to a SUM expression.
4121
void Item_cond::split_sum_func(THD *thd, Item **ref_pointer_array,
4124
List_iterator<Item> li(list);
4126
while ((item= li++))
4127
item->split_sum_func2(thd, ref_pointer_array, fields, li.ref(), true);
4132
Item_cond::used_tables() const
4133
{ // This caches used_tables
4134
return used_tables_cache;
4138
void Item_cond::update_used_tables()
4140
List_iterator_fast<Item> li(list);
4143
used_tables_cache=0;
4147
item->update_used_tables();
4148
used_tables_cache|= item->used_tables();
4149
const_item_cache&= item->const_item();
4154
void Item_cond::print(String *str, enum_query_type query_type)
4157
List_iterator_fast<Item> li(list);
4160
item->print(str, query_type);
4164
str->append(func_name());
4166
item->print(str, query_type);
4172
void Item_cond::neg_arguments(THD *thd)
4174
List_iterator<Item> li(list);
4176
while ((item= li++)) /* Apply not transformation to the arguments */
4178
Item *new_item= item->neg_transformer(thd);
4181
if (!(new_item= new Item_func_not(item)))
4182
return; // Fatal OEM error
4184
li.replace(new_item);
4190
Evaluation of AND(expr, expr, expr ...).
4193
abort_if_null is set for AND expressions for which we don't care if the
4194
result is NULL or 0. This is set for:
4200
1 If all expressions are true
4202
0 If all expressions are false or if we find a NULL expression and
4203
'abort_on_null' is set.
4205
NULL if all expression are either 1 or NULL
4209
int64_t Item_cond_and::val_int()
4212
List_iterator_fast<Item> li(list);
4217
if (!item->val_bool())
4219
if (abort_on_null || !(null_value= item->null_value))
4220
return 0; // return false
4223
return null_value ? 0 : 1;
4227
int64_t Item_cond_or::val_int()
4230
List_iterator_fast<Item> li(list);
4235
if (item->val_bool())
4240
if (item->null_value)
4247
Create an AND expression from two expressions.
4249
@param a expression or NULL
4250
@param b expression.
4251
@param org_item Don't modify a if a == *org_item.
4252
If a == NULL, org_item is set to point at b,
4253
to ensure that future calls will not modify b.
4256
This will not modify item pointed to by org_item or b
4257
The idea is that one can call this in a loop and create and
4258
'and' over all items without modifying any of the original items.
4266
Item *and_expressions(Item *a, Item *b, Item **org_item)
4269
return (*org_item= (Item*) b);
4273
if ((res= new Item_cond_and(a, (Item*) b)))
4275
res->used_tables_cache= a->used_tables() | b->used_tables();
4276
res->not_null_tables_cache= a->not_null_tables() | b->not_null_tables();
4280
if (((Item_cond_and*) a)->add((Item*) b))
4282
((Item_cond_and*) a)->used_tables_cache|= b->used_tables();
4283
((Item_cond_and*) a)->not_null_tables_cache|= b->not_null_tables();
4288
int64_t Item_func_isnull::val_int()
4292
Handle optimization if the argument can't be null
4293
This has to be here because of the test in update_used_tables().
4295
if (!used_tables_cache && !with_subselect)
4296
return cached_value;
4297
return args[0]->is_null() ? 1: 0;
4300
int64_t Item_is_not_null_test::val_int()
4303
if (!used_tables_cache && !with_subselect)
4305
owner->was_null|= (!cached_value);
4306
return(cached_value);
4308
if (args[0]->is_null())
4310
owner->was_null|= 1;
4318
Optimize case of not_null_column IS NULL.
4320
void Item_is_not_null_test::update_used_tables()
4322
if (!args[0]->maybe_null)
4324
used_tables_cache= 0; /* is always true */
4325
cached_value= (int64_t) 1;
4329
args[0]->update_used_tables();
4330
if (!(used_tables_cache=args[0]->used_tables()) && !with_subselect)
4332
/* Remember if the value is always NULL or never NULL */
4333
cached_value= (int64_t) !args[0]->is_null();
4339
int64_t Item_func_isnotnull::val_int()
4342
return args[0]->is_null() ? 0 : 1;
4346
void Item_func_isnotnull::print(String *str, enum_query_type query_type)
4349
args[0]->print(str, query_type);
4350
str->append(STRING_WITH_LEN(" is not null)"));
4354
int64_t Item_func_like::val_int()
4357
String* res = args[0]->val_str(&tmp_value1);
4358
if (args[0]->null_value)
4363
String* res2 = args[1]->val_str(&tmp_value2);
4364
if (args[1]->null_value)
4371
return turboBM_matches(res->ptr(), res->length()) ? 1 : 0;
4372
return my_wildcmp(cmp.cmp_collation.collation,
4373
res->ptr(),res->ptr()+res->length(),
4374
res2->ptr(),res2->ptr()+res2->length(),
4375
escape,wild_one,wild_many) ? 0 : 1;
4380
We can optimize a where if first character isn't a wildcard
4383
Item_func::optimize_type Item_func_like::select_optimize() const
4385
if (args[1]->const_item())
4387
String* res2= args[1]->val_str((String *)&tmp_value2);
4390
return OPTIMIZE_NONE;
4392
if (*res2->ptr() != wild_many)
4394
if (args[0]->result_type() != STRING_RESULT || *res2->ptr() != wild_one)
4398
return OPTIMIZE_NONE;
4402
bool Item_func_like::fix_fields(THD *thd, Item **ref)
4405
if (Item_bool_func2::fix_fields(thd, ref) ||
4406
escape_item->fix_fields(thd, &escape_item))
4409
if (!escape_item->const_during_execution())
4411
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4415
if (escape_item->const_item())
4417
/* If we are on execution stage */
4418
String *escape_str= escape_item->val_str(&tmp_value1);
4421
if (escape_used_in_parsing && escape_str->numchars() > 1)
4423
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4427
if (use_mb(cmp.cmp_collation.collation))
4429
const CHARSET_INFO * const cs= escape_str->charset();
4431
int rc= cs->cset->mb_wc(cs, &wc,
4432
(const unsigned char*) escape_str->ptr(),
4433
(const unsigned char*) escape_str->ptr() +
4434
escape_str->length());
4435
escape= (int) (rc > 0 ? wc : '\\');
4440
In the case of 8bit character set, we pass native
4441
code instead of Unicode code as "escape" argument.
4442
Convert to "cs" if charset of escape differs.
4444
const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
4446
if (escape_str->needs_conversion(escape_str->length(),
4447
escape_str->charset(), cs, &unused))
4451
uint32_t cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
4452
escape_str->length(),
4453
escape_str->charset(), &errors);
4454
escape= cnvlen ? ch : '\\';
4457
escape= *(escape_str->ptr());
4464
We could also do boyer-more for non-const items, but as we would have to
4465
recompute the tables for each row it's not worth it.
4467
if (args[1]->const_item() && !use_strnxfrm(collation.collation))
4469
String* res2 = args[1]->val_str(&tmp_value2);
4471
return false; // Null argument
4473
const size_t len = res2->length();
4474
const char* first = res2->ptr();
4475
const char* last = first + len - 1;
4477
len must be > 2 ('%pattern%')
4478
heuristic: only do TurboBM for pattern_len > 2
4481
if (len > MIN_TURBOBM_PATTERN_LEN + 2 &&
4482
*first == wild_many &&
4485
const char* tmp = first + 1;
4486
for (; *tmp != wild_many && *tmp != wild_one && *tmp != escape; tmp++) ;
4487
canDoTurboBM = (tmp == last) && !use_mb(args[0]->collation.collation);
4491
pattern = first + 1;
4492
pattern_len = (int) len - 2;
4493
int *suff = (int*) thd->alloc((int) (sizeof(int)*
4494
((pattern_len + 1)*2+
4496
bmGs = suff + pattern_len + 1;
4497
bmBc = bmGs + pattern_len + 1;
4498
turboBM_compute_good_suffix_shifts(suff);
4499
turboBM_compute_bad_character_shifts();
4506
void Item_func_like::cleanup()
4508
canDoTurboBM= false;
4509
Item_bool_func2::cleanup();
4512
#ifdef LIKE_CMP_TOUPPER
4513
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
4515
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
4520
Precomputation dependent only on pattern_len.
4523
void Item_func_like::turboBM_compute_suffixes(int *suff)
4525
const int plm1 = pattern_len - 1;
4528
int *const splm1 = suff + plm1;
4529
const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
4531
*splm1 = pattern_len;
4533
if (!cs->sort_order)
4536
for (i = pattern_len - 2; i >= 0; i--)
4538
int tmp = *(splm1 + i - f);
4539
if (g < i && tmp < i - g)
4544
g = i; // g = cmin(i, g)
4546
while (g >= 0 && pattern[g] == pattern[g + plm1 - f])
4555
for (i = pattern_len - 2; 0 <= i; --i)
4557
int tmp = *(splm1 + i - f);
4558
if (g < i && tmp < i - g)
4563
g = i; // g = cmin(i, g)
4566
likeconv(cs, pattern[g]) == likeconv(cs, pattern[g + plm1 - f]))
4576
Precomputation dependent only on pattern_len.
4579
void Item_func_like::turboBM_compute_good_suffix_shifts(int *suff)
4581
turboBM_compute_suffixes(suff);
4583
int *end = bmGs + pattern_len;
4585
for (k = bmGs; k < end; k++)
4591
const int plm1 = pattern_len - 1;
4592
for (i = plm1; i > -1; i--)
4594
if (suff[i] == i + 1)
4596
for (tmp = plm1 - i; j < tmp; j++)
4598
int *tmp2 = bmGs + j;
4599
if (*tmp2 == pattern_len)
4606
for (tmp = plm1 - i; j < tmp; j++)
4609
if (*tmp2 == pattern_len)
4614
for (i = 0; i <= pattern_len - 2; i++)
4615
*(tmp2 - suff[i]) = plm1 - i;
4620
Precomputation dependent on pattern_len.
4623
void Item_func_like::turboBM_compute_bad_character_shifts()
4626
int *end = bmBc + alphabet_size;
4628
const int plm1 = pattern_len - 1;
4629
const CHARSET_INFO *const cs= cmp.cmp_collation.collation;
4631
for (i = bmBc; i < end; i++)
4634
if (!cs->sort_order)
4636
for (j = 0; j < plm1; j++)
4637
bmBc[(uint) (unsigned char) pattern[j]] = plm1 - j;
4641
for (j = 0; j < plm1; j++)
4642
bmBc[(uint) likeconv(cs,pattern[j])] = plm1 - j;
4648
Search for pattern in text.
4651
returns true/false for match/no match
4654
bool Item_func_like::turboBM_matches(const char* text, int text_len) const
4656
register int bcShift;
4657
register int turboShift;
4658
int shift = pattern_len;
4661
const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
4663
const int plm1= pattern_len - 1;
4664
const int tlmpl= text_len - pattern_len;
4667
if (!cs->sort_order)
4671
register int i= plm1;
4672
while (i >= 0 && pattern[i] == text[i + j])
4675
if (i == plm1 - shift)
4681
register const int v = plm1 - i;
4683
bcShift = bmBc[(uint) (unsigned char) text[i + j]] - plm1 + i;
4684
shift = (turboShift > bcShift) ? turboShift : bcShift;
4685
shift = (shift > bmGs[i]) ? shift : bmGs[i];
4686
if (shift == bmGs[i])
4687
u = (pattern_len - shift < v) ? pattern_len - shift : v;
4690
if (turboShift < bcShift)
4691
shift = cmax(shift, u + 1);
4702
register int i = plm1;
4703
while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
4706
if (i == plm1 - shift)
4712
register const int v = plm1 - i;
4714
bcShift = bmBc[(uint) likeconv(cs, text[i + j])] - plm1 + i;
4715
shift = (turboShift > bcShift) ? turboShift : bcShift;
4716
shift = cmax(shift, bmGs[i]);
4717
if (shift == bmGs[i])
4718
u = (pattern_len - shift < v) ? pattern_len - shift : v;
4721
if (turboShift < bcShift)
4722
shift = cmax(shift, u + 1);
4733
Make a logical XOR of the arguments.
4735
If either operator is NULL, return NULL.
4738
(low priority) Change this to be optimized as: @n
4739
A XOR B -> (A) == 1 AND (B) <> 1) OR (A <> 1 AND (B) == 1) @n
4740
To be able to do this, we would however first have to extend the MySQL
4741
range optimizer to handle OR better.
4744
As we don't do any index optimization on XOR this is not going to be
4748
int64_t Item_cond_xor::val_int()
4751
List_iterator<Item> li(list);
4757
result^= (item->val_int() != 0);
4758
if (item->null_value)
4764
return (int64_t) result;
4768
Apply NOT transformation to the item and return a new one.
4771
Transform the item using next rules:
4773
a AND b AND ... -> NOT(a) OR NOT(b) OR ...
4774
a OR b OR ... -> NOT(a) AND NOT(b) AND ...
4782
IS NULL(a) -> IS NOT NULL(a)
4783
IS NOT NULL(a) -> IS NULL(a)
4786
@param thd thread handler
4790
NULL if we cannot apply NOT transformation (see Item::neg_transformer()).
4793
Item *Item_func_not::neg_transformer(THD *thd __attribute__((unused))) /* NOT(x) -> x */
4799
Item *Item_bool_rowready_func2::neg_transformer(THD *thd __attribute__((unused)))
4801
Item *item= negated_item();
4807
a IS NULL -> a IS NOT NULL.
4809
Item *Item_func_isnull::neg_transformer(THD *thd __attribute__((unused)))
4811
Item *item= new Item_func_isnotnull(args[0]);
4817
a IS NOT NULL -> a IS NULL.
4819
Item *Item_func_isnotnull::neg_transformer(THD *thd __attribute__((unused)))
4821
Item *item= new Item_func_isnull(args[0]);
4826
Item *Item_cond_and::neg_transformer(THD *thd) /* NOT(a AND b AND ...) -> */
4827
/* NOT a OR NOT b OR ... */
4830
Item *item= new Item_cond_or(list);
4835
Item *Item_cond_or::neg_transformer(THD *thd) /* NOT(a OR b OR ...) -> */
4836
/* NOT a AND NOT b AND ... */
4839
Item *item= new Item_cond_and(list);
4844
Item *Item_func_nop_all::neg_transformer(THD *thd __attribute__((unused)))
4846
/* "NOT (e $cmp$ ANY (SELECT ...)) -> e $rev_cmp$" ALL (SELECT ...) */
4847
Item_func_not_all *new_item= new Item_func_not_all(args[0]);
4848
Item_allany_subselect *allany= (Item_allany_subselect*)args[0];
4849
allany->func= allany->func_creator(false);
4850
allany->all= !allany->all;
4851
allany->upper_item= new_item;
4855
Item *Item_func_not_all::neg_transformer(THD *thd __attribute__((unused)))
4857
/* "NOT (e $cmp$ ALL (SELECT ...)) -> e $rev_cmp$" ANY (SELECT ...) */
4858
Item_func_nop_all *new_item= new Item_func_nop_all(args[0]);
4859
Item_allany_subselect *allany= (Item_allany_subselect*)args[0];
4860
allany->all= !allany->all;
4861
allany->func= allany->func_creator(true);
4862
allany->upper_item= new_item;
4866
Item *Item_func_eq::negated_item() /* a = b -> a != b */
4868
return new Item_func_ne(args[0], args[1]);
4872
Item *Item_func_ne::negated_item() /* a != b -> a = b */
4874
return new Item_func_eq(args[0], args[1]);
4878
Item *Item_func_lt::negated_item() /* a < b -> a >= b */
4880
return new Item_func_ge(args[0], args[1]);
4884
Item *Item_func_ge::negated_item() /* a >= b -> a < b */
4886
return new Item_func_lt(args[0], args[1]);
4890
Item *Item_func_gt::negated_item() /* a > b -> a <= b */
4892
return new Item_func_le(args[0], args[1]);
4896
Item *Item_func_le::negated_item() /* a <= b -> a > b */
4898
return new Item_func_gt(args[0], args[1]);
4902
just fake method, should never be called.
4904
Item *Item_bool_rowready_func2::negated_item()
4910
Item_equal::Item_equal(Item_field *f1, Item_field *f2)
4911
: Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
4913
const_item_cache= 0;
4914
fields.push_back(f1);
4915
fields.push_back(f2);
4918
Item_equal::Item_equal(Item *c, Item_field *f)
4919
: Item_bool_func(), eval_item(0), cond_false(0)
4921
const_item_cache= 0;
4922
fields.push_back(f);
4927
Item_equal::Item_equal(Item_equal *item_equal)
4928
: Item_bool_func(), eval_item(0), cond_false(0)
4930
const_item_cache= 0;
4931
List_iterator_fast<Item_field> li(item_equal->fields);
4933
while ((item= li++))
4935
fields.push_back(item);
4937
const_item= item_equal->const_item;
4938
cond_false= item_equal->cond_false;
4941
void Item_equal::add(Item *c)
4950
Item_func_eq *func= new Item_func_eq(c, const_item);
4951
func->set_cmp_func();
4952
func->quick_fix_field();
4953
if ((cond_false= !func->val_int()))
4954
const_item_cache= 1;
4957
void Item_equal::add(Item_field *f)
4959
fields.push_back(f);
4962
uint32_t Item_equal::members()
4964
return fields.elements;
4969
Check whether a field is referred in the multiple equality.
4971
The function checks whether field is occurred in the Item_equal object .
4973
@param field field whose occurrence is to be checked
4976
1 if nultiple equality contains a reference to field
4981
bool Item_equal::contains(Field *field)
4983
List_iterator_fast<Item_field> it(fields);
4985
while ((item= it++))
4987
if (field->eq(item->field))
4995
Join members of another Item_equal object.
4997
The function actually merges two multiple equalities.
4998
After this operation the Item_equal object additionally contains
4999
the field items of another item of the type Item_equal.
5000
If the optional constant items are not equal the cond_false flag is
5002
@param item multiple equality whose members are to be joined
5005
void Item_equal::merge(Item_equal *item)
5007
fields.concat(&item->fields);
5008
Item *c= item->const_item;
5012
The flag cond_false will be set to 1 after this, if
5013
the multiple equality already contains a constant and its
5014
value is not equal to the value of c.
5018
cond_false|= item->cond_false;
5023
Order field items in multiple equality according to a sorting criteria.
5025
The function perform ordering of the field items in the Item_equal
5026
object according to the criteria determined by the cmp callback parameter.
5027
If cmp(item_field1,item_field2,arg)<0 than item_field1 must be
5028
placed after item_fiel2.
5030
The function sorts field items by the exchange sort algorithm.
5031
The list of field items is looked through and whenever two neighboring
5032
members follow in a wrong order they are swapped. This is performed
5033
again and again until we get all members in a right order.
5035
@param cmp function to compare field item
5036
@param arg context extra parameter for the cmp function
5039
void Item_equal::sort(Item_field_cmpfunc cmp, void *arg)
5042
List_iterator<Item_field> it(fields);
5045
Item_field *item1= it++;
5046
Item_field **ref1= it.ref();
5050
while ((item2= it++))
5052
Item_field **ref2= it.ref();
5053
if (cmp(item1, item2, arg) < 0)
5055
Item_field *item= *ref1;
5072
Check appearance of new constant items in the multiple equality object.
5074
The function checks appearance of new constant items among
5075
the members of multiple equalities. Each new constant item is
5076
compared with the designated constant item if there is any in the
5077
multiple equality. If there is none the first new constant item
5081
void Item_equal::update_const()
5083
List_iterator<Item_field> it(fields);
5085
while ((item= it++))
5087
if (item->const_item())
5095
bool Item_equal::fix_fields(THD *thd __attribute__((unused)), Item **ref __attribute__((unused)))
5097
List_iterator_fast<Item_field> li(fields);
5099
not_null_tables_cache= used_tables_cache= 0;
5100
const_item_cache= 0;
5101
while ((item= li++))
5103
table_map tmp_table_map;
5104
used_tables_cache|= item->used_tables();
5105
tmp_table_map= item->not_null_tables();
5106
not_null_tables_cache|= tmp_table_map;
5107
if (item->maybe_null)
5110
fix_length_and_dec();
5115
void Item_equal::update_used_tables()
5117
List_iterator_fast<Item_field> li(fields);
5119
not_null_tables_cache= used_tables_cache= 0;
5120
if ((const_item_cache= cond_false))
5124
item->update_used_tables();
5125
used_tables_cache|= item->used_tables();
5126
const_item_cache&= item->const_item();
5130
int64_t Item_equal::val_int()
5132
Item_field *item_field;
5135
List_iterator_fast<Item_field> it(fields);
5136
Item *item= const_item ? const_item : it++;
5137
if ((null_value= item->null_value))
5139
eval_item->store_value(item);
5140
while ((item_field= it++))
5142
/* Skip fields of non-const tables. They haven't been read yet */
5143
if (item_field->field->table->const_table)
5145
if ((null_value= item_field->null_value) || eval_item->cmp(item_field))
5152
void Item_equal::fix_length_and_dec()
5154
Item *item= get_first();
5155
eval_item= cmp_item::get_comparator(item->result_type(),
5156
item->collation.collation);
5159
bool Item_equal::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
5161
List_iterator_fast<Item_field> it(fields);
5163
while ((item= it++))
5165
if (item->walk(processor, walk_subquery, arg))
5168
return Item_func::walk(processor, walk_subquery, arg);
5171
Item *Item_equal::transform(Item_transformer transformer, unsigned char *arg)
5173
List_iterator<Item_field> it(fields);
5175
while ((item= it++))
5177
Item *new_item= item->transform(transformer, arg);
5182
THD::change_item_tree() should be called only if the tree was
5183
really transformed, i.e. when a new item has been created.
5184
Otherwise we'll be allocating a lot of unnecessary memory for
5185
change records at each execution.
5187
if (new_item != item)
5188
current_thd->change_item_tree((Item **) it.ref(), new_item);
5190
return Item_func::transform(transformer, arg);
5193
void Item_equal::print(String *str, enum_query_type query_type)
5195
str->append(func_name());
5197
List_iterator_fast<Item_field> it(fields);
5200
const_item->print(str, query_type);
5204
item->print(str, query_type);
5206
while ((item= it++))
5210
item->print(str, query_type);