Skip to content

Commit 72b6a4b

Browse files
committed
Add tests for multiple cte with same name
1 parent 577958d commit 72b6a4b

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

presto-main/src/test/java/com/facebook/presto/sql/analyzer/TestUtilizedColumnsAnalyzer.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -539,13 +539,49 @@ public void testMultipleCtes()
539539
}
540540

541541
@Test
542-
public void testMultipleCtesWithSameNameFallsBackToAllColumns()
542+
public void testMultipleCtesUsingSameTable()
543543
{
544544
assertUtilizedTableColumns(
545545
"with cte1 as (select x + 1 as c1, y as c2, z + 1 as c3 from t13), cte2 as (with cte1 AS (select y +1 as c1 from t13) select * from cte1) SELECT cte1.c1, cte2.c1 from cte1 join cte2 on cte1.c1=cte2.c1",
546+
ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t13"), ImmutableSet.of("x", "y")));
547+
}
548+
549+
@Test
550+
public void testMultipleCtesSameNameSameTable()
551+
{
552+
// TODO(kevintang2022): Fix visitWithQuery so that it looks up relation properly.
553+
// Currently, it just looks the relations using the name (string) of the CTE
554+
assertUtilizedTableColumns(
555+
"with cte1 as (select x + 1 as c1 from t13), cte2 as (with cte1 as (select x + 1 as c2, y + 1 as c3, z as c4 from t13) select * from cte1) select cte1.c1, cte2.c3 from cte1 join cte2 on true",
546556
ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t13"), ImmutableSet.of("x", "y", "z")));
547557
}
548558

559+
@Test
560+
public void testMultipleCtesSameNameDifferentTable()
561+
{
562+
assertUtilizedTableColumns(
563+
"with cte1 as (select x + 1 as c1 from t13), cte2 as (with cte1 as (select a + 1 as c2, b + 1 as c3, c as c4 from t1) select * from cte1) select cte1.c1 from cte1 join cte2 on true",
564+
ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t13"), ImmutableSet.of("x"),
565+
QualifiedObjectName.valueOf("tpch.s1.t1"), ImmutableSet.of("a")));
566+
}
567+
568+
@Test
569+
public void testMultipleCtesDifferentNameSameTable()
570+
{
571+
assertUtilizedTableColumns(
572+
"with cte1 as (select x + 1 as c1 from t13), cte2 as (with cte3 as (select x + 1 as c2, y + 1 as c3, z as c4 from t13) select * from cte3) select cte1.c1, cte2.c3 from cte1 join cte2 on true",
573+
ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t13"), ImmutableSet.of("x", "y")));
574+
}
575+
576+
@Test
577+
public void testMultipleCtesDifferentNameDifferentTable()
578+
{
579+
assertUtilizedTableColumns(
580+
"with cte1 as (select x + 1 as c1 from t13), cte2 as (with cte3 as (select a + 1 as c2, b + 1 as c3, c as c4 from t1) select * from cte3) select cte1.c1 from cte1 join cte2 on true",
581+
ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t13"), ImmutableSet.of("x"),
582+
QualifiedObjectName.valueOf("tpch.s1.t1"), ImmutableSet.of()));
583+
}
584+
549585
private void assertUtilizedTableColumns(@Language("SQL") String query, Map<QualifiedObjectName, Set<String>> expected)
550586
{
551587
transaction(transactionManager, accessControl)

0 commit comments

Comments
 (0)