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-6584] Validate prefixed column identifiers in SET clause of UPDATE statement #3972

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -5133,12 +5133,14 @@ private void handleScalarSubQuery(SqlSelect parentSelect,
* @param targetColumnList List of target columns, or null if not specified
* @param append Whether to append fields to those in <code>
* baseRowType</code>
* @param targetTableAlias Target table alias, or null if not specified
* @return Rowtype
*/
protected RelDataType createTargetRowType(
SqlValidatorTable table,
@Nullable SqlNodeList targetColumnList,
boolean append) {
boolean append,
@Nullable SqlIdentifier targetTableAlias) {
RelDataType baseRowType = table.getRowType();
if (targetColumnList == null) {
return baseRowType;
Expand All @@ -5156,6 +5158,13 @@ protected RelDataType createTargetRowType(
? ((RelOptTable) table) : null;
for (SqlNode node : targetColumnList) {
SqlIdentifier id = (SqlIdentifier) node;
if (!id.isSimple() && targetTableAlias != null) {
// checks that target column identifiers are prefixed with the target table alias
SqlIdentifier prefixId = id.skipLast(1);
if (!prefixId.toString().equals(targetTableAlias.toString())) {
throw newValidationError(prefixId, RESOURCE.unknownIdentifier(prefixId.toString()));
}
}
RelDataTypeField targetField =
SqlValidatorUtil.getTargetField(
baseRowType, typeFactory, id, catalogReader, relOptTable);
Expand Down Expand Up @@ -5189,7 +5198,8 @@ protected RelDataType createTargetRowType(
createTargetRowType(
table,
insert.getTargetColumnList(),
false);
false,
null);

final SqlNode source = insert.getSource();
if (source instanceof SqlSelect) {
Expand Down Expand Up @@ -5610,7 +5620,7 @@ private static SqlNode getNthExpr(SqlNode query, int ordinal, int sourceCount) {
: relOptTable.unwrapOrThrow(SqlValidatorTable.class);

final RelDataType targetRowType =
createTargetRowType(table, call.getTargetColumnList(), true);
createTargetRowType(table, call.getTargetColumnList(), true, call.getAlias());

final SqlSelect select = SqlNonNullableAccessors.getSourceSelect(call);
validateSelect(select, targetRowType);
Expand Down Expand Up @@ -5651,13 +5661,13 @@ private static SqlNode getNthExpr(SqlNode query, int ordinal, int sourceCount) {
if (updateCall != null) {
requireNonNull(table, () -> "ns.getTable() for " + targetNamespace);
targetRowType =
createTargetRowType(table, updateCall.getTargetColumnList(), true);
createTargetRowType(table, updateCall.getTargetColumnList(), true, call.getAlias());
}
SqlInsert insertCall = call.getInsertCall();
if (insertCall != null) {
requireNonNull(table, () -> "ns.getTable() for " + targetNamespace);
targetRowType =
createTargetRowType(table, insertCall.getTargetColumnList(), false);
createTargetRowType(table, insertCall.getTargetColumnList(), false, null);
}

validateSelect(sqlSelect, targetRowType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ private static void addFields(List<RelDataTypeField> fieldList,
final Table t = table == null ? null : table.unwrap(Table.class);
if (!(t instanceof CustomColumnResolvingTable)) {
final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
return nameMatcher.field(rowType, id.getSimple());
return nameMatcher.field(rowType, Util.last(id.names));
}

final List<Pair<RelDataTypeField, List<String>>> entries =
Expand Down
17 changes: 17 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 @@ -4531,6 +4531,23 @@ private void checkNegWindow(String s, String msg) {
.fails("Table 'SALES.BAD' not found");
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6584">[CALCITE-6584]
* Validate prefixed column identifiers in SET clause of UPDATE statement</a>. */
@Test void testAliasInSetClauseOfUpdate() {
// good examples
sql("UPDATE sales.emp AS e SET e.deptno = 10").ok();
sql("UPDATE emp AS e SET e.deptno = 10").ok();
Copy link
Contributor

Choose a reason for hiding this comment

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

postgres actually rejects this: Query Error: error: column "e" of relation "emp" does not exist


// bad examples
sql("UPDATE sales.emp AS emp SET ^sales.emp^.deptno = 10")
.fails("Unknown identifier 'SALES.EMP'");
sql("UPDATE sales.emp AS e SET ^emp^.deptno = 10").fails("Unknown identifier 'EMP'");
sql("UPDATE emp AS e SET ^emp^.deptno = 10").fails("Unknown identifier 'EMP'");
sql("UPDATE emp AS e SET ^a.b.c.d^.deptno = 10").fails("Unknown identifier 'A.B.C.D'");
sql("UPDATE emp AS e SET ^dept^.deptno = 10").fails("Unknown identifier 'DEPT'");
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-881">[CALCITE-881]
* Allow schema.table.column references in GROUP BY</a>. */
Expand Down
Loading