Skip to content

Commit fa0a4f8

Browse files
pratikpugaliafacebook-github-bot
authored andcommitted
feat: Add HUGEINT key support to MapUnionSumAggregate
Summary: Add HUGEINT support to MapUnionSumAggregate, which allows IPADDRESS (a HUGEINT-based type) to be used as map keys in map_union_sum. Example: ```sql SELECT map_union_sum(m) FROM ( VALUES (MAP(ARRAY[CAST('192.168.1.1' AS IPADDRESS)], ARRAY[10])), (MAP(ARRAY[CAST('192.168.1.1' AS IPADDRESS)], ARRAY[5])), (MAP(ARRAY[CAST('10.0.0.1' AS IPADDRESS)], ARRAY[20])) ) AS t(m) ``` Differential Revision: D92026528
1 parent 12c6ebd commit fa0a4f8

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

velox/functions/prestosql/aggregates/MapUnionSumAggregate.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ void registerMapUnionSumAggregate(
534534
case TypeKind::BIGINT:
535535
return createMapUnionSumAggregate<int64_t>(
536536
valueTypeKind, resultType);
537+
case TypeKind::HUGEINT:
538+
return createMapUnionSumAggregate<int128_t>(
539+
valueTypeKind, resultType);
537540
case TypeKind::REAL:
538541
return createMapUnionSumAggregate<float>(valueTypeKind, resultType);
539542
case TypeKind::DOUBLE:

velox/functions/prestosql/aggregates/tests/MapUnionSumTest.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "velox/common/testutil/OptionalEmpty.h"
1818
#include "velox/exec/tests/utils/AssertQueryBuilder.h"
1919
#include "velox/functions/lib/aggregates/tests/utils/AggregationTestBase.h"
20+
#include "velox/functions/prestosql/types/IPAddressType.h"
2021

2122
using namespace facebook::velox::exec;
2223
using namespace facebook::velox::exec::test;
@@ -571,5 +572,51 @@ TEST_F(MapUnionSumTest, decimalKey) {
571572
{makeRowVector({data})}, {}, {"map_union_sum(c0)"}, {expected});
572573
}
573574

575+
TEST_F(MapUnionSumTest, ipAddressKey) {
576+
// Helper to convert IP string to int128_t.
577+
auto ipToInt128 = [](std::string_view ipAddr) -> int128_t {
578+
return ipaddress::tryGetIPv6asInt128FromString(ipAddr).value();
579+
};
580+
581+
// Test on nulls.
582+
auto nullData =
583+
makeRowVector({makeAllNullMapVector(3, IPADDRESS(), BIGINT())});
584+
testAggregations({nullData}, {}, {"map_union_sum(c0)"}, {"VALUES (NULL)"});
585+
586+
auto ip1 = ipToInt128("192.168.1.1");
587+
auto ip2 = ipToInt128("10.0.0.1");
588+
auto ip3 = ipToInt128("172.16.0.1");
589+
590+
// Test global aggregation with IPADDRESS keys.
591+
auto data = makeMapVector<int128_t, int64_t>(
592+
{{{ip1, 10}, {ip2, 20}}, {{ip1, 5}, {ip3, 15}}},
593+
MAP(IPADDRESS(), BIGINT()));
594+
auto expected = makeRowVector({makeMapVector<int128_t, int64_t>(
595+
{{{ip1, 15}, {ip2, 20}, {ip3, 15}}}, MAP(IPADDRESS(), BIGINT()))});
596+
testAggregations(
597+
{makeRowVector({data})}, {}, {"map_union_sum(c0)"}, {expected});
598+
599+
// Test group-by aggregation with IPADDRESS keys.
600+
auto groupByData = makeRowVector({
601+
makeFlatVector<int64_t>({1, 2, 1, 2}),
602+
makeMapVector<int128_t, int64_t>(
603+
{{{ip1, 10}, {ip2, 20}},
604+
{{ip1, 5}, {ip3, 15}},
605+
{{ip1, 3}, {ip2, 7}},
606+
{{ip3, 25}}},
607+
MAP(IPADDRESS(), BIGINT())),
608+
});
609+
610+
auto groupByExpected = makeRowVector({
611+
makeFlatVector<int64_t>({1, 2}),
612+
makeMapVector<int128_t, int64_t>(
613+
{{{ip1, 13}, {ip2, 27}}, {{ip1, 5}, {ip3, 40}}},
614+
MAP(IPADDRESS(), BIGINT())),
615+
});
616+
617+
testAggregations(
618+
{groupByData}, {"c0"}, {"map_union_sum(c1)"}, {groupByExpected});
619+
}
620+
574621
} // namespace
575622
} // namespace facebook::velox::aggregate::test

0 commit comments

Comments
 (0)