Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding long decimal support for arbitrary() function #9217

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions velox/functions/prestosql/aggregates/ArbitraryAggregate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class ArbitraryAggregate : public SimpleNumericAggregate<T, T, T> {
return sizeof(T);
}

int32_t accumulatorAlignmentSize() const override {
minhancao marked this conversation as resolved.
Show resolved Hide resolved
return 1;
}

void extractValues(char** groups, int32_t numGroups, VectorPtr* result)
override {
BaseAggregate::doExtractValues(groups, numGroups, result, [&](char* group) {
Expand Down Expand Up @@ -139,6 +143,14 @@ class ArbitraryAggregate : public SimpleNumericAggregate<T, T, T> {
}
};

/// Override 'accumulatorAlignmentSize' for UnscaledLongDecimal values as it
/// uses int128_t type. Some CPUs don't support misaligned access to int128_t
/// type.
template <>
inline int32_t ArbitraryAggregate<int128_t>::accumulatorAlignmentSize() const {
return static_cast<int32_t>(sizeof(int128_t));
}

// Arbitrary for non-numeric types. We always keep the first (non-NULL) element
// seen. Arbitrary (x) will produce partial and final aggregations of type x.
class NonNumericArbitrary : public exec::Aggregate {
Expand Down Expand Up @@ -301,6 +313,11 @@ void registerArbitraryAggregate(
return std::make_unique<ArbitraryAggregate<int32_t>>(inputType);
case TypeKind::BIGINT:
return std::make_unique<ArbitraryAggregate<int64_t>>(inputType);
case TypeKind::HUGEINT:
if (inputType->isLongDecimal()) {
return std::make_unique<ArbitraryAggregate<int128_t>>(inputType);
}
VELOX_NYI();
case TypeKind::REAL:
return std::make_unique<ArbitraryAggregate<float>>(inputType);
case TypeKind::DOUBLE:
Expand Down
54 changes: 54 additions & 0 deletions velox/functions/prestosql/aggregates/tests/ArbitraryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,60 @@ TEST_F(ArbitraryTest, interval) {
testAggregations({data}, {}, {"arbitrary(c2)"}, "SELECT null");
}

TEST_F(ArbitraryTest, longDecimal) {
auto data = makeRowVector({// Grouping key.
makeFlatVector<int64_t>({1, 1, 2, 2, 3, 3, 4, 4}),
makeNullableFlatVector<int128_t>(
{HugeInt::build(10, 100),
HugeInt::build(10, 100),
HugeInt::build(10, 200),
HugeInt::build(10, 200),
std::nullopt,
std::nullopt,
std::nullopt,
HugeInt::build(10, 400)},
DECIMAL(38, 8))});

auto expectedResult = makeRowVector({
makeFlatVector<int64_t>({1, 2, 3, 4}),
makeNullableFlatVector<int128_t>(
{HugeInt::build(10, 100),
HugeInt::build(10, 200),
std::nullopt,
minhancao marked this conversation as resolved.
Show resolved Hide resolved
HugeInt::build(10, 400)},
DECIMAL(38, 8)),
});

testAggregations({data}, {"c0"}, {"arbitrary(c1)"}, {expectedResult});
}

TEST_F(ArbitraryTest, shortDecimal) {
auto data = makeRowVector({// Grouping key.
makeFlatVector<int64_t>({1, 1, 2, 2, 3, 3, 4, 4}),
makeNullableFlatVector<int64_t>(
{10000000000000000,
10000000000000000,
20000000000000000,
20000000000000000,
std::nullopt,
std::nullopt,
std::nullopt,
40000000000000000},
DECIMAL(15, 2))});

auto expectedResult = makeRowVector({
makeFlatVector<int64_t>({1, 2, 3, 4}),
makeNullableFlatVector<int64_t>(
{10000000000000000,
20000000000000000,
std::nullopt,
40000000000000000},
DECIMAL(15, 2)),
});

testAggregations({data}, {"c0"}, {"arbitrary(c1)"}, {expectedResult});
}

class ArbitraryWindowTest : public WindowTestBase {};

TEST_F(ArbitraryWindowTest, basic) {
Expand Down
Loading