26 #include <drizzled/cached_item.h>
27 #include <drizzled/check_stack_overrun.h>
28 #include <drizzled/current_session.h>
29 #include <drizzled/error.h>
30 #include <drizzled/internal/my_sys.h>
31 #include <drizzled/item/cache_int.h>
32 #include <drizzled/item/cmpfunc.h>
33 #include <drizzled/item/int_with_ref.h>
34 #include <drizzled/item/subselect.h>
35 #include <drizzled/session.h>
36 #include <drizzled/sql_lex.h>
37 #include <drizzled/sql_select.h>
38 #include <drizzled/system_variables.h>
40 #include <drizzled/time_functions.h>
49 extern const double log_10[309];
51 static Eq_creator eq_creator;
52 static Ne_creator ne_creator;
53 static Gt_creator gt_creator;
54 static Lt_creator lt_creator;
55 static Ge_creator ge_creator;
56 static Le_creator le_creator;
60 static Item_result item_store_type(Item_result a, Item *item,
63 Item_result b= item->result_type();
65 if (a == STRING_RESULT || b == STRING_RESULT)
67 else if (a == REAL_RESULT || b == REAL_RESULT)
69 else if (a == DECIMAL_RESULT || b == DECIMAL_RESULT ||
70 unsigned_flag != item->unsigned_flag)
71 return DECIMAL_RESULT;
76 static void agg_result_type(Item_result *type, Item **items, uint32_t nitems)
78 Item **item, **item_end;
79 bool unsigned_flag= 0;
83 for (item= items, item_end= item + nitems; item < item_end; item++)
85 if ((*item)->type() != Item::NULL_ITEM)
87 *type= (*item)->result_type();
88 unsigned_flag= (*item)->unsigned_flag;
94 for (; item < item_end; item++)
96 if ((*item)->type() != Item::NULL_ITEM)
98 *type= item_store_type(*type, *item, unsigned_flag);
124 static int cmp_row_type(Item* item1, Item* item2)
126 uint32_t n= item1->cols();
127 if (item2->check_cols(n))
129 for (uint32_t i=0; i<n; i++)
131 if (item2->element_index(i)->check_cols(item1->element_index(i)->cols()) ||
132 (item1->element_index(i)->result_type() == ROW_RESULT &&
133 cmp_row_type(item1->element_index(i), item2->element_index(i))))
166 type[0]= items[0]->result_type();
167 for (i= 1 ; i < nitems ; i++)
169 type[0]= item_cmp_type(type[0], items[i]->result_type());
177 if (type[0] == ROW_RESULT && cmp_row_type(items[0], items[i]))
205 if (!nitems || items[0]->result_type() == ROW_RESULT )
206 return (enum_field_types)-1;
207 enum_field_types res= items[0]->field_type();
208 for (i= 1 ; i < nitems ; i++)
209 res= Field::field_type_merge(res, items[i]->field_type());
231 static uint32_t collect_cmp_types(Item **items, uint32_t nitems,
bool skip_nulls=
false)
234 uint32_t found_types;
235 Item_result left_result= items[0]->result_type();
238 for (i= 1; i < nitems ; i++)
240 if (skip_nulls && items[i]->type() == Item::NULL_ITEM)
242 if ((left_result == ROW_RESULT ||
243 items[i]->result_type() == ROW_RESULT) &&
244 cmp_row_type(items[0], items[i]))
246 found_types|= 1<< (uint32_t)item_cmp_type(left_result,
247 items[i]->result_type());
253 if (skip_nulls && !found_types)
254 found_types= 1 << (uint)left_result;
259 Item_bool_func2* Eq_creator::create(Item *a, Item *b)
const
261 return new Item_func_eq(a, b);
265 const Eq_creator* Eq_creator::instance()
271 Item_bool_func2* Ne_creator::create(Item *a, Item *b)
const
273 return new Item_func_ne(a, b);
277 const Ne_creator* Ne_creator::instance()
283 Item_bool_func2* Gt_creator::create(Item *a, Item *b)
const
285 return new Item_func_gt(a, b);
289 const Gt_creator* Gt_creator::instance()
295 Item_bool_func2* Lt_creator::create(Item *a, Item *b)
const
297 return new Item_func_lt(a, b);
301 const Lt_creator* Lt_creator::instance()
307 Item_bool_func2* Ge_creator::create(Item *a, Item *b)
const
309 return new Item_func_ge(a, b);
313 const Ge_creator* Ge_creator::instance()
319 Item_bool_func2* Le_creator::create(Item *a, Item *b)
const
321 return new Item_func_le(a, b);
324 const Le_creator* Le_creator::instance()
336 int64_t Item_func_not::val_int()
339 bool value= args[0]->val_bool();
340 null_value=args[0]->null_value;
341 return ((!null_value && value == 0) ? 1 : 0);
356 Item_func::print(str);
365 int64_t Item_func_not_all::val_int()
368 bool value= args[0]->val_bool();
374 if (empty_underlying_subquery())
377 null_value= args[0]->null_value;
378 return ((!null_value && value == 0) ? 1 : 0);
382 bool Item_func_not_all::empty_underlying_subquery()
384 return ((test_sum_item && !test_sum_item->any_value()) ||
385 (test_sub_item && !test_sub_item->any_value()));
388 void Item_func_not_all::print(
String *str)
391 Item_func::print(str);
406 int64_t Item_func_nop_all::val_int()
409 int64_t value= args[0]->val_int();
415 if (empty_underlying_subquery())
418 null_value= args[0]->null_value;
419 return (null_value || value == 0) ? 0 : 1;
453 Field *field= field_item->field;
456 field->setWriteSet();
458 if (!(*item)->with_subselect && (*item)->const_item())
460 ulong orig_sql_mode= session->
variables.sql_mode;
461 enum_check_fields orig_count_cuted_fields= session->count_cuted_fields;
462 uint64_t orig_field_val= 0;
465 session->
variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
467 session->count_cuted_fields= CHECK_FIELD_IGNORE;
473 if (field_item->depended_from)
475 orig_field_val= field->val_int();
478 if (!(*item)->is_null() && !(*item)->save_in_field(field, 1))
481 test(field->flags & UNSIGNED_FLAG));
488 if (field_item->depended_from)
490 result= field->store(orig_field_val, field->isUnsigned());
494 session->
variables.sql_mode= orig_sql_mode;
495 session->count_cuted_fields= orig_count_cuted_fields;
501 void Item_bool_func2::fix_length_and_dec()
509 if (!args[0] || !args[1])
526 if (args[0]->result_type() == STRING_RESULT &&
527 args[1]->result_type() == STRING_RESULT &&
528 agg_arg_charsets(coll, args, 2, MY_COLL_CMP_CONV, 1))
531 args[0]->cmp_context= args[1]->cmp_context=
532 item_cmp_type(args[0]->result_type(), args[1]->result_type());
535 if (functype() == LIKE_FUNC)
541 Item_field *field_item= NULL;
543 if (args[0]->real_item()->type() == FIELD_ITEM)
545 field_item=
static_cast<Item_field*
>(args[0]->real_item());
546 if (field_item->field->can_be_compared_as_int64_t() &&
547 !(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
551 cmp.set_cmp_func(
this, tmp_arg, tmp_arg+1,
553 args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
558 if (args[1]->real_item()->type() == FIELD_ITEM)
560 field_item=
static_cast<Item_field*
>(args[1]->real_item());
561 if (field_item->field->can_be_compared_as_int64_t() &&
562 !(field_item->is_datetime() &&
563 args[0]->result_type() == STRING_RESULT))
567 cmp.set_cmp_func(
this, tmp_arg, tmp_arg+1,
569 args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
578 Arg_comparator::Arg_comparator():
579 session(current_session),
584 Arg_comparator::Arg_comparator(Item **a1, Item **a2):
587 session(current_session),
592 int Arg_comparator::set_compare_func(Item_bool_func2 *item, Item_result type)
595 func= comparator_matrix[type]
596 [test(owner->functype() == Item_func::EQUAL_FUNC)];
601 uint32_t n= (*a)->cols();
602 if (n != (*b)->cols())
604 my_error(ER_OPERAND_COLUMNS, MYF(0), n);
608 comparators=
new Arg_comparator[n];
609 for (uint32_t i=0; i < n; i++)
611 if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
613 my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
616 comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
627 if (cmp_collation.set((*a)->collation, (*b)->collation) ||
628 cmp_collation.derivation == DERIVATION_NONE)
630 my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
633 if (cmp_collation.collation == &my_charset_bin)
639 if (func == &Arg_comparator::compare_string)
640 func= &Arg_comparator::compare_binary_string;
641 else if (func == &Arg_comparator::compare_e_string)
642 func= &Arg_comparator::compare_e_binary_string;
652 (*a)->walk(&Item::set_no_const_sub,
false, (
unsigned char*) 0);
653 (*b)->walk(&Item::set_no_const_sub,
false, (
unsigned char*) 0);
659 if (func == &Arg_comparator::compare_int_signed)
661 if ((*a)->unsigned_flag)
662 func= (((*b)->unsigned_flag)?
663 &Arg_comparator::compare_int_unsigned :
664 &Arg_comparator::compare_int_unsigned_signed);
665 else if ((*b)->unsigned_flag)
666 func= &Arg_comparator::compare_int_signed_unsigned;
668 else if (func== &Arg_comparator::compare_e_int)
670 if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
671 func= &Arg_comparator::compare_e_int_diff_signedness;
679 if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
681 precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
682 if (func == &Arg_comparator::compare_real)
683 func= &Arg_comparator::compare_real_fixed;
684 else if (func == &Arg_comparator::compare_e_real)
685 func= &Arg_comparator::compare_e_real_fixed;
721 type::cut_t error= type::VALID;
723 type::timestamp_t ret;
725 ret= l_time.store(str->ptr(), str->length(),
726 (TIME_FUZZY_DATE | MODE_INVALID_DATES | (session->
variables.sql_mode & MODE_NO_ZERO_DATE)),
729 if (ret == type::DRIZZLE_TIMESTAMP_DATETIME || ret == type::DRIZZLE_TIMESTAMP_DATE)
736 l_time.convert(value);
744 if (error != type::VALID)
746 make_truncated_value_warning(*session, DRIZZLE_ERROR::WARN_LEVEL_WARN, *str, warn_type, warn_name);
785 enum Arg_comparator::enum_date_cmp_type
786 Arg_comparator::can_compare_as_dates(Item *in_a, Item *in_b,
787 int64_t *const_value)
789 enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
792 if (in_a->type() == Item::ROW_ITEM || in_b->type() == Item::ROW_ITEM)
793 return CMP_DATE_DFLT;
795 if (in_a->is_datetime())
797 if (in_b->is_datetime())
799 cmp_type= CMP_DATE_WITH_DATE;
801 else if (in_b->result_type() == STRING_RESULT)
803 cmp_type= CMP_DATE_WITH_STR;
807 else if (in_b->is_datetime() && in_a->result_type() == STRING_RESULT)
809 cmp_type= CMP_STR_WITH_DATE;
813 if (cmp_type != CMP_DATE_DFLT)
819 if (cmp_type != CMP_DATE_WITH_DATE && str_arg->const_item() &&
820 (str_arg->type() != Item::FUNC_ITEM ||
821 ((Item_func*)str_arg)->functype() != Item_func::GUSERVAR_FUNC))
846 str_val= str_arg->val_str(&tmp);
856 return CMP_DATE_DFLT;
858 if (temporal.from_string(str_val->c_ptr(), str_val->length()))
861 temporal.to_int64_t(&value);
867 if (timevalue.from_string(str_val->c_ptr(), str_val->length()))
870 timevalue.to_uint64_t(timeint);
871 value=
static_cast<int64_t
>(timeint);
876 my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), str_val->c_ptr());
877 return CMP_DATE_DFLT;
889 int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg,
890 Item **a1, Item **a2,
893 enum_date_cmp_type cmp_type;
894 int64_t const_value= -1;
898 if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
901 a_type= (*a)->field_type();
902 b_type= (*b)->field_type();
906 if (const_value != -1)
908 Item_cache_int *cache=
new Item_cache_int();
910 cache->set_used_tables(1);
911 if (!(*a)->is_datetime())
913 cache->store((*a), const_value);
915 a= (Item **)&a_cache;
919 cache->store((*b), const_value);
921 b= (Item **)&b_cache;
924 is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC);
925 func= &Arg_comparator::compare_datetime;
926 get_value_func= &get_datetime_value;
931 return set_compare_func(owner_arg, type);
935 void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
941 a_type= (*a)->field_type();
942 b_type= (*b)->field_type();
946 func= &Arg_comparator::compare_datetime;
947 get_value_func= &get_datetime_value;
981 get_datetime_value(Session *session, Item ***item_arg, Item **cache_arg,
982 Item *warn_item,
bool *is_null)
986 Item *item= **item_arg;
988 if (item->result_as_int64_t())
990 value= item->val_int();
991 *is_null= item->null_value;
992 enum_field_types f_type= item->field_type();
999 if (f_type == DRIZZLE_TYPE_DATE ||
1000 (f_type != DRIZZLE_TYPE_DATETIME && value < 100000000L))
1005 str= item->val_str(&buf);
1006 *is_null= item->null_value;
1010 return ~(uint64_t) 0;
1021 enum_field_types f_type= warn_item->field_type();
1022 type::timestamp_t t_type= f_type == DRIZZLE_TYPE_DATE ? type::DRIZZLE_TIMESTAMP_DATE : type::DRIZZLE_TIMESTAMP_DATETIME;
1035 if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
1036 ((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
1038 Item_cache_int *cache=
new Item_cache_int(DRIZZLE_TYPE_DATETIME);
1040 cache->set_used_tables(1);
1041 cache->store(item, value);
1043 *item_arg= cache_arg;
1070 int Arg_comparator::compare_datetime()
1072 bool is_null=
false;
1073 uint64_t a_value, b_value;
1076 a_value= (*get_value_func)(session, &a, &a_cache, *b, &is_null);
1077 if (!is_nulls_eq && is_null)
1080 owner->null_value= 1;
1085 b_value= (*get_value_func)(session, &b, &b_cache, *a, &is_null);
1089 owner->null_value= is_nulls_eq ? 0 : 1;
1090 return is_nulls_eq ? 1 : -1;
1094 owner->null_value= 0;
1098 return (a_value == b_value);
1099 return (a_value < b_value) ? -1 : ((a_value > b_value) ? 1 : 0);
1103 int Arg_comparator::compare_string()
1106 if ((res1= (*a)->val_str(&owner->tmp_value1)))
1108 if ((res2= (*b)->val_str(&owner->tmp_value2)))
1110 owner->null_value= 0;
1111 return sortcmp(res1,res2,cmp_collation.collation);
1114 owner->null_value= 1;
1130 int Arg_comparator::compare_binary_string()
1133 if ((res1= (*a)->val_str(&owner->tmp_value1)))
1135 if ((res2= (*b)->val_str(&owner->tmp_value2)))
1137 owner->null_value= 0;
1138 uint32_t res1_length= res1->length();
1139 uint32_t res2_length= res2->length();
1140 int cmp= memcmp(res1->ptr(), res2->ptr(), min(res1_length,res2_length));
1141 return cmp ? cmp : (int) (res1_length - res2_length);
1144 owner->null_value= 1;
1154 int Arg_comparator::compare_e_string()
1157 res1= (*a)->val_str(&owner->tmp_value1);
1158 res2= (*b)->val_str(&owner->tmp_value2);
1160 return test(res1 == res2);
1161 return test(sortcmp(res1, res2, cmp_collation.collation) == 0);
1165 int Arg_comparator::compare_e_binary_string()
1168 res1= (*a)->val_str(&owner->tmp_value1);
1169 res2= (*b)->val_str(&owner->tmp_value2);
1171 return test(res1 == res2);
1172 return test(stringcmp(res1, res2) == 0);
1176 int Arg_comparator::compare_real()
1183 volatile double val1, val2;
1184 val1= (*a)->val_real();
1185 if (!(*a)->null_value)
1187 val2= (*b)->val_real();
1188 if (!(*b)->null_value)
1190 owner->null_value= 0;
1191 if (val1 < val2)
return -1;
1192 if (val1 == val2)
return 0;
1196 owner->null_value= 1;
1200 int Arg_comparator::compare_decimal()
1202 type::Decimal value1;
1203 type::Decimal *val1= (*a)->val_decimal(&value1);
1204 if (!(*a)->null_value)
1206 type::Decimal value2;
1207 type::Decimal *val2= (*b)->val_decimal(&value2);
1208 if (!(*b)->null_value)
1210 owner->null_value= 0;
1214 owner->null_value= 1;
1218 int Arg_comparator::compare_e_real()
1220 double val1= (*a)->val_real();
1221 double val2= (*b)->val_real();
1222 if ((*a)->null_value || (*b)->null_value)
1223 return test((*a)->null_value && (*b)->null_value);
1224 return test(val1 == val2);
1227 int Arg_comparator::compare_e_decimal()
1229 type::Decimal value1, value2;
1230 type::Decimal *val1= (*a)->val_decimal(&value1);
1231 type::Decimal *val2= (*b)->val_decimal(&value2);
1232 if ((*a)->null_value || (*b)->null_value)
1233 return test((*a)->null_value && (*b)->null_value);
1238 int Arg_comparator::compare_real_fixed()
1245 volatile double val1, val2;
1246 val1= (*a)->val_real();
1247 if (!(*a)->null_value)
1249 val2= (*b)->val_real();
1250 if (!(*b)->null_value)
1252 owner->null_value= 0;
1253 if (val1 == val2 || fabs(val1 - val2) < precision)
1260 owner->null_value= 1;
1265 int Arg_comparator::compare_e_real_fixed()
1267 double val1= (*a)->val_real();
1268 double val2= (*b)->val_real();
1269 if ((*a)->null_value || (*b)->null_value)
1270 return test((*a)->null_value && (*b)->null_value);
1271 return test(val1 == val2 || fabs(val1 - val2) < precision);
1275 int Arg_comparator::compare_int_signed()
1277 int64_t val1= (*a)->val_int();
1278 if (!(*a)->null_value)
1280 int64_t val2= (*b)->val_int();
1281 if (!(*b)->null_value)
1283 owner->null_value= 0;
1284 if (val1 < val2)
return -1;
1285 if (val1 == val2)
return 0;
1289 owner->null_value= 1;
1298 int Arg_comparator::compare_int_unsigned()
1300 uint64_t val1= (*a)->val_int();
1301 if (!(*a)->null_value)
1303 uint64_t val2= (*b)->val_int();
1304 if (!(*b)->null_value)
1306 owner->null_value= 0;
1307 if (val1 < val2)
return -1;
1308 if (val1 == val2)
return 0;
1312 owner->null_value= 1;
1321 int Arg_comparator::compare_int_signed_unsigned()
1323 int64_t sval1= (*a)->val_int();
1324 if (!(*a)->null_value)
1326 uint64_t uval2= (uint64_t)(*b)->val_int();
1327 if (!(*b)->null_value)
1329 owner->null_value= 0;
1330 if (sval1 < 0 || (uint64_t)sval1 < uval2)
1332 if ((uint64_t)sval1 == uval2)
1337 owner->null_value= 1;
1346 int Arg_comparator::compare_int_unsigned_signed()
1348 uint64_t uval1= (uint64_t)(*a)->val_int();
1349 if (!(*a)->null_value)
1351 int64_t sval2= (*b)->val_int();
1352 if (!(*b)->null_value)
1354 owner->null_value= 0;
1357 if (uval1 < (uint64_t)sval2)
1359 if (uval1 == (uint64_t)sval2)
1364 owner->null_value= 1;
1369 int Arg_comparator::compare_e_int()
1371 int64_t val1= (*a)->val_int();
1372 int64_t val2= (*b)->val_int();
1373 if ((*a)->null_value || (*b)->null_value)
1374 return test((*a)->null_value && (*b)->null_value);
1375 return test(val1 == val2);
1381 int Arg_comparator::compare_e_int_diff_signedness()
1383 int64_t val1= (*a)->val_int();
1384 int64_t val2= (*b)->val_int();
1385 if ((*a)->null_value || (*b)->null_value)
1386 return test((*a)->null_value && (*b)->null_value);
1387 return (val1 >= 0) && test(val1 == val2);
1390 int Arg_comparator::compare_row()
1394 (*a)->bring_value();
1395 (*b)->bring_value();
1396 uint32_t n= (*a)->cols();
1397 for (uint32_t i= 0; i<n; i++)
1399 res= comparators[i].compare();
1400 if (owner->null_value)
1403 switch (owner->functype()) {
1404 case Item_func::NE_FUNC:
1406 case Item_func::LT_FUNC:
1407 case Item_func::LE_FUNC:
1408 case Item_func::GT_FUNC:
1409 case Item_func::GE_FUNC:
1412 if (owner->abort_on_null)
1416 owner->null_value= 0;
1428 owner->null_value= 1;
1435 int Arg_comparator::compare_e_row()
1437 (*a)->bring_value();
1438 (*b)->bring_value();
1439 uint32_t n= (*a)->cols();
1440 for (uint32_t i= 0; i<n; i++)
1442 if (!comparators[i].compare())
1449 void Item_func_truth::fix_length_and_dec()
1461 args[0]->print(str);
1462 str->append(STRING_WITH_LEN(
" is "));
1464 str->append(STRING_WITH_LEN(
"not "));
1466 str->append(STRING_WITH_LEN(
"true"));
1468 str->append(STRING_WITH_LEN(
"false"));
1473 bool Item_func_truth::val_bool()
1475 bool val= args[0]->val_bool();
1476 if (args[0]->null_value)
1482 return (! affirmative);
1488 return (val == value);
1492 return (val != value);
1496 int64_t Item_func_truth::val_int()
1498 return (val_bool() ? 1 : 0);
1502 bool Item_in_optimizer::fix_left(
Session *session,
Item **)
1504 if ((!args[0]->fixed && args[0]->fix_fields(session, args)) ||
1505 (!cache && !(cache= Item_cache::get_cache(args[0]))))
1508 cache->setup(args[0]);
1509 if (cache->cols() == 1)
1511 if ((used_tables_cache= args[0]->used_tables()))
1512 cache->set_used_tables(OUTER_REF_TABLE_BIT);
1514 cache->set_used_tables(0);
1518 uint32_t n= cache->cols();
1519 for (uint32_t i= 0; i < n; i++)
1521 if (args[0]->element_index(i)->used_tables())
1522 ((Item_cache *)cache->element_index(i))->set_used_tables(OUTER_REF_TABLE_BIT);
1524 ((Item_cache *)cache->element_index(i))->set_used_tables(0);
1526 used_tables_cache= args[0]->used_tables();
1528 not_null_tables_cache= args[0]->not_null_tables();
1529 with_sum_func= args[0]->with_sum_func;
1530 if ((const_item_cache= args[0]->const_item()))
1531 cache->store(args[0]);
1536 bool Item_in_optimizer::fix_fields(Session *session, Item **ref)
1539 if (fix_left(session, ref))
1541 if (args[0]->maybe_null)
1544 if (!args[1]->fixed && args[1]->fix_fields(session, args+1))
1546 Item_in_subselect * sub= (Item_in_subselect *)args[1];
1547 if (args[0]->cols() != sub->engine->cols())
1549 my_error(ER_OPERAND_COLUMNS, MYF(0), args[0]->cols());
1552 if (args[1]->maybe_null)
1554 with_sum_func= with_sum_func || args[1]->with_sum_func;
1555 used_tables_cache|= args[1]->used_tables();
1556 not_null_tables_cache|= args[1]->not_null_tables();
1557 const_item_cache&= args[1]->const_item();
1563 int64_t Item_in_optimizer::val_int()
1567 cache->store(args[0]);
1569 if (cache->null_value)
1582 result_for_null_param != UNKNOWN)
1585 null_value= result_for_null_param;
1597 if (cache->cols() == 1)
1599 item_subs->set_cond_guard_var(0,
false);
1600 (void) args[1]->val_bool_result();
1601 result_for_null_param= null_value= !item_subs->engine->no_rows();
1602 item_subs->set_cond_guard_var(0,
true);
1607 uint32_t ncols= cache->cols();
1612 for (i= 0; i < ncols; i++)
1614 if (cache->element_index(i)->null_value)
1615 item_subs->set_cond_guard_var(i,
false);
1618 (void) args[1]->val_bool_result();
1619 result_for_null_param= null_value= !item_subs->engine->no_rows();
1622 for (i= 0; i < ncols; i++)
1623 item_subs->set_cond_guard_var(i,
true);
1629 tmp= args[1]->val_bool_result();
1630 null_value= args[1]->null_value;
1635 void Item_in_optimizer::keep_top_level_cache()
1637 cache->keep_array();
1642 void Item_in_optimizer::cleanup()
1644 item::function::Boolean::cleanup();
1651 bool Item_in_optimizer::is_null()
1653 cache->store(args[0]);
1654 return (null_value= (cache->null_value || args[1]->is_null()));
1680 Item *Item_in_optimizer::transform(Item_transformer transformer,
unsigned char *argument)
1684 assert(arg_count == 2);
1687 new_item= (*args)->
transform(transformer, argument);
1699 assert((args[1])->type() == Item::SUBSELECT_ITEM &&
1701 Item_subselect::IN_SUBS ||
1703 Item_subselect::ALL_SUBS ||
1705 Item_subselect::ANY_SUBS));
1708 in_arg->left_expr= args[0];
1710 return (this->*transformer)(argument);
1715 int64_t Item_func_eq::val_int()
1718 int value= cmp.compare();
1719 return value == 0 ? 1 : 0;
1725 void Item_func_equal::fix_length_and_dec()
1727 Item_bool_func2::fix_length_and_dec();
1728 maybe_null=null_value=0;
1731 int64_t Item_func_equal::val_int()
1734 return cmp.compare();
1737 int64_t Item_func_ne::val_int()
1740 int value= cmp.compare();
1741 return value != 0 && !null_value ? 1 : 0;
1745 int64_t Item_func_ge::val_int()
1748 int value= cmp.compare();
1749 return value >= 0 ? 1 : 0;
1753 int64_t Item_func_gt::val_int()
1756 int value= cmp.compare();
1757 return value > 0 ? 1 : 0;
1760 int64_t Item_func_le::val_int()
1763 int value= cmp.compare();
1764 return value <= 0 && !null_value ? 1 : 0;
1768 int64_t Item_func_lt::val_int()
1771 int value= cmp.compare();
1772 return value < 0 && !null_value ? 1 : 0;
1776 int64_t Item_func_strcmp::val_int()
1779 String *a=args[0]->val_str(&tmp_value1);
1780 String *b=args[1]->val_str(&tmp_value2);
1786 int value= sortcmp(a,b,cmp.cmp_collation.collation);
1788 return !value ? 0 : (value < 0 ? (int64_t) -1 : (int64_t) 1);
1792 bool Item_func_opt_neg::eq(
const Item *item,
bool binary_cmp)
const
1797 if (item->type() != FUNC_ITEM)
1800 if (arg_count != item_func->arg_count ||
1801 functype() != item_func->functype())
1805 for (uint32_t i=0; i < arg_count ; i++)
1806 if (!args[i]->eq(item_func->arguments()[i], binary_cmp))
1812 void Item_func_interval::fix_length_and_dec()
1814 uint32_t rows= row->cols();
1816 use_decimal_comparison= ((row->element_index(0)->result_type() ==
1818 (row->element_index(0)->result_type() ==
1822 bool not_null_consts=
true;
1824 for (uint32_t i= 1; not_null_consts && i < rows; i++)
1826 Item *el= row->element_index(i);
1830 if (not_null_consts &&
1832 (interval_range*) memory::sql_alloc(
sizeof(interval_range) * (rows - 1))))
1834 if (use_decimal_comparison)
1836 for (uint32_t i= 1; i < rows; i++)
1838 Item *el= row->element_index(i);
1839 interval_range *range= intervals + (i-1);
1840 if ((el->result_type() == DECIMAL_RESULT) ||
1841 (el->result_type() == INT_RESULT))
1843 range->type= DECIMAL_RESULT;
1845 type::Decimal *dec= el->val_decimal(&range->dec);
1846 if (dec != &range->dec)
1849 range->dec.fix_buffer_pointer();
1854 range->type= REAL_RESULT;
1855 range->dbl= el->val_real();
1861 for (uint32_t i= 1; i < rows; i++)
1863 intervals[i-1].dbl= row->element_index(i)->val_real();
1870 used_tables_cache|= row->used_tables();
1871 not_null_tables_cache= row->not_null_tables();
1872 with_sum_func= with_sum_func || row->with_sum_func;
1873 const_item_cache&= row->const_item();
1891 int64_t Item_func_interval::val_int()
1898 if (use_decimal_comparison)
1900 dec= row->element_index(0)->val_decimal(&dec_buf);
1901 if (row->element_index(0)->null_value)
1903 class_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
1907 value= row->element_index(0)->val_real();
1908 if (row->element_index(0)->null_value)
1917 while (start != end)
1919 uint32_t mid= (start + end + 1) / 2;
1927 if (dec && range->type == DECIMAL_RESULT)
1930 cmp_result= (range->dbl <= value);
1937 return ((dec && range->type == DECIMAL_RESULT) ?
1939 value < range->dbl) ? 0 : start + 1;
1942 for (i=1 ; i < row->cols() ; i++)
1944 Item *el= row->element_index(i);
1945 if (use_decimal_comparison &&
1946 ((el->result_type() == DECIMAL_RESULT) ||
1947 (el->result_type() == INT_RESULT)))
2000 if (Item_func_opt_neg::fix_fields(session, ref))
2003 session->lex().current_select->between_count++;
2006 if (pred_level && !negated)
2010 not_null_tables_cache= (args[0]->not_null_tables() |
2011 (args[1]->not_null_tables() &
2012 args[2]->not_null_tables()));
2018 void Item_func_between::fix_length_and_dec()
2022 bool datetime_found=
false;
2023 compare_as_dates=
true;
2029 if (!args[0] || !args[1] || !args[2])
2033 if (cmp_type == STRING_RESULT &&
2034 agg_arg_charsets(cmp_collation, args, 3, MY_COLL_CMP_CONV, 1))
2042 if (cmp_type == STRING_RESULT)
2044 for (i= 0; i < 3; i++)
2046 if (args[i]->is_datetime())
2048 datetime_found=
true;
2053 if (!datetime_found)
2054 compare_as_dates=
false;
2056 if (compare_as_dates)
2058 ge_cmp.set_datetime_cmp_func(args, args + 1);
2059 le_cmp.set_datetime_cmp_func(args, args + 2);
2061 else if (args[0]->real_item()->type() == FIELD_ITEM)
2063 Item_field *field_item= (Item_field*) (args[0]->real_item());
2064 if (field_item->field->can_be_compared_as_int64_t())
2071 cmp_type=INT_RESULT;
2073 cmp_type=INT_RESULT;
2079 int64_t Item_func_between::val_int()
2082 if (compare_as_dates)
2086 ge_res= ge_cmp.compare();
2087 if ((null_value= args[0]->null_value))
2089 le_res= le_cmp.compare();
2091 if (!args[1]->null_value && !args[2]->null_value)
2092 return (int64_t) ((ge_res >= 0 && le_res <=0) != negated);
2093 else if (args[1]->null_value)
2095 null_value= le_res > 0;
2099 null_value= ge_res < 0;
2102 else if (cmp_type == STRING_RESULT)
2105 value=args[0]->val_str(&value0);
2106 if ((null_value=args[0]->null_value))
2108 a=args[1]->val_str(&value1);
2109 b=args[2]->val_str(&value2);
2110 if (!args[1]->null_value && !args[2]->null_value)
2111 return (int64_t) ((sortcmp(value,a,cmp_collation.collation) >= 0 &&
2112 sortcmp(value,b,cmp_collation.collation) <= 0) !=
2114 if (args[1]->null_value && args[2]->null_value)
2116 else if (args[1]->null_value)
2119 null_value= sortcmp(value,b,cmp_collation.collation) <= 0;
2124 null_value= sortcmp(value,a,cmp_collation.collation) >= 0;
2127 else if (cmp_type == INT_RESULT)
2129 int64_t value=args[0]->val_int(), a, b;
2130 if ((null_value=args[0]->null_value))
2132 a=args[1]->val_int();
2133 b=args[2]->val_int();
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;
2144 null_value= value >= a;
2147 else if (cmp_type == DECIMAL_RESULT)
2149 type::Decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2150 a_buf, *a_dec, b_buf, *b_dec;
2151 if ((null_value=args[0]->null_value))
2153 a_dec= args[1]->val_decimal(&a_buf);
2154 b_dec= args[2]->val_decimal(&b_buf);
2155 if (!args[1]->null_value && !args[2]->null_value)
2158 if (args[1]->null_value && args[2]->null_value)
2160 else if (args[1]->null_value)
2167 double value= args[0]->val_real(),a,b;
2168 if ((null_value=args[0]->null_value))
2170 a= args[1]->val_real();
2171 b= args[2]->val_real();
2172 if (!args[1]->null_value && !args[2]->null_value)
2173 return (int64_t) ((value >= a && value <= b) != negated);
2174 if (args[1]->null_value && args[2]->null_value)
2176 else if (args[1]->null_value)
2178 null_value= value <= b;
2182 null_value= value >= a;
2185 return (int64_t) (!null_value && negated);
2192 args[0]->print(str);
2194 str->append(STRING_WITH_LEN(
" not"));
2195 str->append(STRING_WITH_LEN(
" between "));
2196 args[1]->print(str);
2197 str->append(STRING_WITH_LEN(
" and "));
2198 args[2]->print(str);
2203 Item_func_ifnull::fix_length_and_dec()
2205 agg_result_type(&hybrid_type, args, 2);
2206 maybe_null= args[1]->maybe_null;
2207 decimals= max(args[0]->decimals, args[1]->decimals);
2208 unsigned_flag= args[0]->unsigned_flag && args[1]->unsigned_flag;
2210 if (hybrid_type == DECIMAL_RESULT || hybrid_type == INT_RESULT)
2212 int len0= args[0]->max_length - args[0]->decimals
2213 - (args[0]->unsigned_flag ? 0 : 1);
2215 int len1= args[1]->max_length - args[1]->decimals
2216 - (args[1]->unsigned_flag ? 0 : 1);
2218 max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2222 max_length= max(args[0]->max_length, args[1]->max_length);
2225 switch (hybrid_type)
2228 agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2231 case DECIMAL_RESULT:
2247 uint32_t Item_func_ifnull::decimal_precision()
const
2249 int max_int_part= max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2250 return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2254 enum_field_types Item_func_ifnull::field_type()
const
2256 return cached_field_type;
2259 Field *Item_func_ifnull::tmp_table_field(Table *table)
2261 return tmp_table_field_from_field_type(table, 0);
2265 Item_func_ifnull::real_op()
2268 double value= args[0]->val_real();
2269 if (!args[0]->null_value)
2274 value= args[1]->val_real();
2275 if ((null_value=args[1]->null_value))
2281 Item_func_ifnull::int_op()
2284 int64_t value=args[0]->val_int();
2285 if (!args[0]->null_value)
2290 value=args[1]->val_int();
2291 if ((null_value=args[1]->null_value))
2301 if (!args[0]->null_value)
2306 value= args[1]->val_decimal(decimal_value);
2307 if ((null_value= args[1]->null_value))
2317 String *res =args[0]->val_str(str);
2318 if (!args[0]->null_value)
2321 res->set_charset(collation.collation);
2324 res=args[1]->val_str(str);
2325 if ((null_value=args[1]->null_value))
2327 res->set_charset(collation.collation);
2362 args[0]->top_level_item();
2364 if (Item_func::fix_fields(session, ref))
2367 not_null_tables_cache= (args[1]->not_null_tables() &
2368 args[2]->not_null_tables());
2374 void Item_func_if::fix_length_and_dec()
2376 maybe_null= args[1]->maybe_null || args[2]->maybe_null;
2377 decimals= max(args[1]->decimals, args[2]->decimals);
2378 unsigned_flag= args[1]->unsigned_flag && args[2]->unsigned_flag;
2380 enum Item_result arg1_type= args[1]->result_type();
2381 enum Item_result arg2_type= args[2]->result_type();
2382 bool null1= args[1]->const_item() && args[1]->null_value;
2383 bool null2= args[2]->const_item() && args[2]->null_value;
2387 cached_result_type= arg2_type;
2388 collation.set(args[2]->collation.collation);
2389 cached_field_type= args[2]->field_type();
2393 cached_result_type= arg1_type;
2394 collation.set(args[1]->collation.collation);
2395 cached_field_type= args[1]->field_type();
2399 agg_result_type(&cached_result_type, args +1, 2);
2400 if (cached_result_type == STRING_RESULT)
2402 if (agg_arg_charsets(collation, args +1, 2, MY_COLL_ALLOW_CONV, 1))
2407 collation.set(&my_charset_bin);
2412 switch (cached_result_type)
2414 case DECIMAL_RESULT:
2417 int len1= args[1]->max_length -args[1]->decimals -(args[1]->unsigned_flag ? 0 : 1);
2418 int len2= args[2]->max_length -args[2]->decimals -(args[2]->unsigned_flag ? 0 : 1);
2419 max_length= max(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
2424 max_length= max(args[1]->max_length, args[2]->max_length);
2434 uint32_t Item_func_if::decimal_precision()
const
2436 int precision= (max(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
2438 return min(precision, DECIMAL_MAX_PRECISION);
2443 Item_func_if::val_real()
2453 Item_func_if::val_int()
2457 int64_t value= arg->
val_int();
2470 res->set_charset(collation.collation);
2489 Item_func_nullif::fix_length_and_dec()
2491 Item_bool_func2::fix_length_and_dec();
2495 max_length=args[0]->max_length;
2496 decimals=args[0]->decimals;
2497 unsigned_flag= args[0]->unsigned_flag;
2498 cached_result_type= args[0]->result_type();
2499 if (cached_result_type == STRING_RESULT &&
2500 agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1))
2517 Item_func_nullif::val_real()
2526 value= args[0]->val_real();
2527 null_value=args[0]->null_value;
2532 Item_func_nullif::val_int()
2541 value=args[0]->val_int();
2542 null_value=args[0]->null_value;
2556 res=args[0]->val_str(str);
2557 null_value=args[0]->null_value;
2572 res= args[0]->val_decimal(decimal_value);
2573 null_value= args[0]->null_value;
2579 Item_func_nullif::is_null()
2581 return (null_value= (!cmp.compare() ? 1 : args[0]->null_value));
2608 uint32_t value_added_map= 0;
2610 if (first_expr_num == -1)
2612 for (uint32_t i=0 ; i < ncases ; i+=2)
2615 if (args[i]->val_bool())
2623 for (uint32_t i=0 ; i < ncases ; i+=2)
2625 cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
2626 assert(cmp_type != ROW_RESULT);
2627 assert(cmp_items[(uint32_t)cmp_type]);
2628 if (!(value_added_map & (1<<(uint32_t)cmp_type)))
2630 cmp_items[(uint32_t)cmp_type]->store_value(args[first_expr_num]);
2631 if ((null_value=args[first_expr_num]->null_value))
2632 return else_expr_num != -1 ? args[else_expr_num] : 0;
2633 value_added_map|= 1<<(uint32_t)cmp_type;
2635 if (!cmp_items[(uint32_t)cmp_type]->cmp(args[i]) && !args[i]->null_value)
2640 return else_expr_num != -1 ? args[else_expr_num] : 0;
2648 Item *item=find_item(str);
2656 if (!(res=item->
val_str(str)))
2662 int64_t Item_func_case::val_int()
2665 char buff[MAX_FIELD_WIDTH];
2666 String dummy_str(buff,
sizeof(buff),default_charset());
2667 Item *item=find_item(&dummy_str);
2680 double Item_func_case::val_real()
2683 char buff[MAX_FIELD_WIDTH];
2684 String dummy_str(buff,
sizeof(buff),default_charset());
2685 Item *item=find_item(&dummy_str);
2702 char buff[MAX_FIELD_WIDTH];
2703 String dummy_str(buff,
sizeof(buff), default_charset());
2704 Item *item= find_item(&dummy_str);
2719 bool Item_func_case::fix_fields(
Session *session,
Item **ref)
2725 unsigned char buff[MAX_FIELD_WIDTH*2+
sizeof(
String)*2+
sizeof(
String*)*2
2726 +
sizeof(double)*2+
sizeof(int64_t)*2];
2727 bool res= Item_func::fix_fields(session, ref);
2738 void Item_func_case::agg_str_lengths(Item* arg)
2740 set_if_bigger(max_length, arg->max_length);
2741 set_if_bigger(decimals, arg->decimals);
2742 unsigned_flag= unsigned_flag && arg->unsigned_flag;
2746 void Item_func_case::agg_num_lengths(Item *arg)
2748 uint32_t len= class_decimal_length_to_precision(arg->max_length, arg->decimals,
2749 arg->unsigned_flag) - arg->decimals;
2750 set_if_bigger(max_length, len);
2751 set_if_bigger(decimals, arg->decimals);
2752 unsigned_flag= unsigned_flag && arg->unsigned_flag;
2756 void Item_func_case::fix_length_and_dec()
2760 uint32_t found_types= 0;
2761 if (!(agg= (Item**) memory::sql_alloc(
sizeof(Item*)*(ncases+1))))
2769 for (nagg= 0 ; nagg < ncases/2 ; nagg++)
2770 agg[nagg]= args[nagg*2+1];
2772 if (else_expr_num != -1)
2773 agg[nagg++]= args[else_expr_num];
2775 agg_result_type(&cached_result_type, agg, nagg);
2776 if ((cached_result_type == STRING_RESULT) &&
2777 agg_arg_charsets(collation, agg, nagg, MY_COLL_ALLOW_CONV, 1))
2785 if (first_expr_num != -1)
2787 agg[0]= args[first_expr_num];
2788 left_result_type= agg[0]->result_type();
2790 for (nagg= 0; nagg < ncases/2 ; nagg++)
2791 agg[nagg+1]= args[nagg*2];
2793 if (!(found_types= collect_cmp_types(agg, nagg)))
2796 for (
int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
2798 if (found_types & (1 << i) && !cmp_items[i])
2800 assert((Item_result)i != ROW_RESULT);
2801 if ((Item_result)i == STRING_RESULT &&
2802 agg_arg_charsets(cmp_collation, agg, nagg, MY_COLL_CMP_CONV, 1))
2805 cmp_item::get_comparator((Item_result)i,
2806 cmp_collation.collation)))
2812 if (else_expr_num == -1 || args[else_expr_num]->maybe_null)
2817 unsigned_flag=
true;
2818 if (cached_result_type == STRING_RESULT)
2820 for (uint32_t i= 0; i < ncases; i+= 2)
2821 agg_str_lengths(args[i + 1]);
2822 if (else_expr_num != -1)
2823 agg_str_lengths(args[else_expr_num]);
2827 for (uint32_t i= 0; i < ncases; i+= 2)
2828 agg_num_lengths(args[i + 1]);
2829 if (else_expr_num != -1)
2830 agg_num_lengths(args[else_expr_num]);
2831 max_length= class_decimal_precision_to_length(max_length + decimals, decimals,
2837 uint32_t Item_func_case::decimal_precision()
const
2840 for (uint32_t i=0 ; i < ncases ; i+=2)
2841 set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
2843 if (else_expr_num != -1)
2844 set_if_bigger(max_int_part, args[else_expr_num]->decimal_int_part());
2845 return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2856 str->append(STRING_WITH_LEN(
"(case "));
2857 if (first_expr_num != -1)
2859 args[first_expr_num]->print(str);
2862 for (uint32_t i=0 ; i < ncases ; i+=2)
2864 str->append(STRING_WITH_LEN(
"when "));
2865 args[i]->print(str);
2866 str->append(STRING_WITH_LEN(
" then "));
2867 args[i+1]->print(str);
2870 if (else_expr_num != -1)
2872 str->append(STRING_WITH_LEN(
"else "));
2873 args[else_expr_num]->print(str);
2876 str->append(STRING_WITH_LEN(
"end)"));
2880 void Item_func_case::cleanup()
2882 Item_func::cleanup();
2883 for (
int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
2885 delete cmp_items[i];
2899 for (uint32_t i=0 ; i < arg_count ; i++)
2902 if ((res=args[i]->val_str(str)))
2909 int64_t Item_func_coalesce::int_op()
2913 for (uint32_t i=0 ; i < arg_count ; i++)
2915 int64_t res=args[i]->val_int();
2916 if (!args[i]->null_value)
2923 double Item_func_coalesce::real_op()
2927 for (uint32_t i=0 ; i < arg_count ; i++)
2929 double res= args[i]->val_real();
2930 if (!args[i]->null_value)
2942 for (uint32_t i= 0; i < arg_count; i++)
2945 if (!args[i]->null_value)
2953 void Item_func_coalesce::fix_length_and_dec()
2956 agg_result_type(&hybrid_type, args, arg_count);
2958 switch (hybrid_type) {
2960 count_only_length();
2961 decimals= NOT_FIXED_DEC;
2962 agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1);
2965 case DECIMAL_RESULT:
2966 count_decimal_length();
2970 count_real_length();
2974 count_only_length();
3005 static inline int cmp_longs (int64_t a_val, int64_t b_val)
3007 return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
3029 static inline int cmp_ulongs (uint64_t a_val, uint64_t b_val)
3031 return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
3058 int cmp_int64_t(
void *, in_int64_t::packed_int64_t *a,
3059 in_int64_t::packed_int64_t *b)
3061 if (a->unsigned_flag != b->unsigned_flag)
3067 if ((a->unsigned_flag && ((uint64_t) a->val) > (uint64_t) INT64_MAX) ||
3068 (b->unsigned_flag && ((uint64_t) b->val) > (uint64_t) INT64_MAX))
3069 return a->unsigned_flag ? 1 : -1;
3074 return cmp_longs (a->val, b->val);
3076 if (a->unsigned_flag)
3077 return cmp_ulongs ((uint64_t) a->val, (uint64_t) b->val);
3079 return cmp_longs (a->val, b->val);
3082 static int cmp_double(
void *,
double *a,
double *b)
3084 return *a < *b ? -1 : *a == *b ? 0 : 1;
3087 static int cmp_row(
void *, cmp_item_row *a, cmp_item_row *b)
3089 return a->compare(b);
3093 static int cmp_decimal(
void *, type::Decimal *a, type::Decimal *b)
3099 a->fix_buffer_pointer();
3100 b->fix_buffer_pointer();
3105 void in_vector::sort()
3107 internal::my_qsort2(base,used_count,size,compare, (
void *) collation);
3111 int in_vector::find(Item *item)
3113 unsigned char *result=get_value(item);
3114 if (!result || !used_count)
3118 start=0; end=used_count-1;
3119 while (start != end)
3121 uint32_t mid=(start+end+1)/2;
3123 if ((res=(*compare)(collation, base+mid*size, result)) == 0)
3130 return (
int) ((*compare)(collation, base+start*size, result) == 0);
3133 in_string::in_string(uint32_t elements,qsort2_cmp cmp_func,
const charset_info_st *
const cs)
3134 :in_vector(elements, sizeof(String), cmp_func, cs),
3135 tmp(buff, sizeof(buff), &my_charset_bin)
3138 in_string::~in_string()
3143 for (uint32_t i=0 ; i < count ; i++)
3144 ((String*) base)[i].free();
3148 void in_string::set(uint32_t pos,Item *item)
3150 String *str=((String*) base)+pos;
3151 String *res=item->val_str(str);
3152 if (res && res != str)
3154 if (res->uses_buffer_owned_by(str))
3156 if (item->type() == Item::FUNC_ITEM)
3161 if (!str->charset())
3163 const charset_info_st *cs;
3164 if (!(cs= item->collation.collation))
3165 cs= &my_charset_bin;
3166 str->set_charset(cs);
3171 unsigned char *in_string::get_value(Item *item)
3173 return (
unsigned char*) item->val_str(&tmp);
3176 in_row::in_row(uint32_t elements, Item *)
3178 base= (
char*)
new cmp_item_row[count= elements];
3179 size=
sizeof(cmp_item_row);
3180 compare= (qsort2_cmp) cmp_row;
3185 used_count= elements;
3191 delete[] (cmp_item_row*) base;
3194 unsigned char *in_row::get_value(Item *item)
3196 tmp.store_value(item);
3197 if (item->is_null())
3199 return (
unsigned char *)&tmp;
3202 void in_row::set(uint32_t pos, Item *item)
3204 ((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3208 in_int64_t::in_int64_t(uint32_t elements) :
3209 in_vector(elements, sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3212 void in_int64_t::set(uint32_t pos,Item *item)
3214 struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3216 buff->val= item->val_int();
3217 buff->unsigned_flag= item->unsigned_flag;
3220 unsigned char *in_int64_t::get_value(Item *item)
3222 tmp.val= item->val_int();
3223 if (item->null_value)
3225 tmp.unsigned_flag= item->unsigned_flag;
3226 return (
unsigned char*) &tmp;
3229 in_datetime::in_datetime(Item *warn_item_arg, uint32_t elements) :
3230 in_int64_t(elements),
3231 session(current_session),
3232 warn_item(warn_item_arg),
3236 void in_datetime::set(uint32_t pos, Item *item)
3238 Item **tmp_item= &item;
3240 struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3242 buff->val= get_datetime_value(session, &tmp_item, 0, warn_item, &is_null);
3243 buff->unsigned_flag= 1L;
3246 unsigned char *in_datetime::get_value(Item *item)
3249 Item **tmp_item= lval_cache ? &lval_cache : &item;
3250 tmp.val= get_datetime_value(session, &tmp_item, &lval_cache, warn_item, &is_null);
3251 if (item->null_value)
3253 tmp.unsigned_flag= 1L;
3254 return (
unsigned char*) &tmp;
3257 in_double::in_double(uint32_t elements)
3258 :in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
3261 void in_double::set(uint32_t pos,Item *item)
3263 ((
double*) base)[pos]= item->val_real();
3266 unsigned char *in_double::get_value(Item *item)
3268 tmp= item->val_real();
3269 if (item->null_value)
3271 return (
unsigned char*) &tmp;
3275 in_decimal::in_decimal(uint32_t elements)
3276 :in_vector(elements, sizeof(type::Decimal),(qsort2_cmp) cmp_decimal, 0)
3280 void in_decimal::set(uint32_t pos, Item *item)
3283 type::Decimal *dec= ((type::Decimal *)base) + pos;
3284 dec->len= DECIMAL_BUFF_LENGTH;
3285 dec->fix_buffer_pointer();
3286 type::Decimal *res= item->val_decimal(dec);
3288 if (!item->null_value && res != dec)
3289 class_decimal2decimal(res, dec);
3293 unsigned char *in_decimal::get_value(Item *item)
3295 type::Decimal *result= item->val_decimal(&val);
3296 if (item->null_value)
3298 return (
unsigned char *)result;
3302 cmp_item* cmp_item::get_comparator(Item_result type,
3303 const charset_info_st *
const cs)
3307 return new cmp_item_sort_string(cs);
3310 return new cmp_item_int;
3313 return new cmp_item_real;
3316 return new cmp_item_row;
3318 case DECIMAL_RESULT:
3319 return new cmp_item_decimal;
3326 cmp_item* cmp_item_sort_string::make_same()
3328 return new cmp_item_sort_string_in_static(cmp_charset);
3331 cmp_item* cmp_item_int::make_same()
3333 return new cmp_item_int();
3336 cmp_item* cmp_item_real::make_same()
3338 return new cmp_item_real();
3341 cmp_item* cmp_item_row::make_same()
3343 return new cmp_item_row();
3347 cmp_item_row::~cmp_item_row()
3351 for (uint32_t i= 0; i < n; i++)
3354 delete comparators[i];
3361 void cmp_item_row::alloc_comparators()
3364 comparators= (cmp_item **) current_session->mem.calloc(
sizeof(cmp_item *)*n);
3368 void cmp_item_row::store_value(Item *item)
3371 alloc_comparators();
3374 item->bring_value();
3375 item->null_value= 0;
3376 for (uint32_t i=0; i < n; i++)
3378 if (!comparators[i])
3379 if (!(comparators[i]=
3380 cmp_item::get_comparator(item->element_index(i)->result_type(),
3381 item->element_index(i)->collation.collation)))
3383 comparators[i]->store_value(item->element_index(i));
3384 item->null_value|= item->element_index(i)->null_value;
3391 void cmp_item_row::store_value_by_template(cmp_item *t, Item *item)
3393 cmp_item_row *tmpl= (cmp_item_row*) t;
3394 if (tmpl->n != item->cols())
3396 my_error(ER_OPERAND_COLUMNS, MYF(0), tmpl->n);
3400 if ((comparators= (cmp_item **) memory::sql_alloc(
sizeof(cmp_item *)*n)))
3402 item->bring_value();
3403 item->null_value= 0;
3404 for (uint32_t i=0; i < n; i++)
3406 if (!(comparators[i]= tmpl->comparators[i]->make_same()))
3408 comparators[i]->store_value_by_template(tmpl->comparators[i],
3409 item->element_index(i));
3410 item->null_value|= item->element_index(i)->null_value;
3416 int cmp_item_row::cmp(Item *arg)
3419 if (arg->cols() != n)
3421 my_error(ER_OPERAND_COLUMNS, MYF(0), n);
3426 for (uint32_t i=0; i < n; i++)
3428 if (comparators[i]->cmp(arg->element_index(i)))
3430 if (!arg->element_index(i)->null_value)
3435 return (arg->null_value= was_null);
3439 int cmp_item_row::compare(cmp_item *c)
3441 cmp_item_row *l_cmp= (cmp_item_row *) c;
3442 for (uint32_t i=0; i < n; i++)
3445 if ((res= comparators[i]->compare(l_cmp->comparators[i])))
3452 void cmp_item_decimal::store_value(Item *item)
3454 type::Decimal *val= item->val_decimal(&value);
3456 if (val && val != &value)
3457 class_decimal2decimal(val, &value);
3461 int cmp_item_decimal::cmp(Item *arg)
3463 type::Decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3464 if (arg->null_value)
3470 int cmp_item_decimal::compare(cmp_item *arg)
3472 cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3477 cmp_item* cmp_item_decimal::make_same()
3479 return new cmp_item_decimal();
3483 void cmp_item_datetime::store_value(Item *item)
3486 Item **tmp_item= lval_cache ? &lval_cache : &item;
3487 value= get_datetime_value(session, &tmp_item, &lval_cache, warn_item, &is_null);
3491 int cmp_item_datetime::cmp(Item *arg)
3494 Item **tmp_item= &arg;
3496 get_datetime_value(session, &tmp_item, 0, warn_item, &is_null);
3500 int cmp_item_datetime::compare(cmp_item *ci)
3502 cmp_item_datetime *l_cmp= (cmp_item_datetime *)ci;
3503 return (value < l_cmp->value) ? -1 : ((value == l_cmp->value) ? 0 : 1);
3507 cmp_item *cmp_item_datetime::make_same()
3509 return new cmp_item_datetime(warn_item);
3513 bool Item_func_in::nulls_in_row()
3515 Item **arg,**arg_end;
3516 for (arg= args+1, arg_end= args+arg_count; arg != arg_end ; arg++)
3518 if ((*arg)->null_inside())
3556 Item **arg, **arg_end;
3558 if (Item_func_opt_neg::fix_fields(session, ref))
3562 if (pred_level && negated)
3566 not_null_tables_cache= ~(table_map) 0;
3567 for (arg= args + 1, arg_end= args + arg_count; arg != arg_end; arg++)
3568 not_null_tables_cache&= (*arg)->not_null_tables();
3569 not_null_tables_cache|= (*args)->not_null_tables();
3576 return cs->coll->strnncollsp(cs,
3577 (
unsigned char *) x->ptr(),x->length(),
3578 (
unsigned char *) y->ptr(),y->length(), 0);
3582 void Item_func_in::fix_length_and_dec()
3584 Item **arg, **arg_end;
3586 bool datetime_found=
false;
3588 bool compare_as_datetime=
false;
3590 uint32_t found_types= 0;
3591 uint32_t type_cnt= 0;
3592 Item_result cmp_type= STRING_RESULT;
3593 left_result_type= args[0]->result_type();
3594 if (!(found_types= collect_cmp_types(args, arg_count,
true)))
3597 for (arg= args + 1, arg_end= args + arg_count; arg != arg_end ; arg++)
3605 for (
int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
3607 if (found_types & 1 << i)
3610 cmp_type= (Item_result) i;
3616 if (cmp_type == STRING_RESULT &&
3617 agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1))
3619 arg_types_compatible=
true;
3627 if (cmp_type == ROW_RESULT)
3629 cmp_item_row *cmp= 0;
3630 if (const_itm && !nulls_in_row())
3632 array=
new in_row(arg_count-1, 0);
3633 cmp= &((in_row*)array)->tmp;
3637 cmp=
new cmp_item_row;
3638 cmp_items[ROW_RESULT]= cmp;
3640 cmp->n= args[0]->cols();
3641 cmp->alloc_comparators();
3644 if (cmp_type == STRING_RESULT || cmp_type == ROW_RESULT)
3646 uint32_t col, num_cols= args[0]->cols();
3648 for (col= 0; col < num_cols; col++)
3650 bool skip_column=
false;
3655 for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
3657 Item *itm= ((cmp_type == STRING_RESULT) ? arg[0] :
3658 arg[0]->element_index(col));
3659 if (itm->result_type() != STRING_RESULT)
3664 else if (itm->is_datetime())
3666 datetime_found=
true;
3673 else if (itm->field_type() == DRIZZLE_TYPE_DATETIME)
3677 if (cmp_type == STRING_RESULT)
3686 if (cmp_type == ROW_RESULT)
3690 cmp= ((in_row*)array)->tmp.comparators + col;
3692 cmp= ((cmp_item_row*)cmp_items[ROW_RESULT])->comparators + col;
3693 *cmp=
new cmp_item_datetime(date_arg);
3696 datetime_found=
false;
3699 compare_as_datetime=
true;
3708 if (type_cnt == 1 && const_itm && !nulls_in_row())
3710 if (compare_as_datetime)
3712 array=
new in_datetime(date_arg, arg_count - 1);
3723 if (args[0]->real_item()->type() == FIELD_ITEM &&
3724 cmp_type != INT_RESULT)
3726 Item_field *field_item= (Item_field*) (args[0]->real_item());
3727 if (field_item->field->can_be_compared_as_int64_t())
3729 bool all_converted=
true;
3730 for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3733 all_converted=
false;
3736 cmp_type= INT_RESULT;
3742 array=
new in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
3743 cmp_collation.collation);
3747 array=
new in_int64_t(arg_count-1);
3751 array=
new in_double(arg_count-1);
3760 ((in_row*)array)->tmp.store_value(args[0]);
3763 case DECIMAL_RESULT:
3764 array=
new in_decimal(arg_count - 1);
3769 if (array && !(getSession().is_fatal_error))
3772 for (uint32_t arg_num=1 ; arg_num < arg_count ; arg_num++)
3776 array->set(j,args[arg_num]);
3782 if ((array->used_count= j))
3788 if (compare_as_datetime)
3789 cmp_items[STRING_RESULT]=
new cmp_item_datetime(date_arg);
3792 for (
int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
3794 if (found_types & (1 << i) && !cmp_items[i])
3796 if ((Item_result)i == STRING_RESULT &&
3797 agg_arg_charsets(cmp_collation, args, arg_count,
3798 MY_COLL_CMP_CONV, 1))
3800 if (!cmp_items[i] && !(cmp_items[i]=
3801 cmp_item::get_comparator((Item_result)i,
3802 cmp_collation.collation)))
3815 args[0]->
print(str);
3817 str->append(STRING_WITH_LEN(
" not"));
3818 str->append(STRING_WITH_LEN(
" in ("));
3820 str->append(STRING_WITH_LEN(
"))"));
3853 uint32_t value_added_map= 0;
3856 int tmp=array->find(args[0]);
3858 return (int64_t) (!
null_value && tmp != negated);
3861 for (uint32_t i= 1 ; i < arg_count ; i++)
3863 Item_result cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
3864 in_item= cmp_items[(uint32_t)cmp_type];
3866 if (!(value_added_map & (1 << (uint32_t)cmp_type)))
3868 in_item->store_value(args[0]);
3872 value_added_map|= 1 << (uint32_t)cmp_type;
3874 if (!in_item->cmp(args[i]) && !args[i]->
null_value)
3875 return (int64_t) (!negated);
3885 :item::function::Boolean(session, item),
3886 abort_on_null(item->abort_on_null),
3887 and_tables_cache(item->and_tables_cache)
3895 void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
3897 List<Item>::iterator li(item->list.begin());
3898 while (
Item *it= li++)
3899 list.push_back(it->copy_andor_structure(session));
3904 Item_cond::fix_fields(Session *session, Item **)
3907 List<Item>::iterator li(list.begin());
3909 void *orig_session_marker= session->session_marker;
3910 unsigned char buff[
sizeof(
char*)];
3911 not_null_tables_cache= used_tables_cache= 0;
3912 const_item_cache=
true;
3914 if (functype() == COND_OR_FUNC)
3915 session->session_marker= 0;
3920 and_tables_cache= ~(table_map) 0;
3941 table_map tmp_table_map;
3942 while (item->type() == Item::COND_ITEM &&
3943 ((Item_cond*) item)->functype() == functype() &&
3944 !((Item_cond*) item)->list.is_empty())
3946 li.replace(((Item_cond*) item)->list);
3947 ((Item_cond*) item)->list.clear();
3951 item->top_level_item();
3954 if ((!item->fixed &&
3955 item->fix_fields(session, li.ref())) ||
3956 (item= &*li)->check_cols(1))
3958 used_tables_cache|= item->used_tables();
3959 if (item->const_item())
3960 and_tables_cache= (table_map) 0;
3963 tmp_table_map= item->not_null_tables();
3964 not_null_tables_cache|= tmp_table_map;
3965 and_tables_cache&= tmp_table_map;
3966 const_item_cache=
false;
3968 with_sum_func= with_sum_func || item->with_sum_func;
3970 if (item->maybe_null)
3973 session->lex().current_select->cond_count+= list.size();
3974 session->session_marker= orig_session_marker;
3975 fix_length_and_dec();
3986 used_tables_cache=0;
3987 const_item_cache=
true;
3989 and_tables_cache= ~(table_map) 0;
3990 not_null_tables_cache= 0;
3994 table_map tmp_table_map;
3995 item->fix_after_pullout(new_parent, li.ref());
3997 used_tables_cache|= item->used_tables();
3998 const_item_cache&= item->const_item();
4000 if (item->const_item())
4001 and_tables_cache= (table_map) 0;
4004 tmp_table_map= item->not_null_tables();
4005 not_null_tables_cache|= tmp_table_map;
4006 and_tables_cache&= tmp_table_map;
4007 const_item_cache=
false;
4013 bool Item_cond::walk(Item_processor processor,
bool walk_subquery,
unsigned char *arg)
4017 while ((item= li++))
4018 if (item->walk(processor, walk_subquery, arg))
4020 return Item_func::walk(processor, walk_subquery, arg);
4046 while ((item= li++))
4048 Item *new_item= item->transform(transformer, arg);
4051 *li.ref()= new_item;
4082 Item_transformer transformer,
unsigned char *arg_t)
4084 if (!(this->*analyzer)(arg_p))
4089 while ((item= li++))
4095 unsigned char *arg_v= *arg_p;
4096 Item *new_item= item->compile(analyzer, &arg_v, transformer, arg_t);
4097 if (new_item && new_item != item)
4098 li.replace(new_item);
4103 void Item_cond::traverse_cond(Cond_traverser traverser,
4104 void *arg, traverse_order order)
4111 (*traverser)(
this, arg);
4112 while ((item= li++))
4114 item->traverse_cond(traverser, arg, order);
4116 (*traverser)(NULL, arg);
4119 while ((item= li++))
4121 item->traverse_cond(traverser, arg, order);
4123 (*traverser)(
this, arg);
4148 while ((item= li++))
4149 item->split_sum_func(session, ref_pointer_array, fields, li.ref(),
true);
4156 return used_tables_cache;
4160 void Item_cond::update_used_tables()
4165 used_tables_cache=0;
4166 const_item_cache=
true;
4169 item->update_used_tables();
4170 used_tables_cache|= item->used_tables();
4171 const_item_cache&= item->const_item();
4186 str->append(func_name(), strlen(func_name()));
4194 void Item_cond::neg_arguments(
Session *session)
4198 while ((item= li++))
4200 Item *new_item= item->neg_transformer(session);
4203 li.replace(new_item);
4236 if (!item->val_bool())
4238 if (abort_on_null || !(
null_value= item->null_value))
4254 if (item->val_bool())
4259 if (item->null_value)
4288 return (*org_item= (
Item*) b);
4312 return cached_value;
4313 return args[0]->
is_null() ? 1: 0;
4321 owner->was_null|= (!cached_value);
4322 return(cached_value);
4326 owner->was_null|= 1;
4340 used_tables_cache= 0;
4341 cached_value= (int64_t) 1;
4345 args[0]->update_used_tables();
4349 cached_value= (int64_t) !args[0]->
is_null();
4358 return args[0]->
is_null() ? 0 : 1;
4365 args[0]->
print(str);
4366 str->append(STRING_WITH_LEN(
" is not null)"));
4388 return my_wildcmp(cmp.cmp_collation.collation,
4389 res->ptr(),res->ptr()+res->length(),
4390 res2->ptr(),res2->ptr()+res2->length(),
4391 make_escape_code(cmp.cmp_collation.collation, escape), internal::wild_one,internal::wild_many) ? 0 : 1;
4406 return OPTIMIZE_NONE;
4408 if (*res2->ptr() != internal::wild_many)
4410 if (args[0]->result_type() != STRING_RESULT || *res2->ptr() != internal::wild_one)
4414 return OPTIMIZE_NONE;
4418 bool Item_func_like::fix_fields(
Session *session,
Item **ref)
4421 if (Item_bool_func2::fix_fields(session, ref) ||
4422 escape_item->fix_fields(session, &escape_item))
4427 my_error(ER_WRONG_ARGUMENTS,MYF(0),
"ESCAPE");
4435 String *escape_str= escape_item->
val_str(&tmp_value1);
4438 escape= (
char *)memory::sql_alloc(escape_str->length());
4439 strcpy(escape, escape_str->ptr());
4443 escape= (
char *)memory::sql_alloc(1);
4444 strcpy(escape,
"\\");
4451 if (args[1]->
const_item() && not collation.collation->use_strnxfrm())
4453 String* res2 = args[1]->
val_str(&tmp_value2);
4457 const size_t len = res2->length();
4458 const char* first = res2->ptr();
4459 const char* last = first + len - 1;
4465 if (len > MIN_TURBOBM_PATTERN_LEN + 2 &&
4466 *first == internal::wild_many &&
4467 *last == internal::wild_many)
4469 const char* tmp = first + 1;
4470 for (; *tmp != internal::wild_many && *tmp != internal::wild_one; tmp++)
4476 canDoTurboBM = (tmp == last) && !use_mb(args[0]->collation.collation);
4480 pattern = first + 1;
4481 pattern_len = (int) len - 2;
4482 int *suff = (
int*) session->mem.
alloc(
sizeof(
int) * ((pattern_len + 1)*2+ alphabet_size));
4483 bmGs = suff + pattern_len + 1;
4484 bmBc = bmGs + pattern_len + 1;
4493 void Item_func_like::cleanup()
4495 canDoTurboBM=
false;
4496 Item_bool_func2::cleanup();
4499 static unsigned char likeconv(
const charset_info_st *cs,
unsigned char a)
4501 #ifdef LIKE_CMP_TOUPPER
4502 return cs->toupper(a);
4504 return cs->sort_order[a];
4514 const int plm1 = pattern_len - 1;
4517 int *
const splm1 = suff + plm1;
4520 *splm1 = pattern_len;
4522 if (!cs->sort_order)
4524 for (
int i = pattern_len - 2; i >= 0; i--)
4526 int tmp = *(splm1 + i - f);
4527 if (g < i && tmp < i - g)
4534 while (g >= 0 && pattern[g] == pattern[g + plm1 - f])
4542 for (
int i = pattern_len - 2; 0 <= i; --i)
4544 int tmp = *(splm1 + i - f);
4545 if (g < i && tmp < i - g)
4553 likeconv(cs, pattern[g]) == likeconv(cs, pattern[g + plm1 - f]))
4570 int *end = bmGs + pattern_len;
4572 for (k = bmGs; k < end; k++)
4578 const int plm1 = pattern_len - 1;
4579 for (i = plm1; i > -1; i--)
4581 if (suff[i] == i + 1)
4583 for (tmp = plm1 - i; j < tmp; j++)
4585 int *tmp2 = bmGs + j;
4586 if (*tmp2 == pattern_len)
4593 for (tmp = plm1 - i; j < tmp; j++)
4596 if (*tmp2 == pattern_len)
4601 for (i = 0; i <= pattern_len - 2; i++)
4602 *(tmp2 - suff[i]) = plm1 - i;
4613 int *end = bmBc + alphabet_size;
4615 const int plm1 = pattern_len - 1;
4618 for (i = bmBc; i < end; i++)
4621 if (!cs->sort_order)
4623 for (j = 0; j < plm1; j++)
4624 bmBc[(uint32_t) (
unsigned char) pattern[j]] = plm1 - j;
4628 for (j = 0; j < plm1; j++)
4629 bmBc[(uint32_t) likeconv(cs,pattern[j])] = plm1 - j;
4645 int shift = pattern_len;
4650 const int plm1= pattern_len - 1;
4651 const int tlmpl= text_len - pattern_len;
4654 if (!cs->sort_order)
4659 while (i >= 0 && pattern[i] == text[i + j])
4662 if (i == plm1 - shift)
4668 const int v = plm1 - i;
4670 bcShift = bmBc[(uint32_t) (
unsigned char) text[i + j]] - plm1 + i;
4671 shift = (turboShift > bcShift) ? turboShift : bcShift;
4672 shift = (shift > bmGs[i]) ? shift : bmGs[i];
4673 if (shift == bmGs[i])
4674 u = (pattern_len - shift < v) ? pattern_len - shift : v;
4677 if (turboShift < bcShift)
4678 shift= max(shift, u + 1);
4690 while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
4693 if (i == plm1 - shift)
4700 const int v= plm1 - i;
4702 bcShift= bmBc[(uint32_t) likeconv(cs, text[i + j])] - plm1 + i;
4703 shift= (turboShift > bcShift) ? turboShift : bcShift;
4704 shift= max(shift, bmGs[i]);
4706 if (shift == bmGs[i])
4707 u= (pattern_len - shift < v) ? pattern_len - shift : v;
4710 if (turboShift < bcShift)
4711 shift= max(shift, u + 1);
4747 result^= (item->val_int() != 0);
4748 if (item->null_value)
4754 return (int64_t) result;
4789 Item *Item_bool_rowready_func2::neg_transformer(
Session *)
4816 Item *Item_cond_and::neg_transformer(
Session *session)
4819 neg_arguments(session);
4825 Item *Item_cond_or::neg_transformer(Session *session)
4828 neg_arguments(session);
4829 Item *item=
new Item_cond_and(list);
4839 allany->func= allany->func_creator(
false);
4840 allany->all= !allany->all;
4841 allany->upper_item= new_item;
4850 allany->all= !allany->all;
4851 allany->func= allany->func_creator(
true);
4852 allany->upper_item= new_item;
4901 : item::function::Boolean(), const_item(0), eval_item(0), cond_false(0)
4903 const_item_cache=
false;
4904 fields.push_back(f1);
4905 fields.push_back(f2);
4908 Item_equal::Item_equal(Item *c, Item_field *f)
4909 : item::function::Boolean(), eval_item(0), cond_false(0)
4911 const_item_cache=
false;
4912 fields.push_back(f);
4917 Item_equal::Item_equal(Item_equal *item_equal)
4918 : item::function::Boolean(), eval_item(0), cond_false(0)
4920 const_item_cache=
false;
4921 List<Item_field>::iterator li(item_equal->fields.begin());
4923 while ((item= li++))
4925 fields.push_back(item);
4928 cond_false= item_equal->cond_false;
4931 void Item_equal::add(Item *c)
4940 Item_func_eq *func=
new Item_func_eq(c,
const_item);
4941 func->set_cmp_func();
4942 func->quick_fix_field();
4943 if ((cond_false= !func->val_int()))
4944 const_item_cache=
true;
4947 void Item_equal::add(Item_field *f)
4949 fields.push_back(f);
4952 uint32_t Item_equal::members()
4954 return fields.size();
4975 while ((item= it++))
4977 if (field->eq(item->field))
4997 fields.concat(&item->fields);
4998 Item *c= item->const_item;
5008 cond_false|= item->cond_false;
5040 while ((item2= it++))
5043 if (cmp(item1, item2, arg) < 0)
5075 while ((item= it++))
5077 if (item->const_item())
5089 not_null_tables_cache= used_tables_cache= 0;
5090 const_item_cache=
false;
5091 while ((item= li++))
5093 table_map tmp_table_map;
5094 used_tables_cache|= item->used_tables();
5095 tmp_table_map= item->not_null_tables();
5096 not_null_tables_cache|= tmp_table_map;
5097 if (item->maybe_null)
5100 fix_length_and_dec();
5105 void Item_equal::update_used_tables()
5107 List<Item_field>::iterator li(fields.begin());
5109 not_null_tables_cache= used_tables_cache= 0;
5110 if ((const_item_cache= cond_false))
5114 item->update_used_tables();
5115 used_tables_cache|= item->used_tables();
5116 const_item_cache&= item->const_item();
5127 eval_item->store_value(item);
5130 while ((item_field= it++))
5133 if (item_field->field->getTable()->const_table)
5142 void Item_equal::fix_length_and_dec()
5144 Item *item= get_first();
5145 eval_item= cmp_item::get_comparator(item->result_type(),
5146 item->collation.collation);
5149 bool Item_equal::walk(Item_processor processor,
bool walk_subquery,
unsigned char *arg)
5151 List<Item_field>::iterator it(fields.begin());
5153 while ((item= it++))
5155 if (item->walk(processor, walk_subquery, arg))
5158 return Item_func::walk(processor, walk_subquery, arg);
5165 while ((item= it++))
5167 Item *new_item= item->transform(transformer, arg);
5170 *(
Item **)it.ref()= new_item;
5177 str->append(func_name(), strlen(func_name()));
5188 while ((item= it++))
5197 cmp_item_datetime::cmp_item_datetime(
Item *warn_item_arg) :
5198 session(current_session),
5199 warn_item(warn_item_arg),