Skip to content

Commit

Permalink
validate cast
Browse files Browse the repository at this point in the history
  • Loading branch information
rui-mo committed Jan 13, 2023
1 parent c0e221b commit f88cc23
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions velox/connectors/hive/HiveConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ bool testFilters(
template <TypeKind ToKind>
velox::variant convertFromString(const std::optional<std::string>& value) {
if (value.has_value()) {
// No need for casting if ToKind is VARCHAR or VARBINARY.
if constexpr (ToKind == TypeKind::VARCHAR || ToKind == TypeKind::VARBINARY) {
return velox::variant(value.value());
}
Expand Down
12 changes: 0 additions & 12 deletions velox/substrait/SubstraitToVeloxExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,18 +456,6 @@ SubstraitVeloxExprConverter::toVeloxExpr(

std::vector<core::TypedExprPtr> inputs{
toVeloxExpr(castExpr.input(), inputType)};

// TODO: refactor this part for input type validation.
for (const auto& input : inputs) {
switch (input->type()->kind()) {
case TypeKind::ARRAY: {
// Cast from array type is not supported. See CastExpr::applyCast.
VELOX_UNSUPPORTED("Invalid from type in casting: {}", input->type());
}
default: {
}
}
}
return std::make_shared<core::CastTypedExpr>(type, inputs, nullOnFailure);
}

Expand Down
24 changes: 24 additions & 0 deletions velox/substrait/SubstraitToVeloxPlanValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ bool SubstraitToVeloxPlanValidator::validateLiteral(
return true;
}

bool SubstraitToVeloxPlanValidator::validateCast(
const ::substrait::Expression::Cast& castExpr,
const RowTypePtr& inputType) {
std::vector<core::TypedExprPtr> inputs{
exprConverter_->toVeloxExpr(castExpr.input(), inputType)};

// Casting from some types is not supported. See CastExpr::applyCast.
for (const auto& input : inputs) {
switch (input->type()->kind()) {
case TypeKind::ARRAY:
case TypeKind::MAP:
case TypeKind::ROW:
case TypeKind::VARBINARY:
VLOG(1) << "Invalid input type in casting: " << input->type() << ".";
return false;
default: {
}
}
}
return true;
}

bool SubstraitToVeloxPlanValidator::validateExpression(
const ::substrait::Expression& expression,
const RowTypePtr& inputType) {
Expand All @@ -104,6 +126,8 @@ bool SubstraitToVeloxPlanValidator::validateExpression(
return validateScalarFunction(expression.scalar_function(), inputType);
case ::substrait::Expression::RexTypeCase::kLiteral:
return validateLiteral(expression.literal(), inputType);
case ::substrait::Expression::RexTypeCase::kCast:
return validateCast(expression.cast(), inputType);
default:
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions velox/substrait/SubstraitToVeloxPlanValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ class SubstraitToVeloxPlanValidator {
const ::substrait::Expression::ScalarFunction& scalarFunction,
const RowTypePtr& inputType);

/// Validate Substrait Cast expression.
bool validateCast(
const ::substrait::Expression::Cast& castExpr,
const RowTypePtr& inputType);

/// Validate Substrait expression.
bool validateExpression(
const ::substrait::Expression& expression,
Expand Down

0 comments on commit f88cc23

Please sign in to comment.