Skip to content

Commit

Permalink
Allow different decimal types in switch expr
Browse files Browse the repository at this point in the history
  • Loading branch information
rui-mo committed Oct 17, 2023
1 parent 56dbf43 commit 6be5f36
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions velox/expression/SwitchExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ SwitchExpr::SwitchExpr(

// Apply type checking.
auto typeExpected = resolveType(inputTypes);
VELOX_CHECK(
*typeExpected == *this->type(),
"Switch expression type different than then clause. Expected {} but got Actual {}.",
typeExpected->toString(),
this->type()->toString());
if (typeExpected->isDecimal()) {
// Regard decimal types as the same regardless of precision and scale.
VELOX_CHECK(this->type()->isDecimal());
} else {
VELOX_CHECK(
*typeExpected == *this->type(),
"Switch expression type different than then clause. Expected {} but got Actual {}.",
typeExpected->toString(),
this->type()->toString());
}
}

void SwitchExpr::evalSpecialForm(
Expand Down Expand Up @@ -254,13 +259,17 @@ TypePtr SwitchExpr::resolveType(const std::vector<TypePtr>& argTypes) {

if (hasElse) {
auto& elseClauseType = argTypes.back();

VELOX_CHECK(
*elseClauseType == *expressionType,
"Else clause of a SWITCH statement must have the same type as 'then' clauses. "
"Expected {}, but got {}.",
expressionType->toString(),
elseClauseType->toString());
if (elseClauseType->isDecimal()) {
// Regard decimals as the same type regardless of precision and scale.
VELOX_CHECK(expressionType->isDecimal());
} else {
VELOX_CHECK(
*elseClauseType == *expressionType,
"Else clause of a SWITCH statement must have the same type as 'then' clauses. "
"Expected {}, but got {}.",
expressionType->toString(),
elseClauseType->toString());
}
}

return expressionType;
Expand Down

0 comments on commit 6be5f36

Please sign in to comment.