Skip to content

Commit 5c08143

Browse files
peterenescufacebook-github-bot
authored andcommitted
fix(array functions): Add support to array_has_duplicates for input of type json (facebookincubator#12158)
Summary: Minor update to function array_has_duplicates which registers json as input in array registration file. No need to cast input as json is already treated as a varchar. Reviewed By: kgpai Differential Revision: D68580104
1 parent 028d9e4 commit 5c08143

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

velox/functions/prestosql/registration/ArrayFunctionsRegistration.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ void registerArrayFunctions(const std::string& prefix) {
298298
registerArrayHasDuplicatesFunctions<int64_t>(prefix);
299299
registerArrayHasDuplicatesFunctions<int128_t>(prefix);
300300
registerArrayHasDuplicatesFunctions<Varchar>(prefix);
301+
registerArrayHasDuplicatesFunctions<Json>(prefix);
301302

302303
registerArrayFrequencyFunctions<bool>(prefix);
303304
registerArrayFrequencyFunctions<int8_t>(prefix);

velox/functions/prestosql/tests/ArrayHasDuplicatesTest.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <optional>
1818
#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h"
19+
#include "velox/functions/prestosql/types/JsonType.h"
1920

2021
using namespace facebook::velox;
2122
using namespace facebook::velox::test;
@@ -145,3 +146,42 @@ TEST_F(ArrayHasDuplicatesTest, nullFreeStrings) {
145146
auto expected = makeFlatVector<bool>({false, true, false, true});
146147
testExpr(expected, "array_has_duplicates(C0)", {array});
147148
}
149+
150+
TEST_F(ArrayHasDuplicatesTest, json) {
151+
auto result = evaluate(
152+
"array_has_duplicates(C0)",
153+
makeRowVector({makeNullableArrayVector<StringView>(
154+
{{R"({"key":"value"})", R"({"key":"value"})"}}, ARRAY(JSON()))}));
155+
assertEqualVectors(makeFlatVector<bool>(true), result);
156+
157+
result = evaluate(
158+
"array_has_duplicates(C0)",
159+
makeRowVector({makeNullableArrayVector<StringView>(
160+
{{R"({"key":"value"})", R"({"key":"another_value"})"},
161+
{R"({"key":"value"})"},
162+
{R"({"key":"same_value"})",
163+
R"({"key":"another_value"})",
164+
R"({"key":"same_value"})"},
165+
{std::nullopt, std::nullopt},
166+
{R"({"key":"value"})",
167+
R"({"key":"another_value"})",
168+
R"({"another_key":"value"})"},
169+
{R"({"key": "value\with\backslash"})",
170+
R"({"key": "value\with\backslash"})"},
171+
{R"({"key": "value\nwith\nnewline"})",
172+
R"({"key": "value\nwith\nnewline"})"},
173+
{R"({"key": "value with \u00A9 and \u20AC"})",
174+
R"({"key": "value with \u00A9 and \u20AC"})"},
175+
{R"({"key": "!@#$%^&*()_+-={}:<>?,./~`"})",
176+
R"({"key": "!@#$%^&*()_+-={}:<>?,./~`"})"},
177+
{R"({"key":"value"})",
178+
std::nullopt,
179+
R"({"key":"another_value"})",
180+
R"({"another_key":"value"})",
181+
std::nullopt}},
182+
ARRAY(JSON()))}));
183+
assertEqualVectors(
184+
makeFlatVector<bool>(
185+
{false, false, true, true, false, true, true, true, true, true}),
186+
result);
187+
}

0 commit comments

Comments
 (0)