Skip to content
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

Add json_array_length Presto Json function #2294

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 7 additions & 0 deletions velox/docs/functions/json.rst
Original file line number Diff line number Diff line change
@@ -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
============
17 changes: 17 additions & 0 deletions velox/functions/prestosql/JsonFunctions.h
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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) {
@@ -43,6 +47,24 @@ TEST_F(JsonFunctionsTest, isJsonScalar) {
EXPECT_EQ(is_json_scalar(R"({"k1":"v1"})"), false);
EXPECT_EQ(is_json_scalar(R"({"k1":[0,1,2]})"), false);
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