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

jhyde pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 851c267448 [CALCITE-5626] Sub-query with fully-qualified table name 
throws 'table not found' during validation
851c267448 is described below

commit 851c2674484fdcde56112aba87e1d0d523071d94
Author: suibianwanwan <[email protected]>
AuthorDate: Wed Jan 15 11:05:55 2025 +0800

    [CALCITE-5626] Sub-query with fully-qualified table name throws 'table not 
found' during validation
    
    Close apache/calcite#4143
---
 .../org/apache/calcite/sql/validate/ListScope.java |  2 +
 .../org/apache/calcite/test/SqlValidatorTest.java  | 22 +++++++++
 core/src/test/resources/sql/sub-query.iq           | 56 ++++++++++++++++++++++
 3 files changed, 80 insertions(+)

diff --git a/core/src/main/java/org/apache/calcite/sql/validate/ListScope.java 
b/core/src/main/java/org/apache/calcite/sql/validate/ListScope.java
index 0d83ccd6bf..81a2a070cf 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/ListScope.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/ListScope.java
@@ -117,6 +117,8 @@ public abstract class ListScope extends DelegatingScope {
           return child;
         }
       }
+      // Make sure namespace has been validated.
+      validator.validateNamespace(child.namespace, validator.getUnknownType());
 
       // Look up the 2 tables independently, in case one is qualified with
       // catalog & schema and the other is not.
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index 6863484517..b25bf13957 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -4545,6 +4545,28 @@ public class SqlValidatorTest extends 
SqlValidatorTestCase {
         .fails("Values passed to IN operator must have compatible types");
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-5626";>[CALCITE-5626]
+   * Sub-query with fully-qualified table name throws 'table not found' during
+   * validation</a>. */
+  @Test void testInSubQueryWithFullyQualifiedName() {
+    // Minimal test case requires fully-qualified column name in WHERE clause 
of
+    // subquery; sub-query.iq contains further non-minimal test cases.
+    sql("select *\n"
+        + "from emp\n"
+        + "where deptno in (select deptno\n"
+        + "  from sales.dept\n"
+        + "  where sales.dept.deptno > 15)").ok();
+
+    // If we change 'sales.dept' to 'sales.dept2', query is genuinely invalid.
+    sql("select *\n"
+        + "from emp\n"
+        + "where deptno in (select deptno\n"
+        + "  from sales.dept\n"
+        + "  where ^sales.dept2^.deptno > 15)")
+        .fails("Table 'SALES.DEPT2' not found");
+  }
+
   @Test void testAnyList() {
     sql("select * from emp where empno = any (10,20)").ok();
 
diff --git a/core/src/test/resources/sql/sub-query.iq 
b/core/src/test/resources/sql/sub-query.iq
index d820b853f6..50bef74d92 100644
--- a/core/src/test/resources/sql/sub-query.iq
+++ b/core/src/test/resources/sql/sub-query.iq
@@ -3758,6 +3758,62 @@ from emp;
 
 !ok
 
+# [CALCITE-5626] Sub-query with fully-qualified table name throws
+# 'table not found' during validation
+select ename, deptno
+from emp
+where deptno in (select dept.deptno
+  from "scott".dept
+  where "scott".dept.deptno > 20);
++--------+--------+
+| ENAME  | DEPTNO |
++--------+--------+
+| ALLEN  |     30 |
+| BLAKE  |     30 |
+| JAMES  |     30 |
+| MARTIN |     30 |
+| TURNER |     30 |
+| WARD   |     30 |
++--------+--------+
+(6 rows)
+
+!ok
+
+# Similar to previous
+select ename, deptno
+from emp
+where deptno in (select deptno
+  from "scott".dept
+  where "scott".dept.deptno > 20);
++--------+--------+
+| ENAME  | DEPTNO |
++--------+--------+
+| ALLEN  |     30 |
+| BLAKE  |     30 |
+| JAMES  |     30 |
+| MARTIN |     30 |
+| TURNER |     30 |
+| WARD   |     30 |
++--------+--------+
+(6 rows)
+
+!ok
+
+# Similar to previous
+select count("scott".emp.sal) as c
+from "scott".emp
+where "scott".emp.deptno in (select "scott".dept.deptno
+  from "scott".dept
+  where "scott".dept.deptno > 20);
++---+
+| C |
++---+
+| 6 |
++---+
+(1 row)
+
+!ok
+
 # Test case for [CALCITE-5789]
 select deptno from dept d1 where exists (
  select 1 from dept d2 where d2.deptno = d1.deptno and exists (

Reply via email to