From 39f78802eb340860f5939e797661923a22b24544 Mon Sep 17 00:00:00 2001 From: Pramod Date: Thu, 18 Aug 2022 17:18:05 -0500 Subject: [PATCH] Address more review comments --- velox/functions/prestosql/JsonFunctions.h | 11 +++++++++- .../prestosql/tests/JsonFunctionsTest.cpp | 21 ++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/velox/functions/prestosql/JsonFunctions.h b/velox/functions/prestosql/JsonFunctions.h index b335a6da388e2..fa25e2222f67b 100644 --- a/velox/functions/prestosql/JsonFunctions.h +++ b/velox/functions/prestosql/JsonFunctions.h @@ -88,7 +88,16 @@ struct JsonArrayContainsFunction { result = false; for (const auto& v : parsedJson) { - if (v == value) { + if (std::is_same_v && v.isBool() && v == value) { + result = true; + break; + } else if (std::is_same_v && v.isInt() && v == value) { + result = true; + break; + } else if (std::is_same_v && v.isDouble() && v == value) { + result = true; + break; + } else if (v.isString() && v == value) { result = true; break; } diff --git a/velox/functions/prestosql/tests/JsonFunctionsTest.cpp b/velox/functions/prestosql/tests/JsonFunctionsTest.cpp index 88e36126dff3a..5f994086579c9 100644 --- a/velox/functions/prestosql/tests/JsonFunctionsTest.cpp +++ b/velox/functions/prestosql/tests/JsonFunctionsTest.cpp @@ -75,12 +75,11 @@ TEST_F(JsonFunctionsTest, jsonArrayLength) { } TEST_F(JsonFunctionsTest, jsonArrayContainsBool) { - EXPECT_EQ(json_array_contains(R"([])", 0), false); - EXPECT_EQ(json_array_contains(R"([1, 2, 3])", 1), false); - EXPECT_EQ(json_array_contains(R"([1.2, 2.3, 3.4])", 2.3), false); + EXPECT_EQ(json_array_contains(R"([])", true), false); + EXPECT_EQ(json_array_contains(R"([1, 2, 3])", false), false); + EXPECT_EQ(json_array_contains(R"([1.2, 2.3, 3.4])", true), false); EXPECT_EQ( - json_array_contains( - R"(["hello", "presto", "world"])", R"("hello")"), + json_array_contains(R"(["hello", "presto", "world"])", false), false); EXPECT_EQ(json_array_contains(R"(1)", true), std::nullopt); EXPECT_EQ( @@ -116,6 +115,7 @@ true, true, true, true, true, true, true, true, true, true, true])", TEST_F(JsonFunctionsTest, jsonArrayContainsInt) { EXPECT_EQ(json_array_contains(R"([])", 0), false); EXPECT_EQ(json_array_contains(R"([1.2, 2.3, 3.4])", 2), false); + EXPECT_EQ(json_array_contains(R"([1.2, 2.0, 3.4])", 2), false); EXPECT_EQ( json_array_contains(R"(["hello", "presto", "world"])", 2), false); @@ -154,6 +154,7 @@ TEST_F(JsonFunctionsTest, jsonArrayContainsInt) { TEST_F(JsonFunctionsTest, jsonArrayContainsDouble) { EXPECT_EQ(json_array_contains(R"([])", 2.3), false); EXPECT_EQ(json_array_contains(R"([1, 2, 3])", 2.3), false); + EXPECT_EQ(json_array_contains(R"([1, 2, 3])", 2.0), false); EXPECT_EQ( json_array_contains(R"(["hello", "presto", "world"])", 2.3), false); @@ -237,6 +238,16 @@ TEST_F(JsonFunctionsTest, jsonArrayContainsString) { R"(["hello", "presto", "world", 1, 2, 3, true, false, 1.2, 2.3, {"k1":[0,1,2], "k2":"v1"}])", "world"), true); + EXPECT_EQ( + json_array_contains( + R"(["the fox jumped over the fence", "hello presto world"])", + "hello velox world"), + false); + EXPECT_EQ( + json_array_contains( + R"(["the fox jumped over the fence", "hello presto world"])", + "the fox jumped over the fence"), + true); } } // namespace