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

GH-35166: [C++] Increase precision of decimals in aggregate functions #44184

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
12 changes: 6 additions & 6 deletions cpp/src/arrow/acero/hash_aggregate_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1798,12 +1798,12 @@ TEST_P(GroupBy, SumMeanProductDecimal) {

AssertDatumsEqual(ArrayFromJSON(struct_({
field("key_0", int64()),
field("hash_sum", decimal128(3, 2)),
field("hash_sum", decimal256(3, 2)),
field("hash_mean", decimal128(3, 2)),
field("hash_mean", decimal256(3, 2)),
field("hash_product", decimal128(3, 2)),
field("hash_product", decimal256(3, 2)),
field("hash_sum", decimal128(38, 2)),
field("hash_sum", decimal256(76, 2)),
field("hash_mean", decimal128(38, 2)),
field("hash_mean", decimal256(76, 2)),
field("hash_product", decimal128(38, 2)),
field("hash_product", decimal256(76, 2)),
}),
R"([
[1, "4.25", "4.25", "2.13", "2.13", "3.25", "3.25"],
Expand Down
50 changes: 35 additions & 15 deletions cpp/src/arrow/compute/kernels/aggregate_basic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,16 @@ struct ProductImpl : public ScalarAggregator {
}

Status Finalize(KernelContext*, Datum* out) override {
std::shared_ptr<DataType> out_type_ = this->out_type;
if (is_decimal(this->out_type->id())) {
ARROW_ASSIGN_OR_RAISE(out_type_, WidenDecimalToMaxPrecision(this->out_type));
}

if ((!options.skip_nulls && this->nulls_observed) ||
(this->count < options.min_count)) {
out->value = std::make_shared<OutputType>(out_type);
out->value = std::make_shared<OutputType>(out_type_);
} else {
out->value = std::make_shared<OutputType>(this->product, out_type);
out->value = std::make_shared<OutputType>(this->product, out_type_);
}
return Status::OK();
}
Expand Down Expand Up @@ -1048,10 +1053,14 @@ void RegisterScalarAggregateBasic(FunctionRegistry* registry) {
func = std::make_shared<ScalarAggregateFunction>("sum", Arity::Unary(), sum_doc,
&default_scalar_aggregate_options);
AddArrayScalarAggKernels(SumInit, {boolean()}, uint64(), func.get());
AddAggKernel(KernelSignature::Make({Type::DECIMAL128}, FirstType), SumInit, func.get(),
SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL256}, FirstType), SumInit, func.get(),
SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL32}, MaxPrecisionDecimalType), SumInit,
func.get(), SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL64}, MaxPrecisionDecimalType), SumInit,
func.get(), SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL128}, MaxPrecisionDecimalType),
SumInit, func.get(), SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL256}, MaxPrecisionDecimalType),
SumInit, func.get(), SimdLevel::NONE);
AddArrayScalarAggKernels(SumInit, SignedIntTypes(), int64(), func.get());
AddArrayScalarAggKernels(SumInit, UnsignedIntTypes(), uint64(), func.get());
AddArrayScalarAggKernels(SumInit, FloatingPointTypes(), float64(), func.get());
Expand All @@ -1076,10 +1085,14 @@ void RegisterScalarAggregateBasic(FunctionRegistry* registry) {
&default_scalar_aggregate_options);
AddArrayScalarAggKernels(MeanInit, {boolean()}, float64(), func.get());
AddArrayScalarAggKernels(MeanInit, NumericTypes(), float64(), func.get());
AddAggKernel(KernelSignature::Make({Type::DECIMAL128}, FirstType), MeanInit, func.get(),
SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL256}, FirstType), MeanInit, func.get(),
SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL32}, MaxPrecisionDecimalType),
MeanInit, func.get(), SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL64}, MaxPrecisionDecimalType),
MeanInit, func.get(), SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL128}, MaxPrecisionDecimalType),
MeanInit, func.get(), SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL256}, MaxPrecisionDecimalType),
MeanInit, func.get(), SimdLevel::NONE);
AddArrayScalarAggKernels(MeanInit, {null()}, float64(), func.get());
// Add the SIMD variants for mean
#if defined(ARROW_HAVE_RUNTIME_AVX2)
Expand Down Expand Up @@ -1125,6 +1138,8 @@ void RegisterScalarAggregateBasic(FunctionRegistry* registry) {
AddMinMaxKernels(MinMaxInitDefault, BaseBinaryTypes(), func.get());
AddMinMaxKernel(MinMaxInitDefault, Type::FIXED_SIZE_BINARY, func.get());
AddMinMaxKernel(MinMaxInitDefault, Type::INTERVAL_MONTHS, func.get());
AddMinMaxKernel(MinMaxInitDefault, Type::DECIMAL32, func.get());
AddMinMaxKernel(MinMaxInitDefault, Type::DECIMAL64, func.get());
AddMinMaxKernel(MinMaxInitDefault, Type::DECIMAL128, func.get());
AddMinMaxKernel(MinMaxInitDefault, Type::DECIMAL256, func.get());
// Add the SIMD variants for min max
Expand Down Expand Up @@ -1160,10 +1175,14 @@ void RegisterScalarAggregateBasic(FunctionRegistry* registry) {
AddArrayScalarAggKernels(ProductInit::Init, UnsignedIntTypes(), uint64(), func.get());
AddArrayScalarAggKernels(ProductInit::Init, FloatingPointTypes(), float64(),
func.get());
AddAggKernel(KernelSignature::Make({Type::DECIMAL128}, FirstType), ProductInit::Init,
func.get(), SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL256}, FirstType), ProductInit::Init,
func.get(), SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL32}, MaxPrecisionDecimalType),
ProductInit::Init, func.get(), SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL64}, MaxPrecisionDecimalType),
ProductInit::Init, func.get(), SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL128}, MaxPrecisionDecimalType),
ProductInit::Init, func.get(), SimdLevel::NONE);
AddAggKernel(KernelSignature::Make({Type::DECIMAL256}, MaxPrecisionDecimalType),
ProductInit::Init, func.get(), SimdLevel::NONE);
AddArrayScalarAggKernels(ProductInit::Init, {null()}, int64(), func.get());
DCHECK_OK(registry->AddFunction(std::move(func)));

Expand All @@ -1185,7 +1204,8 @@ void RegisterScalarAggregateBasic(FunctionRegistry* registry) {
AddBasicAggKernels(IndexInit::Init, PrimitiveTypes(), int64(), func.get());
AddBasicAggKernels(IndexInit::Init, TemporalTypes(), int64(), func.get());
AddBasicAggKernels(IndexInit::Init,
{fixed_size_binary(1), decimal128(1, 0), decimal256(1, 0), null()},
{fixed_size_binary(1), decimal32(1, 0), decimal64(1, 0),
decimal128(1, 0), decimal256(1, 0), null()},
int64(), func.get());
DCHECK_OK(registry->AddFunction(std::move(func)));
}
Expand Down
16 changes: 12 additions & 4 deletions cpp/src/arrow/compute/kernels/aggregate_basic.inc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@ struct SumImpl : public ScalarAggregator {
}

Status Finalize(KernelContext*, Datum* out) override {
std::shared_ptr<DataType> out_type_ = this->out_type;
if (is_decimal(this->out_type->id())) {
ARROW_ASSIGN_OR_RAISE(out_type_, WidenDecimalToMaxPrecision(this->out_type));
}

if ((!options.skip_nulls && this->nulls_observed) ||
(this->count < options.min_count)) {
out->value = std::make_shared<OutputType>(out_type);
out->value = std::make_shared<OutputType>(out_type_);
} else {
out->value = std::make_shared<OutputType>(this->sum, out_type);
out->value = std::make_shared<OutputType>(this->sum, out_type_);
}
return Status::OK();
}
Expand Down Expand Up @@ -220,9 +225,12 @@ struct MeanImpl<ArrowType, SimdLevel, enable_if_decimal<ArrowType>>

template <typename T = ArrowType>
Status FinalizeImpl(Datum* out) {
std::shared_ptr<DataType> out_type_;
ARROW_ASSIGN_OR_RAISE(out_type_, WidenDecimalToMaxPrecision(this->out_type));

if ((!options.skip_nulls && this->nulls_observed) ||
(this->count < options.min_count) || (this->count == 0)) {
out->value = std::make_shared<OutputType>(this->out_type);
out->value = std::make_shared<OutputType>(out_type_);
} else {
SumCType quotient, remainder;
ARROW_ASSIGN_OR_RAISE(std::tie(quotient, remainder), this->sum.Divide(this->count));
Expand All @@ -235,7 +243,7 @@ struct MeanImpl<ArrowType, SimdLevel, enable_if_decimal<ArrowType>>
quotient -= 1;
}
}
out->value = std::make_shared<OutputType>(quotient, this->out_type);
out->value = std::make_shared<OutputType>(quotient, out_type_);
}
return Status::OK();
}
Expand Down
Loading
Loading