WillAyd commented on code in PR #12963:
URL: https://github.com/apache/arrow/pull/12963#discussion_r885846363
##########
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:
This would be true on the first loop
--
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]