wesm commented on a change in pull request #7442:
URL: https://github.com/apache/arrow/pull/7442#discussion_r441613579



##########
File path: cpp/src/arrow/compute/kernels/vector_selection_test.cc
##########
@@ -0,0 +1,1637 @@
+// 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 <algorithm>
+#include <iostream>
+#include <limits>
+#include <memory>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "arrow/compute/api.h"
+#include "arrow/compute/kernels/test_util.h"
+#include "arrow/table.h"
+#include "arrow/testing/gtest_common.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/random.h"
+#include "arrow/testing/util.h"
+
+namespace arrow {
+
+using internal::checked_cast;
+using internal::checked_pointer_cast;
+using util::string_view;
+
+namespace compute {
+
+// ----------------------------------------------------------------------
+// Some random data generation helpers
+
+template <typename Type>
+std::shared_ptr<Array> RandomNumeric(int64_t length, double null_probability,
+                                     random::RandomArrayGenerator* rng) {
+  return rng->Numeric<Type>(length, 0, 127, null_probability);
+}
+
+std::shared_ptr<Array> RandomBoolean(int64_t length, double null_probability,
+                                     random::RandomArrayGenerator* rng) {
+  return rng->Boolean(length, 0.5, null_probability);
+}
+
+std::shared_ptr<Array> RandomString(int64_t length, double null_probability,
+                                    random::RandomArrayGenerator* rng) {
+  return rng->String(length, 0, 32, null_probability);
+}
+
+std::shared_ptr<Array> RandomLargeString(int64_t length, double 
null_probability,
+                                         random::RandomArrayGenerator* rng) {
+  return rng->LargeString(length, 0, 32, null_probability);
+}
+
+std::shared_ptr<Array> RandomFixedSizeBinary(int64_t length, double 
null_probability,
+                                             random::RandomArrayGenerator* 
rng) {
+  const int32_t value_size = 16;
+  int64_t data_nbytes = length * value_size;
+  std::shared_ptr<Buffer> data = *AllocateBuffer(data_nbytes);
+  random_bytes(data_nbytes, /*seed=*/0, data->mutable_data());
+  auto validity = rng->Boolean(length, 1 - null_probability);
+
+  // Assemble the data for a FixedSizeBinaryArray
+  auto values_data = 
std::make_shared<ArrayData>(fixed_size_binary(value_size), length);
+  values_data->buffers = {validity->data()->buffers[1], data};
+  return MakeArray(values_data);
+}
+
+// ----------------------------------------------------------------------
+
+TEST(GetTakeIndices, Basics) {
+  auto CheckCase = [&](const std::string& filter_json, const std::string& 
indices_json,
+                       FilterOptions::NullSelectionBehavior null_selection,
+                       const std::shared_ptr<DataType>& indices_type = 
uint16()) {
+    auto filter = ArrayFromJSON(boolean(), filter_json);
+    auto expected_indices = ArrayFromJSON(indices_type, indices_json);
+    ASSERT_OK_AND_ASSIGN(auto indices,
+                         internal::GetTakeIndices(*filter->data(), 
null_selection));
+    AssertArraysEqual(*expected_indices, *MakeArray(indices), 
/*verbose=*/true);
+  };
+
+  // Drop null cases
+  CheckCase("[]", "[]", FilterOptions::DROP);
+  CheckCase("[null]", "[]", FilterOptions::DROP);
+  CheckCase("[null, false, true, true, false, true]", "[2, 3, 5]", 
FilterOptions::DROP);
+
+  // Emit null cases
+  CheckCase("[]", "[]", FilterOptions::EMIT_NULL);
+  CheckCase("[null]", "[null]", FilterOptions::EMIT_NULL);
+  CheckCase("[null, false, true, true]", "[null, 2, 3]", 
FilterOptions::EMIT_NULL);
+}
+
+// TODO: Add slicing
+
+template <typename IndexArrayType>
+void CheckGetTakeIndicesCase(const Array& untyped_filter) {
+  const auto& filter = checked_cast<const BooleanArray&>(untyped_filter);
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<ArrayData> drop_indices,
+                       internal::GetTakeIndices(*filter.data(), 
FilterOptions::DROP));
+  // Verify DROP indices
+  {
+    IndexArrayType indices(drop_indices);
+    int64_t out_position = 0;
+    for (int64_t i = 0; i < filter.length(); ++i) {
+      if (filter.IsValid(i)) {
+        if (filter.Value(i)) {
+          ASSERT_EQ(indices.Value(out_position), i);
+          ++out_position;
+        }
+      }
+    }
+    // Check that the end length agrees with the output of GetFilterOutputSize
+    ASSERT_EQ(out_position,
+              internal::GetFilterOutputSize(*filter.data(), 
FilterOptions::DROP));
+  }
+
+  ASSERT_OK_AND_ASSIGN(
+      std::shared_ptr<ArrayData> emit_indices,
+      internal::GetTakeIndices(*filter.data(), FilterOptions::EMIT_NULL));
+
+  // Verify EMIT_NULL indices
+  {
+    IndexArrayType indices(emit_indices);
+    int64_t out_position = 0;
+    for (int64_t i = 0; i < filter.length(); ++i) {
+      if (filter.IsValid(i)) {
+        if (filter.Value(i)) {
+          ASSERT_EQ(indices.Value(out_position), i);
+          ++out_position;
+        }
+      } else {
+        ASSERT_TRUE(indices.IsNull(out_position));
+        ++out_position;
+      }
+    }
+
+    // Check that the end length agrees with the output of GetFilterOutputSize
+    ASSERT_EQ(out_position,
+              internal::GetFilterOutputSize(*filter.data(), 
FilterOptions::EMIT_NULL));
+  }
+}
+
+TEST(GetTakeIndices, RandomlyGenerated) {
+  random::RandomArrayGenerator rng(kRandomSeed);
+
+  const int64_t length = 5000;

Review comment:
       OK




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to