Skip to content

Commit

Permalink
Add json_array_length Presto Json function (#2294)
Browse files Browse the repository at this point in the history
Summary:
Adds Json function json_array_length to find the length of Json arrays, if the Json object doesn't represent an array, return NULL.

Pull Request resolved: #2294

Reviewed By: Yuhta

Differential Revision: D38721693

Pulled By: kagamiori

fbshipit-source-id: 39df39eea3f91e9903ffddcb0b6b0e3f69754b37
  • Loading branch information
pramodsatya authored and facebook-github-bot committed Aug 16, 2022
1 parent 098cdf9 commit 8b17442
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions velox/docs/functions/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ JSON Functions
SELECT is_json_scalar('1'); *-- true*
SELECT is_json_scalar('[1, 2, 3]'); *-- false*

.. function:: json_array_length(json) -> bigint

Returns the array length of ``json`` (a string containing a JSON
array). Returns NULL if ``json`` is not an array::

SELECT json_array_length('[1, 2, 3]');

============
JSON Vectors
============
Expand Down
17 changes: 17 additions & 0 deletions velox/functions/prestosql/JsonFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,21 @@ struct JsonExtractScalarFunction {
}
};

template <typename T>
struct JsonArrayLengthFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);

FOLLY_ALWAYS_INLINE bool call(
int64_t& result,
const arg_type<Varchar>& json) {
auto parsedJson = folly::parseJson(json);
if (!parsedJson.isArray()) {
return false;
}

result = parsedJson.size();
return true;
}
};

} // namespace facebook::velox::functions
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ void registerJsonFunctions() {
registerFunction<IsJsonScalarFunction, bool, Varchar>({"is_json_scalar"});
registerFunction<JsonExtractScalarFunction, Varchar, Varchar, Varchar>(
{"json_extract_scalar"});
registerFunction<JsonArrayLengthFunction, int64_t, Varchar>(
{"json_array_length"});
}

} // namespace facebook::velox::functions
22 changes: 22 additions & 0 deletions velox/functions/prestosql/tests/JsonFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class JsonFunctionsTest : public functions::test::FunctionBaseTest {
std::optional<bool> is_json_scalar(std::optional<std::string> json) {
return evaluateOnce<bool>("is_json_scalar(c0)", json);
}

std::optional<int64_t> json_array_length(std::optional<std::string> json) {
return evaluateOnce<int64_t>("json_array_length(c0)", json);
}
};

TEST_F(JsonFunctionsTest, isJsonScalar) {
Expand All @@ -45,6 +49,24 @@ TEST_F(JsonFunctionsTest, isJsonScalar) {
EXPECT_EQ(is_json_scalar(R"({"k1":""})"), false);
}

TEST_F(JsonFunctionsTest, jsonArrayLength) {
EXPECT_EQ(json_array_length(R"([])"), 0);
EXPECT_EQ(json_array_length(R"([1])"), 1);
EXPECT_EQ(json_array_length(R"([1, 2, 3])"), 3);
EXPECT_EQ(
json_array_length(
R"([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])"),
20);

EXPECT_EQ(json_array_length(R"(1)"), std::nullopt);
EXPECT_EQ(json_array_length(R"("hello")"), std::nullopt);
EXPECT_EQ(json_array_length(R"("")"), std::nullopt);
EXPECT_EQ(json_array_length(R"(true)"), std::nullopt);
EXPECT_EQ(json_array_length(R"({"k1":"v1"})"), std::nullopt);
EXPECT_EQ(json_array_length(R"({"k1":[0,1,2]})"), std::nullopt);
EXPECT_EQ(json_array_length(R"({"k1":[0,1,2], "k2":"v1"})"), std::nullopt);
}

} // namespace

} // namespace facebook::velox::functions::prestosql

0 comments on commit 8b17442

Please sign in to comment.