summaryrefslogtreecommitdiff
path: root/sql/sql_cte.cc
diff options
context:
space:
mode:
Diffstat (limited to 'sql/sql_cte.cc')
-rw-r--r--sql/sql_cte.cc31
1 files changed, 18 insertions, 13 deletions
diff --git a/sql/sql_cte.cc b/sql/sql_cte.cc
index d2298619f63..2fdd493b45d 100644
--- a/sql/sql_cte.cc
+++ b/sql/sql_cte.cc
@@ -223,7 +223,8 @@ With_element *With_clause::find_table_def(TABLE_LIST *table,
with_elem= with_elem->next)
{
if (my_strcasecmp(system_charset_info, with_elem->query_name->str,
- table->table_name) == 0)
+ table->table_name) == 0 &&
+ !table->is_fqtn)
{
table->set_derived();
return with_elem;
@@ -812,7 +813,7 @@ st_select_lex_unit *With_element::clone_parsed_spec(THD *thd,
goto err;
lex_start(thd);
with_select= &lex->select_lex;
- with_select->select_number= ++thd->select_number;
+ with_select->select_number= ++thd->stmt_lex->current_select_number;
parse_status= parse_sql(thd, &parser_state, 0);
if (parse_status)
goto err;
@@ -988,23 +989,20 @@ bool With_element::is_anchor(st_select_lex *sel)
With_element *st_select_lex::find_table_def_in_with_clauses(TABLE_LIST *table)
{
- st_select_lex_unit *master_unit= NULL;
With_element *found= NULL;
- for (st_select_lex *sl= this;
- sl;
- sl= master_unit->outer_select())
+ st_select_lex_unit *master_unit;
+ st_select_lex *outer_sl;
+ for (st_select_lex *sl= this; sl; sl= outer_sl)
{
- With_element *with_elem= sl->get_with_element();
/*
If sl->master_unit() is the spec of a with element then the search for
a definition was already done by With_element::check_dependencies_in_spec
and it was unsuccesful. Yet for units cloned from the spec it has not
been done yet.
*/
- With_clause *attached_with_clause=sl->get_with_clause();
- if (attached_with_clause &&
- (found= attached_with_clause->find_table_def(table, NULL)))
- break;
+ master_unit= sl->master_unit();
+ outer_sl= master_unit->outer_select();
+ With_element *with_elem= sl->get_with_element();
if (with_elem)
{
With_clause *containing_with_clause= with_elem->get_owner();
@@ -1012,9 +1010,16 @@ With_element *st_select_lex::find_table_def_in_with_clauses(TABLE_LIST *table)
NULL : with_elem;
if ((found= containing_with_clause->find_table_def(table, barrier)))
break;
- sl= sl->master_unit()->outer_select();
+ if (outer_sl && !outer_sl->get_with_element())
+ break;
+ }
+ else
+ {
+ With_clause *attached_with_clause= sl->get_with_clause();
+ if (attached_with_clause &&
+ (found= attached_with_clause->find_table_def(table, NULL)))
+ break;
}
- master_unit= sl->master_unit();
/* Do not look for the table's definition beyond the scope of the view */
if (master_unit->is_view)
break;