From 1951e7f05ae7b6069eeffdfe8ab304fa3a18a85a Mon Sep 17 00:00:00 2001 From: Igor Babaev Date: Wed, 31 Jan 2018 10:15:17 -0800 Subject: [PATCH] Fixed mdev-15120 CTE table should not belong to database, that is in use When identifying a table name the following should be taken into account: a CTE name cannot be qualified with a database name, otherwise the table name is considered as the name of a non-CTE table. --- mysql-test/r/cte_nonrecursive.result | 10 ++++++++++ mysql-test/t/cte_nonrecursive.test | 15 +++++++++++++++ sql/sql_cte.cc | 3 ++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/cte_nonrecursive.result b/mysql-test/r/cte_nonrecursive.result index b1fd287f1d756..7bd17d272bd95 100644 --- a/mysql-test/r/cte_nonrecursive.result +++ b/mysql-test/r/cte_nonrecursive.result @@ -1369,3 +1369,13 @@ n_nationkey n_name n_regionkey r_regionkey r_name 23 UNITED KINGDOM 3 3 EUROPE drop view v; drop table region, nation; +# +# MDEV-15120: cte name used with database name +# +WITH cte AS (SELECT 1 AS a) SELECT test.cte.a FROM test.cte; +ERROR 42S02: Table 'test.cte' doesn't exist +CREATE DATABASE db1; +USE db1; +WITH cte AS (SELECT 1 AS a) SELECT db1.cte.a FROM db1.cte; +ERROR 42S02: Table 'db1.cte' doesn't exist +DROP DATABASE db1; diff --git a/mysql-test/t/cte_nonrecursive.test b/mysql-test/t/cte_nonrecursive.test index a092a16127793..1af91dd38c05b 100644 --- a/mysql-test/t/cte_nonrecursive.test +++ b/mysql-test/t/cte_nonrecursive.test @@ -929,3 +929,18 @@ select * from v; drop view v; drop table region, nation; + +--echo # +--echo # MDEV-15120: cte name used with database name +--echo # + +--error ER_NO_SUCH_TABLE +WITH cte AS (SELECT 1 AS a) SELECT test.cte.a FROM test.cte; + +CREATE DATABASE db1; +USE db1; + +--error ER_NO_SUCH_TABLE +WITH cte AS (SELECT 1 AS a) SELECT db1.cte.a FROM db1.cte; + +DROP DATABASE db1; diff --git a/sql/sql_cte.cc b/sql/sql_cte.cc index 9ff81a47d3225..a67337433fa68 100644 --- a/sql/sql_cte.cc +++ b/sql/sql_cte.cc @@ -240,7 +240,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.str) == 0) + table->table_name.str) == 0 && + !table->is_fqtn) { table->set_derived(); return with_elem;