Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use sort_unstable_by in primitive sorting #552

Merged
merged 1 commit into from
Jul 20, 2021
Merged
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
24 changes: 13 additions & 11 deletions arrow/src/compute/kernels/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ pub fn sort_limit(
take(values.as_ref(), &indices, None)
}

/// we can only do this if the T is primitive
#[inline]
fn sort_by<T, F>(array: &mut [T], limit: usize, cmp: F)
fn sort_unstable_by<T, F>(array: &mut [T], limit: usize, cmp: F)
where
F: FnMut(&T, &T) -> Ordering,
{
if array.len() == limit {
array.sort_by(cmp);
array.sort_unstable_by(cmp);
} else {
partial_sort(array, limit, cmp);
}
Expand Down Expand Up @@ -437,11 +438,11 @@ fn sort_boolean(
.map(|index| (index, values.value(index as usize)))
.collect::<Vec<(u32, bool)>>();
if !descending {
sort_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
sort_unstable_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
cmp(a.1, b.1)
});
} else {
sort_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
sort_unstable_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
cmp(a.1, b.1).reverse()
});
}
Expand Down Expand Up @@ -531,11 +532,11 @@ where
len = limit.min(len);
}
if !descending {
sort_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
sort_unstable_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
cmp(a.1, b.1)
});
} else {
sort_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
sort_unstable_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
cmp(a.1, b.1).reverse()
});
// reverse to keep a stable ordering
Expand Down Expand Up @@ -669,11 +670,11 @@ where
len = limit.min(len);
}
if !descending {
sort_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
sort_unstable_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
cmp(a.1, b.1)
});
} else {
sort_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
sort_unstable_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
cmp(a.1, b.1).reverse()
});
// reverse to keep a stable ordering
Expand Down Expand Up @@ -735,11 +736,11 @@ where
len = limit.min(len);
}
if !descending {
sort_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
sort_unstable_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
cmp_array(a.1.as_ref(), b.1.as_ref())
});
} else {
sort_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
sort_unstable_by(&mut valids, len.saturating_sub(nulls_len), |a, b| {
cmp_array(a.1.as_ref(), b.1.as_ref()).reverse()
});
// reverse to keep a stable ordering
Expand Down Expand Up @@ -864,7 +865,8 @@ pub fn lexsort_to_indices(
}

let lexicographical_comparator = LexicographicalComparator::try_new(columns)?;
sort_by(&mut value_indices, len, |a, b| {
// uint32 can be sorted unstably
sort_unstable_by(&mut value_indices, len, |a, b| {
lexicographical_comparator.compare(a, b)
});

Expand Down