From d251cedd8d20a3086bb593996bf3af4346e58f12 Mon Sep 17 00:00:00 2001 From: Galina Shalygina Date: Tue, 13 Mar 2018 02:53:48 +0200 Subject: [PATCH] MDEV-15478: Lost name of a explicitly named CTE column used in the non-recursive CTE defined with UNION The problem appears as the columns of the non-recursive CTE weren't renamed. The renaming procedure was called for recursive CTEs only. To fix it in the procedure st_select_lex_unit::prepare With_element::rename_columns_of_derived_unit is called now for both CTEs: recursive and non-recursive. --- mysql-test/r/cte_nonrecursive.result | 37 ++++++++++++++++++++++++++++ mysql-test/t/cte_nonrecursive.test | 23 +++++++++++++++++ sql/sql_union.cc | 2 +- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/cte_nonrecursive.result b/mysql-test/r/cte_nonrecursive.result index f2bfce9f6ba..001df909bcf 100644 --- a/mysql-test/r/cte_nonrecursive.result +++ b/mysql-test/r/cte_nonrecursive.result @@ -1425,3 +1425,40 @@ a DEALLOCATE PREPARE stmt; DROP TABLE t1; DROP VIEW v1,v2; +# +# MDEV-15478: Lost name of a explicitly named CTE column used in +# the non-recursive CTE defined with UNION +# +CREATE TABLE t1 (x int, y int); +INSERT INTO t1 VALUES (1,2),(2,7),(3,3); +WITH cte(a) AS (SELECT 1 UNION SELECT 2) SELECT * FROM cte; +a +1 +2 +WITH cte(a) AS (SELECT 1 UNION SELECT 2) SELECT a FROM cte; +a +1 +2 +WITH cte(a) AS (SELECT 1 UNION ALL SELECT 1) SELECT a FROM cte; +a +1 +1 +WITH cte(a) AS (SELECT x from t1 UNION SELECT 4) SELECT a FROM cte; +a +1 +2 +3 +4 +WITH cte(a) AS (SELECT 4 UNION SELECT x FROM t1 UNION SELECT 5) +SELECT a FROM cte; +a +4 +1 +2 +3 +5 +WITH cte(a,b) AS (SELECT 4,5 UNION SELECT 4,3) SELECT a,b FROM cte; +a b +4 5 +4 3 +DROP TABLE t1; diff --git a/mysql-test/t/cte_nonrecursive.test b/mysql-test/t/cte_nonrecursive.test index 3d073183877..5e1770496f6 100644 --- a/mysql-test/t/cte_nonrecursive.test +++ b/mysql-test/t/cte_nonrecursive.test @@ -989,3 +989,26 @@ DEALLOCATE PREPARE stmt; DROP TABLE t1; DROP VIEW v1,v2; + +--echo # +--echo # MDEV-15478: Lost name of a explicitly named CTE column used in +--echo # the non-recursive CTE defined with UNION +--echo # + +CREATE TABLE t1 (x int, y int); +INSERT INTO t1 VALUES (1,2),(2,7),(3,3); + +WITH cte(a) AS (SELECT 1 UNION SELECT 2) SELECT * FROM cte; + +WITH cte(a) AS (SELECT 1 UNION SELECT 2) SELECT a FROM cte; + +WITH cte(a) AS (SELECT 1 UNION ALL SELECT 1) SELECT a FROM cte; + +WITH cte(a) AS (SELECT x from t1 UNION SELECT 4) SELECT a FROM cte; + +WITH cte(a) AS (SELECT 4 UNION SELECT x FROM t1 UNION SELECT 5) +SELECT a FROM cte; + +WITH cte(a,b) AS (SELECT 4,5 UNION SELECT 4,3) SELECT a,b FROM cte; + +DROP TABLE t1; diff --git a/sql/sql_union.cc b/sql/sql_union.cc index 524f6eb9c8d..19c9330481f 100644 --- a/sql/sql_union.cc +++ b/sql/sql_union.cc @@ -598,7 +598,7 @@ bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, types= first_sl->item_list; else if (sl == first_sl) { - if (is_recursive) + if (with_element) { if (derived->with->rename_columns_of_derived_unit(thd, this)) goto err;