Skip to content

Commit

Permalink
Added json_format udf to Velox (#3398)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #3398

Pull Request resolved: #1567

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: 92ce819c681669af1e5206719079a5fe888d8e12
  • Loading branch information
Arpit Porwal authored and facebook-github-bot committed Dec 2, 2022
1 parent 1eaa9a4 commit c27a3cf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
11 changes: 11 additions & 0 deletions velox/functions/prestosql/JsonFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@

namespace facebook::velox::functions {

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

FOLLY_ALWAYS_INLINE void call(
out_type<Varchar>& result,
const arg_type<Json>& json) {
UDFOutputString::assign(result, folly::toJson(folly::parseJson(json)));
}
};

template <typename T>
struct IsJsonScalarFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void registerJsonFunctions() {
{"json_array_contains"});
registerFunction<JsonArrayContainsFunction, bool, Json, Varchar>(
{"json_array_contains"});
registerFunction<JsonFormatFunction, Varchar, Json>({"json_format"});
}

} // namespace facebook::velox::functions
48 changes: 39 additions & 9 deletions velox/functions/prestosql/tests/JsonFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ namespace {

class JsonFunctionsTest : public functions::test::FunctionBaseTest {
public:
std::optional<std::string> json_format(std::optional<std::string> json) {
return evaluateOnce<std::string>("json_format(c0)", json);
}

std::optional<bool> is_json_scalar(std::optional<std::string> json) {
return evaluateOnce<bool>("is_json_scalar(c0)", json);
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -145,7 +167,8 @@ TEST_F(JsonFunctionsTest, jsonArrayContainsBool) {
EXPECT_EQ(
json_array_contains<bool>(
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(
Expand Down Expand Up @@ -185,12 +208,14 @@ TEST_F(JsonFunctionsTest, jsonArrayContainsInt) {
false);
EXPECT_EQ(
json_array_contains<int64_t>(
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<int64_t>(
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);
}
Expand Down Expand Up @@ -224,12 +249,14 @@ TEST_F(JsonFunctionsTest, jsonArrayContainsDouble) {
false);
EXPECT_EQ(
json_array_contains<double>(
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<double>(
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);
}
Expand Down Expand Up @@ -267,19 +294,22 @@ TEST_F(JsonFunctionsTest, jsonArrayContainsString) {
false);
EXPECT_EQ(
json_array_contains<std::string>(
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<std::string>(
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<std::string>(
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(
Expand Down

0 comments on commit c27a3cf

Please sign in to comment.