Skip to content

Commit b2f1bab

Browse files
wt0530githubgxll
authored andcommitted
[fix][core] Check subquery in update set clause
1 parent 88d1a3c commit b2f1bab

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

core/src/main/java/org/apache/calcite/runtime/CalciteResource.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,9 @@ ExInstWithCause<CalciteException> failedToAccessField(
10281028
@BaseMessage("A table function at most has one input table with row semantics. Table function ''{0}'' has multiple input tables with row semantics")
10291029
ExInst<SqlValidatorException> multipleRowSemanticsTables(String funcName);
10301030

1031+
@BaseMessage("update not support subquery node ''{0}'' ")
1032+
ExInst<SqlValidatorException> updateNotSupport(String nodeinfo);
1033+
10311034
@BaseMessage("MATCH_CONDITION only allowed with ASOF JOIN")
10321035
ExInst<CalciteException> matchConditionRequiresAsof();
10331036

core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.apache.calcite.rel.type.RelDataTypeFactory;
6363
import org.apache.calcite.rel.type.RelDataTypeField;
6464
import org.apache.calcite.rex.*;
65+
import org.apache.calcite.runtime.CalciteContextException;
6566
import org.apache.calcite.schema.ColumnStrategy;
6667
import org.apache.calcite.schema.ModifiableTable;
6768
import org.apache.calcite.schema.ModifiableView;
@@ -115,6 +116,7 @@
115116

116117
import static java.util.Objects.requireNonNull;
117118
import static org.apache.calcite.sql.validate.SqlValidatorImpl.isImplicitKey;
119+
import static org.apache.calcite.util.Static.RESOURCE;
118120

119121
/**
120122
* Converts a SQL parse tree (consisting of
@@ -4204,7 +4206,21 @@ private RelNode convertDelete(SqlDelete call) {
42044206
LogicalTableModify.Operation.DELETE, null, null, false);
42054207
}
42064208

4209+
protected void checkSubqueryInSetClause(SqlUpdate call) {
4210+
if (call.getSourceExpressionList() != null) {
4211+
for (SqlNode node : call.getSourceExpressionList()) {
4212+
if (node instanceof SqlSelect) { // Don't allow sub-query in update set clause.
4213+
final CalciteContextException ex =
4214+
validator.newValidationError(node, RESOURCE.updateNotSupport(node.toString()));
4215+
throw new RuntimeException(ex.getMessage(), ex);
4216+
}
4217+
}
4218+
}
4219+
}
4220+
42074221
private RelNode convertUpdate(SqlUpdate call) {
4222+
checkSubqueryInSetClause(call);
4223+
42084224
// Source table info
42094225
// Map column to table it belongs to
42104226
final Map<String, Integer> aliasTableMap = new HashMap<>();

core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ InvalidOrderBy=Only tables with set semantics may be ordered. Invalid ORDER BY c
335335
MultipleRowSemanticsTables=A table function at most has one input table with row semantics. Table function ''{0}'' has multiple input tables with row semantics
336336
NoOperator=No operator for ''{0}'' with kind: ''{1}'', syntax: ''{2}'' during JSON deserialization
337337
MatchConditionRequiresAsof=MATCH_CONDITION only allowed with ASOF JOIN
338+
updateNotSupport=update not support subquery node ''{0}''
338339
AsofRequiresMatchCondition=ASOF JOIN missing MATCH_CONDITION
339340
AsofMatchMustBeComparison=ASOF JOIN MATCH_CONDITION must be a comparison between columns from the two inputs
340341
AsofConditionMustBeComparison=ASOF JOIN condition must be a conjunction of equality comparisons

0 commit comments

Comments
 (0)