lidavidm commented on a change in pull request #12460:
URL: https://github.com/apache/arrow/pull/12460#discussion_r819061691



##########
File path: cpp/src/arrow/compute/kernels/scalar_cumulative_sum.cc
##########
@@ -0,0 +1,147 @@
+// 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/kernels/common.h"
+#include "arrow/result.h"
+#include "arrow/visit_type_inline.h"
+
+namespace arrow {
+namespace compute {
+namespace internal {
+namespace {
+
+template <typename CType>
+CType CumulativeSum(std::shared_ptr<Array>& input, ArrayData* output, CType 
start) {
+  CType sum = start;
+  CType* data = checked_cast<CType*>(input->data()->buffers[1]->data());
+  CType* out_values = checked_cast<CType*>(output->buffers[1]->mutable_data());
+  for (size_t i = input->offset; i < input->length; ++i) {
+    if (input->IsValid(i)) {
+      sum += data[i];
+      out_values[i] = sum;
+    }
+  }
+
+  return sum;
+}
+
+template <typename Type>
+struct CumulativeSumFunctor {
+  using CType = TypeTraits<Type>::CType;
+
+  Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
+    const NumericScalar& start_scalar =
+        checked_cast<const NumericScalar&>(*batch[1].scalar());
+    CType start = start_scalar.value;
+
+    switch (batch[0].kind()) {
+      case Datum::ARRAY:
+        std::shared_ptr<Array> input = batch[0].make_array();
+        ArrayData* output = out->array().get();
+
+        output->length = input->data()->length;
+        *output->type = std::move(input->type());
+        uint8_t* out_bitmap = output->buffers[0]->mutable_data();

Review comment:
       The zeroth buffer is the validity bitmap, but if all the values in an 
array are valid, Arrow allows omitting the bitmap entirely. In that case, 
buffers[0] itself will be nullptr. (That's what Eduardo was referring to, I 
think.) `mutable_data()` won't be nullptr. (You'll crash if the buffer is 
immutable or inaccessible from the CPU. But those cases shouldn't arise here.)




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