diff --git a/velox/functions/prestosql/JsonFunctions.h b/velox/functions/prestosql/JsonFunctions.h index c31ad5968eb8..1596a95a0c94 100644 --- a/velox/functions/prestosql/JsonFunctions.h +++ b/velox/functions/prestosql/JsonFunctions.h @@ -71,6 +71,9 @@ struct JsonArrayContainsFunction { if (simdjsonParse(paddedJson).get(jsonDoc)) { return false; } + if (jsonDoc.type().error()) { + return false; + } if (jsonDoc.type() != simdjson::ondemand::json_type::array) { return false; @@ -132,6 +135,9 @@ struct JsonArrayLengthFunction { if (simdjsonParse(paddedJson).get(jsonDoc)) { return false; } + if (jsonDoc.type().error()) { + return false; + } if (jsonDoc.type() != simdjson::ondemand::json_type::array) { return false; @@ -159,6 +165,9 @@ struct JsonArrayGetFunction { if (simdjsonParse(paddedJson).get(jsonDoc)) { return false; } + if (jsonDoc.type().error()) { + return false; + } if (jsonDoc.type() != simdjson::ondemand::json_type::array) { return false; diff --git a/velox/functions/prestosql/tests/JsonFunctionsTest.cpp b/velox/functions/prestosql/tests/JsonFunctionsTest.cpp index a0452c82ba40..067d374411f3 100644 --- a/velox/functions/prestosql/tests/JsonFunctionsTest.cpp +++ b/velox/functions/prestosql/tests/JsonFunctionsTest.cpp @@ -356,6 +356,9 @@ TEST_F(JsonFunctionsTest, jsonArrayLength) { EXPECT_EQ(jsonArrayLength(R"({"k1":"v1"})"), std::nullopt); EXPECT_EQ(jsonArrayLength(R"({"k1":[0,1,2]})"), std::nullopt); EXPECT_EQ(jsonArrayLength(R"({"k1":[0,1,2], "k2":"v1"})"), std::nullopt); + + // Malformed Json. + EXPECT_EQ(jsonArrayLength(R"((})"), std::nullopt); } TEST_F(JsonFunctionsTest, jsonArrayGet) { @@ -372,6 +375,9 @@ TEST_F(JsonFunctionsTest, jsonArrayGet) { EXPECT_FALSE(arrayGet("{}", 1).has_value()); EXPECT_FALSE(arrayGet("[]", 1).has_value()); + // Malformed json. + EXPECT_FALSE(arrayGet("([1]})", 0).has_value()); + EXPECT_EQ(arrayGet("[1, 2, 3]", 0), "1"); EXPECT_EQ(arrayGet("[1, 2, 3]", 1), "2"); EXPECT_EQ(arrayGet("[1, 2, 3]", 2), "3"); @@ -589,6 +595,14 @@ TEST_F(JsonFunctionsTest, jsonArrayContainsString) { true); } +TEST_F(JsonFunctionsTest, jsonArrayContainsMalformed) { + auto [jsonVector, _] = makeVectors(R"([]})"); + EXPECT_EQ( + evaluateOnce( + "json_array_contains(c0, 'a')", makeRowVector({jsonVector})), + std::nullopt); +} + TEST_F(JsonFunctionsTest, jsonSize) { EXPECT_EQ(jsonSize(R"({"k1":{"k2": 999}, "k3": 1})", "$.k1.k2"), 0); EXPECT_EQ(jsonSize(R"({"k1":{"k2": 999}, "k3": 1})", "$.k1"), 1);