[MDEV-15120] CTE table should not belong to database, that is in use Created: 2018-01-30  Updated: 2018-06-21  Resolved: 2018-03-12

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

Type: Bug Priority: Major
Reporter: Alice Sherepa Assignee: Igor Babaev
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-16473 WITH statement throws 'no database se... Closed

 Description   

CTE table formally does not belong to database, but its behavior is different from derived table.
testcase:

WITH cte AS (SELECT 1) SELECT * from cte; # returns error if database is not selected
USE test;
WITH cte AS (SELECT 1 AS a) SELECT test.cte.a FROM test.cte; #works, even if database test does not exist
CREATE DATABASE test;
USE test;
WITH cte AS (SELECT 1 AS a) SELECT test.cte.a FROM test.cte; # works
SELECT test.d1.a FROM (SELECT 1 AS a) d1;		# error, derived table does not belong to database test;
SELECT d1.a FROM (SELECT 1 AS a) d1;	# works

MariaDB [(none)]> WITH cte AS (SELECT 1) SELECT * from cte;
ERROR 1046 (3D000): No database selected
 
MariaDB [(none)]> USE test;
ERROR 1049 (42000): Unknown database 'test'
 
MariaDB [(none)]> WITH cte AS (SELECT 1 AS a) SELECT test.cte.a FROM test.cte;
+---+
| a |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
 
MariaDB [(none)]> CREATE DATABASE test;
Query OK, 1 row affected (0.00 sec)
 
MariaDB [(none)]> USE test;
Database changed
 
MariaDB [test]> WITH cte AS (SELECT 1 AS a) SELECT test.cte.a FROM test.cte; 
+---+
| a |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
 
MariaDB [test]> SELECT test.d1.a FROM (SELECT 1 AS a) d1;
ERROR 1054 (42S22): Unknown column 'test.d1.a' in 'field list'
 
MariaDB [test]> SELECT d1.a FROM (SELECT 1 AS a) d1;
+---+
| a |
+---+
| 1 |
+---+
1 row in set (0.00 sec)



 Comments   
Comment by Igor Babaev [ 2018-03-12 ]

A fix for this bug was pushed into the 10.2 tree.

Generated at Thu Feb 08 08:18:50 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.