Skip to content
Closed
Changes from all commits
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
10 changes: 6 additions & 4 deletions arrow/src/compute/kernels/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,18 +480,20 @@ where
let mut nulls = null_indices;

let valids_len = valids.len();
let nulls_len = nulls.len();
let mut nulls_len = nulls.len();
let mut len = values.len();

if let Some(limit) = limit {
len = limit.min(len);
// count how many nulls are present in the limit
nulls_len = nulls.iter().filter(|&idx| *idx as usize <= limit).count();
Copy link
Owner

Choose a reason for hiding this comment

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

Everything else in this function doesn't need to dig into the actual index values so something smells here to me. I thought the limit is applied to the sorted result so I don't know that comparing the limit to the indices is correct here.

}
if !descending {
sort_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
sort_by(&mut valids, len - nulls_len, |a, b| {
cmp(a.1, b.1)
});
} else {
sort_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
sort_by(&mut valids, len - nulls_len, |a, b| {
cmp(a.1, b.1).reverse()
});
// reverse to keep a stable ordering
Expand Down Expand Up @@ -1442,7 +1444,7 @@ mod tests {
nulls_first: true,
}),
Some(3),
vec![None, None, Some(2)],
vec![None, Some(2), Some(0)],
Copy link
Author

Choose a reason for hiding this comment

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

I think the checked in answers are incorrect -- namely the first three items in the input are None, 0 and 2 and yet the output has two nulls in it.

Copy link
Owner

Choose a reason for hiding this comment

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

Is this passing? This looks wrong to me, the unoptimized order (ie /wo limit) would look like None, None, 2, 0, 0, -1, applying the limit: None, None, 2.

);

test_sort_primitive_arrays::<Float32Type>(
Expand Down