Skip to content

Commit

Permalink
[CALCITE-6228] ELEMENT function infers incorrect return type
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
mihaibudiu committed Feb 7, 2024
1 parent f7069cc commit f837ffa
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable {
*/
public static final SqlFunction ELEMENT =
SqlBasicFunction.create("ELEMENT",
ReturnTypes.MULTISET_ELEMENT_NULLABLE,
ReturnTypes.MULTISET_ELEMENT_FORCE_NULLABLE,
OperandTypes.COLLECTION);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,11 @@ public static SqlCall stripSeparator(SqlCall call) {
ARG0.andThen(SqlTypeTransforms.TO_MULTISET);

/**
* Returns the element type of a MULTISET.
* Returns the element type of a MULTISET, with nullability enforced.
*/
public static final SqlReturnTypeInference MULTISET_ELEMENT_NULLABLE =
MULTISET.andThen(SqlTypeTransforms.TO_COLLECTION_ELEMENT_TYPE);
public static final SqlReturnTypeInference MULTISET_ELEMENT_FORCE_NULLABLE =
MULTISET.andThen(SqlTypeTransforms.TO_COLLECTION_ELEMENT_TYPE)
.andThen(SqlTypeTransforms.FORCE_NULLABLE);

/**
* Same as {@link #MULTISET} but returns with nullability if any of the
Expand Down
14 changes: 7 additions & 7 deletions core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1925,17 +1925,17 @@ void testLikeAndSimilarFails() {

@Test void testElement() {
expr("element(multiset[1])")
.columnType("INTEGER NOT NULL");
.columnType("INTEGER");
expr("1.0+element(multiset[1])")
.columnType("DECIMAL(12, 1) NOT NULL");
.columnType("DECIMAL(12, 1)");
expr("element(multiset['1'])")
.columnType("CHAR(1) NOT NULL");
.columnType("CHAR(1)");
expr("element(multiset[1e-2])")
.columnType("DOUBLE NOT NULL");
.columnType("DOUBLE");
expr("element(multiset[multiset[cast(null as tinyint)]])")
.columnType("TINYINT MULTISET NOT NULL");
// Test case for <a href="https://issues.apache.org/jira/projects/CALCITE/issues/CALCITE-6227">
// ELEMENT(NULL) causes an assertion failure</a>.
.columnType("TINYINT MULTISET");
// Test case for https://issues.apache.org/jira/projects/CALCITE/issues/CALCITE-6227
// ELEMENT(NULL) causes an assertion failure.
expr("element(null)")
.columnType("NULL");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9816,7 +9816,7 @@ private static void checkDecodeFunc(SqlOperatorFixture f) {
@Test void testElementFunc() {
final SqlOperatorFixture f = fixture();
f.setFor(SqlStdOperatorTable.ELEMENT, VM_FENNEL, VM_JAVA);
f.checkString("element(multiset['abc'])", "abc", "CHAR(3) NOT NULL");
f.checkString("element(multiset['abc'])", "abc", "CHAR(3)");
f.checkNull("element(multiset[cast(null as integer)])");
}

Expand Down

0 comments on commit f837ffa

Please sign in to comment.