Skip to content

Commit c4cceec

Browse files
committed
Add comments clarification and test for multiple same name for multiple columns
1 parent bc4566d commit c4cceec

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

presto-analyzer/src/main/java/com/facebook/presto/sql/analyzer/RelationId.java

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public Node getSourceNode()
5757
{
5858
return sourceNode;
5959
}
60+
6061
@Override
6162
public boolean equals(Object o)
6263
{

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

+13
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ public void testMultipleCtesSameNameSameTable()
559559
@Test
560560
public void testMultipleCtesSameNameDifferentTable()
561561
{
562+
// This happens because the two CTE1s having the same name, and the CTE1 of t13 has its first column utilized, so the CTE1 of t1 will also have its first column added.
562563
assertUtilizedTableColumns(
563564
"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",
564565
ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t13"), ImmutableSet.of("x"),
@@ -576,12 +577,24 @@ public void testMultipleCtesDifferentNameSameTable()
576577
@Test
577578
public void testMultipleCtesDifferentNameDifferentTable()
578579
{
580+
// This is the same behavior for join queries like "select t1.a from t1 join t13 on true" regardless of wrapping in CTE
579581
assertUtilizedTableColumns(
580582
"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",
581583
ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t13"), ImmutableSet.of("x"),
582584
QualifiedObjectName.valueOf("tpch.s1.t1"), ImmutableSet.of()));
583585
}
584586

587+
@Test
588+
public void testMultipleCtesSameNameDifferentTableMultipleColumnsUtilized()
589+
{
590+
// This will fallback to checking all utilized columns on t1 because CTE1 of t13 only has one item in the select expression, but CTE1 of t1 has 3, so there's a mismatch.
591+
// It's trying to add the third column of CTE1 of t1 but it does not exist.
592+
assertUtilizedTableColumns(
593+
"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, cte2.c4 from cte1 join cte2 on true",
594+
ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t13"), ImmutableSet.of("x"),
595+
QualifiedObjectName.valueOf("tpch.s1.t1"), ImmutableSet.of("a", "b", "c")));
596+
}
597+
585598
private void assertUtilizedTableColumns(@Language("SQL") String query, Map<QualifiedObjectName, Set<String>> expected)
586599
{
587600
transaction(transactionManager, accessControl)

0 commit comments

Comments
 (0)