diff --git a/velox/docs/functions/json.rst b/velox/docs/functions/json.rst index 873239dc84b9c..83821ab8e9d3e 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(JSON '{"a": 1, "b": 2}') + ============ JSON Vectors ============ diff --git a/velox/functions/prestosql/JsonFunctions.h b/velox/functions/prestosql/JsonFunctions.h index a2a3c2d2c28ca..062b26a484d07 100644 --- a/velox/functions/prestosql/JsonFunctions.h +++ b/velox/functions/prestosql/JsonFunctions.h @@ -20,6 +20,18 @@ namespace facebook::velox::functions { +template +struct JsonFormatFunction { + VELOX_DEFINE_FUNCTION_TYPES(T); + FOLLY_ALWAYS_INLINE bool call( + out_type& jsonString, + const arg_type& json) { + folly::parseJson(json.getString()); + jsonString.setNoCopy(json); + return true; + } +}; + 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..8daf821481580 100644 --- a/velox/functions/prestosql/tests/JsonFunctionsTest.cpp +++ b/velox/functions/prestosql/tests/JsonFunctionsTest.cpp @@ -52,6 +52,37 @@ class JsonFunctionsTest : public functions::test::FunctionBaseTest { } }; +TEST_F(JsonFunctionsTest, JsonFormat) { + const auto json_format = [&](std::optional value) { + return evaluateOnce( + "json_format(c0)", {value}, {JSON()}); + }; + + 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\"}"); + + // check keys and values are there + const std::string jsonStr = json_format(R"({"k1":"v1","k2":"v2"})").value(); + folly::dynamic object = folly::parseJson(jsonStr); + + std::set keys{"k1", "k2"}; + + for (const auto& key : object.keys()) { + EXPECT_TRUE(keys.find(key.getString()) != keys.end()); + } + + std::unordered_map jsonMap{ + {"k1", "v1"}, {"k2", "v2"}}; + + for (const auto& key : jsonMap) { + EXPECT_EQ(object.at(key.first), jsonMap.at(key.first)); + } +} + TEST_F(JsonFunctionsTest, isJsonScalarSignatures) { auto signatures = getSignatureStrings("is_json_scalar"); ASSERT_EQ(1, signatures.size());