Skip to content

Commit b3182e5

Browse files
vladimirg-dbcloud-fan
authored andcommitted
[SPARK-50744][SQL] Add a test case for view/CTE name resolution precedence
### What changes were proposed in this pull request? Add an important test case for CTE resolution: ``` CREATE VIEW v1 AS SELECT 1; CREATE VIEW v2 AS SELECT * FROM v1; – The result is 1. – The `v2` body will be inlined in the main query tree during the analysis, but upper `v1` – CTE definition won't take precedence over the lower `v1` view. WITH v1 AS ( SELECT 2 ) SELECT * FROM v2; ``` This is an exception to the usual "CTE name takes precedence over the table/view name". ### Why are the changes needed? To harden Spark testing. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? New test case. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #49378 from vladimirg-db/vladimirg-db/add-cte-vs-view-test-case. Authored-by: Vladimir Golubev <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
1 parent 0467aca commit b3182e5

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

sql/core/src/test/resources/sql-tests/analyzer-results/cte.sql.out

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ CreateViewCommand `t2`, select * from values 0, 1 as t(id), false, false, LocalT
1717
+- LocalRelation [id#x]
1818

1919

20+
-- !query
21+
create temporary view t3 as select * from t
22+
-- !query analysis
23+
CreateViewCommand `t3`, select * from t, false, false, LocalTempView, UNSUPPORTED, true
24+
+- Project [id#x]
25+
+- SubqueryAlias t
26+
+- View (`t`, [id#x])
27+
+- Project [cast(id#x as int) AS id#x]
28+
+- Project [id#x]
29+
+- SubqueryAlias t
30+
+- LocalRelation [id#x]
31+
32+
2033
-- !query
2134
WITH s AS (SELECT 1 FROM s) SELECT * FROM s
2235
-- !query analysis
@@ -76,6 +89,27 @@ WithCTE
7689
+- CTERelationRef xxxx, true, [1#x], false, false
7790

7891

92+
-- !query
93+
WITH t AS (SELECT 1) SELECT * FROM t3
94+
-- !query analysis
95+
WithCTE
96+
:- CTERelationDef xxxx, false
97+
: +- SubqueryAlias t
98+
: +- Project [1 AS 1#x]
99+
: +- OneRowRelation
100+
+- Project [id#x]
101+
+- SubqueryAlias t3
102+
+- View (`t3`, [id#x])
103+
+- Project [cast(id#x as int) AS id#x]
104+
+- Project [id#x]
105+
+- SubqueryAlias t
106+
+- View (`t`, [id#x])
107+
+- Project [cast(id#x as int) AS id#x]
108+
+- Project [id#x]
109+
+- SubqueryAlias t
110+
+- LocalRelation [id#x]
111+
112+
79113
-- !query
80114
WITH s1 AS (SELECT 1 FROM s2), s2 AS (SELECT 1 FROM s1) SELECT * FROM s1, s2
81115
-- !query analysis
@@ -778,3 +812,9 @@ DropTempViewCommand t
778812
DROP VIEW IF EXISTS t2
779813
-- !query analysis
780814
DropTempViewCommand t2
815+
816+
817+
-- !query
818+
DROP VIEW IF EXISTS t3
819+
-- !query analysis
820+
DropTempViewCommand t3

sql/core/src/test/resources/sql-tests/inputs/cte.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
create temporary view t as select * from values 0, 1, 2 as t(id);
22
create temporary view t2 as select * from values 0, 1 as t(id);
3+
create temporary view t3 as select * from t;
34

45
-- WITH clause should not fall into infinite loop by referencing self
56
WITH s AS (SELECT 1 FROM s) SELECT * FROM s;
@@ -10,6 +11,9 @@ SELECT * FROM r;
1011
-- WITH clause should reference the base table
1112
WITH t AS (SELECT 1 FROM t) SELECT * FROM t;
1213

14+
-- Table `t` referenced by a view should take precedence over the top CTE `t`
15+
WITH t AS (SELECT 1) SELECT * FROM t3;
16+
1317
-- WITH clause should not allow cross reference
1418
WITH s1 AS (SELECT 1 FROM s2), s2 AS (SELECT 1 FROM s1) SELECT * FROM s1, s2;
1519

@@ -175,3 +179,4 @@ with cte as (select * from cte) select * from cte;
175179
-- Clean up
176180
DROP VIEW IF EXISTS t;
177181
DROP VIEW IF EXISTS t2;
182+
DROP VIEW IF EXISTS t3;

sql/core/src/test/resources/sql-tests/results/cte.sql.out

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ struct<>
1515

1616

1717

18+
-- !query
19+
create temporary view t3 as select * from t
20+
-- !query schema
21+
struct<>
22+
-- !query output
23+
24+
25+
1826
-- !query
1927
WITH s AS (SELECT 1 FROM s) SELECT * FROM s
2028
-- !query schema
@@ -70,6 +78,16 @@ struct<1:int>
7078
1
7179

7280

81+
-- !query
82+
WITH t AS (SELECT 1) SELECT * FROM t3
83+
-- !query schema
84+
struct<id:int>
85+
-- !query output
86+
0
87+
1
88+
2
89+
90+
7391
-- !query
7492
WITH s1 AS (SELECT 1 FROM s2), s2 AS (SELECT 1 FROM s1) SELECT * FROM s1, s2
7593
-- !query schema
@@ -580,3 +598,11 @@ DROP VIEW IF EXISTS t2
580598
struct<>
581599
-- !query output
582600

601+
602+
603+
-- !query
604+
DROP VIEW IF EXISTS t3
605+
-- !query schema
606+
struct<>
607+
-- !query output
608+

0 commit comments

Comments
 (0)