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


##########
cpp/src/arrow/compute/kernels/vector_swizzle.cc:
##########
@@ -0,0 +1,415 @@
+// 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 
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_integer(output_type->id())) {
+    return Status::Invalid("Output type of inverse_permutation must be 
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)});
+  }
+
+ private:
+  KernelContext* ctx;
+  const ShapeType& indices;
+  const int64_t input_length;
+  const int64_t output_length;
+
+  std::shared_ptr<Buffer> validity_buf = nullptr;
+  std::shared_ptr<Buffer> data_buf = nullptr;

Review Comment:
   Updated.



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