zanmato1984 commented on code in PR #44394:
URL: https://github.com/apache/arrow/pull/44394#discussion_r1916804555


##########
cpp/src/arrow/compute/kernels/vector_swizzle.cc:
##########
@@ -0,0 +1,422 @@
+// 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/api_vector.h"
+
+#include "arrow/compute/api_vector.h"
+#include "arrow/compute/function.h"
+#include "arrow/compute/kernels/codegen_internal.h"
+#include "arrow/compute/registry.h"
+#include "arrow/util/checked_cast.h"
+#include "arrow/util/logging.h"
+
+namespace arrow::compute::internal {
+
+namespace {
+
+// ----------------------------------------------------------------------
+// InversePermutation
+
+const FunctionDoc inverse_permutation_doc(
+    "Return the inverse permutation of the given indices",
+    "For the `i`-th `index` in `indices`, the `index`-th output is `i`", 
{"indices"});
+
+const InversePermutationOptions* GetDefaultInversePermutationOptions() {
+  static const auto kDefaultInversePermutationOptions =
+      InversePermutationOptions::Defaults();
+  return &kDefaultInversePermutationOptions;
+}
+
+using InversePermutationState = OptionsWrapper<InversePermutationOptions>;
+
+/// Resolve the output type of inverse_permutation. The output type is 
specified in the
+/// options, and if null, set it to the input type. The output type must be 
signed
+/// integer.
+Result<TypeHolder> ResolveInversePermutationOutputType(
+    KernelContext* ctx, const std::vector<TypeHolder>& input_types) {
+  DCHECK_EQ(input_types.size(), 1);
+  DCHECK_NE(input_types[0], nullptr);
+
+  std::shared_ptr<DataType> output_type = 
InversePermutationState::Get(ctx).output_type;
+  if (!output_type) {
+    output_type = input_types[0].owned_type;
+  }
+  if (!is_signed_integer(output_type->id())) {
+    return Status::TypeError(
+        "Output type of inverse_permutation must be signed integer, got " +
+        output_type->ToString());
+  }
+
+  return TypeHolder(std::move(output_type));
+}
+
+template <typename ExecType>
+struct InversePermutationImpl {
+  using ThisType = InversePermutationImpl<ExecType>;
+  using IndexType = typename ExecType::IndexType;
+  using IndexCType = typename IndexType::c_type;
+  using ShapeType = typename ExecType::ShapeType;
+
+  static Result<std::shared_ptr<ArrayData>> Exec(
+      KernelContext* ctx, const ShapeType& indices, int64_t input_length,
+      const std::shared_ptr<DataType>& input_type) {
+    const auto& options = InversePermutationState::Get(ctx);
+
+    // Apply default options semantics.
+    int64_t output_length = options.max_index < 0 ? input_length : 
options.max_index + 1;
+    std::shared_ptr<DataType> output_type = options.output_type;
+    if (!output_type) {
+      output_type = input_type;
+    }
+
+    ThisType impl(ctx, indices, input_length, output_length);
+    RETURN_NOT_OK(VisitTypeInline(*output_type, &impl));
+
+    return ArrayData::Make(std::move(output_type), output_length,
+                           {std::move(impl.validity_buf_), 
std::move(impl.data_buf_)});
+  }
+
+  template <typename Type>
+  enable_if_t<is_integer_type<Type>::value, Status> Visit(const Type& 
output_type) {
+    using OutputCType = typename Type::c_type;
+
+    RETURN_NOT_OK(CheckInput(output_type));
+
+    // Dispatch the execution based on whether there are likely many nulls in 
the output.
+    // - If many nulls (i.e. the output is "sparse"), preallocate an all-false 
validity
+    // buffer and a zero-initialized data buffer (just to avoid exposing 
previous memory
+    // contents - even if it is shadowed by the validity bit). The subsequent 
processing
+    // will fill the valid values only.
+    // - Otherwise (i.e. the output is "dense"), the validity buffer is lazily 
allocated
+    // and initialized all-true in the subsequent processing only when needed. 
The data
+    // buffer is preallocated and filled with "impossible" values (that is, 
input_length -
+    // note that the range of inverse_permutation is [0, input_length)) for 
the subsequent
+    // processing to detect validity.
+    if (LikelyManyNulls()) {
+      RETURN_NOT_OK(AllocateValidityBufAndFill(false));
+      RETURN_NOT_OK(AllocateDataBufAndZero(output_type));
+      return Execute<Type, true>();
+    } else {
+      RETURN_NOT_OK(
+          AllocateDataBufAndFill(output_type, 
static_cast<OutputCType>(input_length_)));
+      return Execute<Type, false>();
+    }
+  }
+
+  Status Visit(const DataType& output_type) {
+    DCHECK(false) << "Shouldn't reach here";
+    return Status::Invalid("Shouldn't reach here");
+  }
+
+ private:
+  KernelContext* ctx_;
+  const ShapeType& indices_;
+  const int64_t input_length_;
+  const int64_t output_length_;
+
+  std::shared_ptr<Buffer> validity_buf_;
+  std::shared_ptr<Buffer> data_buf_;
+
+  InversePermutationImpl(KernelContext* ctx, const ShapeType& indices,
+                         int64_t input_length, int64_t output_length)
+      : ctx_(ctx),
+        indices_(indices),
+        input_length_(input_length),
+        output_length_(output_length) {}
+
+  template <typename Type>
+  Status CheckInput(const Type& output_type) {
+    using OutputCType = typename Type::c_type;
+
+    if (static_cast<int64_t>(std::numeric_limits<OutputCType>::max()) < 
input_length_) {
+      return Status::Invalid(
+          "Output type " + output_type.ToString() +
+          " of inverse_permutation is insufficient to store indices of length 
" +
+          std::to_string(input_length_));
+    }
+
+    return Status::OK();
+  }
+
+  bool LikelyManyNulls() { return output_length_ > 2 * input_length_; }
+
+  Status AllocateValidityBufAndFill(bool valid) {
+    DCHECK_EQ(validity_buf_, nullptr);
+
+    ARROW_ASSIGN_OR_RAISE(validity_buf_,
+                          AllocateEmptyBitmap(output_length_, 
ctx_->memory_pool()));
+    auto validity = validity_buf_->mutable_data_as<uint8_t>();
+    std::memset(validity, valid ? 0xff : 0, validity_buf_->size());

Review Comment:
   Very useful advice! Addressed.



-- 
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-unsubscr...@arrow.apache.org

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

Reply via email to