Skip to content
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 @@ -6899,7 +6899,7 @@
* @param sourceCount Number of expressions
* @return Ordinal'th expression, never null
*/
private static SqlNode getNthExpr(SqlNode query, int ordinal, int sourceCount) {

Check failure on line 6902 in core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache_calcite&issues=AaDtItriOryZpzLdJBpC&open=AaDtItriOryZpzLdJBpC&pullRequest=5299
if (query instanceof SqlInsert) {
SqlInsert insert = (SqlInsert) query;
if (insert.getTargetColumnList() != null) {
Expand All @@ -6912,8 +6912,15 @@
}
} else if (query instanceof SqlUpdate) {
SqlUpdate update = (SqlUpdate) query;
if (update.getSourceExpressionList() != null) {
return update.getSourceExpressionList().get(ordinal);
final SqlNodeList sourceExpressionList = update.getSourceExpressionList();
if (sourceExpressionList != null) {
// SET expressions occupy the last positions of the row type.
final int exprOrdinal =
ordinal - (sourceCount - sourceExpressionList.size());
if (exprOrdinal < 0 || exprOrdinal >= sourceExpressionList.size()) {
return query; // give up
}
return sourceExpressionList.get(exprOrdinal);
} else {
return getNthExpr(SqlNonNullableAccessors.getSourceSelect(update),
ordinal, sourceCount);
Expand Down
10 changes: 10 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 @@ -5538,6 +5538,16 @@ 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-7830">[CALCITE-7830]
* UPDATE fails with IndexOutOfBoundsException when a SET value's
* type doesn't match the column</a>. */
@Test void testUpdateTypeMismatchGetNthExprRepro() {
sql("UPDATE emp SET deptno = ^true^")
.fails("Cannot assign to target field 'DEPTNO' of type INTEGER "
+ "from source field 'EXPR\\$0' of type BOOLEAN");
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6584">[CALCITE-6584]
* Validate prefixed column identifiers in SET clause of UPDATE
Expand Down
Loading