Skip to content

Commit

Permalink
[CALCITE-5862] Incorrect spark semantics of array function when eleme…
Browse files Browse the repository at this point in the history
…nts have Numeric and Character types
  • Loading branch information
chucheng92 committed Aug 1, 2023
1 parent 3ab8003 commit 9846350
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -955,10 +955,41 @@ 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();
final List<RelDataType> operandTypes = opBinding.collectOperandTypes();

// only numeric & character types check
boolean hasNumeric = false;
boolean hasCharacter = false;
for (RelDataType type : operandTypes) {
SqlTypeFamily family = type.getSqlTypeName().getFamily();
requireNonNull(family, "array element type family");
switch (family) {
case NUMERIC:
hasNumeric = true;
break;
case CHARACTER:
hasCharacter = true;
break;
default:
break;
}
}

RelDataType type;
boolean useCharacterTypes = hasNumeric && hasCharacter;
if (useCharacterTypes) {
List<RelDataType> characterTypes =
// may include NULL literal
operandTypes.stream().filter(
t -> t.getSqlTypeName().getFamily() != SqlTypeFamily.NUMERIC)
.collect(Collectors.toList());
type = opBinding.getTypeFactory().leastRestrictive(characterTypes);
} 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 9846350

Please sign in to comment.