[MDEV-24597] Explicit column name error in CTE of UNION Created: 2021-01-15  Updated: 2021-03-11  Resolved: 2021-03-11

Status: Closed
Project: MariaDB Server
Component/s: Optimizer - CTE
Affects Version/s: 10.4.12, 10.2, 10.3, 10.4, 10.5
Fix Version/s: 10.2.38, 10.3.29, 10.4.19, 10.5.10

Type: Bug Priority: Major
Reporter: Tarrin Wills Assignee: Igor Babaev
Resolution: Fixed Votes: 0
Labels: CTE, union

Issue Links:
Relates
relates to MDEV-15478 Explicit column name error in CTE of ... Closed

 Description   

The bug described in MDEV-15478 is not fully resolved. When the CTE is used again with UNION in the main statement, it gives error #1054 - Unknown column 'col1' in 'field list'

WITH cte (col1) AS
(
  SELECT 1
  UNION
  SELECT 2
)
SELECT col1 FROM cte
-- union statement added to example in MDEV-15478
UNION
SELECT col1 FROM cte

This can currently be worked around with:

WITH cte (col1) AS
(
  SELECT 1 AS col1 -- alias required
  UNION
  SELECT 2
)
SELECT col1 FROM cte
UNION
SELECT col1 FROM cte



 Comments   
Comment by Alice Sherepa [ 2021-01-15 ]

Thanks for the report!

MariaDB [test]>  WITH cte (col1) AS (SELECT 1 UNION SELECT 2) SELECT col1 FROM cte UNION SELECT col1 FROM cte;
ERROR 1054 (42S22): Unknown column 'col1' in 'field list'
 
MariaDB [test]>  WITH cte (col1) AS (SELECT 1 UNION SELECT 2) SELECT col1 FROM cte UNION SELECT * FROM cte;
+------+
| col1 |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.000 sec)
 
MariaDB [test]> explain extended  WITH cte (col1) AS (SELECT 1 UNION SELECT 2) SELECT col1 FROM cte UNION SELECT * FROM cte;
+------+--------------+------------+------+---------------+------+---------+------+------+----------+----------------+
| id   | select_type  | table      | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra          |
+------+--------------+------------+------+---------------+------+---------+------+------+----------+----------------+
|    1 | PRIMARY      | <derived2> | ALL  | NULL          | NULL | NULL    | NULL | 2    |   100.00 |                |
|    2 | DERIVED      | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | No tables used |
|    3 | UNION        | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | No tables used |
| NULL | UNION RESULT | <union2,3> | ALL  | NULL          | NULL | NULL    | NULL | NULL |     NULL |                |
|    4 | UNION        | <derived5> | ALL  | NULL          | NULL | NULL    | NULL | 2    |   100.00 |                |
|    5 | DERIVED      | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | No tables used |
|    6 | UNION        | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | No tables used |
| NULL | UNION RESULT | <union5,6> | ALL  | NULL          | NULL | NULL    | NULL | NULL |     NULL |                |
| NULL | UNION RESULT | <union1,4> | ALL  | NULL          | NULL | NULL    | NULL | NULL |     NULL |                |
+------+--------------+------------+------+---------------+------+---------+------+------+----------+----------------+
9 rows in set, 1 warning (0.001 sec)
 
Note (Code 1003): with cte(`col1`) as (/* select#2 */ select 1 AS `col1` union /* select#3 */ select 2 AS `2`)/* select#1 */ select `cte`.`col1` AS `col1` from `cte` union /* select#4 */ select `cte`.`1` AS `1` from (/* select#5 */ select 1 AS `col1` union /* select#6 */ select 2 AS `2`) `cte`
 
MariaDB [test]>  WITH cte (col1) AS (SELECT 1 UNION SELECT 2) SELECT col1 FROM cte UNION SELECT `1` FROM cte;
+------+
| col1 |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.001 sec)

Comment by Dmitry Shulga [ 2021-03-10 ]

OK to push

Comment by Igor Babaev [ 2021-03-11 ]

A fix for this bug was pushed into 10.2

Generated at Thu Feb 08 09:31:12 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.