From e125eb40a5a326dc2e2620bcb0c2d9bb1cf582df Mon Sep 17 00:00:00 2001 From: rui-mo Date: Mon, 16 Oct 2023 11:26:23 +0800 Subject: [PATCH] Allow decimal types in switch expr --- velox/expression/SwitchExpr.cpp | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/velox/expression/SwitchExpr.cpp b/velox/expression/SwitchExpr.cpp index 5f6d493ad9a4c..dc1431b531223 100644 --- a/velox/expression/SwitchExpr.cpp +++ b/velox/expression/SwitchExpr.cpp @@ -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( @@ -254,13 +259,17 @@ TypePtr SwitchExpr::resolveType(const std::vector& 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;