diff --git a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java index 706c8e6be0e..befd6e5972d 100644 --- a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java +++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java @@ -2941,6 +2941,19 @@ private void registerQuery( SqlSelect.HAVING_OPERAND); registerSubQueries(selectScope2, SqlNonNullableAccessors.getSelectList(select)); + + if (enclosingNode.getKind() == SqlKind.UPDATE) { + registerSubQueries(selectScope2, + ((SqlUpdate) enclosingNode).getSourceExpressionList()); + } else if (enclosingNode.getKind() == SqlKind.MERGE) { + SqlUpdate updateCall = ((SqlMerge) enclosingNode).getUpdateCall(); + + if (updateCall != null) { + registerSubQueries(selectScope2, + updateCall.getSourceExpressionList()); + } + } + final SqlNodeList orderList = select.getOrderList(); if (orderList != null) { // If the query is 'SELECT DISTINCT', restrict the columns diff --git a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java index 47efce21c62..336251db2ff 100644 --- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java +++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java @@ -3119,6 +3119,18 @@ void checkCorrelatedMapSubQuery(boolean expand) { sql(sql).ok(); } + /** + * Test case for + * [CALCITE-6570] + * UPDATE with sub-query that requires type cast gives AssertionError. + */ + @Test void testUpdateSubQueryWithCast() { + final String sql = "update emp\n" + + "set empno = (\n" + + " select cast(min(empno) as BIGINT) from emp as e where e.deptno = emp.deptno)"; + sql(sql).ok(); + } + /** * Test case for * [CALCITE-3229] @@ -3213,6 +3225,20 @@ void checkCorrelatedMapSubQuery(boolean expand) { sql(sql).ok(); } + /** + * Test case for + * [CALCITE-6570] + * UPDATE with sub-query that requires type cast gives AssertionError. + */ + @Test void testMergeSubQueryWithCast() { + final String sql = "merge into emp t0\n" + + "using emp t1 ON t0.empno = t1.empno\n" + + "when matched then\n" + + "update set deptno = (select cast(deptno as BIGINT) from emp where deptno > 1)"; + + sql(sql).ok(); + } + @Test void testSelectView() { // translated condition: deptno = 20 and sal > 1000 and empno > 100 final String sql = "select * from emp_20 where empno > 100"; diff --git a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml index 5d6f6f2f0ff..0c30abc520c 100644 --- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml +++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml @@ -4708,6 +4708,28 @@ LogicalTableModify(table=[[CATALOG, SALES, EMPNULLABLES]], operation=[MERGE], up LogicalFilter(condition=[IS NULL($7)]) LogicalTableScan(table=[[CATALOG, SALES, EMP]]) LogicalTableScan(table=[[CATALOG, SALES, EMPNULLABLES]]) +]]> + + + + + 1)]]> + + + ($7, 1)]) + LogicalTableScan(table=[[CATALOG, SALES, EMP]]) ]]> @@ -8933,6 +8955,24 @@ LogicalTableModify(table=[[CATALOG, SALES, EMP]], operation=[UPDATE], updateColu LogicalAggregate(group=[{0}], EXPR$0=[MIN($1)]) LogicalProject(DEPTNO=[$7], EMPNO=[$0]) LogicalTableScan(table=[[CATALOG, SALES, EMP]]) +]]> + + + + + + + +