This is an automated email from the ASF dual-hosted git repository.
mbudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new 671cacaca3 [CALCITE-7225] Comparing ROW values with different lengths
causes an IndexOutOfBoudsException
671cacaca3 is described below
commit 671cacaca36fd5f0921a923550ad9de96419e06b
Author: Mihai Budiu <[email protected]>
AuthorDate: Mon Oct 13 14:11:02 2025 -0700
[CALCITE-7225] Comparing ROW values with different lengths causes an
IndexOutOfBoudsException
Signed-off-by: Mihai Budiu <[email protected]>
---
.../java/org/apache/calcite/runtime/CalciteResource.java | 3 +++
.../main/java/org/apache/calcite/sql/type/InferTypes.java | 15 +++++++++++----
.../org/apache/calcite/runtime/CalciteResource.properties | 1 +
.../java/org/apache/calcite/test/SqlValidatorTest.java | 6 ++++++
4 files changed, 21 insertions(+), 4 deletions(-)
diff --git a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
index 1ec9030784..809bdcce70 100644
--- a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
+++ b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
@@ -1157,6 +1157,9 @@ ExInst<RuntimeException>
multipleCapturingGroupsForRegexpFunctions(String value,
@BaseMessage("Index in ROW type does not have a constant integer or string
value")
ExInst<SqlValidatorException> illegalRowIndex();
+ @BaseMessage("Unequal number of entries in ROW expressions")
+ ExInst<SqlValidatorException> unequalRowSizes();
+
@BaseMessage("Cannot infer return type for {0}; operand types: {1}")
ExInst<SqlValidatorException> cannotInferReturnType(String operator, String
types);
}
diff --git a/core/src/main/java/org/apache/calcite/sql/type/InferTypes.java
b/core/src/main/java/org/apache/calcite/sql/type/InferTypes.java
index 1a0cb0dc86..ee1603f290 100644
--- a/core/src/main/java/org/apache/calcite/sql/type/InferTypes.java
+++ b/core/src/main/java/org/apache/calcite/sql/type/InferTypes.java
@@ -25,6 +25,8 @@
import java.util.Arrays;
import java.util.List;
+import static org.apache.calcite.util.Static.RESOURCE;
+
/**
* Strategies for inferring operand types.
*
@@ -66,10 +68,15 @@ private InferTypes() {}
public static final SqlOperandTypeInference RETURN_TYPE =
(callBinding, returnType, operandTypes) -> {
for (int i = 0; i < operandTypes.length; ++i) {
- operandTypes[i] =
- returnType.isStruct()
- ? returnType.getFieldList().get(i).getType()
- : returnType;
+ if (returnType.isStruct()) {
+ if (returnType.getFieldCount() > i) {
+ operandTypes[i] = returnType.getFieldList().get(i).getType();
+ } else {
+ throw callBinding.newError(RESOURCE.unequalRowSizes());
+ }
+ } else {
+ operandTypes[i] = returnType;
+ }
}
};
diff --git
a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
index f917c2aa4b..124e133a14 100644
---
a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
+++
b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
@@ -377,6 +377,7 @@ AsofConditionMustBeComparison=ASOF JOIN condition must be a
conjunction of equal
AsofCannotBeCorrelated=ASOF JOIN does not support correlated subqueries
UnknownRowField=ROW type does not have a field named ''{0}'': {1}
IllegalRowIndexValue=ROW type does not have a field with index {0,number};
legal range is 1 to {1,number}
+UnequalRowSizes=Unequal number of entries in ROW expressions
IllegalRowIndex=Index in ROW type does not have a constant integer or string
value
CannotInferReturnType=Cannot infer return type for {0}; operand types: {1}
# End CalciteResource.properties
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index 9232c0ad93..7165d5d748 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -5770,6 +5770,12 @@ private SqlValidatorFixture
joinCommonColumnsFixture(boolean caseSensitive) {
.fails("Cannot specify NATURAL keyword with ON or USING clause");
}
+ /** Test case for <a
href="https://issues.apache.org/jira/browse/CALCITE-7225">[CALCITE-7225]
+ * Comparing ROW values with different lengths causes an
IndexOutOfBoudsException</a>. */
+ @Test void testUnequalRows() {
+ sql("select ROW(1) = ROW^(1, 2)^").fails("Unequal number of entries in ROW
expressions");
+ }
+
@Test void testNaturalJoinCaseSensitive() {
// With case-insensitive match, more columns are recognized as join columns
// and therefore "*" expands to fewer columns.