Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-6570] Add SCALAR_QUERY to sourceExpressionList of SqlUpdate #3966

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2941,6 +2941,19 @@ private void registerQuery(
SqlSelect.HAVING_OPERAND);
registerSubQueries(selectScope2,
SqlNonNullableAccessors.getSelectList(select));

if (enclosingNode.getKind() == SqlKind.UPDATE) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether there are other statements that would need this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, at least MERGE suffers from the same issue.
Added the corresponding test and fix.

registerSubQueries(selectScope2,
((SqlUpdate) enclosingNode).getSourceExpressionList());
} else if (enclosingNode.getKind() == SqlKind.MERGE) {
SqlUpdate updateCall = ((SqlMerge) enclosingNode).getUpdateCall();
mihaibudiu marked this conversation as resolved.
Show resolved Hide resolved

if (updateCall != null) {
registerSubQueries(selectScope2,
updateCall.getSourceExpressionList());
}
}

final SqlNodeList orderList = select.getOrderList();
if (orderList != null) {
// If the query is 'SELECT DISTINCT', restrict the columns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3119,6 +3119,18 @@ void checkCorrelatedMapSubQuery(boolean expand) {
sql(sql).ok();
}

/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6570">[CALCITE-6570]
* UPDATE with sub-query that requires type cast gives AssertionError</a>.
*/
@Test void testUpdateSubQueryWithCast() {
mihaibudiu marked this conversation as resolved.
Show resolved Hide resolved
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
* <a href="https://issues.apache.org/jira/browse/CALCITE-3229">[CALCITE-3229]
Expand Down Expand Up @@ -3213,6 +3225,20 @@ void checkCorrelatedMapSubQuery(boolean expand) {
sql(sql).ok();
}

/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6570">[CALCITE-6570]
* UPDATE with sub-query that requires type cast gives AssertionError</a>.
*/
@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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
]]>
</Resource>
</TestCase>
<TestCase name="testMergeSubQueryWithCast">
<Resource name="sql">
<![CDATA[merge into emp t0
using emp t1 ON t0.empno = t1.empno
when matched then
update set deptno = (select cast(deptno as BIGINT) from emp where deptno > 1)]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalTableModify(table=[[CATALOG, SALES, EMP]], operation=[MERGE], updateColumnList=[[DEPTNO]], flattened=[true])
LogicalProject($f0=[$18])
LogicalJoin(condition=[true], joinType=[left])
LogicalJoin(condition=[=($9, $0)], joinType=[inner])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalAggregate(group=[{}], agg#0=[SINGLE_VALUE($0)])
LogicalProject(EXPR$0=[CAST($7):BIGINT NOT NULL])
LogicalFilter(condition=[>($7, 1)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
Expand Down Expand Up @@ -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]])
]]>
</Resource>
</TestCase>
<TestCase name="testUpdateSubQueryWithCast">
<Resource name="sql">
<![CDATA[update emp
set empno = (
select cast(min(empno) as BIGINT) from emp as e where e.deptno = emp.deptno)]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalTableModify(table=[[CATALOG, SALES, EMP]], operation=[UPDATE], updateColumnList=[[EMPNO]], sourceExpressionList=[[CAST($0):INTEGER]], flattened=[true])
LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8], EXPR$0=[CAST($10):BIGINT])
LogicalJoin(condition=[=($7, $9)], joinType=[left])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalAggregate(group=[{0}], agg#0=[MIN($1)])
LogicalProject(DEPTNO=[$7], EMPNO=[$0])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
Expand Down
Loading