Skip to content

Commit

Permalink
[CALCITE-5862] Incorrect semantics of array function when elements ha…
Browse files Browse the repository at this point in the history
…ve Numeric and Character types(enabled in Spark Library)
  • Loading branch information
chucheng92 committed Jul 19, 2023
1 parent 3ab8003 commit 6e01d03
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -955,10 +955,38 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
.withKind(SqlKind.CONCAT_WS_MSSQL);

private static RelDataType arrayReturnType(SqlOperatorBinding opBinding) {
RelDataType type =
opBinding.getOperandCount() > 0
? ReturnTypes.LEAST_RESTRICTIVE.inferReturnType(opBinding)
: opBinding.getTypeFactory().createUnknownType();
// only numeric & character types check
List<RelDataType> numericTypes = new ArrayList<>();
List<RelDataType> characterTypes = new ArrayList<>();
List<RelDataType> nullTypes = new ArrayList<>();
for (int i = 0; i < opBinding.getOperandCount(); i++) {
SqlTypeFamily family = opBinding.getOperandType(i).getSqlTypeName().getFamily();
if (family == SqlTypeFamily.NUMERIC) {
numericTypes.add(opBinding.getOperandType(i));
} else if (family == SqlTypeFamily.CHARACTER) {
characterTypes.add(opBinding.getOperandType(i));
} else if (family == SqlTypeFamily.NULL) {
nullTypes.add(opBinding.getOperandType(i));
} else {
break;
}
}

boolean onlyNumericCharacterTypes = opBinding.getOperandCount() > 0
&& numericTypes.size() > 0 && characterTypes.size() > 0
&& numericTypes.size() + characterTypes.size() + nullTypes.size()
== opBinding.getOperandCount();
RelDataType type;
if (onlyNumericCharacterTypes) {
List<RelDataType> types = new ArrayList<>(characterTypes);
types.addAll(nullTypes);
type = opBinding.getTypeFactory().leastRestrictive(types);
} else {
type =
opBinding.getOperandCount() > 0
? ReturnTypes.LEAST_RESTRICTIVE.inferReturnType(opBinding)
: opBinding.getTypeFactory().createUnknownType();
}
requireNonNull(type, "inferred array element type");
return SqlTypeUtil.createArrayType(opBinding.getTypeFactory(), type, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8894,6 +8894,14 @@ private static void checkArrayConcatAggFuncFails(SqlOperatorFixture t) {
"[null, foo]", "CHAR(3) ARRAY NOT NULL");
f2.checkScalar("array(null)",
"[null]", "NULL ARRAY NOT NULL");
f2.checkScalar("array(1, 2, 'Hi')",
"[1, 2, Hi]", "CHAR(2) NOT NULL ARRAY NOT NULL");
f2.checkScalar("array(1, 2, 'Hi', 'Hello')",
"[1, 2, Hi, Hello]", "CHAR(5) NOT NULL ARRAY NOT NULL");
f2.checkScalar("array(1, 2, 'Hi', null)",
"[1, 2, Hi, null]", "CHAR(2) ARRAY NOT NULL");
f2.checkScalar("array(1, 2, 'Hi', cast(null as char(10)))",
"[1, 2, Hi, null]", "CHAR(10) ARRAY NOT NULL");
}

/**
Expand Down

0 comments on commit 6e01d03

Please sign in to comment.