edponce commented on code in PR #12460:
URL: https://github.com/apache/arrow/pull/12460#discussion_r845298282


##########
cpp/src/arrow/compute/kernels/vector_cumulative_ops.cc:
##########
@@ -0,0 +1,183 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/array/array_base.h"
+#include "arrow/compute/api_scalar.h"
+#include "arrow/compute/api_vector.h"
+#include "arrow/compute/cast.h"
+#include "arrow/compute/kernels/base_arithmetic_internal.h"
+#include "arrow/compute/kernels/codegen_internal.h"
+#include "arrow/compute/kernels/common.h"
+#include "arrow/result.h"
+#include "arrow/util/bit_util.h"
+#include "arrow/visit_type_inline.h"
+
+namespace arrow {
+namespace compute {
+namespace internal {
+
+// Base class for all cumulative compute functions. Op can be any binary 
associative
+// operation (+, *, min, etc.) and OptionsType the options type corresponding 
to Op.
+// ArgType and OutType are the input and output types, which will normally be 
the
+// same (e.g. the cumulative sum of an array of Int64Type will result in an 
array of
+// Int64Type).
+template <typename OutType, typename ArgType, typename Op, typename 
OptionsType>
+struct CumulativeGeneric {
+  using OutValue = typename GetOutputType<OutType>::T;
+  using ArgValue = typename GetViewType<ArgType>::T;
+
+  static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
+    const auto& options = OptionsWrapper<OptionsType>::Get(ctx);
+    auto skip_nulls = options.skip_nulls;
+
+    auto out_type = batch.GetDescriptors()[0].type;
+    ARROW_ASSIGN_OR_RAISE(auto cast_value, Cast(Datum(options.start), 
out_type));
+    auto start = UnboxScalar<OutType>::Unbox(*cast_value.scalar());
+
+    int64_t base_output_offset = 0;
+    bool encounted_null = false;
+    ArrayData* out_arr = out->mutable_array();
+
+    switch (batch[0].kind()) {
+      case Datum::ARRAY: {
+        auto st = Call(ctx, base_output_offset, *batch[0].array(), out_arr, 
&start,
+                       skip_nulls, &encounted_null);
+        out_arr->SetNullCount(arrow::kUnknownNullCount);
+        return st;
+      }
+      case Datum::CHUNKED_ARRAY: {
+        const auto& input = batch[0].chunked_array();
+
+        for (const auto& chunk : input->chunks()) {
+          RETURN_NOT_OK(Call(ctx, base_output_offset, *chunk->data(), out_arr, 
&start,
+                             skip_nulls, &encounted_null));
+          base_output_offset += chunk->length();
+        }
+        out_arr->SetNullCount(arrow::kUnknownNullCount);
+        return Status::OK();
+      }
+      default:
+        return Status::NotImplemented(
+            "Unsupported input type for function 'cumulative_<operator>': ",
+            batch[0].ToString());
+    }
+  }
+
+  static Status Call(KernelContext* ctx, int64_t base_output_offset,
+                     const ArrayData& input, ArrayData* output, ArgValue* 
accumulator,
+                     bool skip_nulls, bool* encounted_null) {
+    Status st = Status::OK();
+    auto out_bitmap =
+        output->GetMutableValues<uint8_t>(0) + (base_output_offset / CHAR_BIT);

Review Comment:
   Interesting, so in general we want to pass the aligned pointer and 
operations are responsible for applying any given offsets. I was the one who 
suggested such a change, but we can definitely pass the offset in the 
`bit_util` calls. Thanks for this implementation detail @lidavidm! 



-- 
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]

Reply via email to