summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
Diffstat (limited to 'sql')
-rw-r--r--sql/sql_derived.cc31
-rw-r--r--sql/sql_parse.cc4
2 files changed, 34 insertions, 1 deletions
diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc
index be5905da683..5a85b7ea7e3 100644
--- a/sql/sql_derived.cc
+++ b/sql/sql_derived.cc
@@ -563,6 +563,32 @@ bool mysql_derived_init(THD *thd, LEX *lex, TABLE_LIST *derived)
/*
+ @brief
+ Prevent name resolution out of context of ON expressions in derived tables
+
+ @param
+ join_list list of tables used in from list of a derived
+
+ @details
+ The function sets the Name_resolution_context::outer_context to NULL
+ for all ON expressions contexts in the given join list. It does this
+ recursively for all nested joins the list contains.
+*/
+
+static void nullify_outer_context_for_on_clauses(List<TABLE_LIST>& join_list)
+{
+ List_iterator<TABLE_LIST> li(join_list);
+ while (TABLE_LIST *table= li++)
+ {
+ if (table->on_context)
+ table->on_context->outer_context= NULL;
+ if (table->nested_join)
+ nullify_outer_context_for_on_clauses(table->nested_join->join_list);
+ }
+}
+
+
+/*
Create temporary table structure (but do not fill it)
@param thd Thread handle
@@ -695,7 +721,12 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
/* prevent name resolving out of derived table */
for (SELECT_LEX *sl= first_select; sl; sl= sl->next_select())
{
+ // Prevent it for the WHERE clause
sl->context.outer_context= 0;
+
+ // And for ON clauses, if there are any
+ nullify_outer_context_for_on_clauses(*sl->join_list);
+
if (!derived->is_with_table_recursive_reference() ||
(!derived->with->with_anchor &&
!derived->with->is_with_prepared_anchor()))
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index 573df24cb33..9436e111043 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -7418,6 +7418,7 @@ mysql_new_select(LEX *lex, bool move_down)
DBUG_RETURN(1);
select_lex->select_number= ++thd->lex->stmt_lex->current_select_number;
select_lex->parent_lex= lex; /* Used in init_query. */
+ Name_resolution_context *curr_context= lex->context_stack.head();
select_lex->init_query();
select_lex->init_select();
lex->nest_level++;
@@ -7448,7 +7449,8 @@ mysql_new_select(LEX *lex, bool move_down)
By default we assume that it is usual subselect and we have outer name
resolution context, if no we will assign it to 0 later
*/
- select_lex->context.outer_context= &select_lex->outer_select()->context;
+
+ select_lex->context.outer_context= curr_context;
}
else
{