From 5bdf28ff1ddfe7ed435f091a13ac62b9209776ed Mon Sep 17 00:00:00 2001 From: Arpit Porwal Date: Fri, 2 Dec 2022 21:28:58 -0800 Subject: [PATCH] Add json_format Presto function (#3398) Summary: Pull Request resolved: https://github.com/facebookincubator/velox/pull/3398 Add 'json_format' udf to prestosql. Doc: https://prestodb.io/docs/current/functions/json.html?highlight=json_format#json_format Differential Revision: D41643635 fbshipit-source-id: 50f570c735a538ea17513befcaa1d2f765e8cf53 --- velox/docs/functions/json.rst | 7 +++ velox/functions/prestosql/JsonFunctions.h | 11 +++++ .../JsonFunctionsRegistration.cpp | 1 + .../prestosql/tests/JsonFunctionsTest.cpp | 48 +++++++++++++++---- 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/velox/docs/functions/json.rst b/velox/docs/functions/json.rst index 873239dc84b9c..031272e44d2d8 100644 --- a/velox/docs/functions/json.rst +++ b/velox/docs/functions/json.rst @@ -124,6 +124,13 @@ JSON Functions SELECT json_array_contains('[1, 2, 3]', 2); +.. function:: json_format(json) -> varchar + + Serializes the input JSON value to JSON text conforming to RFC 7159. + The JSON value can be a JSON object, a JSON array, a JSON string, a JSON number, true, false or null + + SELECT json_format('{"k1":1,"k2":23,"k3":456}'); + ============ JSON Vectors ============ diff --git a/velox/functions/prestosql/JsonFunctions.h b/velox/functions/prestosql/JsonFunctions.h index a2a3c2d2c28ca..36513e9b2fbe3 100644 --- a/velox/functions/prestosql/JsonFunctions.h +++ b/velox/functions/prestosql/JsonFunctions.h @@ -20,6 +20,17 @@ namespace facebook::velox::functions { +template +struct JsonFormatFunction { + VELOX_DEFINE_FUNCTION_TYPES(T); + + FOLLY_ALWAYS_INLINE void call( + out_type& result, + const arg_type& json) { + UDFOutputString::assign(result, folly::toJson(folly::parseJson(json))); + } +}; + template struct IsJsonScalarFunction { VELOX_DEFINE_FUNCTION_TYPES(T); diff --git a/velox/functions/prestosql/registration/JsonFunctionsRegistration.cpp b/velox/functions/prestosql/registration/JsonFunctionsRegistration.cpp index 8fb7bec3eec23..3a806cd1f255f 100644 --- a/velox/functions/prestosql/registration/JsonFunctionsRegistration.cpp +++ b/velox/functions/prestosql/registration/JsonFunctionsRegistration.cpp @@ -34,6 +34,7 @@ void registerJsonFunctions() { {"json_array_contains"}); registerFunction( {"json_array_contains"}); + registerFunction({"json_format"}); } } // namespace facebook::velox::functions diff --git a/velox/functions/prestosql/tests/JsonFunctionsTest.cpp b/velox/functions/prestosql/tests/JsonFunctionsTest.cpp index 570bc59adaf44..9a6a30b29705f 100644 --- a/velox/functions/prestosql/tests/JsonFunctionsTest.cpp +++ b/velox/functions/prestosql/tests/JsonFunctionsTest.cpp @@ -24,6 +24,10 @@ namespace { class JsonFunctionsTest : public functions::test::FunctionBaseTest { public: + std::optional json_format(std::optional json) { + return evaluateOnce("json_format(c0)", json); + } + std::optional is_json_scalar(std::optional json) { return evaluateOnce("is_json_scalar(c0)", json); } @@ -52,6 +56,23 @@ class JsonFunctionsTest : public functions::test::FunctionBaseTest { } }; +TEST_F(JsonFunctionsTest, JsonFormatSignatures) { + auto signatures = getSignatureStrings("json_format"); + ASSERT_EQ(1, signatures.size()); + ASSERT_EQ(1, signatures.count("(json) -> varchar")); +} + +TEST_F(JsonFunctionsTest, JsonFormat) { + EXPECT_EQ(json_format(R"(true)"), "true"); + EXPECT_EQ(json_format(R"(null)"), "null"); + EXPECT_EQ(json_format(R"(42)"), "42"); + EXPECT_EQ(json_format(R"("abc")"), "\"abc\""); + EXPECT_EQ(json_format(R"([1, 2, 3])"), "[1,2,3]"); + EXPECT_EQ(json_format(R"({"k1":"v1"})"), "{\"k1\":\"v1\"}"); + EXPECT_EQ( + json_format(R"({"k1":"v1","k2":"v1"})"), "{\"k2\":\"v1\",\"k1\":\"v1\"}"); +} + TEST_F(JsonFunctionsTest, isJsonScalarSignatures) { auto signatures = getSignatureStrings("is_json_scalar"); ASSERT_EQ(1, signatures.size()); @@ -106,7 +127,8 @@ TEST_F(JsonFunctionsTest, jsonArrayLength) { 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])"), + 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); @@ -145,7 +167,8 @@ TEST_F(JsonFunctionsTest, jsonArrayContainsBool) { EXPECT_EQ( json_array_contains( R"([false, false, false, false, false, false, false, -false, false, false, false, false, false, true, false, false, false, false])", +false, false, false, false, false, false, true, false, false, false, +false])", true), true); EXPECT_EQ( @@ -185,12 +208,14 @@ TEST_F(JsonFunctionsTest, jsonArrayContainsInt) { false); EXPECT_EQ( json_array_contains( - R"([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])", + R"([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, + 19, 20])", 17), true); EXPECT_EQ( json_array_contains( - R"([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])", + R"([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, + 19, 20])", 23), false); } @@ -224,12 +249,14 @@ TEST_F(JsonFunctionsTest, jsonArrayContainsDouble) { false); EXPECT_EQ( json_array_contains( - R"([1.2, 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5])", + R"([1.2, 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5, 1.2, + 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5])", 4.5), true); EXPECT_EQ( json_array_contains( - R"([1.2, 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5])", + R"([1.2, 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5, 1.2, + 2.3, 3.4, 4.5, 1.2, 2.3, 3.4, 4.5])", 4.3), false); } @@ -267,19 +294,22 @@ TEST_F(JsonFunctionsTest, jsonArrayContainsString) { false); EXPECT_EQ( json_array_contains( - R"(["hello", "presto", "world", "hello", "presto", "world", "hello", "presto", "world", "hello", + R"(["hello", "presto", "world", "hello", "presto", "world", + "hello", "presto", "world", "hello", "presto", "world", "hello", "presto", "world", "hello", "presto", "world"])", "hello"), true); EXPECT_EQ( json_array_contains( - R"(["hello", "presto", "world", "hello", "presto", "world", "hello", "presto", "world", "hello", + R"(["hello", "presto", "world", "hello", "presto", "world", + "hello", "presto", "world", "hello", "presto", "world", "hello", "presto", "world", "hello", "presto", "world"])", "hola"), false); EXPECT_EQ( json_array_contains( - R"(["hello", "presto", "world", 1, 2, 3, true, false, 1.2, 2.3, {"k1":[0,1,2], "k2":"v1"}])", + R"(["hello", "presto", "world", 1, 2, 3, true, false, 1.2, 2.3, + {"k1":[0,1,2], "k2":"v1"}])", "world"), true); EXPECT_EQ(