pitrou commented on code in PR #12963:
URL: https://github.com/apache/arrow/pull/12963#discussion_r885669372
##########
cpp/src/arrow/compute/kernels/vector_sort.cc:
##########
@@ -1909,6 +1909,220 @@ class SelectKUnstableMetaFunction : public MetaFunction
{
}
};
+// ----------------------------------------------------------------------
+// Rank implementation
+
+const RankOptions* GetDefaultRankOptions() {
+ static const auto kDefaultRankOptions = RankOptions::Defaults();
+ return &kDefaultRankOptions;
+}
+
+class ArrayRanker : public TypeVisitor {
+ public:
+ ArrayRanker(ExecContext* ctx, const Array& array, const ArrayRankOptions&
options,
+ Datum* output)
+ : TypeVisitor(),
+ ctx_(ctx),
+ array_(array),
+ order_(options.order),
+ null_placement_(options.null_placement),
+ tiebreaker_(options.tiebreaker),
+ physical_type_(GetPhysicalType(array.type())),
+ output_(output) {}
+
+ Status Run() { return physical_type_->Accept(this); }
+
+#define VISIT(TYPE) \
+ Status Visit(const TYPE& type) { return RankInternal<TYPE>(); }
+
+ VISIT_SORTABLE_PHYSICAL_TYPES(VISIT)
+
+#undef VISIT
+
+ template <typename InType>
+ Status RankInternal() {
+ using GetView = GetViewType<InType>;
+ using T = typename GetViewType<InType>::T;
+ using ArrayType = typename TypeTraits<InType>::ArrayType;
+
+ ArrayType arr(array_.data());
+ ArraySortOptions array_options(order_, null_placement_);
+
+ auto length = array_.length();
+ ARROW_ASSIGN_OR_RAISE(auto sort_indices,
+ MakeMutableUInt64Array(uint64(), length,
ctx_->memory_pool()));
+ auto sort_begin = sort_indices->GetMutableValues<uint64_t>(1);
+ auto sort_end = sort_begin + length;
+ std::iota(sort_begin, sort_end, 0);
+
+ ARROW_ASSIGN_OR_RAISE(auto array_sorter, GetArraySorter(*physical_type_));
+
+ NullPartitionResult sorted =
+ array_sorter(sort_begin, sort_end, arr, 0, array_options);
+ uint64_t rank = 0;
+
+ ARROW_ASSIGN_OR_RAISE(auto rankings,
+ MakeMutableUInt64Array(uint64(), length,
ctx_->memory_pool()));
+ auto out_begin = rankings->GetMutableValues<uint64_t>(1);
+
+ switch (tiebreaker_) {
+ case RankOptions::Dense: {
+ T curr_value, prev_value;
+ if (null_placement_ == NullPlacement::AtStart && sorted.null_count() >
0) {
+ rank++;
+ for (auto it = sorted.nulls_begin; it < sorted.nulls_end; it++) {
+ out_begin[*it] = rank;
+ }
+ }
+
+ for (auto it = sorted.non_nulls_begin; it < sorted.non_nulls_end;
it++) {
+ curr_value = GetView::LogicalValue(arr.GetView(*it));
+ if (it == sorted.non_nulls_begin || curr_value != prev_value) {
+ rank++;
+ }
+
+ out_begin[*it] = rank;
+ prev_value = curr_value;
+ }
+
+ if (null_placement_ == NullPlacement::AtEnd) {
+ rank++;
+ for (auto it = sorted.nulls_begin; it < sorted.nulls_end; it++) {
+ out_begin[*it] = rank;
+ }
+ }
+ break;
+ }
+ case RankOptions::First: {
+ for (auto it = sorted.overall_begin(); it < sorted.overall_end();
it++) {
+ out_begin[*it] = ++rank;
+ }
+ break;
+ }
+ case RankOptions::Min: {
+ T curr_value, prev_value;
+
+ if (null_placement_ == NullPlacement::AtStart) {
+ rank++;
+ for (auto it = sorted.nulls_begin; it < sorted.nulls_end; it++) {
+ out_begin[*it] = rank;
+ }
+ }
+
+ for (auto it = sorted.non_nulls_begin; it < sorted.non_nulls_end;
it++) {
+ curr_value = GetView::LogicalValue(arr.GetView(*it));
+ if (it == sorted.non_nulls_begin || it == sorted.non_nulls_end ||
Review Comment:
`it == sorted.non_nulls_end` cannot happen here, can it?
##########
cpp/src/arrow/compute/kernels/vector_sort.cc:
##########
@@ -1909,6 +1909,220 @@ class SelectKUnstableMetaFunction : public MetaFunction
{
}
};
+// ----------------------------------------------------------------------
+// Rank implementation
+
+const RankOptions* GetDefaultRankOptions() {
+ static const auto kDefaultRankOptions = RankOptions::Defaults();
+ return &kDefaultRankOptions;
+}
+
+class ArrayRanker : public TypeVisitor {
+ public:
+ ArrayRanker(ExecContext* ctx, const Array& array, const ArrayRankOptions&
options,
+ Datum* output)
+ : TypeVisitor(),
+ ctx_(ctx),
+ array_(array),
+ order_(options.order),
+ null_placement_(options.null_placement),
+ tiebreaker_(options.tiebreaker),
+ physical_type_(GetPhysicalType(array.type())),
+ output_(output) {}
+
+ Status Run() { return physical_type_->Accept(this); }
+
+#define VISIT(TYPE) \
+ Status Visit(const TYPE& type) { return RankInternal<TYPE>(); }
+
+ VISIT_SORTABLE_PHYSICAL_TYPES(VISIT)
+
+#undef VISIT
+
+ template <typename InType>
+ Status RankInternal() {
+ using GetView = GetViewType<InType>;
+ using T = typename GetViewType<InType>::T;
+ using ArrayType = typename TypeTraits<InType>::ArrayType;
+
+ ArrayType arr(array_.data());
+ ArraySortOptions array_options(order_, null_placement_);
+
+ auto length = array_.length();
+ ARROW_ASSIGN_OR_RAISE(auto sort_indices,
+ MakeMutableUInt64Array(uint64(), length,
ctx_->memory_pool()));
+ auto sort_begin = sort_indices->GetMutableValues<uint64_t>(1);
+ auto sort_end = sort_begin + length;
+ std::iota(sort_begin, sort_end, 0);
+
+ ARROW_ASSIGN_OR_RAISE(auto array_sorter, GetArraySorter(*physical_type_));
+
+ NullPartitionResult sorted =
+ array_sorter(sort_begin, sort_end, arr, 0, array_options);
+ uint64_t rank = 0;
+
+ ARROW_ASSIGN_OR_RAISE(auto rankings,
+ MakeMutableUInt64Array(uint64(), length,
ctx_->memory_pool()));
+ auto out_begin = rankings->GetMutableValues<uint64_t>(1);
+
+ switch (tiebreaker_) {
+ case RankOptions::Dense: {
+ T curr_value, prev_value;
+ if (null_placement_ == NullPlacement::AtStart && sorted.null_count() >
0) {
+ rank++;
+ for (auto it = sorted.nulls_begin; it < sorted.nulls_end; it++) {
+ out_begin[*it] = rank;
+ }
+ }
+
+ for (auto it = sorted.non_nulls_begin; it < sorted.non_nulls_end;
it++) {
+ curr_value = GetView::LogicalValue(arr.GetView(*it));
+ if (it == sorted.non_nulls_begin || curr_value != prev_value) {
+ rank++;
+ }
+
+ out_begin[*it] = rank;
+ prev_value = curr_value;
+ }
+
+ if (null_placement_ == NullPlacement::AtEnd) {
+ rank++;
+ for (auto it = sorted.nulls_begin; it < sorted.nulls_end; it++) {
+ out_begin[*it] = rank;
+ }
+ }
+ break;
+ }
+ case RankOptions::First: {
+ for (auto it = sorted.overall_begin(); it < sorted.overall_end();
it++) {
+ out_begin[*it] = ++rank;
+ }
+ break;
+ }
+ case RankOptions::Min: {
+ T curr_value, prev_value;
+
+ if (null_placement_ == NullPlacement::AtStart) {
+ rank++;
+ for (auto it = sorted.nulls_begin; it < sorted.nulls_end; it++) {
+ out_begin[*it] = rank;
+ }
+ }
+
+ for (auto it = sorted.non_nulls_begin; it < sorted.non_nulls_end;
it++) {
+ curr_value = GetView::LogicalValue(arr.GetView(*it));
+ if (it == sorted.non_nulls_begin || it == sorted.non_nulls_end ||
+ curr_value != prev_value) {
+ rank = (it - sorted.overall_begin()) + 1;
+ }
+ out_begin[*it] = rank;
+ prev_value = curr_value;
+ }
+
+ if (null_placement_ == NullPlacement::AtEnd) {
+ rank = sorted.non_nulls_end - sorted.non_nulls_begin + 1;
+ for (auto it = sorted.nulls_begin; it < sorted.nulls_end; it++) {
+ out_begin[*it] = rank;
+ }
+ }
+ break;
+ }
+ case RankOptions::Max: {
+ T curr_value, prev_value;
+
+ rank = length;
+ if (null_placement_ == NullPlacement::AtEnd) {
+ for (auto it = sorted.nulls_begin; it < sorted.nulls_end; it++) {
+ out_begin[*it] = rank;
+ }
+ rank = sorted.non_null_count();
+ }
+
+ uint64_t times_seen = 0;
+ for (auto it = sorted.non_nulls_end - 1; it >= sorted.non_nulls_begin;
it--) {
+ curr_value = GetView::LogicalValue(arr.GetView(*it));
+ if ((it < sorted.non_nulls_end - 1 && (curr_value != prev_value))) {
+ rank -= times_seen;
+ times_seen = 0;
+ }
Review Comment:
I'm curious why you're not using the same logic as in `Min` here, e.g.
```suggestion
if (it == sorted.non_nulls_end - 1 || curr_value != prev_value) {
rank = sorted.overall_end() - it;
}
```
##########
cpp/src/arrow/compute/kernels/vector_sort_test.cc:
##########
@@ -1936,6 +1940,230 @@ TEST_P(TestTableSortIndicesRandom, Sort) {
}
}
+// ----------------------------------------------------------------------
+// Tests for Rank
+
+void AssertRank(const std::shared_ptr<Array>& input, SortOrder order,
+ NullPlacement null_placement, RankOptions::Tiebreaker
tiebreaker,
+ const std::shared_ptr<Array>& expected) {
+ const std::vector<SortKey> sort_keys{SortKey("foo", order)};
+ RankOptions options(sort_keys, null_placement, tiebreaker);
+ ASSERT_OK_AND_ASSIGN(auto actual, CallFunction("rank", {input}, &options));
+ AssertDatumsEqual(expected, actual, /*verbose=*/true);
+}
+
+void AssertRankEmpty(std::shared_ptr<DataType> type, SortOrder order,
+ NullPlacement null_placement, RankOptions::Tiebreaker
tiebreaker) {
+ AssertRank(ArrayFromJSON(type, "[]"), order, null_placement, tiebreaker,
+ ArrayFromJSON(uint64(), "[]"));
+ AssertRank(ArrayFromJSON(type, "[null]"), order, null_placement, tiebreaker,
+ ArrayFromJSON(uint64(), "[1]"));
+}
+
+void AssertRankSimple(const std::shared_ptr<Array>& input, NullPlacement
null_placement,
+ RankOptions::Tiebreaker tiebreaker) {
+ auto expected_asc = ArrayFromJSON(uint64(), "[3, 4, 2, 1, 5]");
+ AssertRank(input, SortOrder::Ascending, null_placement, tiebreaker,
expected_asc);
+
+ auto expected_desc = ArrayFromJSON(uint64(), "[3, 2, 4, 5, 1]");
+ AssertRank(input, SortOrder::Descending, null_placement, tiebreaker,
expected_desc);
+}
+
+void AssertRankAllTiebreakers(const std::shared_ptr<Array>& input) {
+ AssertRank(input, SortOrder::Ascending, NullPlacement::AtEnd,
RankOptions::Min,
+ ArrayFromJSON(uint64(), "[3, 1, 4, 6, 4, 6, 1]"));
+ AssertRank(input, SortOrder::Ascending, NullPlacement::AtEnd,
RankOptions::Max,
+ ArrayFromJSON(uint64(), "[3, 2, 5, 7, 5, 7, 2]"));
+ AssertRank(input, SortOrder::Ascending, NullPlacement::AtEnd,
RankOptions::First,
+ ArrayFromJSON(uint64(), "[3, 1, 4, 6, 5, 7, 2]"));
+ AssertRank(input, SortOrder::Ascending, NullPlacement::AtEnd,
RankOptions::Dense,
+ ArrayFromJSON(uint64(), "[2, 1, 3, 4, 3, 4, 1]"));
+
+ AssertRank(input, SortOrder::Ascending, NullPlacement::AtStart,
RankOptions::Min,
+ ArrayFromJSON(uint64(), "[5, 3, 6, 1, 6, 1, 3]"));
+ AssertRank(input, SortOrder::Ascending, NullPlacement::AtStart,
RankOptions::Max,
+ ArrayFromJSON(uint64(), "[5, 4, 7, 2, 7, 2, 4]"));
+ AssertRank(input, SortOrder::Ascending, NullPlacement::AtStart,
RankOptions::First,
+ ArrayFromJSON(uint64(), "[5, 3, 6, 1, 7, 2, 4]"));
+ AssertRank(input, SortOrder::Ascending, NullPlacement::AtStart,
RankOptions::Dense,
+ ArrayFromJSON(uint64(), "[3, 2, 4, 1, 4, 1, 2]"));
+
+ AssertRank(input, SortOrder::Descending, NullPlacement::AtEnd,
RankOptions::Min,
+ ArrayFromJSON(uint64(), "[3, 4, 1, 6, 1, 6, 4]"));
+ AssertRank(input, SortOrder::Descending, NullPlacement::AtEnd,
RankOptions::Max,
+ ArrayFromJSON(uint64(), "[3, 5, 2, 7, 2, 7, 5]"));
+ AssertRank(input, SortOrder::Descending, NullPlacement::AtEnd,
RankOptions::First,
+ ArrayFromJSON(uint64(), "[3, 4, 1, 6, 2, 7, 5]"));
+ AssertRank(input, SortOrder::Descending, NullPlacement::AtEnd,
RankOptions::Dense,
+ ArrayFromJSON(uint64(), "[2, 3, 1, 4, 1, 4, 3]"));
+
+ AssertRank(input, SortOrder::Descending, NullPlacement::AtStart,
RankOptions::Min,
+ ArrayFromJSON(uint64(), "[5, 6, 3, 1, 3, 1, 6]"));
+ AssertRank(input, SortOrder::Descending, NullPlacement::AtStart,
RankOptions::Max,
+ ArrayFromJSON(uint64(), "[5, 7, 4, 2, 4, 2, 7]"));
+ AssertRank(input, SortOrder::Descending, NullPlacement::AtStart,
RankOptions::First,
+ ArrayFromJSON(uint64(), "[5, 6, 3, 1, 4, 2, 7]"));
+ AssertRank(input, SortOrder::Descending, NullPlacement::AtStart,
RankOptions::Dense,
+ ArrayFromJSON(uint64(), "[3, 4, 2, 1, 2, 1, 4]"));
+}
+
+TEST(TestRankForReal, RankReal) {
+ for (auto null_placement : AllNullPlacements()) {
+ for (auto tiebreaker : AllTiebreakers()) {
+ for (auto real_type : ::arrow::FloatingPointTypes()) {
+ for (auto order : AllOrders()) {
+ AssertRankEmpty(real_type, order, null_placement, tiebreaker);
+ }
+ AssertRankSimple(ArrayFromJSON(real_type, "[2.1, 3.2, 1.0, 0.0, 5.5]"),
+ null_placement, tiebreaker);
+
+ AssertRankAllTiebreakers(
Review Comment:
This part can be moved out of the orders and tie-breakers loops.
--
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]