Skip to content
Open
Show file tree
Hide file tree
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
57 changes: 57 additions & 0 deletions bolt/functions/prestosql/SIMDJsonFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ struct SIMDIsJsonScalarFunction {
}
};

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

FOLLY_ALWAYS_INLINE static void call(
bool& result,
const arg_type<Json>& json) {
ParserContext ctx(json.data(), json.size());
try {
ctx.parseDocument();
result = (ctx.jsonDoc.type() == simdjson::ondemand::json_type::object);
} catch (const simdjson::simdjson_error&) {
result = false;
}
}
};

template <typename T>
struct SonicIsJsonScalarFunction {
BOLT_DEFINE_FUNCTION_TYPES(T);
Expand All @@ -105,6 +122,23 @@ struct SonicIsJsonScalarFunction {
}
};

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

FOLLY_ALWAYS_INLINE static void call(
bool& result,
const arg_type<Json>& json) {
sonic_json::Document jsonDoc;
jsonDoc.Parse(json.data(), json.size());
if (jsonDoc.HasParseError()) {
result = false;
return;
}
result = jsonDoc.IsObject();
}
};

template <typename T>
class WrapperIsJsonScalarFunction {
public:
Expand All @@ -128,6 +162,29 @@ class WrapperIsJsonScalarFunction {
bool useSonic = true;
};

template <typename T>
class WrapperIsJsonObjectFunction {
public:
VELOX_DEFINE_FUNCTION_TYPES(T);

FOLLY_ALWAYS_INLINE void initialize(
const std::vector<TypePtr>& /*inputTypes*/,
const core::QueryConfig& config,
const arg_type<Json>* /*json*/) {
useSonic = config.enableSonicIsJsonScalar();
}

FOLLY_ALWAYS_INLINE void call(bool& result, const arg_type<Json>& json) {
if (useSonic) {
SonicIsJsonObjectFunction<T>::call(result, json);
} else {
SIMDIsJsonObjectFunction<T>::call(result, json);
}
}

bool useSonic = true;
};

template <typename T>
struct SIMDJsonArrayContainsFunction {
BOLT_DEFINE_FUNCTION_TYPES(T);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ void registerJsonFunctions(const std::string& prefix) {
registerFunction<WrapperIsJsonScalarFunction, bool, Varchar>(
{prefix + "is_json_scalar"});

registerFunction<WrapperIsJsonObjectFunction, bool, Json>(
{prefix + "is_json_object"});
registerFunction<WrapperIsJsonObjectFunction, bool, Varchar>(
{prefix + "is_json_object"});

registerFunction<WrapperJsonExtractScalarFunction, Varchar, Json, Varchar>(
{prefix + "json_extract_scalar"});
registerFunction<WrapperJsonExtractScalarFunction, Varchar, Varchar, Varchar>(
Expand Down
32 changes: 32 additions & 0 deletions bolt/functions/prestosql/tests/JsonFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ class JsonFunctionsTest : public functions::test::FunctionBaseTest {
return jsonResult;
}

std::optional<bool> isJsonObject(std::optional<std::string> json) {
auto [jsonVector, varcharVector] = makeVectors(json);
auto jsonResult =
evaluateOnce<bool>("is_json_object(c0)", makeRowVector({jsonVector}));
auto varcharResult = evaluateOnce<bool>(
"is_json_object(c0)", makeRowVector({varcharVector}));
EXPECT_EQ(jsonResult, varcharResult);
return jsonResult;
}

std::optional<int64_t> jsonArrayLength(std::optional<std::string> json) {
auto [jsonVector, varcharVector] = makeVectors(json);
auto jsonResult = evaluateOnce<int64_t>(
Expand Down Expand Up @@ -314,6 +324,13 @@ TEST_F(JsonFunctionsTest, isJsonScalarSignatures) {
ASSERT_EQ(1, signatures.count("(varchar) -> boolean"));
}

TEST_F(JsonFunctionsTest, isJsonObjectSignatures) {
auto signatures = getSignatureStrings("is_json_object");
ASSERT_EQ(2, signatures.size());
ASSERT_EQ(1, signatures.count("(json) -> boolean"));
ASSERT_EQ(1, signatures.count("(varchar) -> boolean"));
}

TEST_F(JsonFunctionsTest, jsonArrayLengthSignatures) {
auto signatures = getSignatureStrings("json_array_length");
ASSERT_EQ(2, signatures.size());
Expand Down Expand Up @@ -370,6 +387,21 @@ TEST_F(JsonFunctionsTest, isJsonScalar) {
EXPECT_EQ(isJsonScalar(R"({"k1":""})"), false);
}

TEST_F(JsonFunctionsTest, isJsonObject) {
EXPECT_EQ(isJsonObject(R"({"k1":"v1"})"), true);
EXPECT_EQ(isJsonObject(R"({"k1":[0,1,2]})"), true);
EXPECT_EQ(isJsonObject(R"({})"), true);

EXPECT_EQ(isJsonObject(R"(1)"), false);
EXPECT_EQ(isJsonObject(R"("hello")"), false);
EXPECT_EQ(isJsonObject(R"(true)"), false);
EXPECT_EQ(isJsonObject(R"(null)"), false);
EXPECT_EQ(isJsonObject(R"([1,2])"), false);

EXPECT_EQ(isJsonObject(R"(not_json)"), false);
EXPECT_EQ(isJsonObject(R"()"), false);
}

TEST_F(JsonFunctionsTest, jsonArrayLength) {
EXPECT_EQ(jsonArrayLength(R"([])"), 0);
EXPECT_EQ(jsonArrayLength(R"([1])"), 1);
Expand Down
Loading