You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/05/17 20:38:01 UTC

[GitHub] [arrow] nirandaperera commented on a change in pull request #9024: ARROW-11044: [C++] Add "replace" kernel

nirandaperera commented on a change in pull request #9024:
URL: https://github.com/apache/arrow/pull/9024#discussion_r633835253



##########
File path: cpp/src/arrow/compute/kernels/scalar_replace.cc
##########
@@ -0,0 +1,309 @@
+// 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/compute/kernels/common.h"
+#include "arrow/util/bit_block_counter.h"
+#include "arrow/util/bit_util.h"
+#include "arrow/util/bitmap_ops.h"
+
+namespace arrow {
+
+using internal::BitBlockCount;
+using internal::BitBlockCounter;
+
+namespace compute {
+namespace internal {
+
+namespace {
+
+void handle_nulls(KernelContext* ctx, const ArrayData& data, const ArrayData& mask,
+                  ArrayData* output) {
+  if (data.MayHaveNulls()) {
+    KERNEL_ASSIGN_OR_RAISE(std::shared_ptr<Buffer> out_nulls, ctx,
+                           ctx->AllocateBitmap(data.length));
+
+    if (mask.MayHaveNulls()) {
+      ::arrow::internal::BitmapAnd(mask.buffers[0]->data(), mask.offset,
+                                   mask.buffers[1]->data(), mask.offset, mask.length,
+                                   output->offset, out_nulls->mutable_data());
+      ::arrow::internal::BitmapOr(data.buffers[0]->data(), data.offset, out_nulls->data(),
+                                  output->offset, data.length, output->offset,
+                                  out_nulls->mutable_data());
+    } else {
+      ::arrow::internal::BitmapOr(data.buffers[0]->data(), data.offset,
+                                  mask.buffers[1]->data(), mask.offset, mask.length,
+                                  output->offset, out_nulls->mutable_data());
+    }
+
+    if (::arrow::internal::CountSetBits(out_nulls->data(), output->offset, data.length) <
+        data.length)
+      output->buffers[0] = out_nulls;
+  }
+}
+
+template <typename Type, typename Enable = void>
+struct ReplaceFunctor {};
+
+// Numeric inputs
+
+template <typename Type>
+struct ReplaceFunctor<Type, enable_if_t<is_number_type<Type>::value>> {
+  using T = typename TypeTraits<Type>::CType;
+
+  static void Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
+    const ArrayData& data = *batch[0].array();
+    const ArrayData& mask = *batch[1].array();
+    const Scalar& replacement = *batch[2].scalar();
+    ArrayData* output = out->mutable_array();
+
+    // Ensure the kernel is configured properly to have no validity bitmap /
+    // null count 0 unless we explicitly propagate it below.
+    DCHECK(output->buffers[0] == nullptr);
+
+    if (replacement.is_valid) {
+      handle_nulls(ctx, data, mask, output);
+
+      KERNEL_ASSIGN_OR_RAISE(std::shared_ptr<Buffer> out_buf, ctx,
+                             ctx->Allocate(data.length * sizeof(T)));
+      T value = UnboxScalar<Type>::Unbox(replacement);
+      const uint8_t* to_replace = mask.buffers[1]->data();
+      const T* in_values = data.GetValues<T>(1);
+      T* out_values = reinterpret_cast<T*>(out_buf->mutable_data());
+      int64_t offset = data.offset;
+      BitBlockCounter bit_counter(to_replace, data.offset, data.length);
+      while (offset < data.offset + data.length) {
+        BitBlockCount block = bit_counter.NextWord();
+        if (block.NoneSet()) {
+          std::memcpy(out_values, in_values, block.length * sizeof(T));
+        } else if (block.AllSet()) {
+          std::fill(out_values, out_values + block.length, value);
+        } else {
+          for (int64_t i = 0; i < block.length; ++i) {
+            out_values[i] =
+                BitUtil::GetBit(to_replace, offset + i) ? value : in_values[i];
+          }
+        }
+        offset += block.length;
+        out_values += block.length;
+        in_values += block.length;
+      }
+      output->buffers[1] = out_buf;

Review comment:
       minor comment, you could use `std::move` here. (applies to several places, where buffer shared_ptr's are assigned)




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

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