cyb70289 commented on a change in pull request #10890:
URL: https://github.com/apache/arrow/pull/10890#discussion_r684593017
##########
File path: cpp/src/arrow/compute/kernels/aggregate_basic.cc
##########
@@ -133,6 +134,158 @@ Result<std::unique_ptr<KernelState>>
MeanInit(KernelContext* ctx,
return visitor.Create();
}
+// ----------------------------------------------------------------------
+// Product implementation
+
+using arrow::internal::MultiplyWithOverflow;
+
+template <typename ArrowType>
+struct ProductImpl : public ScalarAggregator {
+ using ThisType = ProductImpl<ArrowType>;
+ using CType = typename ArrowType::c_type;
+ using ProductType = typename FindAccumulatorType<ArrowType>::Type;
+ using OutputType = typename TypeTraits<ProductType>::ScalarType;
+
+ explicit ProductImpl(const ScalarAggregateOptions& options) { this->options
= options; }
+
+ Status Consume(KernelContext*, const ExecBatch& batch) override {
+ return ConsumeImpl(batch);
+ }
+
+ template <typename T = ArrowType>
+ enable_if_integer<T, Status> ConsumeImpl(const ExecBatch& batch) {
+ if (batch[0].is_array()) {
+ const auto& data = batch[0].array();
+ this->count += data->length - data->GetNullCount();
+ return VisitArrayDataInline<T>(
+ *data,
+ [&](typename TypeTraits<T>::CType value) {
+ if (ARROW_PREDICT_FALSE(
+ MultiplyWithOverflow(this->product, value,
&this->product))) {
+ return Status::Invalid("Overflow in product");
Review comment:
Maybe it's not necessary to check overflow? Call `MultiplyWithOverflow`
without checking the return value just to make sure signed integer overflow
(UB) will not happen.
`numpy.prod` doesn't check overflow,
https://numpy.org/doc/stable/reference/generated/numpy.prod.html.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]