Skip to content

Commit

Permalink
Fix try_cast to throw VeloxRuntimeError (facebookincubator#10483)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebookincubator#10483

Before the fix, try_cast suppress all errors. But VeloxRuntimeError should not
be suppressed. This diff makes try_cast to still throw VeloxRuntimeError.

Reviewed By: kgpai

Differential Revision: D59838302

fbshipit-source-id: 84ab35d0e27c3c67877c5c206cac8a84732ec046
  • Loading branch information
kagamiori authored and facebook-github-bot committed Jul 17, 2024
1 parent c2fa517 commit 86efb63
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion velox/expression/CastExpr-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ void CastExpr::applyToSelectedNoThrowLocal(
rows.template applyToSelected([&](auto row) INLINE_LAMBDA {
try {
func(row);
} catch (...) {
} catch (const VeloxException& e) {
if (!e.isUserError()) {
throw;
}
result->setNull(row, true);
} catch (const std::exception&) {
result->setNull(row, true);
}
});
Expand Down
27 changes: 27 additions & 0 deletions velox/expression/tests/CastExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,15 @@ TEST_F(CastExprTest, timestampToString) {
};
VELOX_ASSERT_THROW(
mustThrow(), "Unable to convert timezone 'America/Los_Angeles' past");

// try_cast should also throw since it's runtime error.
auto tryCastMustThrow = [&]() {
return testTryCast<Timestamp, std::string>(
"string", {Timestamp(253405036800, 0)}, {"10000-02-01 08:00:00.000"});
};
VELOX_ASSERT_THROW(
tryCastMustThrow(),
"Unable to convert timezone 'America/Los_Angeles' past");
}

TEST_F(CastExprTest, dateToTimestamp) {
Expand Down Expand Up @@ -760,6 +769,24 @@ TEST_F(CastExprTest, timestampToDate) {
},
TIMESTAMP(),
DATE());

// Ensure external/date throws since it doesn't know how to convert large
// timestamps.
auto mustThrow = [&]() {
return testCast<Timestamp, int32_t>(
"date", {Timestamp(253405036800, 0)}, {0});
};
VELOX_ASSERT_THROW(
mustThrow(), "Unable to convert timezone 'America/Los_Angeles' past");

// try_cast should also throw since it's runtime error.
auto tryCastMustThrow = [&]() {
return testTryCast<Timestamp, int32_t>(
"date", {Timestamp(253405036800, 0)}, {0});
};
VELOX_ASSERT_THROW(
tryCastMustThrow(),
"Unable to convert timezone 'America/Los_Angeles' past");
}

TEST_F(CastExprTest, timestampInvalid) {
Expand Down

0 comments on commit 86efb63

Please sign in to comment.