Skip to content

Commit

Permalink
[CALCITE-5626] Sub-query with fully-qualified table name throws 'tabl…
Browse files Browse the repository at this point in the history
…e not found' during validation

Close #4143
  • Loading branch information
suibianwanwank authored and julianhyde committed Jan 15, 2025
1 parent 61a3d5e commit 851c267
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public boolean isChildNullable(int i) {
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.
Expand Down
22 changes: 22 additions & 0 deletions core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4545,6 +4545,28 @@ private void checkNegWindow(String s, String msg) {
.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();

Expand Down
56 changes: 56 additions & 0 deletions core/src/test/resources/sql/sub-query.iq
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 851c267

Please sign in to comment.