Skip to content

Commit 35a1247

Browse files
Bikramjeet Vigfacebook-github-bot
Bikramjeet Vig
authored andcommitted
fix: Recursive selector in json path used with scalar json (facebookincubator#12595)
Summary: Pull Request resolved: facebookincubator#12595 Ensure that empty array is returned if the recursive selector is encountered while a json path even if the current node is a scalar. Reviewed By: kgpai Differential Revision: D70926386 fbshipit-source-id: f3853827bb5a5e5ae6f6425d62906c855855268b
1 parent c2eb47b commit 35a1247

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1035,11 +1035,19 @@ TEST_F(JsonFunctionsTest, jsonExtract) {
10351035
EXPECT_EQ(std::nullopt, jsonExtract("null", "$.foo"));
10361036
EXPECT_EQ(std::nullopt, jsonExtract("null", "$.[0]"));
10371037

1038-
// Recurssive opearator
1038+
// Recursive opearator
10391039
EXPECT_EQ("[8.95,12.99,8.99,22.99,19.95]", jsonExtract(kJson, "$..price"));
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+
EXPECT_EQ(
1046+
std::nullopt, jsonExtract(R"({"a": {"b": [123, 456]}})", "$.a.c..[0]"));
1047+
1048+
// Calling Recursive opearator on a scalar
1049+
EXPECT_EQ("[]", jsonExtract(R"({"a": {"b": [123, 456]}})", "$.a.b.[0]..[0]"));
1050+
EXPECT_EQ("[]", jsonExtract("1", "$..key"));
10431051

10441052
// non-definite paths that end up being evaluated vs. not evaluated
10451053
EXPECT_EQ(

0 commit comments

Comments
 (0)