Skip to content

Commit

Permalink
[CALCITE-5976] Use explicit casting if inserted element type in Array…
Browse files Browse the repository at this point in the history
…Prepend/ArrayAppend/ArrayInsert does not equal derived component type
  • Loading branch information
caicancai committed Mar 1, 2024
1 parent cf664f8 commit d4881bb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ private static RelDataType arrayAppendPrependReturnType(SqlOperatorBinding opBin
if (elementType.isNullable()) {
type = opBinding.getTypeFactory().createTypeWithNullability(type, true);
}
if (!componentType.equalsSansFieldNames(elementType)) {
if (!componentType.isNullable() && !componentType.equalsSansFieldNames(elementType)) {
SqlValidatorUtil.
adjustTypeForArrayFunctionConstructor(type, opBinding, 0);
SqlValidatorUtil.
Expand Down Expand Up @@ -1280,7 +1280,7 @@ private static RelDataType arrayInsertReturnType(SqlOperatorBinding opBinding) {
type = opBinding.getTypeFactory().createTypeWithNullability(type, true);
}

if (!componentType.equalsSansFieldNames(elementType)) {
if (!componentType.isNullable() && !componentType.equalsSansFieldNames(elementType)) {
SqlValidatorUtil.
adjustTypeForArrayFunctionConstructor(type, opBinding, 0);
SqlValidatorUtil.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,13 @@ public static void adjustTypeForArrayConstructor(
}
}

/**
* When the array element does not equal the biggest type, make explicit casting.
*
* @param componentType derived array component type
* @param opBinding description of call
* @param index index of opBinding
*/
public static void adjustTypeForArrayFunctionConstructor(
RelDataType componentType, SqlOperatorBinding opBinding, int index) {
if (opBinding instanceof SqlCallBinding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6341,7 +6341,7 @@ void checkRegexpExtract(SqlOperatorFixture f0, FunctionAlias functionAlias) {
f.checkType("array_append(cast(null as integer array), 1)", "INTEGER NOT NULL ARRAY");
f.checkFails("^array_append(array[1, 2], true)^",
"INTEGER is not comparable to BOOLEAN", false);

// cast biggest type
f.checkScalar("array_append(array(1), cast(2 as tinyint))", "[1, 2]",
"INTEGER NOT NULL ARRAY NOT NULL");
f.checkScalar("array_append(array(cast(1 as double)), cast(2 as float))", "[1.0, 2.0]",
Expand Down Expand Up @@ -6617,7 +6617,7 @@ void checkRegexpExtract(SqlOperatorFixture f0, FunctionAlias functionAlias) {
f.checkType("array_prepend(cast(null as integer array), 1)", "INTEGER NOT NULL ARRAY");
f.checkFails("^array_prepend(array[1, 2], true)^",
"INTEGER is not comparable to BOOLEAN", false);

// cast biggest type
f.checkScalar("array_prepend(array(1), cast(3 as float))", "[3.0, 1.0]",
"FLOAT NOT NULL ARRAY NOT NULL");
f.checkScalar("array_prepend(array(1), cast(3 as bigint))", "[3, 1]",
Expand Down Expand Up @@ -6922,7 +6922,7 @@ void checkRegexpExtract(SqlOperatorFixture f0, FunctionAlias functionAlias) {
"(INTEGER NOT NULL, CHAR(1) NOT NULL) MAP NOT NULL ARRAY NOT NULL");
f1.checkScalar("array_insert(array[map[1, 'a']], -1, map[2, 'b'])", "[{2=b}, {1=a}]",
"(INTEGER NOT NULL, CHAR(1) NOT NULL) MAP NOT NULL ARRAY NOT NULL");

// cast biggest type
f1.checkScalar("array_insert(array(1, 2, 3), 3, cast(4 as tinyint))",
"[1, 2, 4, 3]", "INTEGER NOT NULL ARRAY NOT NULL");
f1.checkScalar("array_insert(array(1, 2, 3), 3, cast(4 as double))",
Expand Down

0 comments on commit d4881bb

Please sign in to comment.