Skip to content

fix: Fix try(json_extract()) to suppress error from json parse #13683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions velox/functions/prestosql/JsonFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,9 @@ class JsonFunctionTemplate : public exec::VectorFunction {
}
nullResult = true;
context.setErrors(rows, std::current_exception());
} catch (const std::exception& e) {
nullResult = true;
context.setErrors(rows, std::current_exception());
}
}

Expand Down
11 changes: 11 additions & 0 deletions velox/functions/prestosql/tests/JsonFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,11 @@ TEST_F(JsonFunctionsTest, jsonExtract) {
VELOX_ASSERT_THROW(
jsonExtract(kJson, "concat($..category)"), "Invalid JSON path");
VELOX_ASSERT_THROW(jsonExtract(kJson, "$.store.keys()"), "Invalid JSON path");
VELOX_ASSERT_THROW(
jsonExtract(
R"({3436654998315577471:-768009352,3684989847712002091:-317930923,5235625120989803984:1278962211,6359026774420146638:651644866,6614027999037539496:528067092})",
"$.*"),
"The JSON document has an improper structure");

// Ensure User errors are captured in try() and not thrown.
EXPECT_EQ(
Expand All @@ -1112,6 +1117,12 @@ TEST_F(JsonFunctionsTest, jsonExtract) {
EXPECT_EQ(std::nullopt, jsonExtract(kJson, "max($..price)", true));
EXPECT_EQ(std::nullopt, jsonExtract(kJson, "concat($..category)", true));
EXPECT_EQ(std::nullopt, jsonExtract(kJson, "$.store.keys()", true));
EXPECT_EQ(
std::nullopt,
jsonExtract(
R"({3436654998315577471:-768009352,3684989847712002091:-317930923,5235625120989803984:1278962211,6359026774420146638:651644866,6614027999037539496:528067092})",
"$.*",
true));
}

TEST_F(JsonFunctionsTest, jsonExtractVarcharInput) {
Expand Down
Loading