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


##########
cpp/src/arrow/compute/kernels/vector_swizzle_test.cc:
##########
@@ -0,0 +1,1065 @@
+// 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 <gtest/gtest.h>
+
+#include "arrow/array/concatenate.h"
+#include "arrow/chunked_array.h"
+#include "arrow/compute/api_vector.h"
+#include "arrow/compute/kernels/test_util.h"
+#include "arrow/testing/generator.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/random.h"
+#include "arrow/util/logging.h"
+
+namespace arrow::compute {
+
+namespace {
+
+static const std::vector<std::shared_ptr<DataType>> kSignedIntegerTypes = {
+    int8(), int16(), int32(), int64()};
+
+static const std::vector<std::shared_ptr<DataType>> kIntegerTypes = {
+    int8(), uint8(), int16(), uint16(), int32(), uint32(), int64(), uint64()};
+
+static const std::vector<std::shared_ptr<DataType>> kNumericTypes = {
+    uint8(), int8(),   uint16(), int16(),   uint32(),
+    int32(), uint64(), int64(),  float32(), float64()};
+
+static const std::vector<std::shared_ptr<DataType>> kNumericAndBaseBinaryTypes 
= {
+    uint8(), int8(),    uint16(),  int16(),  uint32(), int32(),        
uint64(),
+    int64(), float32(), float64(), binary(), utf8(),   large_binary(), 
large_utf8()};
+
+using SmallOutputTypes = ::testing::Types<UInt8Type, UInt16Type, Int8Type, 
Int16Type>;
+
+}  // namespace
+
+// ----------------------------------------------------------------------
+// InversePermutation tests
+
+namespace {
+
+Result<Datum> InversePermutation(const Datum& indices, int64_t max_index,
+                                 std::shared_ptr<DataType> output_type) {
+  InversePermutationOptions options{max_index, std::move(output_type)};
+  return InversePermutation(indices, options);
+}
+
+void AssertInversePermutation(const Datum& indices, int64_t max_index,
+                              const std::shared_ptr<DataType>& output_type,
+                              const Datum& expected, bool 
validity_must_be_null) {
+  ASSERT_OK_AND_ASSIGN(auto result, InversePermutation(indices, max_index, 
output_type));
+  ASSERT_EQ(indices.kind(), result.kind());
+  std::shared_ptr<Array> result_array;
+  if (result.is_array()) {
+    result_array = result.make_array();
+  } else {
+    ASSERT_TRUE(result.is_chunked_array());
+    ASSERT_OK_AND_ASSIGN(result_array, 
Concatenate(result.chunked_array()->chunks()));
+  }
+  AssertDatumsEqual(expected, result_array);
+  if (validity_must_be_null) {
+    ASSERT_FALSE(result_array->data()->HasValidityBitmap());
+  }
+}
+
+template <typename InputFunc>
+void DoTestInversePermutationForInputTypes(
+    const std::vector<std::shared_ptr<DataType>>& input_types, InputFunc&& 
input,
+    int64_t max_index, const std::shared_ptr<DataType>& output_type,
+    const Datum& expected, bool validity_must_be_null = false) {
+  for (const auto& input_type : input_types) {
+    ARROW_SCOPED_TRACE("Input type: " + input_type->ToString());
+    auto indices = input(input_type);
+    AssertInversePermutation(indices, max_index, output_type, expected,
+                             validity_must_be_null);
+  }
+}
+
+template <typename InputFunc>
+void DoTestInversePermutationForInputOutputTypes(
+    const std::vector<std::shared_ptr<DataType>>& input_types,
+    const std::vector<std::shared_ptr<DataType>>& output_types, InputFunc&& 
input,
+    int64_t max_index, const std::string& expected_str, bool 
validity_must_be_null) {
+  for (const auto& output_type : kIntegerTypes) {
+    ARROW_SCOPED_TRACE("Output type: " + output_type->ToString());
+    auto expected = ArrayFromJSON(output_type, expected_str);
+    DoTestInversePermutationForInputTypes(input_types, 
std::forward<InputFunc>(input),
+                                          max_index, output_type, expected,
+                                          validity_must_be_null);
+  }
+}
+
+void TestInversePermutationForInputOutputTypes(
+    const std::vector<std::shared_ptr<DataType>>& input_types,
+    const std::vector<std::shared_ptr<DataType>>& output_types,
+    const std::string& indices_str, const std::vector<std::string>& 
indices_chunked_str,
+    int64_t max_index, const std::string& expected_str, bool 
validity_must_be_null) {
+  {
+    ARROW_SCOPED_TRACE("Array");
+    DoTestInversePermutationForInputOutputTypes(
+        input_types, output_types,
+        [&](const std::shared_ptr<DataType>& input_type) {
+          return ArrayFromJSON(input_type, indices_str);
+        },
+        max_index, expected_str, validity_must_be_null);
+  }
+  {
+    ARROW_SCOPED_TRACE("Chunked");
+    DoTestInversePermutationForInputOutputTypes(
+        input_types, output_types,
+        [&](const std::shared_ptr<DataType>& input_type) {
+          return ChunkedArrayFromJSON(input_type, indices_chunked_str);
+        },
+        max_index, expected_str, validity_must_be_null);
+  }
+}
+
+void TestInversePermutationSigned(const std::string& indices_str,
+                                  const std::vector<std::string>& 
indices_chunked_str,
+                                  int64_t max_index, const std::string& 
expected_str,
+                                  bool validity_must_be_null = false) {
+  TestInversePermutationForInputOutputTypes(kSignedIntegerTypes, kIntegerTypes,
+                                            indices_str, indices_chunked_str, 
max_index,
+                                            expected_str, 
validity_must_be_null);
+}
+
+void TestInversePermutation(const std::string& indices_str,
+                            const std::vector<std::string>& 
indices_chunked_str,
+                            int64_t max_index, const std::string& expected_str,
+                            bool validity_must_be_null = false) {
+  TestInversePermutationForInputOutputTypes(kIntegerTypes, kIntegerTypes, 
indices_str,
+                                            indices_chunked_str, max_index, 
expected_str,
+                                            validity_must_be_null);
+}
+
+}  // namespace
+
+TEST(InversePermutation, InvalidOutputType) {
+  {
+    ARROW_SCOPED_TRACE("Output type float");
+    auto indices = ArrayFromJSON(int32(), "[]");
+    ASSERT_RAISES_WITH_MESSAGE(
+        Invalid, "Invalid: Output type of inverse_permutation must be integer, 
got float",

Review Comment:
   Done.



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