Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions cpp/src/arrow/compute/kernels/vector_select_k.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include <algorithm>
#include <queue>
#include <ranges>
#include <span>

#include "arrow/compute/function.h"
Expand Down Expand Up @@ -118,22 +117,22 @@ void HeapSortNonNullsToOutput(std::span<uint64_t> non_null_input_range, Comparat
return;
}
std::span<uint64_t> heap = non_null_input_range.subspan(0, output_range.size());
std::ranges::make_heap(heap, cmp);
std::make_heap(heap.begin(), heap.end(), cmp);

std::span<uint64_t> remaining_input = non_null_input_range.subspan(output_range.size());
for (uint64_t x_index : remaining_input) {
if (cmp(x_index, heap.front())) {
std::ranges::pop_heap(heap, cmp);
std::pop_heap(heap.begin(), heap.end(), cmp);
heap.back() = x_index;
std::ranges::push_heap(heap, cmp);
std::push_heap(heap.begin(), heap.end(), cmp);
}
}

// fill output in reverse when destructing,
// as the "worst" (next-to-would-have-been-replaced) element is at heap-top
for (auto& reverse_out_iter : std::ranges::reverse_view(output_range)) {
reverse_out_iter = heap.front(); // heap-top has the next element
std::ranges::pop_heap(heap, cmp);
for (int64_t i = output_range.size() - 1; i >= 0; --i) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot didn't notice, but this has the same problem as below.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

output_range[i] = heap.front(); // heap-top has the next element
std::pop_heap(heap.begin(), heap.end(), cmp);
// Decrease heap-size by one
heap = heap.first(heap.size() - 1);
}
Expand Down Expand Up @@ -422,9 +421,8 @@ class ChunkedArraySelector : public TypeVisitor {
// so the heap must have been completely filled
DCHECK_EQ(heap.size(), output.non_null_like_range.size());

for (uint64_t& reverse_out_iter :
std::ranges::reverse_view(output.non_null_like_range)) {
reverse_out_iter =
for (int64_t i = output.non_null_like_range.size() - 1; i >= 0; --i) {
output.non_null_like_range[i] =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Copilot comment is right but the suggested solution is ugly. Could be something like:

Suggested change
for (int64_t i = output.non_null_like_range.size() - 1; i >= 0; --i) {
output.non_null_like_range[i] =
for (int64_t i = output.non_null_like_range.size(); i > 0; --i) {
output.non_null_like_range[i - 1] =

or:

Suggested change
for (int64_t i = output.non_null_like_range.size() - 1; i >= 0; --i) {
output.non_null_like_range[i] =
for (auto it = output.non_null_like_range.rbegin();
it != output.non_null_like_range.rend(); ++it) {
*it =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed tests for the all-nulls case in thisisnic#34 @thisisnic

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers @pitrou

heap.top().index + heap.top().offset; // heap-top has the next element
heap.pop();
}
Comment thread
thisisnic marked this conversation as resolved.
Outdated
Expand Down
Loading