33 #include "sql_class.h"
37 #include "sql_parse.h"
38 #include "sql_derived.h"
41 #include "sql_join_buffer.h"
42 #include "sql_optimizer.h"
43 #include "opt_explain_format.h"
45 Item_subselect::Item_subselect():
47 substitution(NULL), in_cond_of_tab(INT_MIN), engine(NULL), old_engine(NULL),
48 used_tables_cache(0), have_to_be_excluded(0), const_item_cache(1),
49 engine_changed(false), changed(false)
61 void Item_subselect::init(st_select_lex *select_lex,
62 select_result_interceptor *result)
69 DBUG_ENTER(
"Item_subselect::init");
70 DBUG_PRINT(
"enter", (
"select_lex: 0x%lx", (
long) select_lex));
71 unit= select_lex->master_unit();
79 engine= unit->item->engine;
80 parsing_place= unit->item->parsing_place;
81 unit->item->engine= 0;
83 engine->change_result(
this, result);
87 SELECT_LEX *outer_select= unit->outer_select();
92 parsing_place= (outer_select->in_sum_expr ?
94 outer_select->parsing_place);
101 SELECT_LEX *upper= unit->outer_select();
102 if (upper->parsing_place == IN_HAVING)
103 upper->subquery_in_having= 1;
109 void Item_subselect::cleanup()
111 DBUG_ENTER(
"Item_subselect::cleanup");
112 Item_result_field::cleanup();
127 traced_before=
false;
133 void Item_singlerow_subselect::cleanup()
135 DBUG_ENTER(
"Item_singlerow_subselect::cleanup");
137 Item_subselect::cleanup();
163 if (unit->thd->stmt_arena->is_conventional())
165 uint cnt= select_lex->item_list.elements;
166 select_lex->item_list.empty();
167 for(; cnt > 0; cnt--)
168 select_lex->item_list.push_back(
new Item_int(NAME_STRING(
"Not_used"),
170 MY_INT64_NUM_DECIMAL_DIGITS));
173 select_lex->select_number,
174 "IN (SELECT)",
"EXISTS (CORRELATED SELECT)");
175 oto1.add(
"put_1_in_SELECT_list",
true);
181 unit->global_parameters->select_limit=
new Item_int((int32) 1);
182 unit->set_limit(unit->global_parameters);
184 select_lex->join->allow_outer_refs=
true;
208 Item *Item_in_subselect::remove_in2exists_conds(
Item* conds)
212 if (conds->type() != Item::COND_ITEM)
219 if (cnd->functype() != Item_func::COND_AND_FUNC)
229 switch (cnd->argument_list()->elements)
234 return cnd->argument_list()->head();
244 DBUG_ASSERT(engine->engine_type() == subselect_engine::SINGLE_SELECT_ENGINE);
245 THD *
const thd= unit->thd;
249 DBUG_ASSERT(join == old_engine_derived->join);
251 DBUG_ASSERT(join->
select_lex == unit->first_select());
252 DBUG_ASSERT(join->
unit == unit);
253 DBUG_ASSERT(unit->global_parameters->select_limit == NULL);
270 join->
conds= remove_in2exists_conds(join->
conds);
274 join->
select_lex->uncacheable&= ~UNCACHEABLE_DEPENDENT;
279 DBUG_ASSERT(unit == join->
select_lex->master_unit());
280 unit->uncacheable&= ~UNCACHEABLE_DEPENDENT;
283 old_engine_derived->join->
select_lex->select_number,
284 "IN (SELECT)",
"materialization");
285 oto1.add(
"chosen",
true);
291 if (new_engine->
setup(unit->get_unit_column_types()))
300 if (change_engine(new_engine))
308 void Item_in_subselect::cleanup()
310 DBUG_ENTER(
"Item_in_subselect::cleanup");
313 left_expr_cache->delete_elements();
314 delete left_expr_cache;
315 left_expr_cache= NULL;
323 unit->first_select()->uncacheable|= UNCACHEABLE_DEPENDENT;
324 unit->uncacheable|= UNCACHEABLE_DEPENDENT;
331 unit->global_parameters->select_limit= NULL;
338 Item_subselect::cleanup();
342 Item_subselect::~Item_subselect()
347 Item_subselect::trans_res
348 Item_subselect::select_transformer(
JOIN *join)
350 DBUG_ENTER(
"Item_subselect::select_transformer");
355 bool Item_subselect::fix_fields(THD *thd,
Item **ref)
357 char const *save_where= thd->where;
361 DBUG_ASSERT(fixed == 0);
369 DBUG_ASSERT(thd == unit->thd);
372 DBUG_ASSERT(engine->
get_item() ==
this);
380 if (!(res= engine->prepare()))
396 if (unit->outer_select()->where == (*ref))
397 unit->outer_select()->where= substitution;
398 else if (unit->outer_select()->having == (*ref))
399 unit->outer_select()->having= substitution;
401 (*ref)= substitution;
402 substitution->item_name= item_name;
403 if (have_to_be_excluded)
406 thd->where=
"checking transformed subquery";
408 ret= (*ref)->fix_fields(thd, ref);
409 thd->where= save_where;
413 if (engine->cols() > max_columns)
415 my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
418 fix_length_and_dec();
423 if ((uncacheable= engine->uncacheable()))
426 if (uncacheable & UNCACHEABLE_RAND)
427 used_tables_cache|= RAND_TABLE_BIT;
432 thd->where= save_where;
444 Item_processor processor,
451 while ((table= li++))
453 if (table->join_cond() &&
454 table->join_cond()->walk(processor, walk_subquery, argument))
457 if (table->nested_join != NULL &&
459 walk_subquery, argument))
474 for (SELECT_LEX *lex= unit->first_select(); lex; lex= lex->next_select())
482 if (item->walk(processor, walk_subquery, argument))
486 if (lex->join_list != NULL &&
490 if (lex->where && (lex->where)->walk(processor, walk_subquery, argument))
493 for (order= lex->group_list.first ; order; order= order->next)
495 if ((*order->item)->walk(processor, walk_subquery, argument))
499 if (lex->having && (lex->having)->walk(processor, walk_subquery,
503 for (order= lex->order_list.first ; order; order= order->next)
505 if ((*order->item)->walk(processor, walk_subquery, argument))
510 return (this->*processor)(argument);
513 bool Item_subselect::walk(Item_processor processor,
bool walk_subquery,
516 return walk_body(processor, walk_subquery, argument);
542 if (m->
type == CTX_WHERE)
548 if (unit->explain_marker == CTX_NONE)
549 unit->explain_marker= CTX_WHERE;
551 DBUG_ASSERT(unit->explain_marker == CTX_WHERE);
556 if (m->
type == CTX_HAVING && unit->explain_marker == CTX_WHERE)
566 if (unit->explain_marker == CTX_NONE)
569 if (unit->explain_marker == m->
type)
583 if (unit->explain_marker == CTX_GROUP_BY_SQ && m->
type == CTX_ORDER_BY_SQ)
585 if (unit->explain_marker == CTX_ORDER_BY_SQ && m->
type == CTX_GROUP_BY_SQ)
588 if (unit->explain_marker == CTX_SELECT_LIST &&
589 (m->
type == CTX_ORDER_BY_SQ || m->
type == CTX_GROUP_BY_SQ))
591 if ((unit->explain_marker == CTX_ORDER_BY_SQ ||
592 unit->explain_marker == CTX_GROUP_BY_SQ) && m->
type == CTX_SELECT_LIST)
595 DBUG_ASSERT(!
"Unexpected combination of item trees!");
599 unit->explain_marker= m->
type;
604 bool Item_subselect::exec()
606 DBUG_ENTER(
"Item_subselect::exec");
611 THD *
const thd= unit->thd;
612 if (thd->is_error() || thd->killed)
615 DBUG_ASSERT(!thd->lex->context_analysis_only);
620 DBUG_EXECUTE_IF(
"subselect_exec_fail", DBUG_RETURN(
true););
627 #ifdef OPTIMIZER_TRACE
629 const bool disable_trace=
631 !trace->feature_enabled(Opt_trace_context::REPEATED_SUBSELECT);
637 trace_exec.add_select_number(unit->first_select()->select_number);
641 bool res= engine->exec();
667 st_select_lex *removed_select)
671 used_tables_cache= 0;
677 for (SELECT_LEX *sel= unit->first_select(); sel; sel= sel->next_select())
680 sel->where->fix_after_pullout(parent_select, removed_select);
683 sel->having->fix_after_pullout(parent_select, removed_select);
697 for (
ORDER *order= (
ORDER*) sel->order_list.first;
705 (*
group->item)->fix_after_pullout(parent_select, removed_select);
709 bool Item_in_subselect::walk(Item_processor processor,
bool walk_subquery,
712 if (left_expr->walk(processor, walk_subquery, argument))
719 return walk_body(processor, walk_subquery, argument);
726 bool Item_in_subselect::exec()
728 DBUG_ENTER(
"Item_in_subselect::exec");
731 engine->engine_type() == subselect_engine::HASH_SJ_ENGINE));
750 if (left_expr_cache != NULL)
752 const int result= test_if_item_cache_changed(*left_expr_cache);
765 null_value= was_null=
false;
766 const bool retval= Item_subselect::exec();
771 Item::Type Item_subselect::type()
const
773 return SUBSELECT_ITEM;
777 void Item_subselect::fix_length_and_dec()
779 engine->fix_length_and_dec(0);
783 table_map Item_subselect::used_tables()
const
785 return (table_map) (engine->uncacheable() ? used_tables_cache : 0L);
789 bool Item_subselect::const_item()
const
791 if (unit->thd->lex->context_analysis_only)
794 if (!unit->thd->lex->is_query_tables_locked())
796 return const_item_cache;
799 Item *Item_subselect::get_tmp_table_item(THD *thd_arg)
801 if (!with_sum_func && !const_item())
803 return copy_or_same(thd_arg);
806 void Item_subselect::update_used_tables()
808 if (!engine->uncacheable())
811 if (!(used_tables_cache & ~engine->upper_select_const_tables()))
822 engine->print(str, query_type);
826 str->append(
"(...)");
830 Item_singlerow_subselect::Item_singlerow_subselect(st_select_lex *select_lex)
833 DBUG_ENTER(
"Item_singlerow_subselect::Item_singlerow_subselect");
834 init(select_lex,
new select_singlerow_subselect(
this));
836 max_columns= UINT_MAX;
843 DBUG_ENTER(
"Item_singlerow_subselect::invalidate_and_restore_select_lex");
844 st_select_lex *result= unit->first_select();
860 Item_maxmin_subselect::Item_maxmin_subselect(THD *thd_param,
862 st_select_lex *select_lex,
867 DBUG_ENTER(
"Item_maxmin_subselect::Item_maxmin_subselect");
869 init(select_lex,
new select_max_min_finder_subselect(
this, max_arg,
879 used_tables_cache= parent->get_used_tables_cache();
880 const_item_cache= parent->get_const_item_cache();
885 void Item_maxmin_subselect::cleanup()
887 DBUG_ENTER(
"Item_maxmin_subselect::cleanup");
888 Item_singlerow_subselect::cleanup();
897 str->append(max?
"<max>":
"<min>", 5);
902 void Item_singlerow_subselect::reset()
906 value->null_value= TRUE;
920 Item_subselect::trans_res
923 DBUG_ENTER(
"Item_singlerow_subselect::select_transformer");
928 THD *
const thd= unit->thd;
929 Query_arena *arena= thd->stmt_arena;
931 if (!select_lex->master_unit()->is_union() &&
932 !select_lex->table_list.elements &&
933 select_lex->item_list.elements == 1 &&
934 !select_lex->item_list.head()->with_sum_func &&
942 !(select_lex->item_list.head()->type() == FIELD_ITEM ||
943 select_lex->item_list.head()->type() == REF_ITEM) &&
950 !arena->is_stmt_prepare_or_first_sp_execute()
954 have_to_be_excluded= 1;
955 if (thd->lex->describe)
957 char warn_buff[MYSQL_ERRMSG_SIZE];
958 sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number);
959 push_warning(thd, Sql_condition::WARN_LEVEL_NOTE,
960 ER_SELECT_REDUCED, warn_buff);
962 substitution= select_lex->item_list.head();
967 substitution->walk(&Item::remove_dependence_processor, 0,
968 (uchar *) select_lex->outer_select());
969 DBUG_RETURN(RES_REDUCE);
975 void Item_singlerow_subselect::store(uint
i,
Item *item)
978 row[
i]->cache_value();
981 enum Item_result Item_singlerow_subselect::result_type()
const
983 return engine->type();
990 enum_field_types Item_singlerow_subselect::field_type()
const
992 return engine->field_type();
995 void Item_singlerow_subselect::fix_length_and_dec()
997 if ((max_columns= engine->cols()) == 1)
999 engine->fix_length_and_dec(row= &value);
1005 engine->fix_length_and_dec(row);
1008 unsigned_flag= value->unsigned_flag;
1014 if (engine->no_tables())
1015 maybe_null= engine->may_be_null();
1018 void Item_singlerow_subselect::no_rows_in_result()
1025 if (unit->uncacheable & UNCACHEABLE_DEPENDENT)
1029 uint Item_singlerow_subselect::cols()
1031 return engine->cols();
1034 bool Item_singlerow_subselect::check_cols(uint c)
1036 if (c != engine->cols())
1038 my_error(ER_OPERAND_COLUMNS, MYF(0), c);
1044 bool Item_singlerow_subselect::null_inside()
1046 for (uint i= 0; i < max_columns ; i++)
1048 if (row[i]->null_value)
1054 void Item_singlerow_subselect::bring_value()
1056 if (!exec() && assigned())
1062 double Item_singlerow_subselect::val_real()
1064 DBUG_ASSERT(fixed == 1);
1065 if (!
no_rows && !exec() && !value->null_value)
1068 return value->val_real();
1077 longlong Item_singlerow_subselect::val_int()
1079 DBUG_ASSERT(fixed == 1);
1080 if (!
no_rows && !exec() && !value->null_value)
1083 return value->val_int();
1094 if (!
no_rows && !exec() && !value->null_value)
1097 return value->val_str(str);
1109 if (!
no_rows && !exec() && !value->null_value)
1112 return value->val_decimal(decimal_value);
1122 bool Item_singlerow_subselect::get_date(
MYSQL_TIME *ltime, uint fuzzydate)
1124 if (!
no_rows && !exec() && !value->null_value)
1127 return value->get_date(ltime, fuzzydate);
1137 bool Item_singlerow_subselect::get_time(
MYSQL_TIME *ltime)
1139 if (!
no_rows && !exec() && !value->null_value)
1142 return value->get_time(ltime);
1154 if (!
no_rows && !exec() && !value->null_value)
1167 Item_exists_subselect::Item_exists_subselect(st_select_lex *select_lex):
1169 sj_convert_priority(0), embedding_join_nest(NULL)
1171 DBUG_ENTER(
"Item_exists_subselect::Item_exists_subselect");
1172 init(select_lex,
new select_exists_subselect(
this));
1173 max_columns= UINT_MAX;
1182 str->append(STRING_WITH_LEN(
"exists"));
1187 bool Item_in_subselect::test_limit(st_select_lex_unit *unit_arg)
1189 if (unit_arg->fake_select_lex &&
1190 unit_arg->fake_select_lex->test_limit())
1193 SELECT_LEX *sl= unit_arg->first_select();
1194 for (; sl; sl= sl->next_select())
1196 if (sl->test_limit())
1202 Item_in_subselect::Item_in_subselect(
Item * left_exp,
1203 st_select_lex *select_lex):
1205 left_expr_cache_filled(false), need_expr_cache(TRUE), expr(NULL),
1206 optimizer(NULL), was_null(FALSE), abort_on_null(FALSE),
1207 in2exists_info(NULL), pushed_cond_guards(NULL), upper_item(NULL)
1209 DBUG_ENTER(
"Item_in_subselect::Item_in_subselect");
1210 init(select_lex,
new select_exists_subselect(
this));
1211 max_columns= UINT_MAX;
1215 test_limit(select_lex->master_unit());
1219 Item_allany_subselect::Item_allany_subselect(
Item * left_exp,
1220 chooser_compare_func_creator fc,
1221 st_select_lex *select_lex,
1225 DBUG_ENTER(
"Item_allany_subselect::Item_allany_subselect");
1226 left_expr= left_exp;
1227 func= func_creator(all_arg);
1228 init(select_lex,
new select_exists_subselect(
this));
1233 test_limit(select_lex->master_unit());
1238 void Item_exists_subselect::fix_length_and_dec()
1242 max_columns= engine->cols();
1250 unit->global_parameters->select_limit=
new Item_int((int32) 1);
1254 double Item_exists_subselect::val_real()
1256 DBUG_ASSERT(fixed == 1);
1262 return (
double) value;
1265 longlong Item_exists_subselect::val_int()
1267 DBUG_ASSERT(fixed == 1);
1292 DBUG_ASSERT(fixed == 1);
1295 str->set((ulonglong)value,&my_charset_bin);
1315 DBUG_ASSERT(fixed == 1);
1318 int2my_decimal(E_DEC_FATAL_ERROR, value, 0, decimal_value);
1319 return decimal_value;
1325 DBUG_ASSERT(fixed == 1);
1335 double Item_in_subselect::val_real()
1342 DBUG_ASSERT(fixed == 1);
1348 if (was_null && !value)
1350 return (
double) value;
1354 longlong Item_in_subselect::val_int()
1361 DBUG_ASSERT(fixed == 1);
1367 if (was_null && !value)
1380 DBUG_ASSERT(fixed == 1);
1386 if (was_null && !value)
1391 str->set((ulonglong)value, &my_charset_bin);
1398 DBUG_ASSERT(fixed == 1);
1404 if (was_null && !value)
1416 DBUG_ASSERT(fixed == 1);
1422 if (was_null && !value)
1424 int2my_decimal(E_DEC_FATAL_ERROR, value, 0, decimal_value);
1425 return decimal_value;
1463 Item_subselect::trans_res
1464 Item_in_subselect::single_value_transformer(
JOIN *join,
1468 bool subquery_maybe_null=
false;
1469 DBUG_ENTER(
"Item_in_subselect::single_value_transformer");
1476 if (select_lex->item_list.elements > 1)
1478 my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
1479 DBUG_RETURN(RES_ERROR);
1482 THD *
const thd= unit->thd;
1491 for (SELECT_LEX* lex= select_lex->master_unit()->first_select();
1492 lex != NULL && lex->master_unit() == select_lex->master_unit();
1493 lex= lex->next_select())
1494 if (lex->item_list.head()->maybe_null)
1495 subquery_maybe_null=
true;
1509 if (!func->eqne_op() &&
1510 !select_lex->master_unit()->uncacheable &&
1511 (abort_on_null || (upper_item && upper_item->top_level()) ||
1512 (!left_expr->maybe_null && !subquery_maybe_null)))
1517 DBUG_RETURN(RES_OK);
1521 if (!select_lex->group_list.elements &&
1522 !select_lex->having &&
1523 !select_lex->with_sum_func &&
1524 !(select_lex->next_select()) &&
1525 select_lex->table_list.elements &&
1526 !(substype() == ALL_SUBS && subquery_maybe_null))
1529 select_lex->select_number,
1530 "> ALL/ANY (SELECT)",
"SELECT(MIN)");
1531 oto1.add(
"chosen",
true);
1533 nesting_map save_allow_sum_func;
1551 upper_item->set_sum_test(item);
1552 join->ref_ptrs[0]= item;
1559 DBUG_EXECUTE(
"where",
1560 print_where(item,
"rewrite with MIN/MAX", QT_ORDINARY););
1561 if (thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY)
1568 if (item->get_arg(0)->type() == Item::FIELD_ITEM)
1569 DBUG_ASSERT(select_lex->non_agg_field_used());
1570 select_lex->set_non_agg_field_used(
false);
1573 save_allow_sum_func= thd->lex->allow_sum_func;
1574 thd->lex->allow_sum_func|=
1575 (nesting_map)1 << thd->lex->current_select->nest_level;
1581 if (item->fix_fields(thd, 0))
1582 DBUG_RETURN(RES_ERROR);
1583 thd->lex->allow_sum_func= save_allow_sum_func;
1593 select_lex->select_number,
1594 "> ALL/ANY (SELECT)",
"MIN (SELECT)");
1595 oto1.add(
"chosen",
true);
1598 substype()==ANY_SUBS);
1600 upper_item->set_sub_test(item);
1603 upper_item->set_subselect(
this);
1612 substitution= func->create(left_expr->real_item(), subs);
1613 DBUG_RETURN(RES_OK);
1619 SELECT_LEX_UNIT *master_unit= select_lex->master_unit();
1620 substitution= optimizer;
1622 SELECT_LEX *current= thd->lex->current_select;
1624 thd->lex->current_select= current->outer_select();
1626 if (!optimizer || optimizer->fix_left(thd, 0))
1628 thd->lex->current_select= current;
1629 DBUG_RETURN(RES_ERROR);
1631 thd->lex->current_select= current;
1634 optimizer->keep_top_level_cache();
1642 (
char *)
"<no matter>", (
char *)in_left_expr_name);
1644 DBUG_RETURN(RES_ERROR);
1647 if (!left_expr->const_item())
1648 left->depended_from= select_lex->outer_select();
1652 DBUG_ASSERT(in2exists_info == NULL);
1653 in2exists_info=
new In2exists_info;
1654 in2exists_info->originally_dependent=
1655 master_unit->uncacheable & UNCACHEABLE_DEPENDENT;
1656 master_unit->uncacheable|= UNCACHEABLE_DEPENDENT;
1659 if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards)
1661 if (!(pushed_cond_guards= (
bool*)join->thd->alloc(
sizeof(
bool))))
1662 DBUG_RETURN(RES_ERROR);
1663 pushed_cond_guards[0]= TRUE;
1668 DBUG_RETURN(retval);
1711 Item_subselect::trans_res
1715 THD *
const thd= unit->thd;
1716 DBUG_ENTER(
"Item_in_subselect::single_value_in_to_exists_transformer");
1719 "IN (SELECT)",
"EXISTS (CORRELATED SELECT)");
1720 oto1.add(
"chosen",
true);
1722 select_lex->uncacheable|= UNCACHEABLE_DEPENDENT;
1723 in2exists_info->added_to_where=
false;
1725 if (join->
having || select_lex->with_sum_func ||
1726 select_lex->group_list.elements)
1734 this->full_name()));
1735 item->set_created_by_in2exists();
1736 if (!abort_on_null && left_expr->maybe_null)
1744 OUTER_FIELD_IS_NOT_NULL);
1745 item->set_created_by_in2exists();
1753 select_lex->having= join->
having= and_items(join->
having, item);
1754 if (join->
having == item)
1755 item->item_name.
set(in_having_cond);
1756 select_lex->having->top_level_item();
1757 select_lex->having_fix_field= 1;
1763 "evaluating_constant_having_conditions");
1764 tmp= join->
having->fix_fields(thd, 0);
1765 select_lex->having_fix_field= 0;
1767 DBUG_RETURN(RES_ERROR);
1771 Item *orig_item= select_lex->item_list.head()->real_item();
1773 if (select_lex->table_list.elements)
1782 item->set_created_by_in2exists();
1783 if (!abort_on_null && orig_item->maybe_null)
1786 having->set_created_by_in2exists();
1787 if (left_expr->maybe_null)
1790 get_cond_guard(0), NULL,
1792 OUTER_FIELD_IS_NOT_NULL)))
1793 DBUG_RETURN(RES_ERROR);
1794 having->set_created_by_in2exists();
1801 having->item_name.
set(in_having_cond);
1802 select_lex->having= join->
having= having;
1803 select_lex->having_fix_field= 1;
1810 "evaluating_constant_having_conditions");
1811 tmp= join->
having->fix_fields(thd, 0);
1812 select_lex->having_fix_field= 0;
1814 DBUG_RETURN(RES_ERROR);
1817 item->set_created_by_in2exists();
1823 if (!abort_on_null && left_expr->maybe_null)
1827 OUTER_FIELD_IS_NOT_NULL)))
1828 DBUG_RETURN(RES_ERROR);
1829 item->set_created_by_in2exists();
1835 item->item_name.
set(in_additional_cond);
1842 select_lex->where= join->
conds= and_items(join->
conds, item);
1843 select_lex->where->top_level_item();
1844 in2exists_info->added_to_where=
true;
1850 "evaluating_constant_where_conditions");
1851 if (join->
conds->fix_fields(thd, 0))
1852 DBUG_RETURN(RES_ERROR);
1857 if (select_lex->master_unit()->is_union())
1868 (
char *)
"<no matter>",
1869 (
char *)
"<result>"));
1870 new_having->set_created_by_in2exists();
1871 if (!abort_on_null && left_expr->maybe_null)
1877 OUTER_FIELD_IS_NOT_NULL)))
1878 DBUG_RETURN(RES_ERROR);
1879 new_having->set_created_by_in2exists();
1881 new_having->item_name.
set(in_having_cond);
1882 select_lex->having= join->
having= new_having;
1883 select_lex->having_fix_field= 1;
1890 "evaluating_constant_having_conditions");
1891 tmp= join->
having->fix_fields(thd, 0);
1892 select_lex->having_fix_field= 0;
1894 DBUG_RETURN(RES_ERROR);
1901 orig_item->walk(&Item::remove_dependence_processor, 0,
1902 (uchar *) select_lex->outer_select());
1912 substitution= func->create(left_expr->real_item(), orig_item);
1913 have_to_be_excluded= 1;
1914 if (thd->lex->describe)
1916 char warn_buff[MYSQL_ERRMSG_SIZE];
1917 sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number);
1918 push_warning(thd, Sql_condition::WARN_LEVEL_NOTE,
1919 ER_SELECT_REDUCED, warn_buff);
1921 DBUG_RETURN(RES_REDUCE);
1927 DBUG_RETURN(RES_OK);
1931 Item_subselect::trans_res
1932 Item_in_subselect::row_value_transformer(
JOIN *join)
1935 uint cols_num= left_expr->cols();
1937 DBUG_ENTER(
"Item_in_subselect::row_value_transformer");
1940 if (select_lex->item_list.elements != left_expr->cols())
1942 my_error(ER_OPERAND_COLUMNS, MYF(0), left_expr->cols());
1943 DBUG_RETURN(RES_ERROR);
1953 SELECT_LEX_UNIT *master_unit= select_lex->master_unit();
1954 substitution= optimizer;
1956 THD *
const thd= unit->thd;
1957 SELECT_LEX *current= thd->lex->current_select;
1958 thd->lex->current_select= current->outer_select();
1960 if (!optimizer || optimizer->fix_left(thd, 0))
1962 thd->lex->current_select= current;
1963 DBUG_RETURN(RES_ERROR);
1967 optimizer->keep_top_level_cache();
1969 thd->lex->current_select= current;
1970 DBUG_ASSERT(in2exists_info == NULL);
1971 in2exists_info=
new In2exists_info;
1972 in2exists_info->originally_dependent=
1973 master_unit->uncacheable & UNCACHEABLE_DEPENDENT;
1974 master_unit->uncacheable|= UNCACHEABLE_DEPENDENT;
1976 if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards)
1978 if (!(pushed_cond_guards= (
bool*)join->thd->alloc(
sizeof(
bool) *
1979 left_expr->cols())))
1980 DBUG_RETURN(RES_ERROR);
1981 for (uint i= 0; i < cols_num; i++)
1982 pushed_cond_guards[i]= TRUE;
2009 Item_subselect::trans_res
2013 THD *
const thd= unit->thd;
2014 Item *having_item= 0;
2015 uint cols_num= left_expr->cols();
2016 bool is_having_used= (join->
having || select_lex->with_sum_func ||
2017 select_lex->group_list.first ||
2018 !select_lex->table_list.elements);
2020 DBUG_ENTER(
"Item_in_subselect::row_value_in_to_exists_transformer");
2022 "IN (SELECT)",
"EXISTS (CORRELATED SELECT)");
2023 oto1.add(
"chosen",
true);
2025 select_lex->uncacheable|= UNCACHEABLE_DEPENDENT;
2026 in2exists_info->added_to_where=
false;
2043 Item *item_having_part2= 0;
2044 for (uint i= 0; i < cols_num; i++)
2046 Item *item_i= join->ref_ptrs[
i];
2047 Item **pitem_i= &join->ref_ptrs[
i];
2048 DBUG_ASSERT((left_expr->fixed && item_i->fixed) ||
2049 (item_i->type() == REF_ITEM &&
2050 ((
Item_ref*)(item_i))->ref_type() == Item_ref::OUTER_REF));
2051 if (item_i-> check_cols(left_expr->element_index(i)->cols()))
2052 DBUG_RETURN(RES_ERROR);
2056 (*optimizer->get_cache())->
2058 (
char *)
"<no matter>",
2059 (
char *)in_left_expr_name),
2063 (
char *)
"<no matter>",
2064 (
char *)
"<list ref>")
2066 item_eq->set_created_by_in2exists();
2071 (
char *)
"<no matter>",
2072 (
char *)
"<list ref>")
2074 item_isnull->set_created_by_in2exists();
2076 col_item->set_created_by_in2exists();
2077 if (!abort_on_null && left_expr->element_index(i)->maybe_null)
2081 OUTER_FIELD_IS_NOT_NULL)))
2082 DBUG_RETURN(RES_ERROR);
2083 col_item->set_created_by_in2exists();
2085 having_item= and_items(having_item, col_item);
2091 (
char *)
"<no matter>",
2092 (
char *)
"<list ref>"));
2093 item_nnull_test->set_created_by_in2exists();
2094 if (!abort_on_null && left_expr->element_index(i)->maybe_null)
2096 if (!(item_nnull_test=
2099 OUTER_FIELD_IS_NOT_NULL)))
2100 DBUG_RETURN(RES_ERROR);
2101 item_nnull_test->set_created_by_in2exists();
2103 item_having_part2= and_items(item_having_part2, item_nnull_test);
2104 item_having_part2->top_level_item();
2106 having_item= and_items(having_item, item_having_part2);
2107 having_item->top_level_item();
2128 Item *where_item= 0;
2129 for (uint i= 0; i < cols_num; i++)
2131 Item *item_i= join->ref_ptrs[
i];
2132 Item **pitem_i= &join->ref_ptrs[
i];
2133 DBUG_ASSERT((left_expr->fixed && item_i->fixed) ||
2134 (item_i->type() == REF_ITEM &&
2135 ((
Item_ref*)(item_i))->ref_type() == Item_ref::OUTER_REF));
2136 if (item_i->check_cols(left_expr->element_index(i)->cols()))
2137 DBUG_RETURN(RES_ERROR);
2140 (*optimizer->get_cache())->addr(i),
2141 (
char *)
"<no matter>", (
char *)in_left_expr_name);
2143 DBUG_RETURN(RES_ERROR);
2146 if (!left_expr->element_index(i)->const_item())
2147 left->depended_from= select_lex->outer_select();
2154 (
char *)
"<no matter>",
2155 (
char *)
"<list ref>")
2157 item->set_created_by_in2exists();
2165 (
char *)
"<no matter>",
2166 (
char *)
"<list ref>"));
2168 having_col_item->set_created_by_in2exists();
2173 (
char *)
"<no matter>",
2174 (
char *)
"<list ref>")
2176 item_isnull->set_created_by_in2exists();
2178 item->set_created_by_in2exists();
2183 if (left_expr->element_index(i)->maybe_null)
2187 OUTER_FIELD_IS_NOT_NULL)))
2188 DBUG_RETURN(RES_ERROR);
2189 item->set_created_by_in2exists();
2190 if (!(having_col_item=
2194 OUTER_FIELD_IS_NOT_NULL)))
2195 DBUG_RETURN(RES_ERROR);
2196 having_col_item->set_created_by_in2exists();
2198 having_item= and_items(having_item, having_col_item);
2201 where_item= and_items(where_item, item);
2208 select_lex->where= join->
conds= and_items(join->
conds, where_item);
2209 select_lex->where->top_level_item();
2210 in2exists_info->added_to_where=
true;
2212 "evaluating_constant_where_conditions");
2213 if (join->
conds->fix_fields(thd, 0))
2214 DBUG_RETURN(RES_ERROR);
2220 and_items(join->
having, having_item);
2221 if (having_item == select_lex->having)
2222 having_item->item_name.
set(in_having_cond);
2223 select_lex->having->top_level_item();
2229 select_lex->having_fix_field= 1;
2231 "evaluating_constant_having_conditions");
2232 res= join->
having->fix_fields(thd, 0);
2233 select_lex->having_fix_field= 0;
2236 DBUG_RETURN(RES_ERROR);
2240 DBUG_RETURN(RES_OK);
2244 Item_subselect::trans_res
2245 Item_in_subselect::select_transformer(
JOIN *join)
2272 Item_subselect::trans_res
2275 THD *
const thd= unit->thd;
2276 SELECT_LEX *current= thd->lex->current_select;
2277 const char *save_where= thd->where;
2278 Item_subselect::trans_res res= RES_ERROR;
2281 DBUG_ENTER(
"Item_in_subselect::select_in_like_transformer");
2290 SELECT_LEX *sl= current->master_unit()->first_select();
2291 for (; sl; sl= sl->next_select())
2293 DBUG_ASSERT(!sl->join->order || sl->join->order.src == ESC_GROUP_BY);
2298 DBUG_RETURN(RES_OK);
2300 thd->where=
"IN/ALL/ANY subquery";
2311 Prepared_stmt_arena_holder ps_arena_holder(thd);
2318 thd->lex->current_select= current->outer_select();
2319 result= (!left_expr->fixed &&
2320 left_expr->fix_fields(thd, optimizer->arguments()));
2322 left_expr= optimizer->arguments()[0];
2324 thd->lex->current_select= current;
2344 Prepared_stmt_arena_holder ps_arena_holder(thd);
2346 if (left_expr->cols() == 1)
2347 res= single_value_transformer(join, func);
2351 if (func != &eq_creator)
2353 my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
2354 DBUG_RETURN(RES_ERROR);
2356 res= row_value_transformer(join);
2361 thd->where= save_where;
2369 str->append(STRING_WITH_LEN(
"<exists>"));
2372 left_expr->
print(str, query_type);
2373 str->append(STRING_WITH_LEN(
" in "));
2379 bool Item_in_subselect::fix_fields(THD *thd_arg,
Item **ref)
2384 return !( (*ref)=
new Item_int(1));
2386 if ((thd_arg->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW) &&
2387 left_expr && !left_expr->fixed)
2388 result = left_expr->fix_fields(thd_arg, &left_expr);
2390 return result || Item_subselect::fix_fields(thd_arg, ref);
2395 st_select_lex *removed_select)
2401 used_tables_cache|= left_expr->used_tables();
2419 Next_select_func end_select;
2420 bool use_result_field= FALSE;
2422 outer_join= unit->outer_select()->join;
2442 end_select= outer_join->join_tab[outer_join->
primary_tables-1].next_select;
2443 if (end_select == end_send_group || end_select == end_write_group)
2444 use_result_field= TRUE;
2449 for (uint i= 0; i < left_expr->cols(); i++)
2452 left_expr->element_index(i),
2454 if (!cur_item_cache || left_expr_cache->push_front(cur_item_cache))
2488 st_select_lex *root=
2489 static_cast<st_select_lex*
>(
static_cast<void*
>(arg));
2490 st_select_lex *sl= unit->outer_select();
2502 while (sl != root && sl != NULL)
2503 sl= sl->outer_select();
2505 unit->exclude_tree();
2510 Item_subselect::trans_res
2511 Item_allany_subselect::select_transformer(
JOIN *join)
2513 DBUG_ENTER(
"Item_allany_subselect::select_transformer");
2515 upper_item->show= 1;
2517 DBUG_RETURN(retval);
2524 str->append(STRING_WITH_LEN(
"<exists>"));
2527 left_expr->
print(str, query_type);
2529 str->append(func->symbol(all));
2530 str->append(all ?
" all " :
" any ", 5);
2543 result->set_thd(item->unit->thd);
2547 subselect_single_select_engine::
2548 subselect_single_select_engine(st_select_lex *select,
2549 select_result_interceptor *result_arg,
2552 prepared(0), executed(0), optimize_error(0), select_lex(select), join(0)
2554 select_lex->master_unit()->item= item_arg;
2560 DBUG_ENTER(
"subselect_single_select_engine::cleanup");
2561 prepared= executed= optimize_error=
false;
2570 DBUG_ENTER(
"subselect_union_engine::cleanup");
2576 bool subselect_union_engine::is_executed()
const
2578 return unit->executed;
2582 subselect_union_engine::subselect_union_engine(st_select_lex_unit *u,
2583 select_result_interceptor *result_arg,
2588 unit->item= item_arg;
2622 THD *
const thd= item->unit->thd;
2623 join=
new JOIN(thd, select_lex->item_list,
2624 select_lex->options | SELECT_NO_UNLOCK, result);
2625 if (!join || !result)
2628 SELECT_LEX *save_select= thd->lex->current_select;
2629 thd->lex->current_select= select_lex;
2630 if (join->
prepare(select_lex->table_list.first,
2631 select_lex->with_wild,
2633 select_lex->order_list.elements +
2634 select_lex->group_list.elements,
2635 select_lex->order_list.first,
2636 select_lex->group_list.first,
2639 select_lex->master_unit()))
2641 thd->lex->current_select= save_select;
2646 bool subselect_union_engine::prepare()
2648 THD *
const thd= unit->thd;
2650 DBUG_ASSERT(thd == item->unit->thd);
2651 return unit->prepare(thd, result, SELECT_NO_UNLOCK);
2655 bool subselect_indexsubquery_engine::prepare()
2671 res_type= STRING_RESULT;
2672 res_field_type= MYSQL_TYPE_VAR_STRING;
2673 for (uint i= 0; (sel_item= li++); i++)
2675 item->max_length= sel_item->max_length;
2676 res_type= sel_item->result_type();
2677 res_field_type= sel_item->field_type();
2678 item->decimals= sel_item->decimals;
2679 item->unsigned_flag= sel_item->unsigned_flag;
2680 maybe_null= sel_item->maybe_null;
2681 if (!(row[i]= Item_cache::get_cache(sel_item)))
2683 row[
i]->setup(sel_item);
2684 row[
i]->store(sel_item);
2686 if (item_list.elements > 1)
2687 res_type= ROW_RESULT;
2690 void subselect_single_select_engine::fix_length_and_dec(
Item_cache **row)
2692 DBUG_ASSERT(row || select_lex->item_list.elements==1);
2693 set_row(select_lex->item_list, row);
2694 item->collation.set(row[0]->collation);
2699 void subselect_union_engine::fix_length_and_dec(
Item_cache **row)
2701 DBUG_ASSERT(row || unit->first_select()->item_list.elements==1);
2703 if (unit->first_select()->item_list.elements == 1)
2705 set_row(unit->types, row);
2706 item->collation.set(row[0]->collation);
2710 bool maybe_null_saved= maybe_null;
2711 set_row(unit->types, row);
2712 maybe_null= maybe_null_saved;
2716 void subselect_indexsubquery_engine::fix_length_and_dec(
Item_cache **row)
2722 int read_first_record_seq(
JOIN_TAB *tab);
2725 bool subselect_single_select_engine::exec()
2727 DBUG_ENTER(
"subselect_single_select_engine::exec");
2733 THD *
const thd= item->unit->thd;
2734 char const *save_where= thd->where;
2735 SELECT_LEX *save_select= thd->lex->current_select;
2736 thd->lex->current_select= select_lex;
2739 SELECT_LEX_UNIT *unit= select_lex->master_unit();
2741 unit->set_limit(unit->global_parameters);
2743 DBUG_EXECUTE_IF(
"bug11747970_simulate_error",
2744 DBUG_SET(
"+d,bug11747970_raise_error"););
2748 optimize_error=
true;
2752 if (item->engine_changed)
2758 if (select_lex->uncacheable &&
2764 item->assigned((executed= 0));
2768 item->reset_value_registration();
2770 JOIN_TAB **last_changed_tab= changed_tabs;
2771 if (item->have_guarded_conds())
2779 for (uint i= join->
const_tables; i < join->primary_tables; i++)
2784 for (uint i= 0; i < tab->ref.
key_parts; i++)
2786 bool *cond_guard= tab->ref.cond_guards[
i];
2787 if (cond_guard && !*cond_guard)
2803 tab->save_read_record= tab->read_record.read_record;
2804 tab->read_record.read_record= rr_sequential;
2806 tab->read_record.record= tab->table->record[0];
2807 tab->read_record.thd= join->thd;
2808 tab->read_record.ref_length= tab->table->file->
ref_length;
2810 *(last_changed_tab++)= tab;
2821 for (
JOIN_TAB **ptab= changed_tabs; ptab != last_changed_tab; ptab++)
2824 tab->read_record.record= 0;
2825 tab->read_record.ref_length= 0;
2827 tab->read_record.read_record= tab->save_read_record;
2828 tab->save_read_first_record= NULL;
2832 rc= join->
error || thd->is_fatal_error;
2836 thd->where= save_where;
2837 thd->lex->current_select= save_select;
2841 bool subselect_union_engine::exec()
2843 THD *
const thd= unit->thd;
2844 DBUG_ASSERT(thd == item->unit->thd);
2845 char const *save_where= thd->where;
2846 const bool res= (unit->optimize() || unit->exec());
2847 thd->where= save_where;
2865 DBUG_ENTER(
"subselect_indexsubquery_engine::scan_table");
2868 DBUG_ASSERT(engine_type() != HASH_SJ_ENGINE);
2870 if ((table->file->inited &&
2878 table->file->extra_opt(HA_EXTRA_CACHE,
2879 current_thd->variables.read_buff_size);
2884 if (error && error != HA_ERR_END_OF_FILE)
2893 if (!cond || cond->val_int())
2901 DBUG_RETURN(error != 0);
2953 bool *convert_error)
2955 DBUG_ENTER(
"subselect_indexsubquery_engine::copy_ref_key");
2957 *require_scan=
false;
2958 *convert_error=
false;
2959 for (uint part_no= 0; part_no < tab->ref.
key_parts; part_no++)
2965 const enum store_key::store_key_result store_res= s_key->
copy();
2966 tab->ref.key_err= store_res;
2968 if (s_key->null_key)
2977 DBUG_ASSERT(engine_type() != HASH_SJ_ENGINE);
2979 const bool *cond_guard= tab->ref.cond_guards[part_no];
2987 if (cond_guard && !*cond_guard)
2989 DBUG_ASSERT(!(static_cast <Item_in_subselect*>(item)
2990 ->is_top_level_item()));
2992 *require_scan=
true;
3005 if (store_res == store_key::STORE_KEY_FATAL)
3011 tab->table->status= STATUS_NOT_FOUND;
3012 *convert_error=
true;
3074 bool subselect_indexsubquery_engine::exec()
3076 DBUG_ENTER(
"subselect_indexsubquery_engine::exec");
3078 bool null_finding= 0;
3083 item_in->value=
false;
3088 bool err= mysql_handle_single_derived(table->in_use->lex, tl,
3089 mysql_derived_create) ||
3090 mysql_handle_single_derived(table->in_use->lex, tl,
3091 mysql_derived_materialize);
3092 if (!tab->table->in_use->lex->describe)
3093 mysql_handle_single_derived(table->in_use->lex, tl,
3094 mysql_derived_cleanup);
3104 *tab->ref.null_ref_key= 0;
3105 item_in->was_null=
false;
3109 bool require_scan, convert_error;
3117 DBUG_RETURN(scan_result);
3120 if (!table->file->inited &&
3128 make_prev_keypart_map(tab->ref.
key_parts),
3131 error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3141 if ((!cond || cond->val_int()) && (!having || having->val_int()))
3143 item_in->value=
true;
3152 item_in->was_null=
true;
3161 if (error && error != HA_ERR_END_OF_FILE)
3169 if (!check_null || null_finding)
3177 *tab->ref.null_ref_key= 1;
3179 if ((error= (safe_index_read(tab) == 1)))
3184 DBUG_RETURN(error != 0);
3188 uint subselect_single_select_engine::cols()
const
3190 return select_lex->item_list.elements;
3194 uint subselect_union_engine::cols()
const
3196 DBUG_ASSERT(unit->is_prepared());
3197 return unit->types.elements;
3201 uint8 subselect_single_select_engine::uncacheable()
const
3203 return select_lex->uncacheable;
3207 uint8 subselect_union_engine::uncacheable()
const
3209 return unit->uncacheable;
3213 void subselect_single_select_engine::exclude()
3215 select_lex->master_unit()->exclude_level();
3218 void subselect_union_engine::exclude()
3220 unit->exclude_level();
3224 void subselect_indexsubquery_engine::exclude()
3231 table_map subselect_engine::calc_const_tables(
TABLE_LIST *table)
3234 for (;
table; table= table->next_leaf)
3236 TABLE *tbl= table->table;
3237 if (tbl && tbl->const_table)
3244 table_map subselect_single_select_engine::upper_select_const_tables()
const
3246 return calc_const_tables(select_lex->outer_select()->leaf_tables);
3250 table_map subselect_union_engine::upper_select_const_tables()
const
3252 return calc_const_tables(unit->outer_select()->leaf_tables);
3256 void subselect_single_select_engine::print(
String *str,
3257 enum_query_type query_type)
3259 select_lex->print(item->unit->thd, str, query_type);
3263 void subselect_union_engine::print(
String *str, enum_query_type query_type)
3265 unit->print(str, query_type);
3293 void subselect_indexsubquery_engine::print(
String *str,
3294 enum_query_type query_type)
3297 str->append(STRING_WITH_LEN(
"<primary_index_lookup>("));
3299 str->append(STRING_WITH_LEN(
"<index_lookup>("));
3301 str->append(STRING_WITH_LEN(
" in "));
3302 if (tab->table->pos_in_table_list &&
3310 str->append(tab->table->alias, strlen(tab->table->alias));
3312 else if (tab->table->s->
table_category == TABLE_CATEGORY_TEMPORARY)
3315 str->append(STRING_WITH_LEN(
"<temporary table>"));
3318 str->append(tab->table->s->table_name.str, tab->table->s->table_name.length);
3319 KEY *key_info= tab->table->key_info+ tab->ref.
key;
3320 str->append(STRING_WITH_LEN(
" on "));
3321 str->append(key_info->
name);
3323 str->append(STRING_WITH_LEN(
" checking NULL"));
3326 str->append(STRING_WITH_LEN(
" where "));
3327 cond->
print(str, query_type);
3331 str->append(STRING_WITH_LEN(
" having "));
3332 having->
print(str, query_type);
3350 select_result_interceptor *res)
3354 return select_lex->join->change_result(result);
3371 select_result_interceptor *res)
3374 int rc= unit->change_result(res, result);
3393 select_result_interceptor *res)
3410 return(select_lex->table_list.elements == 0);
3424 bool subselect_single_select_engine::may_be_null()
const
3440 for (SELECT_LEX *sl= unit->first_select(); sl; sl= sl->next_select())
3442 if (sl->table_list.elements)
3495 select_union *tmp_result_sink;
3502 DBUG_ENTER(
"subselect_hash_sj_engine::setup");
3511 if (!(tmp_result_sink=
new select_union))
3513 THD *
const thd= item->unit->thd;
3514 if (tmp_result_sink->create_result_table(
3515 thd, tmp_columns,
true,
3516 thd->variables.option_bits | TMP_TABLE_ALL_COLUMNS,
3517 "materialized-subquery",
true,
true))
3520 tmp_table= tmp_result_sink->table;
3521 tmp_key= tmp_table->key_info;
3531 if (tmp_table->s->keys == 0)
3533 DBUG_ASSERT(tmp_table->s->db_type() == myisam_hton);
3535 tmp_table->s->uniques ||
3536 tmp_table->key_info->
key_length >= tmp_table->file->max_key_length() ||
3538 tmp_table->file->max_key_parts());
3539 free_tmp_table(thd, tmp_table);
3544 result= tmp_result_sink;
3550 DBUG_ASSERT(tmp_table->s->keys == 1 &&
3551 tmp_columns->elements == tmp_key_parts);
3563 if (tmp_tab == NULL)
3565 tmp_tab->table= tmp_table;
3566 tmp_tab->ref.
key= 0;
3569 (uchar*) thd->calloc(ALIGN_SIZE(tmp_key->
key_length) * 2)) ||
3572 !(tmp_tab->ref.
items=
3573 (
Item**) thd->alloc(
sizeof(
Item*) * tmp_key_parts)))
3576 uchar *cur_ref_buff= tmp_tab->ref.
key_buff;
3591 DBUG_ASSERT(cond == NULL);
3602 tmp_table_ref->
init_one_table(
"", 0,
"materialized-subquery", 21,
3603 "materialized-subquery", TL_READ);
3604 tmp_table_ref->table= tmp_table;
3609 context->first_name_resolution_table=
3610 context->last_name_resolution_table= tmp_table_ref;
3613 for (uint part_no= 0; part_no < tmp_key_parts; part_no++)
3620 tmp_tab->ref.
items[part_no]= item_in->left_expr->element_index(part_no);
3622 if (!(right_col_item=
new Item_field(thd, context,
3623 key_parts[part_no].field)) ||
3641 cur_ref_buff + (nullable ? 1 : 0),
3642 nullable ? cur_ref_buff : 0,
3643 key_parts[part_no].length,
3644 tmp_tab->ref.
items[part_no]);
3647 !item_in->is_top_level_item())
3650 DBUG_ASSERT(tmp_key_parts == 1);
3652 tmp_tab->ref.null_ref_key= cur_ref_buff;
3653 mat_table_has_nulls= NEX_UNKNOWN;
3657 tmp_tab->ref.null_ref_key= NULL;
3658 mat_table_has_nulls= NEX_IRRELEVANT_OR_FALSE;
3661 cur_ref_buff+= key_parts[part_no].store_length;
3663 tmp_tab->ref.key_err= 1;
3666 if (cond->fix_fields(thd, &cond))
3676 materialize_engine->
prepare();
3684 subselect_hash_sj_engine::~subselect_hash_sj_engine()
3702 DBUG_ENTER(
"subselect_hash_sj_engine::cleanup");
3703 is_materialized=
false;
3705 THD *
const thd= item->unit->thd;
3706 DEBUG_SYNC(thd,
"before_index_end_in_subselect");
3707 if (tab->table->file->inited)
3709 free_tmp_table(thd, tab->table);
3711 materialize_engine->
cleanup();
3730 TABLE *
const table= tab->table;
3731 DBUG_ENTER(
"subselect_hash_sj_engine::exec");
3737 if (!is_materialized)
3740 THD *
const thd= item->unit->thd;
3741 SELECT_LEX *save_select= thd->lex->current_select;
3742 thd->lex->current_select= materialize_engine->select_lex;
3743 if ((res= materialize_engine->join->
optimize()))
3746 materialize_engine->join->
exec();
3747 if ((res=
test(materialize_engine->join->
error || thd->is_fatal_error)))
3759 is_materialized= TRUE;
3762 table->file->info(HA_STATUS_VARIABLE);
3765 tmp_param= &(item_in->unit->outer_select()->join->tmp_table_param);
3766 if (tmp_param && !tmp_param->copy_field)
3770 thd->lex->current_select= save_select;
3775 if (table->file->stats.records == 0)
3778 item_in->value=
false;
3787 if (item_in->left_expr->element_index(0)->null_value)
3798 DBUG_ASSERT(item_in->left_expr->cols() == 1);
3799 item_in->value=
true;
3803 if (subselect_indexsubquery_engine::exec())
3806 if (!item_in->value &&
3807 mat_table_has_nulls != NEX_IRRELEVANT_OR_FALSE)
3814 if (mat_table_has_nulls == NEX_UNKNOWN)
3817 *tab->ref.null_ref_key= 1;
3818 if (!table->file->inited &&
3821 if (safe_index_read(tab) == 1)
3823 *tab->ref.null_ref_key= 0;
3824 mat_table_has_nulls=
3825 (table->status == 0) ? NEX_TRUE : NEX_IRRELEVANT_OR_FALSE;
3827 if (mat_table_has_nulls == NEX_TRUE)
3835 item_in->value=
false;
3836 item_in->was_null=
true;
3849 str->append(STRING_WITH_LEN(
" <materialize> ("));
3850 materialize_engine->print(str, query_type);
3851 str->append(STRING_WITH_LEN(
" ), "));
3855 str->append(STRING_WITH_LEN(
3856 "<the access method for lookups is not yet created>"