summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mysql-test/r/cte_nonrecursive.result12
-rw-r--r--mysql-test/t/cte_nonrecursive.test12
-rw-r--r--sql/sql_cte.cc6
3 files changed, 30 insertions, 0 deletions
diff --git a/mysql-test/r/cte_nonrecursive.result b/mysql-test/r/cte_nonrecursive.result
index 71bf617df0a..bd681dbb343 100644
--- a/mysql-test/r/cte_nonrecursive.result
+++ b/mysql-test/r/cte_nonrecursive.result
@@ -927,3 +927,15 @@ Sergei Golubchik Development DE
Claudio Nanni Support ES
Sergei Petrunia Development RU
drop table employees;
+#
+# MDEV-11818: EXPLAIN EXTENDED for a query with optimized away CTE table
+#
+CREATE TABLE t1 (i INT, c VARCHAR(3));
+INSERT INTO t1 VALUES (1,'foo');
+EXPLAIN EXTENDED
+WITH cte AS ( SELECT * FROM t1 ) SELECT i FROM cte;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 system NULL NULL NULL NULL 1 100.00
+Warnings:
+Note 1003 with cte as (select `test`.`t1`.`i` AS `i`,`test`.`t1`.`c` AS `c` from `test`.`t1`)select 1 AS `i` from dual
+DROP TABLE t1;
diff --git a/mysql-test/t/cte_nonrecursive.test b/mysql-test/t/cte_nonrecursive.test
index 8dbde472c16..b3a127475f2 100644
--- a/mysql-test/t/cte_nonrecursive.test
+++ b/mysql-test/t/cte_nonrecursive.test
@@ -609,3 +609,15 @@ where
and T2.name <> T1.name);
drop table employees;
+
+--echo #
+--echo # MDEV-11818: EXPLAIN EXTENDED for a query with optimized away CTE table
+--echo #
+
+CREATE TABLE t1 (i INT, c VARCHAR(3));
+INSERT INTO t1 VALUES (1,'foo');
+
+EXPLAIN EXTENDED
+WITH cte AS ( SELECT * FROM t1 ) SELECT i FROM cte;
+
+DROP TABLE t1;
diff --git a/sql/sql_cte.cc b/sql/sql_cte.cc
index a4ceae52e5e..42cc20647a4 100644
--- a/sql/sql_cte.cc
+++ b/sql/sql_cte.cc
@@ -1246,6 +1246,12 @@ bool st_select_lex::check_subqueries_with_recursive_references()
void With_clause::print(String *str, enum_query_type query_type)
{
+ /*
+ Any with clause contains just definitions of CTE tables.
+ No data expansion is applied to these definitions.
+ */
+ query_type= (enum_query_type) (query_type | QT_NO_DATA_EXPANSION);
+
str->append(STRING_WITH_LEN("with "));
if (with_recursive)
str->append(STRING_WITH_LEN("recursive "));