This is an automated email from the ASF dual-hosted git repository.

xiangfu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git


The following commit(s) were added to refs/heads/master by this push:
     new 128003a8f4a handle cases where CTE has the same name as table (#17591)
128003a8f4a is described below

commit 128003a8f4a5da594a5c31d8af3adeb8bbee9890
Author: dang-stripe <[email protected]>
AuthorDate: Fri Jan 30 21:28:49 2026 -0800

    handle cases where CTE has the same name as table (#17591)
---
 .../pinot/sql/parsers/parser/TableNameExtractor.java      | 11 ++++++-----
 .../pinot/sql/parsers/parser/TableNameExtractorTest.java  | 15 +++++++++++++++
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git 
a/pinot-common/src/main/java/org/apache/pinot/sql/parsers/parser/TableNameExtractor.java
 
b/pinot-common/src/main/java/org/apache/pinot/sql/parsers/parser/TableNameExtractor.java
index 4350a6f21ee..5814db0079f 100644
--- 
a/pinot-common/src/main/java/org/apache/pinot/sql/parsers/parser/TableNameExtractor.java
+++ 
b/pinot-common/src/main/java/org/apache/pinot/sql/parsers/parser/TableNameExtractor.java
@@ -146,15 +146,16 @@ public class TableNameExtractor {
   }
 
   private void visitWithItem(SqlWithItem withItem) {
-    // Track the CTE name so we don't treat it as a table later
+    // Extract table names from the CTE query definition before adding the CTE 
alias to
+    // filter. This handles cases where the CTE has the same name as the 
original table.
+    // Otherwise the table won't be added to the result.
+    if (withItem.query != null) {
+      extractTableNames(withItem.query);
+    }
     if (withItem.name != null) {
       String cteName = withItem.name.getSimple();
       _cteNames.add(cteName);
     }
-    // Extract table names from the CTE query definition, not the CTE alias
-    if (withItem.query != null) {
-      extractTableNames(withItem.query);
-    }
   }
 
   private void visitSelect(SqlSelect select) {
diff --git 
a/pinot-common/src/test/java/org/apache/pinot/sql/parsers/parser/TableNameExtractorTest.java
 
b/pinot-common/src/test/java/org/apache/pinot/sql/parsers/parser/TableNameExtractorTest.java
index b8c5c2fa95f..1129380b815 100644
--- 
a/pinot-common/src/test/java/org/apache/pinot/sql/parsers/parser/TableNameExtractorTest.java
+++ 
b/pinot-common/src/test/java/org/apache/pinot/sql/parsers/parser/TableNameExtractorTest.java
@@ -180,6 +180,21 @@ public class TableNameExtractorTest {
     assertTrue(Arrays.asList(tableNames).contains("products"), "Should contain 
products table");
   }
 
+  @Test
+  public void testResolveTableNameWithCTESameNameAsTable() {
+    // Test when CTE has the same name as the table it references (shadowing 
case)
+    String cteQuery = "WITH users AS ("
+        + "  SELECT * FROM users WHERE active = true"
+        + ") "
+        + "SELECT * FROM users";
+
+    String[] tableNames = TableNameExtractor.resolveTableName(cteQuery);
+
+    assertNotNull(tableNames, "Table names should not be null");
+    assertEquals(tableNames.length, 1, "Should resolve exactly one table");
+    assertEquals(tableNames[0], "users", "Should resolve the actual table name 
when CTE has same name");
+  }
+
   @Test
   public void testResolveTableNameWithSubqueryAlias() {
     // Test with subquery alias


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to