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


##########
cpp/src/arrow/compute/kernels/vector_swizzle_test.cc:
##########
@@ -0,0 +1,781 @@
+// 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>> kBinaryTypes = {
+    binary(), utf8(), large_binary(), large_utf8()};
+
+using SmallSignedIntegerTypes = ::testing::Types<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));
+  ValidateOutput(result);
+  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());
+    ASSERT_OK_AND_ASSIGN(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 : output_types) {
+    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::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) -> Result<Datum> {
+          auto chunked = ChunkedArrayFromJSON(input_type, indices_chunked_str);
+          return Concatenate(chunked->chunks());
+        },
+        max_index, expected_str, validity_must_be_null);
+  }
+  {
+    ARROW_SCOPED_TRACE("Chunked");
+    DoTestInversePermutationForInputOutputTypes(
+        input_types, output_types,
+        [&](const std::shared_ptr<DataType>& input_type) -> Result<Datum> {
+          return ChunkedArrayFromJSON(input_type, indices_chunked_str);
+        },
+        max_index, expected_str, validity_must_be_null);
+  }
+}
+
+void TestInversePermutation(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, 
kSignedIntegerTypes,
+                                            indices_chunked_str, max_index, 
expected_str,
+                                            validity_must_be_null);
+}
+
+}  // namespace
+
+TEST(InversePermutation, InvalidOutputType) {
+  {
+    ARROW_SCOPED_TRACE("Output type unsigned");
+    auto indices = ArrayFromJSON(int32(), "[]");
+    ASSERT_RAISES_WITH_MESSAGE(
+        TypeError,
+        "Type error: Output type of inverse_permutation must be signed 
integer, got "
+        "uint32",
+        InversePermutation(indices, /*max_index=*/0, 
/*output_type=*/uint32()));
+  }
+  {
+    ARROW_SCOPED_TRACE("Output type float");
+    auto indices = ArrayFromJSON(int32(), "[]");
+    ASSERT_RAISES_WITH_MESSAGE(
+        TypeError,
+        "Type error: Output type of inverse_permutation must be signed 
integer, got "
+        "float",
+        InversePermutation(indices, /*max_index=*/0, 
/*output_type=*/float32()));
+  }
+  {
+    ARROW_SCOPED_TRACE("Output type string");
+    auto indices = ArrayFromJSON(int32(), "[]");
+    ASSERT_RAISES_WITH_MESSAGE(
+        TypeError,
+        "Type error: Output type of inverse_permutation must be signed 
integer, got "
+        "string",
+        InversePermutation(indices, /*max_index=*/0, /*output_type=*/utf8()));
+  }
+}
+
+TEST(InversePermutation, DefaultOptions) {
+  {
+    ARROW_SCOPED_TRACE("Default options values");
+    InversePermutationOptions options;
+    ASSERT_EQ(options.max_index, -1);
+    ASSERT_EQ(options.output_type, nullptr);
+  }
+  {
+    ARROW_SCOPED_TRACE("Default options semantics");
+    for (const auto& input_type : kSignedIntegerTypes) {
+      ARROW_SCOPED_TRACE("Input type: " + input_type->ToString());
+      auto indices = ArrayFromJSON(input_type, "[0]");
+      ASSERT_OK_AND_ASSIGN(Datum result, InversePermutation(indices));
+      AssertDatumsEqual(indices, result);
+    }
+  }
+}
+
+TEST(InversePermutation, InvalidIndex) {
+  {
+    ARROW_SCOPED_TRACE("Negative index");
+    auto indices = ArrayFromJSON(int32(), "[-1]");
+    ASSERT_RAISES_WITH_MESSAGE(IndexError, "Index error: Index out of bounds: 
-1",
+                               InversePermutation(indices));
+  }
+  {
+    ARROW_SCOPED_TRACE("Exceeds max_index");
+    auto indices = ArrayFromJSON(int32(), "[42]");
+    ASSERT_RAISES_WITH_MESSAGE(
+        IndexError, "Index error: Index out of bounds: 42",
+        InversePermutation(indices, /*max_index=*/1, /*output_type=*/int32()));
+  }
+}
+
+TEST(InversePermutation, Basic) {
+  {
+    ARROW_SCOPED_TRACE("Basic");
+    std::vector<std::string> indices_chunked{
+        "[]", "[9, 7, 5, 3, 1]", "[0]", "[2, 4, 6]", "[8]", "[]"};
+    int64_t max_index = 9;
+    auto expected = "[5, 4, 6, 3, 7, 2, 8, 1, 9, 0]";
+    TestInversePermutation(indices_chunked, max_index, expected,
+                           /*validity_must_be_null=*/true);
+  }
+  {
+    ARROW_SCOPED_TRACE("Basic with nulls");
+    std::vector<std::string> indices_chunked{
+        "[]", "[9, 7, 5, 3, 1]", "[null]", "[null, null, null]", "[null]", 
"[]"};
+    int64_t max_index = 9;
+    auto expected = "[null, 4, null, 3, null, 2, null, 1, null, 0]";
+    TestInversePermutation(indices_chunked, max_index, expected);
+  }
+  {
+    ARROW_SCOPED_TRACE("Output greater than input");
+    std::vector<std::string> indices_chunked{"[]", "[1]", "[]", "[2]"};
+    int64_t max_index = 6;
+    auto expected = "[null, 0, 1, null, null, null, null]";
+    TestInversePermutation(indices_chunked, max_index, expected);
+  }
+  {
+    ARROW_SCOPED_TRACE("Input all null");
+    std::vector<std::string> indices_chunked{"[]", "[null]", "[]", "[null]"};
+    int64_t max_index = 1;
+    auto expected = "[null, null]";
+    TestInversePermutation(indices_chunked, max_index, expected);
+  }
+  {
+    ARROW_SCOPED_TRACE("Empty input output null");
+    std::vector<std::string> indices_chunked{"[]", "[]", "[]", "[]"};
+    int64_t max_index = 6;
+    auto expected = "[null, null, null, null, null, null, null]";
+    TestInversePermutation(indices_chunked, max_index, expected);
+  }
+  {
+    ARROW_SCOPED_TRACE("Input duplicated indices");
+    std::vector<std::string> indices_chunked{"[]", "[1, 2]", "[3, 1, 2, 3, 1]",
+                                             "[]", "[2]",    "[3]"};
+    int64_t max_index = 4;
+    auto expected = "[null, 6, 7, 8, null]";
+    TestInversePermutation(indices_chunked, max_index, expected);
+  }
+}
+
+template <typename ArrowType>
+class TestInversePermutationSmallOutputType : public ::testing::Test {
+ protected:
+  using CType = typename TypeTraits<ArrowType>::CType;
+
+  std::shared_ptr<DataType> type_singleton() {
+    return TypeTraits<ArrowType>::type_singleton();
+  }
+};
+
+TYPED_TEST_SUITE(TestInversePermutationSmallOutputType, 
SmallSignedIntegerTypes);
+
+TYPED_TEST(TestInversePermutationSmallOutputType, JustEnoughOutputType) {
+  auto output_type = this->type_singleton();
+  int64_t input_length =
+      static_cast<int64_t>(std::numeric_limits<typename 
TestFixture::CType>::max());
+  auto expected =
+      ArrayFromJSON(output_type, "[" + std::to_string(input_length - 1) + "]");
+  DoTestInversePermutationForInputTypes(
+      kSignedIntegerTypes,
+      [&](const std::shared_ptr<DataType>& input_type) -> Result<Datum> {
+        return ConstantArrayGenerator::Zeroes(input_length, input_type);
+      },
+      /*max_index=*/0, output_type, expected);
+}
+
+TYPED_TEST(TestInversePermutationSmallOutputType, InsufficientOutputType) {
+  auto output_type = this->type_singleton();
+  int64_t input_length =
+      static_cast<int64_t>(std::numeric_limits<typename 
TestFixture::CType>::max()) + 1;
+  for (const auto& input_type : kSignedIntegerTypes) {
+    ARROW_SCOPED_TRACE("Input type: " + input_type->ToString());
+    auto indices = ConstantArrayGenerator::Zeroes(input_length, input_type);
+    ASSERT_RAISES_WITH_MESSAGE(
+        Invalid,
+        "Invalid: Output type " + output_type->ToString() +
+            " of inverse_permutation is insufficient to store indices of 
length " +
+            std::to_string(input_length),
+        InversePermutation(indices, /*max_index=*/0, output_type));
+  }
+}
+
+// ----------------------------------------------------------------------
+// Scatter tests
+//
+// Shorthand notation:
+//
+//   A = Array
+//   C = ChunkedArray
+
+namespace {
+
+Result<Datum> Scatter(const Datum& values, const Datum& indices, int64_t 
max_index) {
+  ScatterOptions options{max_index};
+  ARROW_ASSIGN_OR_RAISE(Datum result, Scatter(values, indices, options));
+  ValidateOutput(result);
+  return result;
+}
+
+void AssertScatterAAA(const std::shared_ptr<Array>& values,
+                      const std::shared_ptr<Array>& indices, int64_t max_index,
+                      const std::shared_ptr<Array>& expected) {
+  ASSERT_OK_AND_ASSIGN(Datum result, Scatter(values, indices, max_index));
+  AssertDatumsEqual(expected, result);
+}
+
+void AssertScatterCAC(const std::shared_ptr<ChunkedArray>& values,
+                      const std::shared_ptr<Array>& indices, int64_t max_index,
+                      const std::shared_ptr<Array>& expected) {
+  ASSERT_OK_AND_ASSIGN(Datum result, Scatter(values, indices, max_index));
+  ASSERT_TRUE(result.is_chunked_array());
+  ASSERT_OK_AND_ASSIGN(auto result_array, 
Concatenate(result.chunked_array()->chunks()));
+  AssertDatumsEqual(expected, result_array);
+}
+
+void AssertScatterACC(const std::shared_ptr<Array>& values,
+                      const std::shared_ptr<ChunkedArray>& indices, int64_t 
max_index,
+                      const std::shared_ptr<Array>& expected) {
+  ASSERT_OK_AND_ASSIGN(Datum result, Scatter(values, indices, max_index));
+  ASSERT_TRUE(result.is_chunked_array());
+  ASSERT_OK_AND_ASSIGN(auto result_array, 
Concatenate(result.chunked_array()->chunks()));
+  AssertDatumsEqual(expected, result_array);
+}
+
+void AssertScatterCCC(const std::shared_ptr<ChunkedArray>& values,
+                      const std::shared_ptr<ChunkedArray>& indices, int64_t 
max_index,
+                      const std::shared_ptr<Array>& expected) {
+  ASSERT_OK_AND_ASSIGN(Datum result, Scatter(values, indices, max_index));
+  ASSERT_TRUE(result.is_chunked_array());
+  ASSERT_OK_AND_ASSIGN(auto result_array, 
Concatenate(result.chunked_array()->chunks()));
+  AssertDatumsEqual(expected, result_array);
+}
+
+void DoTestScatterAAA(const std::shared_ptr<Array>& values,
+                      const std::shared_ptr<Array>& indices, int64_t max_index,
+                      const std::shared_ptr<Array>& expected) {
+  AssertScatterAAA(values, indices, max_index, expected);
+}
+
+/// The following helper functions are based on the invariant:
+/// Scatter([V, V], [I', I''], 2 * (m + 1) - 1) == Concat(E, E)
+///
+/// where
+///   V = values
+///   I = indices
+///   m = max_index
+///   I' = ReplaceWithMask(I, i > m, null)
+///   I'' = ReplaceWithMask(I, i < 0, null) + m + 1

Review Comment:
   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