Skip to content

Commit 54216ca

Browse files
committed
[CALCITE-6570] Add SCALAR_QUERY to sourceExpressionList
1 parent 91fe118 commit 54216ca

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,6 +2941,12 @@ private void registerQuery(
29412941
SqlSelect.HAVING_OPERAND);
29422942
registerSubQueries(selectScope2,
29432943
SqlNonNullableAccessors.getSelectList(select));
2944+
2945+
if (enclosingNode.getKind() == SqlKind.UPDATE) {
2946+
registerSubQueries(selectScope2,
2947+
((SqlUpdate) enclosingNode).getSourceExpressionList());
2948+
}
2949+
29442950
final SqlNodeList orderList = select.getOrderList();
29452951
if (orderList != null) {
29462952
// If the query is 'SELECT DISTINCT', restrict the columns

core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,6 +3119,13 @@ void checkCorrelatedMapSubQuery(boolean expand) {
31193119
sql(sql).ok();
31203120
}
31213121

3122+
@Test void testUpdateSubQueryWithCast() {
3123+
final String sql = "update emp\n"
3124+
+ "set empno = (\n"
3125+
+ " select cast(min(empno) as BIGINT) from emp as e where e.deptno = emp.deptno)";
3126+
sql(sql).ok();
3127+
}
3128+
31223129
/**
31233130
* Test case for
31243131
* <a href="https://issues.apache.org/jira/browse/CALCITE-3229">[CALCITE-3229]

core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8933,6 +8933,24 @@ LogicalTableModify(table=[[CATALOG, SALES, EMP]], operation=[UPDATE], updateColu
89338933
LogicalAggregate(group=[{0}], EXPR$0=[MIN($1)])
89348934
LogicalProject(DEPTNO=[$7], EMPNO=[$0])
89358935
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
8936+
]]>
8937+
</Resource>
8938+
</TestCase>
8939+
<TestCase name="testUpdateSubQueryWithCast">
8940+
<Resource name="sql">
8941+
<![CDATA[update emp
8942+
set empno = (
8943+
select cast(min(empno) as BIGINT) from emp as e where e.deptno = emp.deptno)]]>
8944+
</Resource>
8945+
<Resource name="plan">
8946+
<![CDATA[
8947+
LogicalTableModify(table=[[CATALOG, SALES, EMP]], operation=[UPDATE], updateColumnList=[[EMPNO]], sourceExpressionList=[[CAST($0):INTEGER]], flattened=[true])
8948+
LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8], EXPR$0=[CAST($10):BIGINT])
8949+
LogicalJoin(condition=[=($7, $9)], joinType=[left])
8950+
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
8951+
LogicalAggregate(group=[{0}], agg#0=[MIN($1)])
8952+
LogicalProject(DEPTNO=[$7], EMPNO=[$0])
8953+
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
89368954
]]>
89378955
</Resource>
89388956
</TestCase>

0 commit comments

Comments
 (0)