Skip to content

Commit d251ced

Browse files
committed
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.
1 parent 8c8028c commit d251ced

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

mysql-test/r/cte_nonrecursive.result

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,3 +1425,40 @@ a
14251425
DEALLOCATE PREPARE stmt;
14261426
DROP TABLE t1;
14271427
DROP VIEW v1,v2;
1428+
#
1429+
# MDEV-15478: Lost name of a explicitly named CTE column used in
1430+
# the non-recursive CTE defined with UNION
1431+
#
1432+
CREATE TABLE t1 (x int, y int);
1433+
INSERT INTO t1 VALUES (1,2),(2,7),(3,3);
1434+
WITH cte(a) AS (SELECT 1 UNION SELECT 2) SELECT * FROM cte;
1435+
a
1436+
1
1437+
2
1438+
WITH cte(a) AS (SELECT 1 UNION SELECT 2) SELECT a FROM cte;
1439+
a
1440+
1
1441+
2
1442+
WITH cte(a) AS (SELECT 1 UNION ALL SELECT 1) SELECT a FROM cte;
1443+
a
1444+
1
1445+
1
1446+
WITH cte(a) AS (SELECT x from t1 UNION SELECT 4) SELECT a FROM cte;
1447+
a
1448+
1
1449+
2
1450+
3
1451+
4
1452+
WITH cte(a) AS (SELECT 4 UNION SELECT x FROM t1 UNION SELECT 5)
1453+
SELECT a FROM cte;
1454+
a
1455+
4
1456+
1
1457+
2
1458+
3
1459+
5
1460+
WITH cte(a,b) AS (SELECT 4,5 UNION SELECT 4,3) SELECT a,b FROM cte;
1461+
a b
1462+
4 5
1463+
4 3
1464+
DROP TABLE t1;

mysql-test/t/cte_nonrecursive.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,3 +989,26 @@ DEALLOCATE PREPARE stmt;
989989

990990
DROP TABLE t1;
991991
DROP VIEW v1,v2;
992+
993+
--echo #
994+
--echo # MDEV-15478: Lost name of a explicitly named CTE column used in
995+
--echo # the non-recursive CTE defined with UNION
996+
--echo #
997+
998+
CREATE TABLE t1 (x int, y int);
999+
INSERT INTO t1 VALUES (1,2),(2,7),(3,3);
1000+
1001+
WITH cte(a) AS (SELECT 1 UNION SELECT 2) SELECT * FROM cte;
1002+
1003+
WITH cte(a) AS (SELECT 1 UNION SELECT 2) SELECT a FROM cte;
1004+
1005+
WITH cte(a) AS (SELECT 1 UNION ALL SELECT 1) SELECT a FROM cte;
1006+
1007+
WITH cte(a) AS (SELECT x from t1 UNION SELECT 4) SELECT a FROM cte;
1008+
1009+
WITH cte(a) AS (SELECT 4 UNION SELECT x FROM t1 UNION SELECT 5)
1010+
SELECT a FROM cte;
1011+
1012+
WITH cte(a,b) AS (SELECT 4,5 UNION SELECT 4,3) SELECT a,b FROM cte;
1013+
1014+
DROP TABLE t1;

sql/sql_union.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result,
598598
types= first_sl->item_list;
599599
else if (sl == first_sl)
600600
{
601-
if (is_recursive)
601+
if (with_element)
602602
{
603603
if (derived->with->rename_columns_of_derived_unit(thd, this))
604604
goto err;

0 commit comments

Comments
 (0)