Skip to content

Commit 49605f4

Browse files
Bikramjeet Vigfacebook-github-bot
Bikramjeet Vig
authored andcommitted
fix: Recursive selector in json path used with scalar json
Summary: Ensure that empty array is returned if the recursive selector is encountered while a json path even if the current node is a scalar. Differential Revision: D70926386
1 parent 58ff291 commit 49605f4

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

velox/functions/prestosql/json/SIMDJsonExtractor.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ simdjson::error_code SIMDJsonExtractor::extract(
135135
return consumer(jsonDoc);
136136
}
137137
VELOX_CHECK_GT(tokens_.size(), 0);
138-
if (tokens_[0].selector == JsonPathTokenizer::Selector::WILDCARD) {
138+
auto& selector = tokens_[0].selector;
139+
if (tokens_[0].selector == JsonPathTokenizer::Selector::WILDCARD ||
140+
selector == JsonPathTokenizer::Selector::RECURSIVE) {
139141
isDefinitePath = false;
140142
}
141143
return simdjson::SUCCESS;

velox/functions/prestosql/tests/JsonFunctionsTest.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,12 @@ TEST_F(JsonFunctionsTest, jsonExtract) {
10401040
EXPECT_EQ(
10411041
"[8.95,12.99,8.99,22.99,19.95,8.95,12.99,8.99,22.99,19.95,8.95,12.99,8.99,22.99]",
10421042
jsonExtract(kJson, "$..*..price"));
1043+
EXPECT_EQ("[]", jsonExtract(kJson, "$..nonExistentKey"));
1044+
EXPECT_EQ(std::nullopt, jsonExtract(kJson, "$.nonExistentKey..price"));
1045+
1046+
// Calling Recurssive opearator on a scalar
1047+
EXPECT_EQ("[]", jsonExtract(R"({"a": {"b": [123, 456]}})", "$.a.b.[0]..[0]"));
1048+
EXPECT_EQ("[]", jsonExtract("1", "$..key"));
10431049

10441050
// non-definite paths that end up being evaluated vs. not evaluated
10451051
EXPECT_EQ(

0 commit comments

Comments
 (0)