This is an automated email from the ASF dual-hosted git repository.
mihaibudiu 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 6602a58e7c [CALCITE-7847] Implicit coercion casts MULTISET operands of
a comparison to ARRAY, and loses the nullability of ROW operands
6602a58e7c is described below
commit 6602a58e7cb94ab686d9fc287602852e00d4fa95
Author: Vladimir Sitnikov <[email protected]>
AuthorDate: Sun Oct 11 23:40:28 2026 +0300
[CALCITE-7847] Implicit coercion casts MULTISET operands of a comparison to
ARRAY, and loses the nullability of ROW operands
AbstractTypeCoercion#commonTypeForBinaryComparison built the common
type of two MULTISET operands with createArrayType. A comparison of
MULTISET values whose element types have different type names therefore
cast both operands to ARRAY: SqlToRelConverter translated
multiset[deptno] = multiset[cast(deptno as decimal(5, 2))]
into a comparison of two CASTs to DECIMAL(12, 2) NOT NULL ARRAY NOT NULL.
The ROW branch of the same method returned a NOT NULL struct even when
one operand was nullable; the other branches return a nullable type in
that case. The validator and SqlToRelConverter apply each operand's
own nullability to the cast target, so this shows only to callers that
use the result directly, such as a custom TypeCoercion.
Build a MULTISET for MULTISET operands, and make the ROW result
nullable when either operand is nullable.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../validate/implicit/AbstractTypeCoercion.java | 7 ++-
.../apache/calcite/test/SqlToRelConverterTest.java | 8 +++
.../org/apache/calcite/test/TypeCoercionTest.java | 60 +++++++++++++++++++---
.../apache/calcite/test/SqlToRelConverterTest.xml | 21 ++++++++
4 files changed, 86 insertions(+), 10 deletions(-)
diff --git
a/core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
b/core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
index 22f6e16c61..72fa78d728 100644
---
a/core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
+++
b/core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
@@ -706,7 +706,9 @@ private RelDataType getTightestCommonTypeOrThrow(
return null;
}
// The only maxCardinality that seems to be supported is -1, i.e.,
unlimited.
- RelDataType resultType = factory.createArrayType(type, -1);
+ RelDataType resultType = typeName1 == SqlTypeName.ARRAY
+ ? factory.createArrayType(type, -1)
+ : factory.createMultisetType(type, -1);
return factory.createTypeWithNullability(resultType, anyNullable);
}
@@ -749,7 +751,8 @@ private RelDataType getTightestCommonTypeOrThrow(
}
resultFields.add(new RelDataTypeFieldImpl(leftField.getName(), i,
type));
}
- return factory.createStructType(resultFields);
+ RelDataType resultType = factory.createStructType(resultFields);
+ return factory.createTypeWithNullability(resultType, anyNullable);
}
return null;
diff --git
a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
index acdfeb7bb6..9dae64864f 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
@@ -2472,6 +2472,14 @@ private void checkCallTypesConsistent(String sql) {
sql(sql).withExpand(true).ok();
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7847">[CALCITE-7847]
+ * Implicit coercion casts MULTISET operands of a comparison to ARRAY, and
+ * loses the nullability of ROW operands</a>. */
+ @Test void testMultisetComparisonCoercion() {
+ sql("select multiset[deptno] = multiset[cast(deptno as decimal(5, 2))]
from dept").ok();
+ }
+
@Test void testMultisetOfColumnsRex() {
sql("select 'abc',multiset[deptno,sal] from emp").ok();
}
diff --git a/core/src/test/java/org/apache/calcite/test/TypeCoercionTest.java
b/core/src/test/java/org/apache/calcite/test/TypeCoercionTest.java
index 32db4091bf..f77096b36b 100644
--- a/core/src/test/java/org/apache/calcite/test/TypeCoercionTest.java
+++ b/core/src/test/java/org/apache/calcite/test/TypeCoercionTest.java
@@ -484,6 +484,36 @@ private static ImmutableList<RelDataType> combine(
f.recordType("a", f.arrayType(f.nullableIntType)));
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7847">[CALCITE-7847]
+ * Implicit coercion casts MULTISET operands of a comparison to ARRAY, and
+ * loses the nullability of ROW operands</a>. The comparison common type of
+ * two ROW values is nullable if either of them is nullable. */
+ @Test void testComparisonCommonTypeOfRowIsNullableIfEitherIs() {
+ final Fixture f = fixture();
+ f.comparisonCommonType(f.nullable(f.recordType("a", f.intType)),
+ f.recordType("a", f.charType), f.nullable(f.recordType("a",
f.intType)));
+ f.comparisonCommonType(f.nullable(f.recordType("a", f.intType)),
+ f.nullable(f.recordType("a", f.charType)),
+ f.nullable(f.recordType("a", f.intType)));
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7847">[CALCITE-7847]
+ * Implicit coercion casts MULTISET operands of a comparison to ARRAY, and
+ * loses the nullability of ROW operands</a>. The comparison common type of
+ * two MULTISET values is a MULTISET, and of two ARRAY values an ARRAY. */
+ @Test void testComparisonCommonTypeOfMultisetIsMultiset() {
+ final Fixture f = fixture();
+ f.comparisonCommonType(f.multisetType(f.intType),
+ f.multisetType(f.charType), f.multisetType(f.intType));
+ f.comparisonCommonType(f.nullable(f.multisetType(f.intType)),
+ f.multisetType(f.charType), f.nullable(f.multisetType(f.intType)));
+ f.comparisonCommonType(f.arrayType(f.intType),
+ f.arrayType(f.charType), f.arrayType(f.intType));
+ f.comparisonCommonType(f.multisetType(f.intType), f.arrayType(f.intType),
null);
+ }
+
/**
* Test case for
* <a
href="https://issues.apache.org/jira/browse/CALCITE-6631">[CALCITE-6631]
@@ -947,6 +977,14 @@ RelDataType arrayType(RelDataType type) {
return typeFactory.createArrayType(type, -1);
}
+ RelDataType multisetType(RelDataType type) {
+ return typeFactory.createMultisetType(type, -1);
+ }
+
+ RelDataType nullable(RelDataType type) {
+ return typeFactory.createTypeWithNullability(type, true);
+ }
+
RelDataType mapType(RelDataType keyType, RelDataType valType) {
return typeFactory.createMapType(keyType, valType);
}
@@ -1055,20 +1093,26 @@ private void comparisonCommonType(
RelDataType type2,
@Nullable RelDataType expected) {
RelDataType result = typeCoercion.commonTypeForBinaryComparison(type1,
type2);
- assertThat("Expected " + toStringNullable(expected)
- + " as comparison common type for " + type1
- + " and " + type2
- + ", but found " + toStringNullable(result),
+ assertThat("Expected " + fullTypeString(expected)
+ + " as comparison common type for " + fullTypeString(type1)
+ + " and " + fullTypeString(type2)
+ + ", but found " + fullTypeString(result),
result,
sameInstance(expected));
RelDataType result1 = typeCoercion.commonTypeForBinaryComparison(type2,
type1);
- assertThat("Expected " + toStringNullable(expected)
- + " as common type for " + type2
- + " and " + type1
- + ", but found " + toStringNullable(result1),
+ assertThat("Expected " + fullTypeString(expected)
+ + " as common type for " + fullTypeString(type2)
+ + " and " + fullTypeString(type1)
+ + ", but found " + fullTypeString(result1),
result1, sameInstance(expected));
}
+ /** Returns the full type string, which, unlike {@link
RelDataType#toString},
+ * includes nullability. */
+ private String fullTypeString(@Nullable RelDataType type) {
+ return type == null ? "NULL" : type.getFullTypeString();
+ }
+
/** Decision method for finding a wider type. */
private void checkWiderType(
RelDataType type1,
diff --git
a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
index d217079f7a..36200c6707 100644
--- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
@@ -6114,6 +6114,27 @@ LogicalProject(EXPR$0=['a'], EXPR$1=[$SLICE($2)])
<![CDATA[select 'a',multiset[10] from dept]]>
</Resource>
</TestCase>
+ <TestCase name="testMultisetComparisonCoercion">
+ <Resource name="sql">
+ <![CDATA[select multiset[deptno] = multiset[cast(deptno as decimal(5,
2))] from dept]]>
+ </Resource>
+ <Resource name="plan">
+ <![CDATA[
+LogicalProject(EXPR$0=[=(CAST($SLICE($2)):DECIMAL(12, 2) NOT NULL MULTISET NOT
NULL, CAST($SLICE($3)):DECIMAL(12, 2) NOT NULL MULTISET NOT NULL)])
+ LogicalProject(DEPTNO=[$0], NAME=[$1], EXPR$0=[$2], EXPR$00=[$4])
+ LogicalCorrelate(correlation=[$cor1], joinType=[inner],
requiredColumns=[{3}])
+ LogicalProject(DEPTNO=[$0], NAME=[$1], EXPR$0=[$2],
DEPTNO0=[CAST($0):DECIMAL(5, 2) NOT NULL])
+ LogicalCorrelate(correlation=[$cor0], joinType=[inner],
requiredColumns=[{0}])
+ LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+ Collect(field=[EXPR$0])
+ LogicalProject(EXPR$0=[$cor0.DEPTNO])
+ LogicalValues(tuples=[[{ 0 }]])
+ Collect(field=[EXPR$0])
+ LogicalProject(EXPR$0=[$cor1.DEPTNO0])
+ LogicalValues(tuples=[[{ 0 }]])
+]]>
+ </Resource>
+ </TestCase>
<TestCase name="testMultisetOfColumns">
<Resource name="plan">
<![CDATA[