This is an automated email from the ASF dual-hosted git repository. xiong 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 abb3e5be43 [CALCITE-5961] Type inference of ARRAY_COMPACT is incorrect abb3e5be43 is described below commit abb3e5be43c9d030bea6489cc8ee42a2905fc4e8 Author: Ran Tao <chucheng...@gmail.com> AuthorDate: Thu Aug 31 20:18:12 2023 +0800 [CALCITE-5961] Type inference of ARRAY_COMPACT is incorrect --- .../org/apache/calcite/sql/fun/SqlLibraryOperators.java | 17 ++++++++++++++++- .../java/org/apache/calcite/test/SqlOperatorTest.java | 10 +++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java index f850ab1cc0..2835184788 100644 --- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java +++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java @@ -1093,11 +1093,26 @@ public abstract class SqlLibraryOperators { SqlLibraryOperators::arrayAppendPrependReturnType, OperandTypes.ARRAY_ELEMENT); + @SuppressWarnings("argument.type.incompatible") + private static RelDataType arrayCompactReturnType(SqlOperatorBinding opBinding) { + final RelDataType arrayType = opBinding.collectOperandTypes().get(0); + if (arrayType.getSqlTypeName() == SqlTypeName.NULL) { + return arrayType; + } + RelDataType type = arrayType.getComponentType(); + // force set nullable=false, and there are no side effects for 'NULL' type + if (type != null && type.isNullable()) { + type = opBinding.getTypeFactory().createTypeWithNullability(type, false); + } + requireNonNull(type, "inferred array element type"); + return SqlTypeUtil.createArrayType(opBinding.getTypeFactory(), type, arrayType.isNullable()); + } + /** The "ARRAY_COMPACT(array)" function. */ @LibraryOperator(libraries = {SPARK}) public static final SqlFunction ARRAY_COMPACT = SqlBasicFunction.create(SqlKind.ARRAY_COMPACT, - ReturnTypes.ARG0_NULLABLE, + SqlLibraryOperators::arrayCompactReturnType, OperandTypes.ARRAY); /** The "ARRAY_CONCAT(array [, array]*)" function. */ diff --git a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java index e4b774ac59..f322eb6b9c 100644 --- a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java +++ b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java @@ -5970,11 +5970,15 @@ public class SqlOperatorTest { final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.SPARK); f.checkScalar("array_compact(array[null, 1, null, 2])", "[1, 2]", - "INTEGER ARRAY NOT NULL"); + "INTEGER NOT NULL ARRAY NOT NULL"); + f.checkScalar("array_compact(array[1, 2])", "[1, 2]", + "INTEGER NOT NULL ARRAY NOT NULL"); + f.checkScalar("array_compact(array[null, 'hello', null, 'world'])", "[hello, world]", + "CHAR(5) NOT NULL ARRAY NOT NULL"); + f.checkScalar("array_compact(array['hello', 'world'])", "[hello, world]", + "CHAR(5) NOT NULL ARRAY NOT NULL"); f.checkScalar("array_compact(array[null])", "[]", "NULL ARRAY NOT NULL"); - f.checkScalar("array_compact(array(null))", "[]", - "NULL ARRAY NOT NULL"); f.checkScalar("array_compact(array())", "[]", "UNKNOWN NOT NULL ARRAY NOT NULL"); f.checkNull("array_compact(null)");