27 #include "sql_optimizer.h"
28 #include "sql_parse.h"
37 get_year_value(THD *thd,
Item ***item_arg,
Item **cache_arg,
38 Item *warn_item,
bool *is_null);
40 static Item_result item_store_type(Item_result a,
Item *item,
41 my_bool unsigned_flag)
43 Item_result b= item->result_type();
45 if (a == STRING_RESULT || b == STRING_RESULT)
47 else if (a == REAL_RESULT || b == REAL_RESULT)
49 else if (a == DECIMAL_RESULT || b == DECIMAL_RESULT ||
50 unsigned_flag != item->unsigned_flag)
51 return DECIMAL_RESULT;
56 static void agg_result_type(Item_result *
type,
Item **
items, uint nitems)
58 Item **item, **item_end;
59 my_bool unsigned_flag= 0;
63 for (item= items, item_end= item + nitems; item < item_end; item++)
65 if ((*item)->type() != Item::NULL_ITEM)
67 *type= (*item)->result_type();
68 unsigned_flag= (*item)->unsigned_flag;
74 for (; item < item_end; item++)
76 if ((*item)->type() != Item::NULL_ITEM)
77 *type= item_store_type(*type, *item, unsigned_flag);
102 static int cmp_row_type(
Item* item1,
Item* item2)
104 uint
n= item1->cols();
105 if (item2->check_cols(n))
107 for (uint
i=0;
i<
n;
i++)
109 if (item2->element_index(
i)->check_cols(item1->element_index(
i)->cols()) ||
110 (item1->element_index(
i)->result_type() == ROW_RESULT &&
111 cmp_row_type(item1->element_index(
i), item2->element_index(
i))))
141 static int agg_cmp_type(Item_result *type,
Item **items, uint nitems)
144 type[0]= items[0]->result_type();
145 for (i= 1 ; i < nitems ; i++)
147 type[0]= item_cmp_type(type[0], items[i]->result_type());
155 if (type[0] == ROW_RESULT && cmp_row_type(items[0], items[i]))
183 if (!nitems || items[0]->result_type() == ROW_RESULT )
184 return (enum_field_types)-1;
185 enum_field_types res= items[0]->field_type();
186 for (i= 1 ; i < nitems ; i++)
188 return real_type_to_type(res);
209 static uint collect_cmp_types(
Item **items, uint nitems,
bool skip_nulls= FALSE)
213 Item_result left_result= items[0]->result_type();
214 DBUG_ASSERT(nitems > 1);
216 for (i= 1; i < nitems ; i++)
218 if (skip_nulls && items[i]->
type() == Item::NULL_ITEM)
220 if ((left_result == ROW_RESULT ||
221 items[i]->result_type() == ROW_RESULT) &&
222 cmp_row_type(items[0], items[i]))
224 found_types|= 1
U << (uint)item_cmp_type(left_result,
225 items[i]->result_type());
231 if (skip_nulls && !found_types)
232 found_types= 1
U << (uint)left_result;
239 my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
240 c1.collation->name,c1.derivation_name(),
241 c2.collation->name,c2.derivation_name(),
287 longlong Item_func_not::val_int()
289 DBUG_ASSERT(fixed == 1);
291 null_value=args[0]->null_value;
292 return ((!null_value && value == 0) ? 1 : 0);
318 DBUG_ASSERT(fixed == 1);
325 if (empty_underlying_subquery())
328 null_value= args[0]->null_value;
329 return ((!null_value && value == 0) ? 1 : 0);
333 bool Item_func_not_all::empty_underlying_subquery()
335 DBUG_ASSERT(subselect || !(test_sum_item || test_sub_item));
346 subselect->substype() != Item_subselect::ANY_SUBS &&
347 !subselect->unit->item->is_evaluated())
348 subselect->unit->item->exec();
349 return ((test_sum_item && !test_sum_item->any_value()) ||
350 (test_sub_item && !test_sub_item->any_value()));
358 args[0]->
print(str, query_type);
373 DBUG_ASSERT(fixed == 1);
374 longlong value= args[0]->val_int();
380 if (empty_underlying_subquery())
383 null_value= args[0]->null_value;
384 return (null_value || value == 0) ? 0 : 1;
415 static bool convert_constant_item(THD *thd,
Item_field *field_item,
418 Field *field= field_item->field;
421 if ((*item)->const_item())
424 sql_mode_t orig_sql_mode= thd->variables.sql_mode;
425 enum_check_fields orig_count_cuted_fields= thd->count_cuted_fields;
426 my_bitmap_map *old_maps[2];
427 ulonglong UNINIT_VAR(orig_field_val);
429 LINT_INIT(old_maps[0]);
430 LINT_INIT(old_maps[1]);
433 dbug_tmp_use_all_columns(table, old_maps,
434 table->read_set, table->write_set);
436 thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
438 thd->count_cuted_fields= CHECK_FIELD_IGNORE;
446 bool save_field_value= (field_item->depended_from &&
447 (field_item->const_item() ||
448 !(field->table->status &
449 (STATUS_GARBAGE | STATUS_NOT_FOUND))));
450 if (save_field_value)
451 orig_field_val= field->val_int();
453 if (!(*item)->is_null() &&
454 (((rc= (*item)->save_in_field(field, 1)) == TYPE_OK) ||
455 rc == TYPE_NOTE_TIME_TRUNCATED))
467 if (field->type() == MYSQL_TYPE_LONGLONG)
469 field_cmp= stored_field_cmp_to_item(thd, field, *item);
470 DBUG_PRINT(
"info", (
"convert_constant_item %d", field_cmp));
475 Item *tmp= field->type() == MYSQL_TYPE_TIME ?
483 (*item)->val_time_temporal(),
486 field->is_temporal_with_date() ?
494 max((*item)->datetime_precision(),
496 (*item)->val_date_temporal(),
500 test(field->flags & UNSIGNED_FLAG));
502 thd->change_item_tree(item, tmp);
507 if (save_field_value)
509 result= field->store(orig_field_val, TRUE);
511 DBUG_ASSERT(!result);
513 thd->variables.sql_mode= orig_sql_mode;
514 thd->count_cuted_fields= orig_count_cuted_fields;
516 dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_maps);
522 bool Item_bool_func2::convert_constant_arg(THD *thd,
Item *field,
Item **item)
524 if (field->real_item()->type() != FIELD_ITEM)
528 if (field_item->field->can_be_compared_as_longlong() &&
529 !(field_item->is_temporal_with_date() &&
530 (*item)->result_type() == STRING_RESULT))
532 if (convert_constant_item(thd, field_item, item))
534 cmp.set_cmp_func(
this, tmp_arg, tmp_arg + 1, INT_RESULT);
535 field->cmp_context= (*item)->cmp_context= INT_RESULT;
543 void Item_bool_func2::fix_length_and_dec()
552 if (!args[0] || !args[1])
555 DBUG_ENTER(
"Item_bool_func2::fix_length_and_dec");
561 if (args[0]->result_type() == STRING_RESULT &&
562 args[1]->result_type() == STRING_RESULT &&
563 agg_arg_charsets_for_comparison(cmp.cmp_collation, args, 2))
566 args[0]->cmp_context= args[1]->cmp_context=
567 item_cmp_type(args[0]->result_type(), args[1]->result_type());
570 if (functype() == LIKE_FUNC)
577 if (!thd->lex->is_ps_or_view_context_analysis())
579 if (convert_constant_arg(thd, args[0], &args[1]) ||
580 convert_constant_arg(thd, args[1], &args[0]))
591 func= comparator_matrix[
type]
592 [is_owner_equal_func()];
597 uint n= (*a)->cols();
598 if (n != (*b)->cols())
600 my_error(ER_OPERAND_COLUMNS, MYF(0), n);
606 for (uint i=0; i <
n; i++)
608 if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
610 my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
613 if (comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i),
625 if (cmp_collation.set((*a)->collation, (*b)->collation) ||
626 cmp_collation.derivation == DERIVATION_NONE)
628 my_coll_agg_error((*a)->collation, (*b)->collation,
632 if (cmp_collation.collation == &my_charset_bin)
638 if (func == &Arg_comparator::compare_string)
641 func= &Arg_comparator::compare_e_binary_string;
651 (*a)->walk(&Item::set_no_const_sub, FALSE, (uchar*) 0);
652 (*b)->walk(&Item::set_no_const_sub, FALSE, (uchar*) 0);
658 if ((*a)->is_temporal() && (*b)->is_temporal())
660 func= is_owner_equal_func() ?
664 else if (func == &Arg_comparator::compare_int_signed)
666 if ((*a)->unsigned_flag)
667 func= (((*b)->unsigned_flag)?
670 else if ((*b)->unsigned_flag)
673 else if (func== &Arg_comparator::compare_e_int)
675 if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
684 if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
686 precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
687 if (func == &Arg_comparator::compare_real)
688 func= &Arg_comparator::compare_real_fixed;
689 else if (func == &Arg_comparator::compare_e_real)
690 func= &Arg_comparator::compare_e_real_fixed;
727 if (!str_to_datetime(str, l_time,
728 (TIME_FUZZY_DATE | MODE_INVALID_DATES |
729 (thd->variables.sql_mode &
730 (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE))), &status) &&
731 (l_time->time_type == MYSQL_TIMESTAMP_DATETIME ||
732 l_time->time_type == MYSQL_TIMESTAMP_DATE))
741 status.warnings= MYSQL_TIME_WARN_TRUNCATED;
744 if (status.warnings > 0)
745 make_truncated_value_warning(thd, Sql_condition::WARN_LEVEL_WARN,
771 static ulonglong get_date_from_str(THD *thd,
String *str,
772 timestamp_type warn_type,
773 const char *warn_name,
bool *error_arg)
780 return TIME_to_longlong_datetime_packed(&l_time);
795 bool Arg_comparator::get_date_from_const(
Item *date_arg,
797 ulonglong *const_value)
799 THD *thd= current_thd;
809 if (!thd->lex->is_ps_or_view_context_analysis() &&
810 str_arg->const_item() &&
811 (str_arg->type() != Item::FUNC_ITEM ||
812 ((
Item_func*) str_arg)->functype() != Item_func::GUSERVAR_FUNC))
815 if (str_arg->field_type() == MYSQL_TYPE_TIME)
819 if (str_arg->null_value)
825 DBUG_ASSERT(str_arg->result_type() == STRING_RESULT);
828 timestamp_type t_type= (date_arg->field_type() == MYSQL_TYPE_DATE ?
829 MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME);
830 str_val= str_arg->val_str(&tmp);
831 if (str_arg->null_value)
833 value= get_date_from_str(thd, str_val, t_type,
834 date_arg->item_name.
ptr(), &error);
877 Arg_comparator::can_compare_as_dates(
Item *a,
Item *b, ulonglong *const_value)
879 if (a->type() == Item::ROW_ITEM || b->type() == Item::ROW_ITEM)
882 if (a->is_temporal_with_date())
884 if (b->is_temporal_with_date())
888 else if (b->result_type() == STRING_RESULT)
890 return !get_date_from_const(a, b, const_value);
895 else if (b->is_temporal_with_date() &&
896 a->result_type() == STRING_RESULT)
898 return !get_date_from_const(b, a, const_value);
931 get_time_value(THD *thd,
Item ***item_arg,
Item **cache_arg,
932 Item *warn_item,
bool *is_null)
935 Item *item= **item_arg;
963 *is_null= item->null_value;
969 if (item->const_item() && cache_arg &&
970 item->type() != Item::CACHE_ITEM &&
971 (item->type() != Item::FUNC_ITEM ||
972 ((
Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
976 cache->set_used_tables(1);
977 cache->store(item, value);
979 *item_arg= cache_arg;
989 ulonglong const_value= (ulonglong)-1;
992 set_null= set_null && owner_arg;
996 if (can_compare_as_dates(*a, *b, &const_value))
998 a_type= (*a)->field_type();
999 b_type= (*b)->field_type();
1003 if (const_value != (ulonglong)-1)
1011 cache->set_used_tables(1);
1012 if (!(*a)->is_temporal_with_date())
1014 cache->store((*a), const_value);
1016 a= (
Item **)&a_cache;
1020 cache->store((*b), const_value);
1022 b= (
Item **)&b_cache;
1025 is_nulls_eq= is_owner_equal_func();
1026 func= &Arg_comparator::compare_datetime;
1027 get_value_a_func= &get_datetime_value;
1028 get_value_b_func= &get_datetime_value;
1029 cmp_collation.set(&my_charset_numeric);
1030 set_cmp_context_for_datetime();
1033 else if (type == STRING_RESULT && (*a)->field_type() == MYSQL_TYPE_TIME &&
1034 (*b)->field_type() == MYSQL_TYPE_TIME)
1039 is_nulls_eq= is_owner_equal_func();
1040 func= &Arg_comparator::compare_datetime;
1041 get_value_a_func= &get_time_value;
1042 get_value_b_func= &get_time_value;
1043 set_cmp_context_for_datetime();
1046 else if (type == STRING_RESULT &&
1047 (*a)->result_type() == STRING_RESULT &&
1048 (*b)->result_type() == STRING_RESULT)
1051 coll.set((*a)->collation.collation);
1052 if (agg_item_set_converter(coll, owner->func_name(),
1053 b, 1, MY_COLL_CMP_CONV, 1))
1056 else if (try_year_cmp_func(type))
1061 return set_compare_func(owner_arg, type);
1069 bool Arg_comparator::try_year_cmp_func(Item_result type)
1071 if (type == ROW_RESULT)
1074 bool a_is_year= (*a)->field_type() == MYSQL_TYPE_YEAR;
1075 bool b_is_year= (*b)->field_type() == MYSQL_TYPE_YEAR;
1077 if (!a_is_year && !b_is_year)
1080 if (a_is_year && b_is_year)
1082 get_value_a_func= &get_year_value;
1083 get_value_b_func= &get_year_value;
1085 else if (a_is_year && (*b)->is_temporal_with_date())
1087 get_value_a_func= &get_year_value;
1088 get_value_b_func= &get_datetime_value;
1090 else if (b_is_year && (*a)->is_temporal_with_date())
1092 get_value_b_func= &get_year_value;
1093 get_value_a_func= &get_datetime_value;
1098 is_nulls_eq= is_owner_equal_func();
1099 func= &Arg_comparator::compare_datetime;
1100 set_cmp_context_for_datetime();
1127 if (!thd->lex->is_ps_or_view_context_analysis() &&
1128 (*value)->const_item() && type != (*value)->result_type())
1130 Item_cache *cache= Item_cache::get_cache(*value, type);
1131 cache->setup(*value);
1146 a_type= (*a)->field_type();
1147 b_type= (*b)->field_type();
1151 func= &Arg_comparator::compare_datetime;
1152 get_value_a_func= &get_datetime_value;
1153 get_value_b_func= &get_datetime_value;
1154 set_cmp_context_for_datetime();
1189 get_datetime_value(THD *thd,
Item ***item_arg,
Item **cache_arg,
1190 Item *warn_item,
bool *is_null)
1194 Item *item= **item_arg;
1196 if (item->is_temporal())
1199 *is_null= item->null_value;
1203 str= item->val_str(&buf);
1204 *is_null= item->null_value;
1207 return ~(ulonglong) 0;
1217 enum_field_types f_type= warn_item->field_type();
1218 timestamp_type t_type= f_type ==
1219 MYSQL_TYPE_DATE ? MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME;
1220 value= (longlong) get_date_from_str(thd, str, t_type, warn_item->item_name.
ptr(), &error);
1232 if (item->const_item() && cache_arg &&
1233 item->type() != Item::CACHE_ITEM &&
1234 (item->type() != Item::FUNC_ITEM ||
1235 ((
Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
1239 cache->set_used_tables(1);
1240 cache->store(item, value);
1242 *item_arg= cache_arg;
1270 get_year_value(THD *thd,
Item ***item_arg,
Item **cache_arg,
1271 Item *warn_item,
bool *is_null)
1274 Item *item= **item_arg;
1276 value= item->val_int();
1277 *is_null= item->null_value;
1279 return ~(ulonglong) 0;
1289 Item *real_item= item->real_item();
1291 if (real_item->type() == Item::FIELD_ITEM)
1293 else if (real_item->type() == Item::CACHE_ITEM)
1295 if (!(field && field->type() == MYSQL_TYPE_YEAR && field->field_length == 4))
1303 return year_to_longlong_datetime_packed(value);
1333 int Arg_comparator::compare_datetime()
1335 bool a_is_null, b_is_null;
1336 longlong a_value, b_value;
1339 a_value= (*get_value_a_func)(thd, &a, &a_cache, *b, &a_is_null);
1340 if (!is_nulls_eq && a_is_null)
1343 owner->null_value= 1;
1348 b_value= (*get_value_b_func)(thd, &b, &b_cache, *a, &b_is_null);
1349 if (a_is_null || b_is_null)
1352 owner->null_value= is_nulls_eq ? 0 : 1;
1353 return is_nulls_eq ? (a_is_null == b_is_null) : -1;
1358 owner->null_value= 0;
1362 return (a_value == b_value);
1363 return a_value < b_value ? -1 : (a_value > b_value ? 1 : 0);
1367 int Arg_comparator::compare_string()
1370 if ((res1= (*a)->val_str(&value1)))
1372 if ((res2= (*b)->val_str(&value2)))
1375 owner->null_value= 0;
1376 return sortcmp(res1,res2,cmp_collation.collation);
1380 owner->null_value= 1;
1399 if ((res1= (*a)->val_str(&value1)))
1401 if ((res2= (*b)->val_str(&value2)))
1404 owner->null_value= 0;
1405 uint res1_length= res1->length();
1406 uint res2_length= res2->length();
1407 int cmp= memcmp(res1->ptr(), res2->ptr(), min(res1_length,res2_length));
1408 return cmp ? cmp : (int) (res1_length - res2_length);
1412 owner->null_value= 1;
1425 res1= (*a)->val_str(&value1);
1426 res2= (*b)->val_str(&value2);
1428 return test(res1 == res2);
1429 return test(sortcmp(res1, res2, cmp_collation.collation) == 0);
1433 int Arg_comparator::compare_e_binary_string()
1436 res1= (*a)->val_str(&value1);
1437 res2= (*b)->val_str(&value2);
1439 return test(res1 == res2);
1440 return test(stringcmp(res1, res2) == 0);
1444 int Arg_comparator::compare_real()
1451 volatile double val1, val2;
1452 val1= (*a)->val_real();
1453 if (!(*a)->null_value)
1455 val2= (*b)->val_real();
1456 if (!(*b)->null_value)
1459 owner->null_value= 0;
1460 if (val1 < val2)
return -1;
1461 if (val1 == val2)
return 0;
1466 owner->null_value= 1;
1470 int Arg_comparator::compare_decimal()
1473 my_decimal *val1= (*a)->val_decimal(&decimal1);
1474 if (!(*a)->null_value)
1477 my_decimal *val2= (*b)->val_decimal(&decimal2);
1478 if (!(*b)->null_value)
1481 owner->null_value= 0;
1486 owner->null_value= 1;
1490 int Arg_comparator::compare_e_real()
1492 double val1= (*a)->val_real();
1493 double val2= (*b)->val_real();
1494 if ((*a)->null_value || (*b)->null_value)
1495 return test((*a)->null_value && (*b)->null_value);
1496 return test(val1 == val2);
1499 int Arg_comparator::compare_e_decimal()
1502 my_decimal *val1= (*a)->val_decimal(&decimal1);
1503 my_decimal *val2= (*b)->val_decimal(&decimal2);
1504 if ((*a)->null_value || (*b)->null_value)
1505 return test((*a)->null_value && (*b)->null_value);
1510 int Arg_comparator::compare_real_fixed()
1517 volatile double val1, val2;
1518 val1= (*a)->val_real();
1519 if (!(*a)->null_value)
1521 val2= (*b)->val_real();
1522 if (!(*b)->null_value)
1525 owner->null_value= 0;
1526 if (val1 == val2 || fabs(val1 - val2) < precision)
1534 owner->null_value= 1;
1539 int Arg_comparator::compare_e_real_fixed()
1541 double val1= (*a)->val_real();
1542 double val2= (*b)->val_real();
1543 if ((*a)->null_value || (*b)->null_value)
1544 return test((*a)->null_value && (*b)->null_value);
1545 return test(val1 == val2 || fabs(val1 - val2) < precision);
1549 int Arg_comparator::compare_int_signed()
1551 longlong val1= (*a)->val_int();
1552 if (!(*a)->null_value)
1554 longlong val2= (*b)->val_int();
1555 if (!(*b)->null_value)
1558 owner->null_value= 0;
1559 if (val1 < val2)
return -1;
1560 if (val1 == val2)
return 0;
1565 owner->null_value= 1;
1594 longlong val1= (*a)->val_time_temporal();
1595 if (!(*a)->null_value)
1597 longlong val2= (*b)->val_time_temporal();
1598 if (!(*b)->null_value)
1601 owner->null_value= 0;
1602 return val1 < val2 ? -1 : val1 > val2 ? 1 : 0;
1606 owner->null_value= 1;
1616 longlong val1= (*a)->val_time_temporal();
1617 longlong val2= (*b)->val_time_temporal();
1618 if ((*a)->null_value || (*b)->null_value)
1619 return test((*a)->null_value && (*b)->null_value);
1620 return test(val1 == val2);
1631 ulonglong val1= (*a)->val_int();
1632 if (!(*a)->null_value)
1634 ulonglong val2= (*b)->val_int();
1635 if (!(*b)->null_value)
1638 owner->null_value= 0;
1639 if (val1 < val2)
return -1;
1640 if (val1 == val2)
return 0;
1645 owner->null_value= 1;
1656 longlong sval1= (*a)->val_int();
1657 if (!(*a)->null_value)
1659 ulonglong uval2= (ulonglong)(*b)->val_int();
1660 if (!(*b)->null_value)
1663 owner->null_value= 0;
1664 if (sval1 < 0 || (ulonglong)sval1 < uval2)
1666 if ((ulonglong)sval1 == uval2)
1672 owner->null_value= 1;
1683 ulonglong uval1= (ulonglong)(*a)->val_int();
1684 if (!(*a)->null_value)
1686 longlong sval2= (*b)->val_int();
1687 if (!(*b)->null_value)
1690 owner->null_value= 0;
1693 if (uval1 < (ulonglong)sval2)
1695 if (uval1 == (ulonglong)sval2)
1701 owner->null_value= 1;
1706 int Arg_comparator::compare_e_int()
1708 longlong val1= (*a)->val_int();
1709 longlong val2= (*b)->val_int();
1710 if ((*a)->null_value || (*b)->null_value)
1711 return test((*a)->null_value && (*b)->null_value);
1712 return test(val1 == val2);
1720 longlong val1= (*a)->val_int();
1721 longlong val2= (*b)->val_int();
1722 if ((*a)->null_value || (*b)->null_value)
1723 return test((*a)->null_value && (*b)->null_value);
1724 return (val1 >= 0) &&
test(val1 == val2);
1727 int Arg_comparator::compare_row()
1731 (*a)->bring_value();
1732 (*b)->bring_value();
1734 if ((*a)->null_value || (*b)->null_value)
1736 owner->null_value= 1;
1740 uint n= (*a)->cols();
1741 for (uint i= 0; i<
n; i++)
1743 res= comparators[
i].compare();
1745 if (owner->null_value && owner->type() == Item::FUNC_ITEM)
1748 switch (((
Item_func*)owner)->functype()) {
1749 case Item_func::NE_FUNC:
1751 case Item_func::LT_FUNC:
1752 case Item_func::LE_FUNC:
1753 case Item_func::GT_FUNC:
1754 case Item_func::GE_FUNC:
1761 owner->null_value= 0;
1773 owner->null_value= 1;
1780 int Arg_comparator::compare_e_row()
1782 (*a)->bring_value();
1783 (*b)->bring_value();
1784 uint n= (*a)->cols();
1785 for (uint i= 0; i<
n; i++)
1787 if (!comparators[i].
compare())
1794 void Item_func_truth::fix_length_and_dec()
1806 args[0]->
print(str, query_type);
1807 str->append(STRING_WITH_LEN(
" is "));
1809 str->append(STRING_WITH_LEN(
"not "));
1811 str->append(STRING_WITH_LEN(
"true"));
1813 str->append(STRING_WITH_LEN(
"false"));
1821 if (args[0]->null_value)
1827 return (! affirmative);
1833 return (val == value);
1837 return (val != value);
1841 longlong Item_func_truth::val_int()
1847 bool Item_in_optimizer::fix_left(THD *thd,
Item **ref)
1855 if ((!args[0]->fixed && args[0]->fix_fields(thd, args)) ||
1856 (!cache && !(cache= Item_cache::get_cache(args[0]))))
1859 cache->setup(args[0]);
1861 if (cache->cols() == 1)
1867 uint n= cache->cols();
1868 for (uint i= 0; i <
n; i++)
1871 set_used_tables(args[0]->element_index(i)->used_tables());
1875 with_sum_func= args[0]->with_sum_func;
1877 cache->store(args[0]);
1882 bool Item_in_optimizer::fix_fields(THD *thd,
Item **ref)
1884 DBUG_ASSERT(fixed == 0);
1885 if (fix_left(thd, ref))
1887 if (args[0]->maybe_null)
1890 if (!args[1]->fixed && args[1]->fix_fields(thd, args+1))
1893 if (args[0]->cols() != sub->engine->cols())
1895 my_error(ER_OPERAND_COLUMNS, MYF(0), args[0]->cols());
1898 if (args[1]->maybe_null)
1900 with_sum_func= with_sum_func || args[1]->with_sum_func;
1904 if (!sub->is_top_level_item())
1921 st_select_lex *removed_select)
2018 DBUG_ASSERT(fixed == 1);
2019 cache->store(args[0]);
2020 cache->cache_value();
2022 if (cache->null_value)
2031 if (item_subs->is_top_level_item())
2054 bool all_left_cols_null=
true;
2055 const uint ncols= cache->cols();
2061 for (uint i= 0; i < ncols; i++)
2063 if (cache->element_index(i)->null_value)
2064 item_subs->set_cond_guard_var(i, FALSE);
2066 all_left_cols_null=
false;
2069 if (all_left_cols_null && result_for_null_param != UNKNOWN &&
2079 null_value= result_for_null_param;
2084 (void) item_subs->val_bool_result();
2085 if (!item_subs->value)
2086 null_value= item_subs->null_value;
2089 if (all_left_cols_null)
2090 result_for_null_param= null_value;
2094 for (uint i= 0; i < ncols; i++)
2095 item_subs->set_cond_guard_var(i, TRUE);
2099 tmp= args[1]->val_bool_result();
2100 null_value= args[1]->null_value;
2105 void Item_in_optimizer::keep_top_level_cache()
2107 cache->keep_array();
2112 void Item_in_optimizer::cleanup()
2114 DBUG_ENTER(
"Item_in_optimizer::cleanup");
2115 Item_bool_func::cleanup();
2122 bool Item_in_optimizer::is_null()
2155 DBUG_ASSERT(!current_thd->stmt_arena->is_stmt_prepare());
2156 DBUG_ASSERT(arg_count == 2);
2159 new_item= args[0]->
transform(transformer, argument);
2168 if (args[0] != new_item)
2169 current_thd->change_item_tree(args, new_item);
2178 DBUG_ASSERT((args[1])->
type() == Item::SUBSELECT_ITEM &&
2180 Item_subselect::IN_SUBS ||
2182 Item_subselect::ALL_SUBS ||
2184 Item_subselect::ANY_SUBS));
2188 if (in_arg->left_expr != args[0])
2189 current_thd->change_item_tree(&in_arg->left_expr, args[0]);
2191 return (this->*transformer)(argument);
2195 longlong Item_func_eq::val_int()
2197 DBUG_ASSERT(fixed == 1);
2198 int value= cmp.compare();
2199 return value == 0 ? 1 : 0;
2208 maybe_null=null_value=0;
2211 longlong Item_func_equal::val_int()
2213 DBUG_ASSERT(fixed == 1);
2214 return cmp.compare();
2217 longlong Item_func_ne::val_int()
2219 DBUG_ASSERT(fixed == 1);
2220 int value= cmp.compare();
2221 return value != 0 && !null_value ? 1 : 0;
2225 longlong Item_func_ge::val_int()
2227 DBUG_ASSERT(fixed == 1);
2228 int value= cmp.compare();
2229 return value >= 0 ? 1 : 0;
2233 longlong Item_func_gt::val_int()
2235 DBUG_ASSERT(fixed == 1);
2236 int value= cmp.compare();
2237 return value > 0 ? 1 : 0;
2240 longlong Item_func_le::val_int()
2242 DBUG_ASSERT(fixed == 1);
2243 int value= cmp.compare();
2244 return value <= 0 && !null_value ? 1 : 0;
2248 longlong Item_func_lt::val_int()
2250 DBUG_ASSERT(fixed == 1);
2251 int value= cmp.compare();
2252 return value < 0 && !null_value ? 1 : 0;
2256 longlong Item_func_strcmp::val_int()
2258 DBUG_ASSERT(fixed == 1);
2259 String *a=args[0]->val_str(&cmp.value1);
2260 String *b=args[1]->val_str(&cmp.value2);
2266 int value= sortcmp(a,b,cmp.cmp_collation.collation);
2268 return !value ? 0 : (value < 0 ? (longlong) -1 : (longlong) 1);
2277 if (item->type() != FUNC_ITEM)
2280 if (arg_count != item_func->arg_count ||
2281 functype() != item_func->functype())
2285 for (uint i=0; i < arg_count ; i++)
2286 if (!args[i]->
eq(item_func->arguments()[
i], binary_cmp))
2292 void Item_func_interval::fix_length_and_dec()
2294 uint rows= row->cols();
2296 use_decimal_comparison= ((row->element_index(0)->result_type() ==
2298 (row->element_index(0)->result_type() ==
2302 bool not_null_consts= TRUE;
2304 for (uint i= 1; not_null_consts && i < rows; i++)
2306 Item *el= row->element_index(i);
2307 not_null_consts&= el->const_item() & !el->is_null();
2310 if (not_null_consts &&
2314 if (use_decimal_comparison)
2316 for (uint i= 1; i < rows; i++)
2318 Item *el= row->element_index(i);
2320 if ((el->result_type() == DECIMAL_RESULT) ||
2321 (el->result_type() == INT_RESULT))
2323 range->type= DECIMAL_RESULT;
2325 my_decimal *dec= el->val_decimal(&range->dec);
2326 if (dec != &range->dec)
2333 range->type= REAL_RESULT;
2334 range->dbl= el->val_real();
2340 for (uint i= 1; i < rows; i++)
2342 intervals[i-1].dbl= row->element_index(i)->val_real();
2351 with_sum_func= with_sum_func || row->with_sum_func;
2372 DBUG_ASSERT(fixed == 1);
2377 if (use_decimal_comparison)
2379 dec= row->element_index(0)->val_decimal(&dec_buf);
2380 if (row->element_index(0)->null_value)
2382 my_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
2386 value= row->element_index(0)->val_real();
2387 if (row->element_index(0)->null_value)
2396 while (start != end)
2398 uint mid= (start + end + 1) / 2;
2406 if (dec && range->type == DECIMAL_RESULT)
2409 cmp_result= (range->dbl <= value);
2416 return ((dec && range->type == DECIMAL_RESULT) ?
2418 value < range->dbl) ? 0 : start + 1;
2421 for (i=1 ; i < row->cols() ; i++)
2423 Item *el= row->element_index(i);
2424 if (use_decimal_comparison &&
2425 ((el->result_type() == DECIMAL_RESULT) ||
2426 (el->result_type() == INT_RESULT)))
2428 my_decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
2437 double val= el->val_real();
2479 if (Item_func_opt_neg::fix_fields(thd, ref))
2482 thd->lex->current_select->between_count++;
2485 if (pred_level && !negated)
2490 (args[1]->not_null_tables() &
2491 args[2]->not_null_tables()));
2497 void Item_func_between::fix_length_and_dec()
2501 int datetime_items_found= 0;
2502 int time_items_found= 0;
2503 compare_as_dates_with_strings=
false;
2504 compare_as_temporal_times= compare_as_temporal_dates=
false;
2505 THD *thd= current_thd;
2511 if (!args[0] || !args[1] || !args[2])
2513 if ( agg_cmp_type(&cmp_type, args, 3))
2515 if (cmp_type == STRING_RESULT &&
2516 agg_arg_charsets_for_comparison(cmp_collation, args, 3))
2524 if (cmp_type == STRING_RESULT)
2526 for (i= 0; i < 3; i++)
2528 if (args[i]->is_temporal_with_date())
2529 datetime_items_found++;
2531 if (args[i]->field_type() == MYSQL_TYPE_TIME)
2536 if (datetime_items_found + time_items_found == 3)
2538 if (time_items_found == 3)
2541 cmp_type= INT_RESULT;
2542 compare_as_temporal_times=
true;
2550 cmp_type= INT_RESULT;
2551 compare_as_temporal_dates=
true;
2554 else if (datetime_items_found > 0)
2560 compare_as_dates_with_strings=
true;
2561 ge_cmp.set_datetime_cmp_func(
this, args, args + 1);
2562 le_cmp.set_datetime_cmp_func(
this, args, args + 2);
2564 else if (args[0]->real_item()->
type() == FIELD_ITEM &&
2565 thd->lex->sql_command != SQLCOM_CREATE_VIEW &&
2566 thd->lex->sql_command != SQLCOM_SHOW_CREATE)
2569 if (field_item->field->can_be_compared_as_longlong())
2575 const bool cvt_arg1= convert_constant_item(thd, field_item, &args[1]);
2576 const bool cvt_arg2= convert_constant_item(thd, field_item, &args[2]);
2577 if (args[0]->is_temporal())
2579 if (cvt_arg1 || cvt_arg2)
2580 cmp_type=INT_RESULT;
2584 if (cvt_arg1 && cvt_arg2)
2585 cmp_type=INT_RESULT;
2588 if (args[0]->is_temporal() &&
2589 args[1]->is_temporal() &&
2590 args[2]->is_temporal())
2606 if (field_item->field_type() == MYSQL_TYPE_TIME)
2607 compare_as_temporal_times=
true;
2608 else if (field_item->is_temporal_with_date())
2609 compare_as_temporal_dates=
true;
2616 longlong Item_func_between::val_int()
2618 DBUG_ASSERT(fixed == 1);
2619 if (compare_as_dates_with_strings)
2623 ge_res= ge_cmp.compare();
2624 if ((null_value= args[0]->null_value))
2626 le_res= le_cmp.compare();
2628 if (!args[1]->null_value && !args[2]->null_value)
2629 return (longlong) ((ge_res >= 0 && le_res <=0) != negated);
2630 else if (args[1]->null_value)
2632 null_value= le_res > 0;
2636 null_value= ge_res < 0;
2639 else if (cmp_type == STRING_RESULT)
2642 value=args[0]->val_str(&value0);
2643 if ((null_value=args[0]->null_value))
2645 a=args[1]->val_str(&value1);
2646 b=args[2]->val_str(&value2);
2647 if (!args[1]->null_value && !args[2]->null_value)
2648 return (longlong) ((sortcmp(value,a,cmp_collation.collation) >= 0 &&
2649 sortcmp(value,b,cmp_collation.collation) <= 0) !=
2651 if (args[1]->null_value && args[2]->null_value)
2653 else if (args[1]->null_value)
2656 null_value= sortcmp(value,b,cmp_collation.collation) <= 0;
2661 null_value= sortcmp(value,a,cmp_collation.collation) >= 0;
2664 else if (cmp_type == INT_RESULT)
2666 longlong a, b, value;
2670 if ((null_value=args[0]->null_value))
2672 if (compare_as_temporal_times)
2677 else if (compare_as_temporal_dates)
2684 a= args[1]->val_int();
2685 b= args[2]->val_int();
2687 if (!args[1]->null_value && !args[2]->null_value)
2688 return (longlong) ((value >= a && value <= b) != negated);
2689 if (args[1]->null_value && args[2]->null_value)
2691 else if (args[1]->null_value)
2693 null_value= value <= b;
2697 null_value= value >= a;
2700 else if (cmp_type == DECIMAL_RESULT)
2702 my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2703 a_buf, *a_dec, b_buf, *b_dec;
2704 if ((null_value=args[0]->null_value))
2706 a_dec= args[1]->val_decimal(&a_buf);
2707 b_dec= args[2]->val_decimal(&b_buf);
2708 if (!args[1]->null_value && !args[2]->null_value)
2711 if (args[1]->null_value && args[2]->null_value)
2713 else if (args[1]->null_value)
2720 double value= args[0]->val_real(),a,b;
2721 if ((null_value=args[0]->null_value))
2723 a= args[1]->val_real();
2724 b= args[2]->val_real();
2725 if (!args[1]->null_value && !args[2]->null_value)
2726 return (longlong) ((value >= a && value <= b) != negated);
2727 if (args[1]->null_value && args[2]->null_value)
2729 else if (args[1]->null_value)
2731 null_value= value <= b;
2735 null_value= value >= a;
2738 return (longlong) (!null_value && negated);
2745 args[0]->
print(str, query_type);
2747 str->append(STRING_WITH_LEN(
" not"));
2748 str->append(STRING_WITH_LEN(
" between "));
2749 args[1]->
print(str, query_type);
2750 str->append(STRING_WITH_LEN(
" and "));
2751 args[2]->
print(str, query_type);
2756 Item_func_ifnull::fix_length_and_dec()
2759 agg_result_type(&hybrid_type, args, 2);
2761 maybe_null=args[1]->maybe_null;
2762 decimals= max(args[0]->decimals, args[1]->decimals);
2763 unsigned_flag= args[0]->unsigned_flag && args[1]->unsigned_flag;
2765 if (hybrid_type == DECIMAL_RESULT || hybrid_type == INT_RESULT)
2767 int len0= args[0]->max_char_length() - args[0]->decimals
2768 - (args[0]->unsigned_flag ? 0 : 1);
2770 int len1= args[1]->max_char_length() - args[1]->decimals
2771 - (args[1]->unsigned_flag ? 0 : 1);
2773 char_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2776 char_length= max(args[0]->max_char_length(), args[1]->max_char_length());
2778 switch (hybrid_type) {
2783 case DECIMAL_RESULT:
2793 fix_char_length(char_length);
2797 uint Item_func_ifnull::decimal_precision()
const
2799 int arg0_int_part= args[0]->decimal_int_part();
2800 int arg1_int_part= args[1]->decimal_int_part();
2801 int max_int_part= max(arg0_int_part, arg1_int_part);
2802 int precision= max_int_part + decimals;
2807 Field *Item_func_ifnull::tmp_table_field(
TABLE *table)
2815 DBUG_ASSERT(fixed == 1);
2816 double value= args[0]->val_real();
2817 if (!args[0]->null_value)
2822 value= args[1]->val_real();
2823 if ((null_value=args[1]->null_value))
2831 DBUG_ASSERT(fixed == 1);
2832 longlong value=args[0]->val_int();
2833 if (!args[0]->null_value)
2838 value=args[1]->val_int();
2839 if ((null_value=args[1]->null_value))
2847 DBUG_ASSERT(fixed == 1);
2848 my_decimal *value= args[0]->val_decimal(decimal_value);
2849 if (!args[0]->null_value)
2854 value= args[1]->val_decimal(decimal_value);
2855 if ((null_value= args[1]->null_value))
2863 DBUG_ASSERT(fixed == 1);
2864 if (!args[0]->get_date(ltime, fuzzydate))
2865 return (null_value=
false);
2866 return (null_value= args[1]->get_date(ltime, fuzzydate));
2870 bool Item_func_ifnull::time_op(
MYSQL_TIME *ltime)
2872 DBUG_ASSERT(fixed == 1);
2873 if (!args[0]->get_time(ltime))
2874 return (null_value=
false);
2875 return (null_value= args[1]->get_time(ltime));
2882 DBUG_ASSERT(fixed == 1);
2883 String *res =args[0]->val_str(str);
2884 if (!args[0]->null_value)
2887 res->set_charset(collation.collation);
2890 res=args[1]->val_str(str);
2891 if ((null_value=args[1]->null_value))
2893 res->set_charset(collation.collation);
2927 DBUG_ASSERT(fixed == 0);
2928 args[0]->top_level_item();
2930 if (Item_func::fix_fields(thd, ref))
2934 args[2]->not_null_tables());
2940 void Item_func_if::cache_type_info(
Item *source)
2942 collation.set(source->collation);
2943 cached_field_type= source->field_type();
2944 cached_result_type= source->result_type();
2945 decimals= source->decimals;
2946 max_length= source->max_length;
2947 maybe_null= source->maybe_null;
2948 unsigned_flag= source->unsigned_flag;
2953 Item_func_if::fix_length_and_dec()
2956 if (args[1]->
type() == NULL_ITEM)
2958 cache_type_info(args[2]);
2961 if (args[2]->
type() == NULL_ITEM)
2962 cached_field_type= MYSQL_TYPE_STRING;
2965 if (args[2]->
type() == NULL_ITEM)
2967 cache_type_info(args[1]);
2972 agg_result_type(&cached_result_type, args + 1, 2);
2974 maybe_null= args[1]->maybe_null || args[2]->maybe_null;
2975 decimals= max(args[1]->decimals, args[2]->decimals);
2976 unsigned_flag=args[1]->unsigned_flag && args[2]->unsigned_flag;
2978 if (cached_result_type == STRING_RESULT)
2985 collation.set_numeric();
2989 if ((cached_result_type == DECIMAL_RESULT )
2990 || (cached_result_type == INT_RESULT))
2992 int len1= args[1]->max_length - args[1]->decimals
2993 - (args[1]->unsigned_flag ? 0 : 1);
2995 int len2= args[2]->max_length - args[2]->decimals
2996 - (args[2]->unsigned_flag ? 0 : 1);
2998 char_length= max(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
3001 char_length= max(args[1]->max_char_length(), args[2]->max_char_length());
3002 fix_char_length(char_length);
3006 uint Item_func_if::decimal_precision()
const
3008 int arg1_prec= args[1]->decimal_int_part();
3009 int arg2_prec= args[2]->decimal_int_part();
3010 int precision=max(arg1_prec,arg2_prec) + decimals;
3016 Item_func_if::val_real()
3018 DBUG_ASSERT(fixed == 1);
3020 double value= arg->val_real();
3021 null_value=arg->null_value;
3026 Item_func_if::val_int()
3028 DBUG_ASSERT(fixed == 1);
3030 longlong value=arg->val_int();
3031 null_value=arg->null_value;
3036 Item_func_if::val_str(
String *str)
3038 DBUG_ASSERT(fixed == 1);
3040 switch (field_type())
3042 case MYSQL_TYPE_DATETIME:
3043 case MYSQL_TYPE_TIMESTAMP:
3044 return val_string_from_datetime(str);
3045 case MYSQL_TYPE_DATE:
3046 return val_string_from_date(str);
3047 case MYSQL_TYPE_TIME:
3048 return val_string_from_time(str);
3053 if ((res= item->val_str(str)))
3055 res->set_charset(collation.collation);
3067 Item_func_if::val_decimal(
my_decimal *decimal_value)
3069 DBUG_ASSERT(fixed == 1);
3071 my_decimal *value= arg->val_decimal(decimal_value);
3072 null_value= arg->null_value;
3077 bool Item_func_if::get_date(
MYSQL_TIME *ltime, uint fuzzydate)
3079 DBUG_ASSERT(fixed == 1);
3081 return (null_value= arg->get_date(ltime, fuzzydate));
3085 bool Item_func_if::get_time(
MYSQL_TIME *ltime)
3087 DBUG_ASSERT(fixed == 1);
3089 return (null_value= arg->get_time(ltime));
3094 Item_func_nullif::fix_length_and_dec()
3096 Item_bool_func2::fix_length_and_dec();
3100 max_length=args[0]->max_length;
3101 decimals=args[0]->decimals;
3102 unsigned_flag= args[0]->unsigned_flag;
3103 cached_result_type= args[0]->result_type();
3104 if (cached_result_type == STRING_RESULT &&
3105 agg_arg_charsets_for_comparison(collation, args, arg_count))
3124 DBUG_ASSERT(fixed == 1);
3131 value= args[0]->val_real();
3132 null_value=args[0]->null_value;
3137 Item_func_nullif::val_int()
3139 DBUG_ASSERT(fixed == 1);
3146 value=args[0]->val_int();
3147 null_value=args[0]->null_value;
3152 Item_func_nullif::val_str(
String *str)
3154 DBUG_ASSERT(fixed == 1);
3161 res=args[0]->val_str(str);
3162 null_value=args[0]->null_value;
3168 Item_func_nullif::val_decimal(
my_decimal * decimal_value)
3170 DBUG_ASSERT(fixed == 1);
3177 res= args[0]->val_decimal(decimal_value);
3178 null_value= args[0]->null_value;
3184 Item_func_nullif::is_null()
3186 return (null_value= (!cmp.compare() ? 1 : args[0]->null_value));
3213 uint value_added_map= 0;
3215 if (first_expr_num == -1)
3217 for (uint i=0 ; i < ncases ; i+=2)
3228 for (uint i=0 ; i < ncases ; i+=2)
3230 if (args[i]->real_item()->
type() == NULL_ITEM)
3232 cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
3233 DBUG_ASSERT(cmp_type != ROW_RESULT);
3234 DBUG_ASSERT(cmp_items[(uint)cmp_type]);
3235 if (!(value_added_map & (1
U << (uint)cmp_type)))
3237 cmp_items[(uint)cmp_type]->store_value(args[first_expr_num]);
3238 if ((null_value=args[first_expr_num]->null_value))
3239 return else_expr_num != -1 ? args[else_expr_num] : 0;
3240 value_added_map|= 1
U << (uint)cmp_type;
3242 if (!cmp_items[(uint)cmp_type]->cmp(args[i]) && !args[
i]->null_value)
3247 return else_expr_num != -1 ? args[else_expr_num] : 0;
3253 DBUG_ASSERT(fixed == 1);
3254 switch (field_type()) {
3255 case MYSQL_TYPE_DATETIME:
3256 case MYSQL_TYPE_TIMESTAMP:
3257 return val_string_from_datetime(str);
3258 case MYSQL_TYPE_DATE:
3259 return val_string_from_date(str);
3260 case MYSQL_TYPE_TIME:
3261 return val_string_from_time(str);
3268 if ((res= item->val_str(str)))
3270 res->set_charset(collation.collation);
3282 longlong Item_func_case::val_int()
3284 DBUG_ASSERT(fixed == 1);
3285 char buff[MAX_FIELD_WIDTH];
3286 String dummy_str(buff,
sizeof(buff),default_charset());
3295 res=item->val_int();
3296 null_value=item->null_value;
3300 double Item_func_case::val_real()
3302 DBUG_ASSERT(fixed == 1);
3303 char buff[MAX_FIELD_WIDTH];
3304 String dummy_str(buff,
sizeof(buff),default_charset());
3313 res= item->val_real();
3314 null_value=item->null_value;
3321 DBUG_ASSERT(fixed == 1);
3322 char buff[MAX_FIELD_WIDTH];
3323 String dummy_str(buff,
sizeof(buff), default_charset());
3333 res= item->val_decimal(decimal_value);
3334 null_value= item->null_value;
3339 bool Item_func_case::get_date(
MYSQL_TIME *ltime, uint fuzzydate)
3341 DBUG_ASSERT(fixed == 1);
3342 char buff[MAX_FIELD_WIDTH];
3343 String dummy_str(buff,
sizeof(buff), default_charset());
3346 return (null_value=
true);
3347 return (null_value= item->get_date(ltime, fuzzydate));
3351 bool Item_func_case::get_time(
MYSQL_TIME *ltime)
3353 DBUG_ASSERT(fixed == 1);
3354 char buff[MAX_FIELD_WIDTH];
3355 String dummy_str(buff,
sizeof(buff), default_charset());
3358 return (null_value=
true);
3359 return (null_value= item->get_time(ltime));
3363 bool Item_func_case::fix_fields(THD *thd,
Item **ref)
3369 uchar buff[MAX_FIELD_WIDTH*2+
sizeof(
String)*2+
sizeof(
String*)*2+
sizeof(double)*2+
sizeof(longlong)*2];
3370 bool res= Item_func::fix_fields(thd, ref);
3381 void Item_func_case::agg_num_lengths(
Item *arg)
3383 uint len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
3384 arg->unsigned_flag) - arg->decimals;
3385 set_if_bigger(max_length, len);
3386 set_if_bigger(decimals, arg->decimals);
3387 unsigned_flag= unsigned_flag && arg->unsigned_flag;
3402 static void change_item_tree_if_needed(THD *thd,
3406 if (*place == new_value)
3409 thd->change_item_tree(place, new_value);
3413 void Item_func_case::fix_length_and_dec()
3417 uint found_types= 0;
3418 THD *thd= current_thd;
3420 if (!(agg= (
Item**) sql_alloc(
sizeof(
Item*)*(ncases+1))))
3427 if (else_expr_num == -1 || args[else_expr_num]->maybe_null)
3435 for (nagg= 0; nagg < ncases / 2; nagg++)
3436 agg[nagg]= args[nagg * 2 + 1];
3438 if (else_expr_num != -1)
3439 agg[nagg++]= args[else_expr_num];
3442 agg_result_type(&cached_result_type, agg, nagg);
3443 if (cached_result_type == STRING_RESULT)
3452 for (nagg= 0 ; nagg < ncases / 2 ; nagg++)
3453 change_item_tree_if_needed(thd, &args[nagg * 2 + 1], agg[nagg]);
3455 if (else_expr_num != -1)
3456 change_item_tree_if_needed(thd, &args[else_expr_num], agg[nagg++]);
3468 collation.set_numeric();
3471 unsigned_flag= TRUE;
3472 for (uint i= 0; i < nagg; i++)
3473 agg_num_lengths(agg[i]);
3474 max_length= my_decimal_precision_to_length_no_truncation(max_length +
3484 if (first_expr_num != -1)
3487 agg[0]= args[first_expr_num];
3488 left_result_type= agg[0]->result_type();
3496 for (nagg= 0; nagg < ncases/2 ; nagg++)
3497 agg[nagg+1]= args[nagg*2];
3499 if (!(found_types= collect_cmp_types(agg, nagg)))
3501 if (found_types & (1
U << STRING_RESULT))
3527 if (agg_arg_charsets_for_comparison(cmp_collation, agg, nagg))
3534 change_item_tree_if_needed(thd, &args[first_expr_num], agg[0]);
3536 for (nagg= 0; nagg < ncases / 2; nagg++)
3537 change_item_tree_if_needed(thd, &args[nagg * 2], agg[nagg + 1]);
3539 for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
3541 if (found_types & (1
U << i) && !cmp_items[
i])
3543 DBUG_ASSERT((Item_result)i != ROW_RESULT);
3545 cmp_item::get_comparator((Item_result)i,
3546 cmp_collation.collation)))
3556 for (i= 0; i < ncases; i+= 2)
3557 args[i]->cmp_context= item_cmp_type(left_result_type,
3558 args[i]->result_type());
3564 uint Item_func_case::decimal_precision()
const
3567 for (uint i=0 ; i < ncases ; i+=2)
3568 set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
3570 if (else_expr_num != -1)
3571 set_if_bigger(max_int_part, args[else_expr_num]->decimal_int_part());
3583 str->append(STRING_WITH_LEN(
"(case "));
3584 if (first_expr_num != -1)
3586 args[first_expr_num]->
print(str, query_type);
3589 for (uint i=0 ; i < ncases ; i+=2)
3591 str->append(STRING_WITH_LEN(
"when "));
3592 args[
i]->
print(str, query_type);
3593 str->append(STRING_WITH_LEN(
" then "));
3594 args[i+1]->
print(str, query_type);
3597 if (else_expr_num != -1)
3599 str->append(STRING_WITH_LEN(
"else "));
3600 args[else_expr_num]->
print(str, query_type);
3603 str->append(STRING_WITH_LEN(
"end)"));
3607 void Item_func_case::cleanup()
3610 DBUG_ENTER(
"Item_func_case::cleanup");
3611 Item_func::cleanup();
3612 for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
3614 delete cmp_items[
i];
3627 DBUG_ASSERT(fixed == 1);
3629 for (uint i=0 ; i < arg_count ; i++)
3632 if ((res=args[i]->val_str(str)))
3641 DBUG_ASSERT(fixed == 1);
3643 for (uint i=0 ; i < arg_count ; i++)
3645 longlong res=args[
i]->val_int();
3646 if (!args[i]->null_value)
3655 DBUG_ASSERT(fixed == 1);
3657 for (uint i=0 ; i < arg_count ; i++)
3659 double res= args[
i]->val_real();
3660 if (!args[i]->null_value)
3670 DBUG_ASSERT(fixed == 1);
3672 for (uint i= 0; i < arg_count; i++)
3674 my_decimal *res= args[
i]->val_decimal(decimal_value);
3675 if (!args[i]->null_value)
3686 DBUG_ASSERT(fixed == 1);
3687 for (uint i= 0; i < arg_count; i++)
3689 if (!args[i]->get_date(ltime, fuzzydate))
3690 return (null_value=
false);
3692 return (null_value=
true);
3696 bool Item_func_coalesce::time_op(
MYSQL_TIME *ltime)
3698 DBUG_ASSERT(fixed == 1);
3699 for (uint i= 0; i < arg_count; i++)
3701 if (!args[i]->get_time(ltime))
3702 return (null_value=
false);
3704 return (null_value=
true);
3708 void Item_func_coalesce::fix_length_and_dec()
3711 agg_result_type(&hybrid_type, args, arg_count);
3712 switch (hybrid_type) {
3717 case DECIMAL_RESULT:
3755 static inline int cmp_longs (longlong a_val, longlong b_val)
3757 return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
3779 static inline int cmp_ulongs (ulonglong a_val, ulonglong b_val)
3781 return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
3808 int cmp_longlong(
void *cmp_arg,
3812 if (a->unsigned_flag != b->unsigned_flag)
3818 if ((a->unsigned_flag && ((ulonglong) a->val) > (ulonglong) LONGLONG_MAX) ||
3819 (b->unsigned_flag && ((ulonglong) b->val) > (ulonglong) LONGLONG_MAX))
3820 return a->unsigned_flag ? 1 : -1;
3825 return cmp_longs (a->val, b->val);
3827 if (a->unsigned_flag)
3828 return cmp_ulongs ((ulonglong) a->val, (ulonglong) b->val);
3830 return cmp_longs (a->val, b->val);
3833 static int cmp_double(
void *cmp_arg,
double *a,
double *b)
3835 return *a < *b ? -1 : *a == *b ? 0 : 1;
3840 return a->compare(b);
3850 a->fix_buffer_pointer();
3851 b->fix_buffer_pointer();
3856 int in_vector::find(
Item *item)
3858 uchar *result=get_value(item);
3859 if (!result || !used_count)
3863 start=0; end=used_count-1;
3864 while (start != end)
3866 uint mid=(start+end+1)/2;
3868 if ((res=(*
compare)(collation, base+mid*
size, result)) == 0)
3875 return (
int) ((*compare)(collation, base+start*
size, result) == 0);
3878 in_string::in_string(uint elements,qsort2_cmp cmp_func,
3881 tmp(buff, sizeof(buff), &my_charset_bin)
3884 in_string::~in_string()
3889 for (uint i=0 ; i < count ; i++)
3894 void in_string::set(uint pos,
Item *item)
3897 String *res=item->val_str(str);
3898 if (res && res != str)
3900 if (res->uses_buffer_owned_by(str))
3902 if (item->type() == Item::FUNC_ITEM)
3907 if (!str->charset())
3910 if (!(cs= item->collation.collation))
3911 cs= &my_charset_bin;
3912 str->set_charset(cs);
3917 uchar *in_string::get_value(
Item *item)
3919 return (uchar*) item->val_str(&tmp);
3922 in_row::in_row(uint elements,
Item * item)
3926 compare= (qsort2_cmp) cmp_row;
3931 used_count= elements;
3941 uchar *in_row::get_value(
Item *item)
3943 tmp.store_value(item);
3944 if (item->is_null())
3946 return (uchar *)&tmp;
3949 void in_row::set(uint pos,
Item *item)
3951 DBUG_ENTER(
"in_row::set");
3952 DBUG_PRINT(
"enter", (
"pos: %u item: 0x%lx", pos, (ulong) item));
3953 ((
cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3957 in_longlong::in_longlong(uint elements)
3958 :
in_vector(elements,sizeof(packed_longlong),(qsort2_cmp) cmp_longlong, 0)
3961 void in_longlong::set(uint pos,
Item *item)
3963 struct packed_longlong *buff= &((packed_longlong*) base)[pos];
3965 buff->val= item->val_int();
3966 buff->unsigned_flag= item->unsigned_flag;
3969 uchar *in_longlong::get_value(
Item *item)
3971 tmp.val= item->val_int();
3972 if (item->null_value)
3974 tmp.unsigned_flag= item->unsigned_flag;
3975 return (uchar*) &tmp;
3979 void in_time_as_longlong::set(uint pos,
Item *item)
3981 struct packed_longlong *buff= &((packed_longlong*) base)[pos];
3983 buff->unsigned_flag= item->unsigned_flag;
3987 uchar *in_time_as_longlong::get_value(
Item *item)
3990 if (item->null_value)
3992 tmp.unsigned_flag= item->unsigned_flag;
3993 return (uchar*) &tmp;
3997 void in_datetime_as_longlong::set(uint pos,
Item *item)
3999 struct packed_longlong *buff= &((packed_longlong*) base)[pos];
4001 buff->unsigned_flag= item->unsigned_flag;
4005 uchar *in_datetime_as_longlong::get_value(
Item *item)
4008 if (item->null_value)
4010 tmp.unsigned_flag= item->unsigned_flag;
4011 return (uchar*) &tmp;
4015 void in_datetime::set(uint pos,
Item *item)
4017 Item **tmp_item= &item;
4019 struct packed_longlong *buff= &((packed_longlong*) base)[pos];
4021 buff->val= get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
4022 buff->unsigned_flag= 1L;
4026 uchar *in_datetime::get_value(
Item *item)
4029 Item **tmp_item= lval_cache ? &lval_cache : &item;
4030 tmp.val= get_datetime_value(thd, &tmp_item, &lval_cache, warn_item, &is_null);
4031 if (item->null_value)
4033 tmp.unsigned_flag= 1L;
4034 return (uchar*) &tmp;
4038 in_double::in_double(uint elements)
4039 :
in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
4042 void in_double::set(uint pos,
Item *item)
4044 ((
double*) base)[pos]= item->val_real();
4047 uchar *in_double::get_value(
Item *item)
4049 tmp= item->val_real();
4050 if (item->null_value)
4052 return (uchar*) &tmp;
4056 in_decimal::in_decimal(uint elements)
4061 void in_decimal::set(uint pos,
Item *item)
4066 dec->fix_buffer_pointer();
4069 if (!item->null_value && res != dec)
4070 my_decimal2decimal(res, dec);
4074 uchar *in_decimal::get_value(
Item *item)
4077 if (item->null_value)
4079 return (uchar *)result;
4083 cmp_item* cmp_item::get_comparator(Item_result type,
4095 case DECIMAL_RESULT:
4105 cmp_item* cmp_item_sort_string::make_same()
4110 cmp_item* cmp_item_int::make_same()
4115 cmp_item* cmp_item_real::make_same()
4120 cmp_item* cmp_item_row::make_same()
4126 cmp_item_row::~cmp_item_row()
4128 DBUG_ENTER(
"~cmp_item_row");
4129 DBUG_PRINT(
"enter",(
"this: 0x%lx", (
long)
this));
4132 for (uint i= 0; i <
n; i++)
4135 delete comparators[
i];
4142 void cmp_item_row::alloc_comparators()
4149 void cmp_item_row::store_value(
Item *item)
4151 DBUG_ENTER(
"cmp_item_row::store_value");
4153 alloc_comparators();
4156 item->bring_value();
4157 item->null_value= 0;
4158 for (uint i=0; i <
n; i++)
4160 if (!comparators[i])
4161 if (!(comparators[i]=
4162 cmp_item::get_comparator(item->element_index(i)->result_type(),
4163 item->element_index(i)->collation.collation)))
4165 comparators[
i]->store_value(item->element_index(i));
4166 item->null_value|= item->element_index(i)->null_value;
4173 void cmp_item_row::store_value_by_template(
cmp_item *t,
Item *item)
4176 if (tmpl->n != item->cols())
4178 my_error(ER_OPERAND_COLUMNS, MYF(0), tmpl->n);
4184 item->bring_value();
4185 item->null_value= 0;
4186 for (uint i=0; i <
n; i++)
4188 if (!(comparators[i]= tmpl->comparators[i]->make_same()))
4190 comparators[
i]->store_value_by_template(tmpl->comparators[i],
4191 item->element_index(i));
4192 item->null_value|= item->element_index(i)->null_value;
4198 int cmp_item_row::cmp(
Item *arg)
4201 if (arg->cols() !=
n)
4203 my_error(ER_OPERAND_COLUMNS, MYF(0), n);
4208 for (uint i=0; i <
n; i++)
4210 if (comparators[i]->cmp(arg->element_index(i)))
4212 if (!arg->element_index(i)->null_value)
4217 return (arg->null_value= was_null);
4224 for (uint i=0; i <
n; i++)
4227 if ((res= comparators[i]->
compare(l_cmp->comparators[i])))
4234 void cmp_item_decimal::store_value(
Item *item)
4238 if (val && val != &value)
4239 my_decimal2decimal(val, &value);
4243 int cmp_item_decimal::cmp(
Item *arg)
4245 my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
4246 if (arg->null_value)
4259 cmp_item* cmp_item_decimal::make_same()
4265 void cmp_item_datetime::store_value(
Item *item)
4268 Item **tmp_item= lval_cache ? &lval_cache : &item;
4269 value= get_datetime_value(thd, &tmp_item, &lval_cache, warn_item, &is_null);
4273 int cmp_item_datetime::cmp(
Item *arg)
4276 Item **tmp_item= &arg;
4278 get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
4285 return (value < l_cmp->value) ? -1 : ((value == l_cmp->value) ? 0 : 1);
4289 cmp_item *cmp_item_datetime::make_same()
4295 bool Item_func_in::nulls_in_row()
4297 Item **arg,**arg_end;
4298 for (arg= args+1, arg_end= args+arg_count; arg != arg_end ; arg++)
4300 if ((*arg)->null_inside())
4338 Item **arg, **arg_end;
4340 if (Item_func_opt_neg::fix_fields(thd, ref))
4344 if (pred_level && negated)
4349 for (arg= args + 1, arg_end= args + arg_count; arg != arg_end; arg++)
4358 return cs->coll->strnncollsp(cs,
4359 (uchar *) x->ptr(),x->length(),
4360 (uchar *) y->ptr(),y->length(), 0);
4364 void Item_func_in::fix_length_and_dec()
4366 Item **arg, **arg_end;
4368 THD *thd= current_thd;
4369 bool datetime_found= FALSE;
4371 bool compare_as_datetime= FALSE;
4373 uint found_types= 0;
4374 uint type_cnt= 0,
i;
4375 Item_result cmp_type= STRING_RESULT;
4376 left_result_type= args[0]->result_type();
4377 if (!(found_types= collect_cmp_types(args, arg_count,
true)))
4380 for (arg= args + 1, arg_end= args + arg_count; arg != arg_end ; arg++)
4382 if (!arg[0]->const_item())
4388 for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
4390 if (found_types & (1
U << i))
4393 cmp_type= (Item_result) i;
4399 if (cmp_type == STRING_RESULT &&
4400 agg_arg_charsets_for_comparison(cmp_collation, args, arg_count))
4402 arg_types_compatible= TRUE;
4410 if (cmp_type == ROW_RESULT)
4413 if (const_itm && !nulls_in_row())
4415 array=
new in_row(arg_count-1, 0);
4416 cmp= &((
in_row*)array)->tmp;
4422 cmp_items[ROW_RESULT]= cmp;
4424 cmp->n= args[0]->cols();
4425 cmp->alloc_comparators();
4428 if (cmp_type == STRING_RESULT || cmp_type == ROW_RESULT)
4430 uint col, cols= args[0]->cols();
4432 for (col= 0; col < cols; col++)
4434 bool skip_column= FALSE;
4439 for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
4441 Item *itm= ((cmp_type == STRING_RESULT) ? arg[0] :
4442 arg[0]->element_index(col));
4443 if (itm->result_type() != STRING_RESULT)
4448 else if (itm->is_temporal_with_date())
4450 datetime_found= TRUE;
4457 else if (itm->field_type() == MYSQL_TYPE_DATETIME)
4461 if (cmp_type == STRING_RESULT)
4470 if (cmp_type == ROW_RESULT)
4474 cmp= ((
in_row*)array)->tmp.comparators + col;
4476 cmp= ((
cmp_item_row*)cmp_items[ROW_RESULT])->comparators + col;
4480 datetime_found= FALSE;
4483 compare_as_datetime= TRUE;
4492 if (type_cnt == 1 && const_itm && !nulls_in_row())
4494 if (compare_as_datetime)
4505 bool datetime_as_longlong=
false;
4506 if (args[0]->real_item()->
type() == FIELD_ITEM &&
4507 thd->lex->sql_command != SQLCOM_CREATE_VIEW &&
4508 thd->lex->sql_command != SQLCOM_SHOW_CREATE &&
4509 cmp_type != INT_RESULT)
4512 if (field_item->field->can_be_compared_as_longlong())
4514 bool all_converted=
true;
4515 for (arg=args + 1, arg_end= args + arg_count; arg != arg_end ; arg++)
4517 if (!convert_constant_item (thd, field_item, &arg[0]))
4518 all_converted=
false;
4522 cmp_type= INT_RESULT;
4523 datetime_as_longlong= field_item->is_temporal();
4529 array=
new in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
4530 cmp_collation.collation);
4533 array= datetime_as_longlong ?
4534 args[0]->field_type() == MYSQL_TYPE_TIME ?
4548 ((
in_row*)array)->tmp.store_value(args[0]);
4550 case DECIMAL_RESULT:
4558 if (array && !(thd->is_fatal_error))
4561 for (uint i=1 ; i < arg_count ; i++)
4563 array->set(j,args[i]);
4564 if (!args[i]->null_value)
4569 if ((array->used_count= j))
4575 if (compare_as_datetime)
4579 for (i= 0; i <= (uint) DECIMAL_RESULT; i++)
4581 if (found_types & (1
U << i) && !cmp_items[
i])
4583 if ((Item_result)i == STRING_RESULT &&
4584 agg_arg_charsets_for_comparison(cmp_collation, args, arg_count))
4586 if (!cmp_items[i] && !(cmp_items[i]=
4587 cmp_item::get_comparator((Item_result)i,
4588 cmp_collation.collation)))
4600 for (arg= args + 1, arg_end= args + arg_count; arg != arg_end ; arg++)
4602 arg[0]->cmp_context= item_cmp_type(left_result_type, arg[0]->result_type());
4611 args[0]->
print(str, query_type);
4613 str->append(STRING_WITH_LEN(
" not"));
4614 str->append(STRING_WITH_LEN(
" in ("));
4615 print_args(str, 1, query_type);
4616 str->append(STRING_WITH_LEN(
"))"));
4645 longlong Item_func_in::val_int()
4648 DBUG_ASSERT(fixed == 1);
4649 uint value_added_map= 0;
4652 int tmp=array->find(args[0]);
4653 null_value=args[0]->null_value || (!tmp && have_null);
4654 return (longlong) (!null_value && tmp != negated);
4657 if ((null_value= args[0]->real_item()->type() == NULL_ITEM))
4661 for (uint i= 1 ; i < arg_count ; i++)
4663 if (args[i]->real_item()->type() == NULL_ITEM)
4668 Item_result cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
4669 in_item= cmp_items[(uint)cmp_type];
4670 DBUG_ASSERT(in_item);
4671 if (!(value_added_map & (1
U << (uint)cmp_type)))
4673 in_item->store_value(args[0]);
4674 if ((null_value= args[0]->null_value))
4676 value_added_map|= 1
U << (uint)cmp_type;
4678 if (!in_item->cmp(args[i]) && !args[
i]->null_value)
4679 return (longlong) (!negated);
4680 have_null|= args[
i]->null_value;
4683 null_value= have_null;
4684 return (longlong) (!null_value && negated);
4688 longlong Item_func_bit_or::val_int()
4690 DBUG_ASSERT(fixed == 1);
4691 ulonglong arg1= (ulonglong) args[0]->val_int();
4692 if (args[0]->null_value)
4697 ulonglong arg2= (ulonglong) args[1]->val_int();
4698 if (args[1]->null_value)
4704 return (longlong) (arg1 | arg2);
4708 longlong Item_func_bit_and::val_int()
4710 DBUG_ASSERT(fixed == 1);
4711 ulonglong arg1= (ulonglong) args[0]->val_int();
4712 if (args[0]->null_value)
4717 ulonglong arg2= (ulonglong) args[1]->val_int();
4718 if (args[1]->null_value)
4724 return (longlong) (arg1 & arg2);
4727 Item_cond::Item_cond(THD *thd,
Item_cond *item)
4729 abort_on_null(item->abort_on_null)
4737 void Item_cond::copy_andor_arguments(THD *thd,
Item_cond *item,
bool real_items)
4740 while (
Item *it= li++)
4741 list.push_back((real_items ? it->real_item() : it)->
4747 Item_cond::fix_fields(THD *thd,
Item **ref)
4749 DBUG_ASSERT(fixed == 0);
4752 Switch_resolve_place SRP(&thd->lex->current_select->resolve_place,
4753 st_select_lex::RESOLVE_NONE,
4754 functype() != COND_AND_FUNC);
4755 uchar buff[
sizeof(
char*)];
4759 if (functype() == COND_AND_FUNC && abort_on_null)
4783 while (item->type() == Item::COND_ITEM &&
4784 ((
Item_cond*) item)->functype() == functype() &&
4792 item->top_level_item();
4795 if ((!item->fixed &&
4796 item->fix_fields(thd, li.ref())) ||
4797 (item= *li.ref())->check_cols(1))
4799 used_tables_cache|= item->used_tables();
4802 if (functype() == COND_AND_FUNC && abort_on_null)
4806 with_sum_func|= item->with_sum_func;
4808 with_stored_program|= item->has_stored_program();
4809 if (item->maybe_null)
4812 thd->lex->current_select->cond_count+= list.elements;
4813 fix_length_and_dec();
4820 st_select_lex *removed_select)
4828 if (functype() == COND_AND_FUNC && abort_on_null)
4836 used_tables_cache|= item->used_tables();
4838 if (functype() == COND_AND_FUNC && abort_on_null)
4846 bool Item_cond::walk(Item_processor processor,
bool walk_subquery, uchar *arg)
4850 while ((item= li++))
4851 if (item->walk(processor, walk_subquery, arg))
4853 return Item_func::walk(processor, walk_subquery, arg);
4877 DBUG_ASSERT(!current_thd->stmt_arena->is_stmt_prepare());
4881 while ((item= li++))
4893 if (new_item != item)
4894 current_thd->change_item_tree(li.ref(), new_item);
4926 Item_transformer transformer, uchar *arg_t)
4928 if (!(this->*analyzer)(arg_p))
4933 while ((item= li++))
4939 uchar *arg_v= *arg_p;
4940 Item *new_item= item->compile(analyzer, &arg_v, transformer, arg_t);
4941 if (new_item == NULL)
4943 if (new_item != item)
4944 current_thd->change_item_tree(li.ref(), new_item);
4949 void Item_cond::traverse_cond(Cond_traverser traverser,
4950 void *arg, traverse_order order)
4957 (*traverser)(
this, arg);
4958 while ((item= li++))
4960 item->traverse_cond(traverser, arg, order);
4962 (*traverser)(NULL, arg);
4965 while ((item= li++))
4967 item->traverse_cond(traverser, arg, order);
4969 (*traverser)(
this, arg);
4995 while ((item= li++))
4996 item->
split_sum_func2(thd, ref_pointer_array, fields, li.ref(), TRUE);
5000 void Item_cond::update_used_tables()
5005 used_tables_cache=0;
5007 with_subselect=
false;
5008 with_stored_program=
false;
5011 item->update_used_tables();
5012 used_tables_cache|= item->used_tables();
5015 with_stored_program|= item->has_stored_program();
5026 item->
print(str, query_type);
5030 str->append(func_name());
5032 item->
print(str, query_type);
5038 void Item_cond::neg_arguments(THD *thd)
5042 while ((item= li++))
5044 Item *new_item= item->neg_transformer(thd);
5050 (void) li.replace(new_item);
5077 DBUG_ASSERT(fixed == 1);
5085 if (abort_on_null || !(null_value= item->null_value))
5089 return null_value ? 0 : 1;
5093 longlong Item_cond_or::val_int()
5095 DBUG_ASSERT(fixed == 1);
5106 if (item->null_value)
5135 return (*org_item= (
Item*) b);
5141 res->set_used_tables(a->used_tables() | b->used_tables());
5142 res->set_not_null_tables(a->not_null_tables() | b->not_null_tables());
5148 ((
Item_cond_and*) a)->set_used_tables(a->used_tables() | b->used_tables());
5149 ((
Item_cond_and*) a)->set_not_null_tables(a->not_null_tables() |
5150 b->not_null_tables());
5155 longlong Item_func_isnull::val_int()
5157 DBUG_ASSERT(fixed == 1);
5163 return cached_value;
5164 return args[0]->is_null() ? 1: 0;
5167 longlong Item_is_not_null_test::val_int()
5169 DBUG_ASSERT(fixed == 1);
5170 DBUG_ENTER(
"Item_is_not_null_test::val_int");
5171 if (!used_tables_cache && !with_subselect && !with_stored_program)
5178 owner->was_null|= (!cached_value);
5179 DBUG_PRINT(
"info", (
"cached: %ld", (
long) cached_value));
5180 DBUG_RETURN(cached_value);
5182 if (args[0]->is_null())
5184 DBUG_PRINT(
"info", (
"null"));
5185 owner->was_null|= 1;
5198 used_tables_cache= initial_pseudo_tables;
5199 if (!args[0]->maybe_null)
5204 args[0]->update_used_tables();
5206 with_stored_program= args[0]->has_stored_program();
5207 used_tables_cache|= args[0]->used_tables();
5208 if (used_tables_cache == initial_pseudo_tables && !with_subselect &&
5209 !with_stored_program)
5211 cached_value= !args[0]->is_null();
5215 longlong Item_func_isnotnull::val_int()
5217 DBUG_ASSERT(fixed == 1);
5218 return args[0]->is_null() ? 0 : 1;
5225 args[0]->
print(str, query_type);
5226 str->append(STRING_WITH_LEN(
" is not null)"));
5230 longlong Item_func_like::val_int()
5232 DBUG_ASSERT(fixed == 1);
5233 String* res = args[0]->val_str(&cmp.value1);
5234 if (args[0]->null_value)
5239 String* res2 = args[1]->val_str(&cmp.value2);
5240 if (args[1]->null_value)
5247 return turboBM_matches(res->ptr(), res->length()) ? 1 : 0;
5248 return my_wildcmp(cmp.cmp_collation.collation,
5249 res->ptr(),res->ptr()+res->length(),
5250 res2->ptr(),res2->ptr()+res2->length(),
5251 escape,wild_one,wild_many) ? 0 : 1;
5261 if (!args[1]->const_item())
5262 return OPTIMIZE_NONE;
5266 return OPTIMIZE_NONE;
5268 if (!res2->length())
5271 DBUG_ASSERT(res2->ptr());
5272 char first= res2->ptr()[0];
5273 return (first == wild_many || first == wild_one) ?
5274 OPTIMIZE_NONE : OPTIMIZE_OP;
5278 bool Item_func_like::fix_fields(THD *thd,
Item **ref)
5280 DBUG_ASSERT(fixed == 0);
5281 if (Item_bool_func2::fix_fields(thd, ref) ||
5282 escape_item->fix_fields(thd, &escape_item))
5285 if (!escape_item->const_during_execution())
5287 my_error(ER_WRONG_ARGUMENTS,MYF(0),
"ESCAPE");
5291 if (escape_item->const_item())
5294 String *escape_str= escape_item->val_str(&cmp.value1);
5297 const char *escape_str_ptr= escape_str->ptr();
5298 if (escape_used_in_parsing && (
5299 (((thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
5300 escape_str->numchars() != 1) ||
5301 escape_str->numchars() > 1)))
5303 my_error(ER_WRONG_ARGUMENTS,MYF(0),
"ESCAPE");
5307 if (use_mb(cmp.cmp_collation.collation))
5311 int rc= cs->cset->mb_wc(cs, &wc,
5312 (
const uchar*) escape_str_ptr,
5313 (
const uchar*) escape_str_ptr +
5314 escape_str->length());
5315 escape= (int) (rc > 0 ? wc :
'\\');
5326 if (escape_str->needs_conversion(escape_str->length(),
5327 escape_str->charset(), cs, &unused))
5331 uint32 cnvlen= copy_and_convert(&ch, 1, cs, escape_str_ptr,
5332 escape_str->length(),
5333 escape_str->charset(), &errors);
5334 escape= cnvlen ? ch :
'\\';
5337 escape= escape_str_ptr ? *escape_str_ptr :
'\\';
5347 if (args[1]->const_item() && !use_strnxfrm(collation.collation) &&
5348 !(specialflag & SPECIAL_NO_NEW_FUNC))
5350 String* res2 = args[1]->val_str(&cmp.value2);
5354 const size_t len = res2->length();
5355 const char* first = res2->ptr();
5356 const char* last = first + len - 1;
5362 if (len > MIN_TURBOBM_PATTERN_LEN + 2 &&
5363 *first == wild_many &&
5366 const char* tmp = first + 1;
5367 for (; *tmp != wild_many && *tmp != wild_one && *tmp != escape; tmp++) ;
5368 canDoTurboBM = (tmp == last) && !use_mb(args[0]->collation.collation);
5372 pattern_len = (int) len - 2;
5373 pattern = thd->strmake(first + 1, pattern_len);
5374 DBUG_PRINT(
"info", (
"Initializing pattern: '%s'", first));
5375 int *suff = (
int*) thd->alloc((
int) (
sizeof(
int)*
5376 ((pattern_len + 1)*2+
5378 bmGs = suff + pattern_len + 1;
5379 bmBc = bmGs + pattern_len + 1;
5380 turboBM_compute_good_suffix_shifts(suff);
5381 turboBM_compute_bad_character_shifts();
5382 DBUG_PRINT(
"info",(
"done"));
5389 void Item_func_like::cleanup()
5391 canDoTurboBM= FALSE;
5392 Item_bool_func2::cleanup();
5408 int Item_func_regex::regcomp(
bool send_error)
5410 char buff[MAX_FIELD_WIDTH];
5411 String tmp(buff,
sizeof(buff),&my_charset_bin);
5412 String *res= args[1]->val_str(&tmp);
5415 if (args[1]->null_value)
5420 if (!stringcmp(res, &prev_regexp))
5422 prev_regexp.copy(*res);
5427 if (cmp_collation.collation != regex_lib_charset)
5431 if (conv.copy(res->ptr(), res->length(), res->charset(),
5432 regex_lib_charset, &dummy_errors))
5437 if ((error= my_regcomp(&preg, res->c_ptr_safe(),
5438 regex_lib_flags, regex_lib_charset)))
5442 (void) my_regerror(error, &preg, buff,
sizeof(buff));
5443 my_error(ER_REGEXP_ERROR, MYF(0), buff);
5453 Item_func_regex::fix_fields(THD *thd,
Item **ref)
5455 DBUG_ASSERT(fixed == 0);
5456 if ((!args[0]->fixed &&
5457 args[0]->fix_fields(thd, args)) || args[0]->check_cols(1) ||
5459 args[1]->fix_fields(thd, args + 1)) || args[1]->check_cols(1))
5461 with_sum_func=args[0]->with_sum_func || args[1]->with_sum_func;
5463 with_stored_program= args[0]->has_stored_program() ||
5464 args[1]->has_stored_program();
5468 if (agg_arg_charsets_for_comparison(cmp_collation, args, 2))
5471 regex_lib_flags= (cmp_collation.collation->state &
5472 (MY_CS_BINSORT | MY_CS_CSSORT)) ?
5473 MY_REG_EXTENDED | MY_REG_NOSUB :
5474 MY_REG_EXTENDED | MY_REG_NOSUB | MY_REG_ICASE;
5479 regex_lib_charset= (cmp_collation.collation->mbminlen > 1) ?
5480 &my_charset_utf8_general_ci :
5481 cmp_collation.collation;
5483 used_tables_cache=args[0]->used_tables() | args[1]->used_tables();
5485 args[1]->not_null_tables());
5487 if (!regex_compiled && args[1]->const_item())
5489 int comp_res= regcomp(TRUE);
5499 maybe_null= args[0]->maybe_null;
5508 longlong Item_func_regex::val_int()
5510 DBUG_ASSERT(fixed == 1);
5511 char buff[MAX_FIELD_WIDTH];
5512 String tmp(buff,
sizeof(buff),&my_charset_bin);
5513 String *res= args[0]->val_str(&tmp);
5515 if ((null_value= (args[0]->null_value ||
5516 (!regex_is_const && regcomp(FALSE)))))
5519 if (cmp_collation.collation != regex_lib_charset)
5523 if (conv.copy(res->ptr(), res->length(), res->charset(),
5524 regex_lib_charset, &dummy_errors))
5531 return my_regexec(&preg,res->c_ptr_safe(),0,(
my_regmatch_t*) 0,0) ? 0 : 1;
5535 void Item_func_regex::cleanup()
5537 DBUG_ENTER(
"Item_func_regex::cleanup");
5538 Item_bool_func::cleanup();
5543 prev_regexp.length(0);
5549 #ifdef LIKE_CMP_TOUPPER
5550 #define likeconv(cs,A) (uchar) (cs)->toupper(A)
5552 #define likeconv(cs,A) (uchar) (cs)->sort_order[(uchar) (A)]
5560 void Item_func_like::turboBM_compute_suffixes(
int *suff)
5562 const int plm1 = pattern_len - 1;
5565 int *
const splm1 = suff + plm1;
5568 *splm1 = pattern_len;
5570 if (!cs->sort_order)
5573 for (i = pattern_len - 2; i >= 0; i--)
5575 int tmp = *(splm1 + i - f);
5576 if (g < i && tmp < i - g)
5583 while (g >= 0 && pattern[g] == pattern[g + plm1 - f])
5592 for (i = pattern_len - 2; 0 <=
i; --
i)
5594 int tmp = *(splm1 + i - f);
5595 if (g < i && tmp < i - g)
5603 likeconv(cs, pattern[g]) == likeconv(cs, pattern[g + plm1 - f]))
5616 void Item_func_like::turboBM_compute_good_suffix_shifts(
int *suff)
5618 turboBM_compute_suffixes(suff);
5620 int *end = bmGs + pattern_len;
5622 for (k = bmGs; k < end; k++)
5628 const int plm1 = pattern_len - 1;
5629 for (i = plm1; i > -1; i--)
5631 if (suff[i] == i + 1)
5633 for (tmp = plm1 - i; j < tmp; j++)
5635 int *tmp2 = bmGs + j;
5636 if (*tmp2 == pattern_len)
5643 for (tmp = plm1 - i; j < tmp; j++)
5646 if (*tmp2 == pattern_len)
5651 for (i = 0; i <= pattern_len - 2; i++)
5652 *(tmp2 - suff[i]) = plm1 -
i;
5660 void Item_func_like::turboBM_compute_bad_character_shifts()
5663 int *end = bmBc + alphabet_size;
5665 const int plm1 = pattern_len - 1;
5668 for (i = bmBc; i < end; i++)
5671 if (!cs->sort_order)
5673 for (j = 0; j < plm1; j++)
5674 bmBc[(uint) (uchar) pattern[j]] = plm1 - j;
5678 for (j = 0; j < plm1; j++)
5679 bmBc[(uint) likeconv(cs,pattern[j])] = plm1 - j;
5691 bool Item_func_like::turboBM_matches(
const char* text,
int text_len)
const
5695 int shift = pattern_len;
5700 const int plm1= pattern_len - 1;
5701 const int tlmpl= text_len - pattern_len;
5704 if (!cs->sort_order)
5709 while (i >= 0 && pattern[i] == text[i + j])
5712 if (i == plm1 - shift)
5718 const int v = plm1 -
i;
5720 bcShift = bmBc[(uint) (uchar) text[i + j]] - plm1 + i;
5721 shift = max(turboShift, bcShift);
5722 shift = max(shift, bmGs[i]);
5723 if (shift == bmGs[i])
5724 u = min(pattern_len - shift, v);
5727 if (turboShift < bcShift)
5728 shift = max(shift, u + 1);
5740 while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
5743 if (i == plm1 - shift)
5749 const int v = plm1 -
i;
5751 bcShift = bmBc[(uint) likeconv(cs, text[i + j])] - plm1 +
i;
5752 shift = max(turboShift, bcShift);
5753 shift = max(shift, bmGs[i]);
5754 if (shift == bmGs[i])
5755 u = min(pattern_len - shift, v);
5758 if (turboShift < bcShift)
5759 shift = max(shift, u + 1);
5787 DBUG_ASSERT(fixed == 1);
5790 for (uint i= 0; i < arg_count; i++)
5792 result^= (args[
i]->val_int() != 0);
5793 if (args[i]->null_value)
5834 Item *Item_bool_rowready_func2::neg_transformer(THD *thd)
5855 new_item=
new(thd->mem_root)
Item_func_xor(neg_operand, args[1]);
5858 new_item=
new(thd->mem_root)
Item_func_xor(args[0], neg_operand);
5862 new_item=
new(thd->mem_root)
Item_func_xor(neg_operand, args[1]);
5888 Item *Item_cond_and::neg_transformer(THD *thd)
5897 Item *Item_cond_or::neg_transformer(THD *thd)
5911 allany->func= allany->func_creator(FALSE);
5912 allany->all= !allany->all;
5913 allany->upper_item= new_item;
5922 allany->all= !allany->all;
5923 allany->func= allany->func_creator(TRUE);
5924 allany->upper_item= new_item;
5974 compare_as_dates(FALSE)
5977 fields.push_back(f1);
5978 fields.push_back(f2);
5985 fields.push_back(f);
5987 compare_as_dates= f->is_temporal_with_date();
5991 Item_equal::Item_equal(
Item_equal *item_equal)
5997 while ((item= li++))
5999 fields.push_back(item);
6001 const_item= item_equal->const_item;
6002 compare_as_dates= item_equal->compare_as_dates;
6003 cond_false= item_equal->cond_false;
6007 void Item_equal::compare_const(
Item *c)
6009 if (compare_as_dates)
6011 cmp.set_datetime_cmp_func(
this, &c, &const_item);
6012 cond_false= cmp.compare();
6017 if(func->set_cmp_func())
6019 func->quick_fix_field();
6020 cond_false= !func->val_int();
6035 compare_as_dates= f->is_temporal_with_date();
6042 void Item_equal::add(
Item *c)
6056 fields.push_back(f);
6059 uint Item_equal::members()
6061 return fields.elements;
6082 while ((item= it++))
6084 if (field->eq(item->field))
6104 fields.concat(&item->fields);
6105 Item *c= item->const_item;
6115 cond_false|= item->cond_false;
6138 fields.
sort((Node_cmp_func)compare, arg);
6156 while ((item= it++))
6158 if (item->const_item() &&
6172 !item->is_outer_field())
6180 bool Item_equal::fix_fields(THD *thd,
Item **ref)
6186 while ((item= li++))
6188 table_map tmp_table_map;
6189 used_tables_cache|= item->used_tables();
6190 tmp_table_map= item->not_null_tables();
6192 if (item->maybe_null)
6195 fix_length_and_dec();
6200 void Item_equal::update_used_tables()
6207 with_subselect=
false;
6208 with_stored_program=
false;
6211 item->update_used_tables();
6212 used_tables_cache|= item->used_tables();
6216 with_stored_program|= item->has_stored_program();
6220 longlong Item_equal::val_int()
6226 Item *item= const_item ? const_item : it++;
6227 eval_item->store_value(item);
6228 if ((null_value= item->null_value))
6230 while ((item_field= it++))
6233 if (item_field->field->table->const_table)
6235 if (eval_item->cmp(item_field) || (null_value= item_field->null_value))
6242 void Item_equal::fix_length_and_dec()
6245 eval_item= cmp_item::get_comparator(item->result_type(),
6246 item->collation.collation);
6249 bool Item_equal::walk(Item_processor processor,
bool walk_subquery, uchar *arg)
6253 while ((item= it++))
6255 if (item->walk(processor, walk_subquery, arg))
6258 return Item_func::walk(processor, walk_subquery, arg);
6263 DBUG_ASSERT(!current_thd->stmt_arena->is_stmt_prepare());
6267 while ((item= it++))
6279 if (new_item != item)
6280 current_thd->change_item_tree((
Item **) it.ref(), new_item);
6287 str->append(func_name());
6292 const_item->
print(str, query_type);
6296 item->
print(str, query_type);
6298 while ((item= it++))
6302 item->
print(str, query_type);
6322 str->append(
"is_not_null_compl");
6325 str->append(
"found_match");
6328 str->append(
"outer_field_is_not_null");
6333 if (trig_tab != NULL)
6336 str->append(trig_tab->table->alias);
6341 str->append(trig_tab->
last_inner->table->alias);
6346 args[0]->
print(str, query_type);
6347 str->append(
", true)");
6374 DBUG_ASSERT(field != NULL);
6376 const JOIN_TAB *field_tab= field->field->table->reginfo.join_tab;
6413 const JOIN_TAB *last= field_tab->last_sj_inner_tab;
6415 while ((item= it++))
6417 if (item->field->table->reginfo.join_tab >= first &&
6418 item->field->table->reginfo.join_tab <= last)
6448 return fields.head();
6473 while ((item= it++))
6476 const JOIN_TAB *tab= item->field->table->reginfo.join_tab;
6484 while ((existing= mit++))
6486 if (existing->real_item()->
eq(item,
false))
6487 added_fields.push_back(sj_nest->nested_join->sjm.
mat_fields[fieldno]);
6491 fields.concat(&added_fields);
6515 while ((existing= mit++))
6517 if (existing->real_item()->
eq(args[1],
false) &&
6518 (args[0]->used_tables() & ~sj_nest->sj_inner_tables))
6519 current_thd->change_item_tree(args+1,
6520 sj_nest->nested_join->sjm.
mat_fields[fieldno]);