[MDEV-12402] CTE with correlated subquery does not recognize column names from outer tables Created: 2017-03-29  Updated: 2017-03-30  Resolved: 2017-03-30

Status: Closed
Project: MariaDB Server
Component/s: Optimizer - CTE
Affects Version/s: 10.2
Fix Version/s: N/A

Type: Bug Priority: Minor
Reporter: Elena Stepanova Assignee: Igor Babaev
Resolution: Not a Bug Votes: 0
Labels: None

Issue Links:
Relates

 Description   

I don't know if it is supposed to work; if it is not, please feel free to close as not a bug.

DROP TABLE IF EXISTS t1, t2;
 
CREATE TABLE t1 (a INT);
CREATE TABLE t2 (b INT);
 
# This query works of course
SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t2 WHERE b = a );
 
# But this does not
SELECT * FROM t1 WHERE EXISTS ( WITH cte AS ( SELECT * FROM t2 WHERE b = a ) SELECT * FROM cte );

MariaDB [test]> SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t2 WHERE b = a );
Empty set (0.01 sec)
 
MariaDB [test]> SELECT * FROM t1 WHERE EXISTS ( WITH cte AS ( SELECT * FROM t2 WHERE b = a ) SELECT * FROM cte );
ERROR 1054 (42S22): Unknown column 'a' in 'where clause'

The example is oversimplified and probably does not make much sense, it's just meant to demonstrate the problem, but I guess there might more realistic cases affected by it.



 Comments   
Comment by Igor Babaev [ 2017-03-30 ]

See my comment.

Comment by Igor Babaev [ 2017-03-30 ]

Elena,

CTEs are like derived tables: they do not allow outer references.

So it's not a bug, It's rather a limitation of our derived tables.

Generated at Thu Feb 08 07:57:26 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.