diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 5f941939fa2..38d1526f917 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -2436,16 +2436,6 @@ bool Item_in_subselect::setup_mat_engine() if (mat_engine->init(&select_engine->join->fields_list)) DBUG_RETURN(TRUE); - /* - Reset the "LIMIT 1" set in Item_exists_subselect::fix_length_and_dec. - TODO: - Currently we set the subquery LIMIT to infinity, and this is correct - because we forbid at parse time LIMIT inside IN subqueries (see - Item_in_subselect::test_limit). However, once we allow this, here - we should set the correct limit if given in the query. - */ - unit->global_parameters->select_limit= NULL; - engine= mat_engine; DBUG_RETURN(FALSE); } diff --git a/sql/opt_subselect.cc b/sql/opt_subselect.cc index 46d8591aaf1..e5e21976d84 100644 --- a/sql/opt_subselect.cc +++ b/sql/opt_subselect.cc @@ -3566,14 +3566,29 @@ bool JOIN::choose_subquery_plan() else in_subs->exec_method= Item_in_subselect::IN_TO_EXISTS; - if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION) + if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION) { + /* TODO: should we set/unset this flag for both select_lex and its unit? */ + in_subs->unit->uncacheable&= ~UNCACHEABLE_DEPENDENT; + select_lex->uncacheable&= ~UNCACHEABLE_DEPENDENT; - // TODO: should we unset the UNCACHEABLE_DEPENDENT flag fro - // select_lex->uncacheable; ? - // This affects how we execute JOIN::join_free - full or not. - // inner_join->restore_plan (keyuse, best_positions, best_read) - ; + /* + Reset the "LIMIT 1" set in Item_exists_subselect::fix_length_and_dec. + TODO: + Currently we set the subquery LIMIT to infinity, and this is correct + because we forbid at parse time LIMIT inside IN subqueries (see + Item_in_subselect::test_limit). However, once we allow this, here + we should set the correct limit if given in the query. + */ + in_subs->unit->global_parameters->select_limit= NULL; + in_subs->unit->set_limit(unit->global_parameters); + /* + Set the limit of this JOIN object as well, because normally its being + set in the beginning of JOIN::optimize, which was already done. + */ + select_limit= in_subs->unit->select_limit_cnt; + + // TODO: inner_join->restore_plan (keyuse, best_positions, best_read) } else if (in_subs->exec_method == Item_in_subselect::IN_TO_EXISTS) res= in_subs->inject_in_to_exists_cond(this); diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 69a0e9c6e14..d9ceb431bb3 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -3115,6 +3115,7 @@ bool st_select_lex::optimize_unflattened_subqueries() DBUG_ASSERT(!item_in || (item_in && !item_in->is_min_max_optimized)); if (item_in && item_in->create_in_to_exists_cond(inner_join)) return TRUE; + /* We need only 1 row to determine existence */ un->set_limit(un->global_parameters); un->thd->lex->current_select= sl; res= inner_join->optimize();