aocsa commented on a change in pull request #11019:
URL: https://github.com/apache/arrow/pull/11019#discussion_r700701250



##########
File path: cpp/src/arrow/compute/kernels/select_k_test.cc
##########
@@ -0,0 +1,714 @@
+// 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 <functional>
+#include <iostream>
+#include <limits>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "arrow/array/array_decimal.h"
+#include "arrow/array/concatenate.h"
+#include "arrow/compute/api_vector.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"
+#include "arrow/type_traits.h"
+
+namespace arrow {
+
+using internal::checked_cast;
+using internal::checked_pointer_cast;
+
+namespace compute {
+
+namespace {
+
+// Convert arrow::Type to arrow::DataType. If arrow::Type isn't
+// parameter free, this returns an arrow::DataType with the default
+// parameter.
+template <typename ArrowType>
+enable_if_t<TypeTraits<ArrowType>::is_parameter_free, 
std::shared_ptr<DataType>>
+TypeToDataType() {
+  return TypeTraits<ArrowType>::type_singleton();
+}
+
+template <typename ArrowType>
+enable_if_t<std::is_same<ArrowType, TimestampType>::value, 
std::shared_ptr<DataType>>
+TypeToDataType() {
+  return timestamp(TimeUnit::MILLI);
+}
+
+template <typename ArrowType>
+enable_if_t<std::is_same<ArrowType, Time32Type>::value, 
std::shared_ptr<DataType>>
+TypeToDataType() {
+  return time32(TimeUnit::MILLI);
+}
+
+template <typename ArrowType>
+enable_if_t<std::is_same<ArrowType, Time64Type>::value, 
std::shared_ptr<DataType>>
+TypeToDataType() {
+  return time64(TimeUnit::NANO);
+}
+
+// ----------------------------------------------------------------------
+// Tests for SelectK
+
+template <typename ArrayType>
+auto GetLogicalValue(const ArrayType& array, uint64_t index)
+    -> decltype(array.GetView(index)) {
+  return array.GetView(index);
+}
+
+Decimal128 GetLogicalValue(const Decimal128Array& array, uint64_t index) {
+  return Decimal128(array.Value(index));
+}
+
+Decimal256 GetLogicalValue(const Decimal256Array& array, uint64_t index) {
+  return Decimal256(array.Value(index));
+}
+
+}  // namespace
+
+template <typename ArrayType, SortOrder order>
+class SelectKComparator {
+ public:
+  template <typename Type>
+  bool operator()(const Type& lval, const Type& rval) {
+    if (is_floating_type<typename ArrayType::TypeClass>::value) {
+      // NaNs ordered after non-NaNs
+      if (rval != rval) return true;
+      if (lval != lval) return false;
+    }
+    if (order == SortOrder::Ascending) {
+      return lval <= rval;
+    } else {
+      return rval <= lval;
+    }
+  }
+};
+
+template <SortOrder order>
+Result<std::shared_ptr<Array>> SelectK(const ChunkedArray& values, int64_t k) {
+  if (order == SortOrder::Descending) {
+    return TopK(values, k);
+  } else {
+    return BottomK(values, k);
+  }
+}
+
+template <SortOrder order>
+Result<std::shared_ptr<Array>> SelectK(const Array& values, int64_t k) {
+  if (order == SortOrder::Descending) {
+    return TopK(values, k);
+  } else {
+    return BottomK(values, k);
+  }
+}
+template <typename ArrowType>
+class TestSelectKBase : public TestBase {
+  using ArrayType = typename TypeTraits<ArrowType>::ArrayType;
+
+ protected:
+  void Validate(const ArrayType& array, int k, ArrayType& select_k, SortOrder 
order) {
+    ASSERT_OK_AND_ASSIGN(auto sorted_indices, SortIndices(array, order));
+    ASSERT_OK_AND_ASSIGN(Datum sorted_datum,
+                         Take(array, sorted_indices, 
TakeOptions::NoBoundsCheck()));
+    std::shared_ptr<Array> sorted_array_out = sorted_datum.make_array();
+
+    const ArrayType& sorted_array = 
*checked_pointer_cast<ArrayType>(sorted_array_out);
+
+    if (k < array.length()) {
+      for (uint64_t i = 0; i < (uint64_t)select_k.length(); ++i) {
+        const auto lval = GetLogicalValue(select_k, i);
+        const auto rval = GetLogicalValue(sorted_array, i);
+        ASSERT_TRUE(lval == rval);
+      }
+    }
+  }
+  template <SortOrder order>
+  void AssertSelectKArray(const std::shared_ptr<Array> values, int n) {
+    std::shared_ptr<Array> select_k;
+    ASSERT_OK_AND_ASSIGN(select_k, SelectK<order>(*values, n));

Review comment:
       for constant parameters I prefer to use templates, hopefully sometime we 
will be able to use const_if 




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