Skip to content

Commit

Permalink
Add JSON to Map Union Sum (#12208)
Browse files Browse the repository at this point in the history
Summary:

Add support for JSON for map_union_sum

Differential Revision: D68867704
  • Loading branch information
natashasehgal authored and facebook-github-bot committed Feb 3, 2025
1 parent e484679 commit 1e30d79
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions velox/functions/prestosql/aggregates/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ velox_link_libraries(
velox_functions_aggregates
velox_functions_lib
velox_functions_util
velox_presto_types
Folly::folly)

if(${VELOX_BUILD_TESTING})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ void registerMapUnionSumAggregate(
"real",
"double",
"varchar",
};
"json"};
const std::vector<std::string> valueTypes = {
"tinyint",
"smallint",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
#include "velox/functions/prestosql/aggregates/RegisterAggregateFunctions.h"
#include "velox/exec/Aggregate.h"
#include "velox/functions/prestosql/types/JsonType.h"

namespace facebook::velox::aggregate::prestosql {

Expand Down Expand Up @@ -155,6 +156,7 @@ void registerAllAggregateFunctions(
bool withCompanionFunctions,
bool onlyPrestoSignatures,
bool overwrite) {
registerJsonType();
registerApproxDistinctAggregates(prefix, withCompanionFunctions, overwrite);
registerApproxMostFrequentAggregate(
prefix, withCompanionFunctions, overwrite);
Expand Down
38 changes: 38 additions & 0 deletions velox/functions/prestosql/aggregates/tests/MapUnionSumTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,44 @@ TEST_F(MapUnionSumTest, groupByVarcharKey) {
testAggregations({data}, {"c0"}, {"map_union_sum(c1)"}, {expected});
}

TEST_F(MapUnionSumTest, groupByJsonKey) {
std::vector<std::string> jsonKeyStrings = {
"\"key1\"",
"\"key2\"",
"\"key3\"",
"\"key4\"",
"\"key5\"",
};
std::vector<StringView> jsonKeys;
jsonKeys.reserve(jsonKeyStrings.size());
for (const auto& key : jsonKeyStrings) {
jsonKeys.emplace_back(key);
}
auto data = makeRowVector({
makeFlatVector<int64_t>({1, 2, 1, 2, 1}),
makeNullableMapVector<StringView, int64_t>({
{},
std::nullopt,
{{{jsonKeys[0], 10}, {jsonKeys[1], 20}}},
{{{jsonKeys[0], 11}, {jsonKeys[2], 30}, {jsonKeys[3], 40}}},
{{{jsonKeys[2], 30}, {jsonKeys[4], 50}, {jsonKeys[0], 12}}},
}),
});

auto expected = makeRowVector({
makeFlatVector<int64_t>({1, 2}),
makeMapVector<StringView, int64_t>({
{{jsonKeys[0], 22},
{jsonKeys[1], 20},
{jsonKeys[2], 30},
{jsonKeys[4], 50}},
{{jsonKeys[0], 11}, {jsonKeys[2], 30}, {jsonKeys[3], 40}},
}),
});

testAggregations({data}, {"c0"}, {"map_union_sum(c1)"}, {expected});
}

TEST_F(MapUnionSumTest, floatingPointKeys) {
auto data = makeRowVector({
makeFlatVector<int32_t>({1, 2, 1, 2, 1, 1, 2, 2}),
Expand Down

0 comments on commit 1e30d79

Please sign in to comment.