You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "js8544 (via GitHub)" <gi...@apache.org> on 2023/09/06 09:12:32 UTC

[GitHub] [arrow] js8544 commented on a diff in pull request #37536: GH-37090: [C++] Add sum_checked aggregate function

js8544 commented on code in PR #37536:
URL: https://github.com/apache/arrow/pull/37536#discussion_r1316987434


##########
cpp/src/arrow/compute/api_aggregate.h:
##########
@@ -284,6 +284,21 @@ Result<Datum> Sum(
     const ScalarAggregateOptions& options = ScalarAggregateOptions::Defaults(),
     ExecContext* ctx = NULLPTR);
 
+/// \brief Sum values of a numeric array. Return error if overflow occurs
+///
+/// \param[in] value datum to sum, expecting Array or ChunkedArray
+/// \param[in] options see ScalarAggregateOptions for more information
+/// \param[in] ctx the function execution context, optional
+/// \return datum of the computed sum as a Scalar
+///
+/// \since 1.0.0

Review Comment:
   Sorry! Updated to 14



##########
cpp/src/arrow/compute/kernels/aggregate_basic_internal.h:
##########
@@ -176,11 +202,11 @@ struct MeanImpl;
 
 template <typename ArrowType, SimdLevel::type SimdLevel>
 struct MeanImpl<ArrowType, SimdLevel, enable_if_decimal<ArrowType>>
-    : public SumImpl<ArrowType, SimdLevel> {
-  using SumImpl<ArrowType, SimdLevel>::SumImpl;
-  using SumImpl<ArrowType, SimdLevel>::options;
-  using SumCType = typename SumImpl<ArrowType, SimdLevel>::SumCType;
-  using OutputType = typename SumImpl<ArrowType, SimdLevel>::OutputType;
+    : public SumImpl<ArrowType, SimdLevel, false> {

Review Comment:
   Done



##########
cpp/src/arrow/compute/kernels/aggregate_internal.h:
##########
@@ -221,27 +221,34 @@ enable_if_t<std::is_floating_point<SumType>::value, SumType> SumArray(
 }
 
 // naive summation for integers and decimals
-template <typename ValueType, typename SumType, SimdLevel::type SimdLevel,
+template <typename ValueType, typename SumType, SimdLevel::type SimdLevel, bool Checked,
           typename ValueFunc>
 enable_if_t<!std::is_floating_point<SumType>::value, SumType> SumArray(
-    const ArraySpan& data, ValueFunc&& func) {
+    const ArraySpan& data, ValueFunc&& func, Status* status) {
   using arrow::internal::VisitSetBitRunsVoid;
 
   SumType sum = 0;
   const ValueType* values = data.GetValues<ValueType>(1);
-  VisitSetBitRunsVoid(data.buffers[0].data, data.offset, data.length,
-                      [&](int64_t pos, int64_t len) {
-                        for (int64_t i = 0; i < len; ++i) {
-                          sum += func(values[pos + i]);
-                        }
-                      });
+  VisitSetBitRunsVoid(
+      data.buffers[0].data, data.offset, data.length, [&](int64_t pos, int64_t len) {
+        for (int64_t i = 0; i < len; ++i) {
+          if constexpr (Checked) {
+            sum = AddChecked::Call<SumType>(nullptr, sum, func(values[pos + i]), status);
+            if (ARROW_PREDICT_FALSE(!status->ok())) {

Review Comment:
   Done



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org