You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/03/08 05:07:52 UTC
[GitHub] [arrow] sundy-li opened a new pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
sundy-li opened a new pull request #9602:
URL: https://github.com/apache/arrow/pull/9602
- Introduce `limit: Option<usize>` for sort functions, then we can use [partial_sort](https://crates.io/crates/partial_sort) to achieve better performance in queries with limit & sort. Datafusion can use it later if it can push up the limit to the sort plan (currently use None as default).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] alamb commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-792265078
> In ClickHouse, PartialSortingTransform(Each block in each thread)
This is a very cool idea (effectively to compute top-k for each block in parallel and then merge them together). Thank you for the explanation @sundy-li
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r587330687
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -517,20 +650,32 @@ where
},
);
- if !options.descending {
- valids.sort_by(|a, b| cmp_array(a.1.as_ref(), b.1.as_ref()))
+ let mut len = values.len();
+ let descending = options.descending;
+
+ if let Some(size) = limit {
+ len = size.min(len);
+ }
+
+ // we are not using partial_sort here, because array is ArrayRef. Something is not working good in that.
Review comment:
Sorry, there is a bug in the previous version. It's fixed in the newest [commit](https://github.com/sundy-li/partial_sort/commit/372778d0011b4d647ed4ee13d1dacb0865eb266d#diff-b1a35a68f14e696205874893c07fd24fdb88882b47c23cc0e0c80a30c7d53759R103-R141)
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] alamb commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-800137796
Merged. Thanks again @sundy-li for getting this done. It is a cool piece of functioanlity
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r588974704
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -37,10 +37,33 @@ use TimeUnit::*;
/// Returns an `ArrowError::ComputeError(String)` if the array type is either unsupported by `sort_to_indices` or `take`.
///
pub fn sort(values: &ArrayRef, options: Option<SortOptions>) -> Result<ArrayRef> {
- let indices = sort_to_indices(values, options)?;
+ let indices = sort_to_indices(values, options, None)?;
take(values.as_ref(), &indices, None)
}
+/// Sort the `ArrayRef` partially.
+/// Return an sorted `ArrayRef`, discarding the data after limit.
+pub fn partial_sort(
+ values: &ArrayRef,
+ options: Option<SortOptions>,
+ limit: Option<usize>,
+) -> Result<ArrayRef> {
+ let indices = sort_to_indices(values, options, limit)?;
+ take(values.as_ref(), &indices, None)
+}
+
+#[inline]
+fn sort_by<T, F>(array: &mut [T], limit: usize, cmp: F)
+where
+ F: FnMut(&T, &T) -> Ordering,
Review comment:
Just to be compatible with `std::sort_by`.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] jorgecarleitao commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r584928250
##########
File path: rust/arrow/Cargo.toml
##########
@@ -51,6 +51,8 @@ flatbuffers = "^0.8"
hex = "0.4"
prettytable-rs = { version = "0.8.0", optional = true }
lexical-core = "^0.7"
+partial_sort = "0.1.1"
+pdqsort = "1.0.3"
Review comment:
I am -0 on this without any benchmarks justifying this. Can't we at least feature-gate this?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] alamb edited a comment on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
alamb edited a comment on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-800137796
Merged. Thanks again @sundy-li for getting this done. It is a cool piece of functionality
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r588964696
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -1560,7 +1891,14 @@ mod tests {
Some(2),
Some(17),
])) as ArrayRef];
- test_lex_sort_arrays(input, expected);
+ test_lex_sort_arrays(input.clone(), expected, None);
+
+ let expected = vec![Arc::new(PrimitiveArray::<Int64Type>::from(vec![
+ Some(-1),
+ Some(0),
+ Some(2),
+ ])) as ArrayRef];
+ test_lex_sort_arrays(input, expected, Some(3));
Review comment:
Here input size is 4, expected size is 3.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] jorgecarleitao commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-792303834
Thanks for that, @Dandandan, all great points π, to which I generally agree with.
> It would be easier to review the code / run Miri checks, etc. when it was included in the PR
I agree. My concern atm is that our own crate does not pass miri checks (our miri checks are `cargo miri test || true`), thus, any potential soundness issues new code may have cannot be identified by our own CI. I only recently was able to have [a version of a arrow crate](https://github.com/jorgecarleitao/arrow2) to pass miri checks, and I am still [deactivating some checks](https://github.com/jorgecarleitao/arrow2/blob/main/.github/workflows/test.yml#L160) and investigating. My hypothesis is that it is very difficult to write `safe` code in this crate's design and thus my state of mind is that our best bet is to push the soundness checks about specific parts of the code to places where those can be done in a more contained environment (i.e. without all the transmutes and the like that we do here). We should still of course try to fix our own miri checks. xD
> Build times become larger compared to having the same code in the project (as the crate will be downloaded / compiled separately). Of course one extra small dependency won't matter that much, but it accumulates with lots of dependencies.
I agree. And I think that this is already a problem for DataFusion and parquet. I do get the feeling that the culprits there are crossbeam, tokio, tower and the like. These small crates seem to be downloaded and compiled in parallel and thus have a small impact when compared to the big beasts that block compilation (i.e. we have unbalanced-sized tasks). In arrows' case, the largest is flatbuffer I think, which we could feature-gate under e.g. `io_ipc` in case people do not need that. We can't get away with these during testing, but I think it is by design; if we want, IMO we need a new compilation unit (e.g. a crate).
> Has some risk that the crated will become unmaintained at some point, but causes a bug / security problem, etc. in Arrow. Or we would like to improve it, but a crate maintainer might be unresponsive. A recent example of this I saw in the memmap crate which now has a fork memmap2 which is quite hard to find as the docs / repo of memmap are still online.
I agree. That IMO should be sufficient for us to bring the crate to arrow.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] alamb commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r588875039
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -1560,7 +1891,14 @@ mod tests {
Some(2),
Some(17),
])) as ArrayRef];
- test_lex_sort_arrays(input, expected);
+ test_lex_sort_arrays(input.clone(), expected, None);
+
+ let expected = vec![Arc::new(PrimitiveArray::<Int64Type>::from(vec![
+ Some(-1),
+ Some(0),
+ Some(2),
+ ])) as ArrayRef];
+ test_lex_sort_arrays(input, expected, Some(3));
Review comment:
here too I recommend using a limit that is less than the length of the array
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -1224,6 +1460,18 @@ mod tests {
descending: false,
nulls_first: true,
}),
+ None,
+ vec![Some(1.0), Some(f64::NAN), Some(f64::NAN), Some(f64::NAN)],
+ );
+
+ // limit
+ test_sort_primitive_arrays::<Float64Type>(
+ vec![Some(f64::NAN), Some(f64::NAN), Some(f64::NAN), Some(1.0)],
Review comment:
I recommend using actual (values here (not just `Nan`)) as well as picking a limit other than 4 (which is the length of the array)
##########
File path: rust/datafusion/src/physical_plan/sort.rs
##########
@@ -156,12 +156,14 @@ fn sort_batches(
)?;
// sort combined record batch
+ // TODO: pushup the limit expression to sort
Review comment:
The way I have seen this done in other systems is a rewrite pass a `(Sort) -> (Limit)` combination into a new operator `TopK`
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Dandandan commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
Dandandan commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r593904547
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -686,90 +815,124 @@ pub fn lexsort_to_indices(columns: &[SortColumn]) -> Result<UInt32Array> {
};
let mut value_indices = (0..row_count).collect::<Vec<usize>>();
- value_indices.sort_by(lex_comparator);
+ let mut len = value_indices.len();
+
+ if let Some(limit) = limit {
+ len = limit.min(len);
+ }
+ sort_by(&mut value_indices, len, lex_comparator);
Ok(UInt32Array::from(
- value_indices
- .into_iter()
- .map(|i| i as u32)
+ (&value_indices)[0..len]
+ .iter()
+ .map(|i| *i as u32)
.collect::<Vec<u32>>(),
))
}
+pub fn partial_sort<T, F>(v: &mut [T], limit: usize, mut is_less: F)
+where
+ F: FnMut(&T, &T) -> Ordering,
+{
+ let (before, _mid, _after) = v.select_nth_unstable_by(limit, &mut is_less);
+ before.sort_unstable_by(is_less);
Review comment:
Clear, thanks @sundy-li.
Maybe some docs can be added about this?
I think it probably is not really a problem, but might be surprising by someone using the kernel. And we might want to see whether we could use a (faster) unstable version of the normal sort kernel as well (I think that could be used by DataFusion as well).
What do you think @alamb
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Dandandan commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
Dandandan commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-792262775
I think there are some very nice use-cases of this optimization (top-k kind of queries), I think it would be very cool to have more advanced features like this in Arrow/DataFusion/...
The partial_sort crate seems also quite small (about 60 lines without tests/imports). What about having the code in Arrow for now, and (try to) limit the amount of unsafety @sundy-li and we can have a next review iteration?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li edited a comment on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li edited a comment on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-788550187
I removed `pdqsort`, because the performance didn't show any improvement during the benches.
Added a separate function `partial_sort `as @jorgecarleitao suggested.
I did not find any other `partial_sort` function in Rust like std::partial_sort in c++. So I created [one](https://github.com/sundy-li/partial_sort).
And the [benche results about partial_sort in arrow](https://github.com/apache/arrow/blob/f19fc1644a07cefcf584d450ad70c5708926c252/rust/arrow/benches/sort_kernel.rs#L76-L106) are:
```
sort 2^12 time: [753.58 us 755.43 us 758.14 us]
sort nulls 2^12 time: [633.41 us 635.28 us 637.51 us]
sort 2^12 limit 10 time: [49.246 us 49.820 us 50.667 us]
sort 2^12 limit 100 time: [115.11 us 116.26 us 117.76 us]
sort 2^12 limit 1000 time: [113.61 us 114.21 us 114.85 us]
sort nulls 2^12 limit 10 time: [66.283 us 66.725 us 67.347 us]
sort nulls 2^12 limit 100 time: [66.455 us 66.932 us 67.504 us]
sort nulls 2^12 limit 1000 time: [65.030 us 65.218 us 65.412 us]
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] paddyhoran commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
paddyhoran commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r586321776
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -517,20 +650,32 @@ where
},
);
- if !options.descending {
- valids.sort_by(|a, b| cmp_array(a.1.as_ref(), b.1.as_ref()))
+ let mut len = values.len();
+ let descending = options.descending;
+
+ if let Some(size) = limit {
+ len = size.min(len);
+ }
+
+ // we are not using partial_sort here, because array is ArrayRef. Something is not working good in that.
Review comment:
What does "not working good" mean here?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r593899532
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -686,90 +815,124 @@ pub fn lexsort_to_indices(columns: &[SortColumn]) -> Result<UInt32Array> {
};
let mut value_indices = (0..row_count).collect::<Vec<usize>>();
- value_indices.sort_by(lex_comparator);
+ let mut len = value_indices.len();
+
+ if let Some(limit) = limit {
+ len = limit.min(len);
+ }
+ sort_by(&mut value_indices, len, lex_comparator);
Ok(UInt32Array::from(
- value_indices
- .into_iter()
- .map(|i| i as u32)
+ (&value_indices)[0..len]
+ .iter()
+ .map(|i| *i as u32)
.collect::<Vec<u32>>(),
))
}
+pub fn partial_sort<T, F>(v: &mut [T], limit: usize, mut is_less: F)
+where
+ F: FnMut(&T, &T) -> Ordering,
+{
+ let (before, _mid, _after) = v.select_nth_unstable_by(limit, &mut is_less);
+ before.sort_unstable_by(is_less);
Review comment:
There are some reasons for using unstable_sort.
1. `select_nth_unstable_by` is unstable, so using `before.sort_stable_by` don't ensure it's stable. Currently we do not have select_nth_stable_by
2. `sort_unstable` is faster than sort_stable, refer to [doc](https://doc.rust-lang.org/std/primitive.slice.html#method.sort_unstable)
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Dandandan commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
Dandandan commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-792286475
@jorgecarleitao
I'm perfectly fine with that approach too.
I do think that depending on crates that are useful instead of building our own or even duplicating code is what we should do in general, and indeed will benefit others in the Rust community.
My reasoning behind my suggestion above was
* The crate has no currently no users and (I think) only 1 maintainer.
* The code size is small
* The PR author is the same as the maintainer of the crate (and I guess it was created for this use case for now?)
* It would be easier to review the code / run Miri checks, etc. when it was included in the PR
Of course, there might be some other parties that would be interested in `partial_sort` or maybe it will at some point make it to the standard library.
Also dependencies have *some* costs, some of them being:
* Has some risk that the crated will become unmaintained at some point, but causes a bug / security problem, etc. in Arrow. Or we would like to improve it, but a crate maintainer might be unresponsive. A recent example of this I saw in the `memmap` crate which now has a fork `memmap2` which is quite hard to find as the docs / repo of `memmap` are still online.
* Non-semver breaking changes can not be caught in PRs
* Build times become larger compared to having the same code in the project (as the crate will be downloaded / compiled separately). Of course one extra small dependency won't matter that much, but it accumulates with lots of dependencies.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-797294233
Hi, all. From other user's adivice, partial_sort could be
```
pub fn partial_sort<T, F>(v: &mut [T], limit: usize, mut is_less: F)
where
F: FnMut(&T, &T) -> Ordering,
{
let (before, _mid, _after) = v.select_nth_unstable_by(limit, &mut is_less);
before.sort_unstable_by(is_less);
}
```
So I abandon to use of custom partial_sort. This could fix the issue and risk we are arguing about.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r593899532
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -686,90 +815,124 @@ pub fn lexsort_to_indices(columns: &[SortColumn]) -> Result<UInt32Array> {
};
let mut value_indices = (0..row_count).collect::<Vec<usize>>();
- value_indices.sort_by(lex_comparator);
+ let mut len = value_indices.len();
+
+ if let Some(limit) = limit {
+ len = limit.min(len);
+ }
+ sort_by(&mut value_indices, len, lex_comparator);
Ok(UInt32Array::from(
- value_indices
- .into_iter()
- .map(|i| i as u32)
+ (&value_indices)[0..len]
+ .iter()
+ .map(|i| *i as u32)
.collect::<Vec<u32>>(),
))
}
+pub fn partial_sort<T, F>(v: &mut [T], limit: usize, mut is_less: F)
+where
+ F: FnMut(&T, &T) -> Ordering,
+{
+ let (before, _mid, _after) = v.select_nth_unstable_by(limit, &mut is_less);
+ before.sort_unstable_by(is_less);
Review comment:
There are some reasons for using unstable_sort.
1. `select_nth_unstable_by` is unstable, so using `before.sort_stable_by`cadon't ensure it's stable. Currently we do not have select_nth_stable_by
2. `sort_unstable` is faster than sort_stable, refer to [doc](https://doc.rust-lang.org/std/primitive.slice.html#method.sort_unstable)
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li closed pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li closed pull request #9602:
URL: https://github.com/apache/arrow/pull/9602
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] codecov-io commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kenerl
Posted by GitBox <gi...@apache.org>.
codecov-io commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-787460272
# [Codecov](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=h1) Report
> Merging [#9602](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=desc) (85439c1) into [master](https://codecov.io/gh/apache/arrow/commit/5ae63f8ec0c14d5e871aa79d90e5d163b369e704?el=desc) (5ae63f8) will **decrease** coverage by `0.02%`.
> The diff coverage is `60.24%`.
[](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #9602 +/- ##
==========================================
- Coverage 82.32% 82.30% -0.03%
==========================================
Files 245 245
Lines 56383 56555 +172
==========================================
+ Hits 46419 46546 +127
- Misses 9964 10009 +45
```
| [Impacted Files](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=tree) | Coverage Ξ | |
|---|---|---|
| [rust/arrow/src/compute/kernels/sort.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9hcnJvdy9zcmMvY29tcHV0ZS9rZXJuZWxzL3NvcnQucnM=) | `91.10% <60.00%> (-2.46%)` | :arrow_down: |
| [rust/datafusion/src/physical\_plan/sort.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9kYXRhZnVzaW9uL3NyYy9waHlzaWNhbF9wbGFuL3NvcnQucnM=) | `90.22% <100.00%> (+0.05%)` | :arrow_up: |
| [rust/arrow/src/datatypes/field.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9hcnJvdy9zcmMvZGF0YXR5cGVzL2ZpZWxkLnJz) | `55.47% <0.00%> (-0.66%)` | :arrow_down: |
| [...integration-testing/src/flight\_server\_scenarios.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9pbnRlZ3JhdGlvbi10ZXN0aW5nL3NyYy9mbGlnaHRfc2VydmVyX3NjZW5hcmlvcy5ycw==) | `0.00% <0.00%> (ΓΈ)` | |
| [...-testing/src/flight\_server\_scenarios/middleware.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9pbnRlZ3JhdGlvbi10ZXN0aW5nL3NyYy9mbGlnaHRfc2VydmVyX3NjZW5hcmlvcy9taWRkbGV3YXJlLnJz) | `0.00% <0.00%> (ΓΈ)` | |
| [...ng/src/flight\_server\_scenarios/auth\_basic\_proto.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9pbnRlZ3JhdGlvbi10ZXN0aW5nL3NyYy9mbGlnaHRfc2VydmVyX3NjZW5hcmlvcy9hdXRoX2Jhc2ljX3Byb3RvLnJz) | `0.00% <0.00%> (ΓΈ)` | |
| [...ng/src/flight\_server\_scenarios/integration\_test.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9pbnRlZ3JhdGlvbi10ZXN0aW5nL3NyYy9mbGlnaHRfc2VydmVyX3NjZW5hcmlvcy9pbnRlZ3JhdGlvbl90ZXN0LnJz) | `0.00% <0.00%> (ΓΈ)` | |
| [rust/arrow/src/csv/reader.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9hcnJvdy9zcmMvY3N2L3JlYWRlci5ycw==) | `93.17% <0.00%> (+0.02%)` | :arrow_up: |
| [rust/arrow/src/compute/kernels/cast.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9hcnJvdy9zcmMvY29tcHV0ZS9rZXJuZWxzL2Nhc3QucnM=) | `97.33% <0.00%> (+0.03%)` | :arrow_up: |
| [rust/arrow/src/datatypes/schema.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9hcnJvdy9zcmMvZGF0YXR5cGVzL3NjaGVtYS5ycw==) | `73.39% <0.00%> (+0.76%)` | :arrow_up: |
| ... and [2 more](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Ξ = absolute <relative> (impact)`, `ΓΈ = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=footer). Last update [5ae63f8...85439c1](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-792216219
> For example, using a priority queue to keep only the top k values in memory.
Yes, but lots of codes may duplicate with sort kernel. partial_sort used priority queue inside. It maybe good to do sorting in pipeline OLAP systems
In ClickHouse, PartialSortingTransform(Each block in each thread) --> MergeSortingTransform(Blocks to one block in each thread) --> MergingSortedTransform(N Block in N Thread to one block) .
```
ββexplainβββββββββββββββββββββββββββββββββ
β (Expression) β
β ExpressionTransform β
β (Limit) β
β Limit β
β (MergingSorted) β
β MergingSortedTransform 16 β 1 β
β (MergeSorting) β
β MergeSortingTransform Γ 16 β
β (PartialSorting) β
β LimitsCheckingTransform Γ 16 β
β PartialSortingTransform Γ 16 β
β (Expression) β
β ExpressionTransform Γ 16 β
β (SettingQuotaAndLimits) β
β (ReadFromStorage) β
β NumbersMt Γ 16 0 β 1 β
ββββββββββββββββββββββββββββββββββββββββββ
```
@alamb @jorgecarleitao Thanks for all your reviews. I also have the consideration about unsafe codes in partial_sort may break `arrow`, because it was just created, without any used in production(BTW I am new to rust).
We can keep this MR open currently until you think it's safe enough or must have it.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r593899532
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -686,90 +815,124 @@ pub fn lexsort_to_indices(columns: &[SortColumn]) -> Result<UInt32Array> {
};
let mut value_indices = (0..row_count).collect::<Vec<usize>>();
- value_indices.sort_by(lex_comparator);
+ let mut len = value_indices.len();
+
+ if let Some(limit) = limit {
+ len = limit.min(len);
+ }
+ sort_by(&mut value_indices, len, lex_comparator);
Ok(UInt32Array::from(
- value_indices
- .into_iter()
- .map(|i| i as u32)
+ (&value_indices)[0..len]
+ .iter()
+ .map(|i| *i as u32)
.collect::<Vec<u32>>(),
))
}
+pub fn partial_sort<T, F>(v: &mut [T], limit: usize, mut is_less: F)
+where
+ F: FnMut(&T, &T) -> Ordering,
+{
+ let (before, _mid, _after) = v.select_nth_unstable_by(limit, &mut is_less);
+ before.sort_unstable_by(is_less);
Review comment:
There are some reasons for using unstable_sort.
1. `select_nth_unstable_by` is unstable, so using `before.sort_stable_by`cadon't ensure it's stable.
2. `sort_unstable` is faster than sort_stable, refer to [doc](https://doc.rust-lang.org/std/primitive.slice.html#method.sort_unstable)
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-788550187
I removed `pdqsort`, because the performance didn't show any improvement during the benches.
Added a separate function `partial_sort `as @jorgecarleitao suggested.
I did not find any other `partial_sort` function in Rust like std::partial_sort in c++. So I created [one](https://github.com/sundy-li/partial_sort).
And the benche results about partial_sort in arrow are:
```
sort 2^10 time: [156.26 us 156.97 us 157.88 us]
sort 2^12 time: [753.58 us 755.43 us 758.14 us]
sort nulls 2^10 time: [133.73 us 134.42 us 135.27 us]
sort nulls 2^12 time: [633.41 us 635.28 us 637.51 us]
sort 2^12 limit 10 time: [49.246 us 49.820 us 50.667 us]
sort 2^12 limit 100 time: [115.11 us 116.26 us 117.76 us]
sort 2^12 limit 1000 time: [113.61 us 114.21 us 114.85 us]
sort nulls 2^12 limit 10 time: [66.283 us 66.725 us 67.347 us]
sort nulls 2^12 limit 100 time: [66.455 us 66.932 us 67.504 us]
sort nulls 2^12 limit 1000 time: [65.030 us 65.218 us 65.412 us]
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] codecov-io edited a comment on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kenerl
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-787460272
# [Codecov](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=h1) Report
> Merging [#9602](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=desc) (06c503c) into [master](https://codecov.io/gh/apache/arrow/commit/5ae63f8ec0c14d5e871aa79d90e5d163b369e704?el=desc) (5ae63f8) will **increase** coverage by `0.03%`.
> The diff coverage is `82.68%`.
[](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #9602 +/- ##
==========================================
+ Coverage 82.32% 82.36% +0.03%
==========================================
Files 245 245
Lines 56383 56661 +278
==========================================
+ Hits 46419 46668 +249
- Misses 9964 9993 +29
```
| [Impacted Files](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=tree) | Coverage Ξ | |
|---|---|---|
| [rust/arrow/src/compute/kernels/sort.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9hcnJvdy9zcmMvY29tcHV0ZS9rZXJuZWxzL3NvcnQucnM=) | `93.60% <82.62%> (+0.04%)` | :arrow_up: |
| [rust/datafusion/src/physical\_plan/sort.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9kYXRhZnVzaW9uL3NyYy9waHlzaWNhbF9wbGFuL3NvcnQucnM=) | `90.22% <100.00%> (+0.05%)` | :arrow_up: |
| [rust/arrow/src/datatypes/field.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9hcnJvdy9zcmMvZGF0YXR5cGVzL2ZpZWxkLnJz) | `55.47% <0.00%> (-0.66%)` | :arrow_down: |
| [...integration-testing/src/flight\_server\_scenarios.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9pbnRlZ3JhdGlvbi10ZXN0aW5nL3NyYy9mbGlnaHRfc2VydmVyX3NjZW5hcmlvcy5ycw==) | `0.00% <0.00%> (ΓΈ)` | |
| [...-testing/src/flight\_server\_scenarios/middleware.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9pbnRlZ3JhdGlvbi10ZXN0aW5nL3NyYy9mbGlnaHRfc2VydmVyX3NjZW5hcmlvcy9taWRkbGV3YXJlLnJz) | `0.00% <0.00%> (ΓΈ)` | |
| [...ng/src/flight\_server\_scenarios/auth\_basic\_proto.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9pbnRlZ3JhdGlvbi10ZXN0aW5nL3NyYy9mbGlnaHRfc2VydmVyX3NjZW5hcmlvcy9hdXRoX2Jhc2ljX3Byb3RvLnJz) | `0.00% <0.00%> (ΓΈ)` | |
| [...ng/src/flight\_server\_scenarios/integration\_test.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9pbnRlZ3JhdGlvbi10ZXN0aW5nL3NyYy9mbGlnaHRfc2VydmVyX3NjZW5hcmlvcy9pbnRlZ3JhdGlvbl90ZXN0LnJz) | `0.00% <0.00%> (ΓΈ)` | |
| [rust/arrow/src/csv/reader.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9hcnJvdy9zcmMvY3N2L3JlYWRlci5ycw==) | `93.17% <0.00%> (+0.02%)` | :arrow_up: |
| [rust/arrow/src/compute/kernels/cast.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9hcnJvdy9zcmMvY29tcHV0ZS9rZXJuZWxzL2Nhc3QucnM=) | `97.33% <0.00%> (+0.03%)` | :arrow_up: |
| [rust/arrow/src/datatypes/schema.rs](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree#diff-cnVzdC9hcnJvdy9zcmMvZGF0YXR5cGVzL3NjaGVtYS5ycw==) | `73.39% <0.00%> (+0.76%)` | :arrow_up: |
| ... and [2 more](https://codecov.io/gh/apache/arrow/pull/9602/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Ξ = absolute <relative> (impact)`, `ΓΈ = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=footer). Last update [5ae63f8...06c503c](https://codecov.io/gh/apache/arrow/pull/9602?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r586975774
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -278,12 +347,27 @@ fn sort_boolean(
let valids_len = valids.len();
let nulls_len = nulls.len();
- if !descending {
- valids.sort_by(|a, b| a.1.cmp(&b.1));
- } else {
- valids.sort_by(|a, b| a.1.cmp(&b.1).reverse());
- // reverse to keep a stable ordering
- nulls.reverse();
+ let mut len = values.len();
+ match limit {
+ Some(limit) => {
+ len = limit.min(len);
+ if !descending {
+ valids.partial_sort(len, |a, b| cmp(a.1, b.1));
Review comment:
> Sorry not "fast path" but would the performance be the same as `sort_by`?
I'm afraid not. The sort function in rust is merge sort, it is slightly faster than the heap sort for larger sets to sort all elements.
But it requires twice the memory of the heap sort because of the second array.
```
sort 2^12 time: [799.70 us 806.41 us 814.15 us]
sort 2^12 limit 2^12 time: [1.2848 ms 1.3012 ms 1.3229 ms]
sort nulls 2^12 time: [647.20 us 649.27 us 651.61 us]
sort nulls 2^12 limit 2^12 time: [780.17 us 788.48 us 798.04 us]
```
We can make a new function to reduce the repeated patterns or make partial_sort fallback to default sort when limit equals to len.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] alamb commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-792324683
> From our side, I am not sure I agree with copy-pasting code from a crate to our own crate. IMO we should instead actively engage with our "providers" by contributing upstream.
@jorgecarleitao as you and @Dandandan have elucidated, I think there are tradeoffs to the two approaches. I am happy with either approach in general -- I suggested the "include it in arrow approach" in this case as I felt it had less overhead for @sundy-li who has already worked on this PR a great deal, but that may not be the best approach either short or long term.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] paddyhoran commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
paddyhoran commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r586323373
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -278,12 +347,27 @@ fn sort_boolean(
let valids_len = valids.len();
let nulls_len = nulls.len();
- if !descending {
- valids.sort_by(|a, b| a.1.cmp(&b.1));
- } else {
- valids.sort_by(|a, b| a.1.cmp(&b.1).reverse());
- // reverse to keep a stable ordering
- nulls.reverse();
+ let mut len = values.len();
+ match limit {
+ Some(limit) => {
+ len = limit.min(len);
+ if !descending {
+ valids.partial_sort(len, |a, b| cmp(a.1, b.1));
Review comment:
Does `partial_sort` have a fast path when `len` is the length of the array? The pattern in this block is repeated a few times, if we could just use `partial_sort` it could be simplified to a simple `if let` that updates `len`?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] alamb closed pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
alamb closed pull request #9602:
URL: https://github.com/apache/arrow/pull/9602
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] paddyhoran commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
paddyhoran commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r586324951
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -278,12 +347,27 @@ fn sort_boolean(
let valids_len = valids.len();
let nulls_len = nulls.len();
- if !descending {
- valids.sort_by(|a, b| a.1.cmp(&b.1));
- } else {
- valids.sort_by(|a, b| a.1.cmp(&b.1).reverse());
- // reverse to keep a stable ordering
- nulls.reverse();
+ let mut len = values.len();
+ match limit {
+ Some(limit) => {
+ len = limit.min(len);
+ if !descending {
+ valids.partial_sort(len, |a, b| cmp(a.1, b.1));
Review comment:
Sorry not "fast path" but would the performance be the same as `sort_by`?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Dandandan commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
Dandandan commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r593885246
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -686,90 +815,124 @@ pub fn lexsort_to_indices(columns: &[SortColumn]) -> Result<UInt32Array> {
};
let mut value_indices = (0..row_count).collect::<Vec<usize>>();
- value_indices.sort_by(lex_comparator);
+ let mut len = value_indices.len();
+
+ if let Some(limit) = limit {
+ len = limit.min(len);
+ }
+ sort_by(&mut value_indices, len, lex_comparator);
Ok(UInt32Array::from(
- value_indices
- .into_iter()
- .map(|i| i as u32)
+ (&value_indices)[0..len]
+ .iter()
+ .map(|i| *i as u32)
.collect::<Vec<u32>>(),
))
}
+pub fn partial_sort<T, F>(v: &mut [T], limit: usize, mut is_less: F)
+where
+ F: FnMut(&T, &T) -> Ordering,
+{
+ let (before, _mid, _after) = v.select_nth_unstable_by(limit, &mut is_less);
+ before.sort_unstable_by(is_less);
Review comment:
I am wondering, why can we use unstable sort here, while we use stable sort for the normal sort kernel?
Shouldn't that be an option of the kernel?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li edited a comment on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li edited a comment on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-788550187
I removed `pdqsort`, because the performance didn't show any improvement during the benches.
Added a separate function `partial_sort `as @jorgecarleitao suggested.
I did not find any other `partial_sort` function in Rust like std::partial_sort in c++. So I created [one](https://github.com/sundy-li/partial_sort).
And the [benche results about partial_sort in arrow](https://github.com/apache/arrow/blob/f19fc1644a07cefcf584d450ad70c5708926c252/rust/arrow/benches/sort_kernel.rs#L76-L106) are:
```
sort 2^10 time: [156.26 us 156.97 us 157.88 us]
sort 2^12 time: [753.58 us 755.43 us 758.14 us]
sort nulls 2^10 time: [133.73 us 134.42 us 135.27 us]
sort nulls 2^12 time: [633.41 us 635.28 us 637.51 us]
sort 2^12 limit 10 time: [49.246 us 49.820 us 50.667 us]
sort 2^12 limit 100 time: [115.11 us 116.26 us 117.76 us]
sort 2^12 limit 1000 time: [113.61 us 114.21 us 114.85 us]
sort nulls 2^12 limit 10 time: [66.283 us 66.725 us 67.347 us]
sort nulls 2^12 limit 100 time: [66.455 us 66.932 us 67.504 us]
sort nulls 2^12 limit 1000 time: [65.030 us 65.218 us 65.412 us]
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] jorgecarleitao commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r584928250
##########
File path: rust/arrow/Cargo.toml
##########
@@ -51,6 +51,8 @@ flatbuffers = "^0.8"
hex = "0.4"
prettytable-rs = { version = "0.8.0", optional = true }
lexical-core = "^0.7"
+partial_sort = "0.1.1"
+pdqsort = "1.0.3"
Review comment:
I am -1 on this. Either add a feature gate or something else.
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -36,8 +36,12 @@ use TimeUnit::*;
///
/// Returns an `ArrowError::ComputeError(String)` if the array type is either unsupported by `sort_to_indices` or `take`.
///
-pub fn sort(values: &ArrayRef, options: Option<SortOptions>) -> Result<ArrayRef> {
- let indices = sort_to_indices(values, options)?;
+pub fn sort(
+ values: &ArrayRef,
+ options: Option<SortOptions>,
+ limit: Option<usize>,
Review comment:
I think that this should be done on a separate function instead of adding another parameter to this one. It would also allow to feature-gate said function with the two new dependencies.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li edited a comment on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li edited a comment on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-788550187
I removed `pdqsort`, because the performance didn't show any improvement during the benches in my pc.
Added a separate function `partial_sort `as @jorgecarleitao suggested.
I did not find any other `partial_sort` function in Rust like std::partial_sort in c++. So I created [one](https://github.com/sundy-li/partial_sort).
And the [benche results about partial_sort in arrow](https://github.com/apache/arrow/blob/dc3167e3826177af1a6feec516a528a4ee38c674/rust/arrow/benches/sort_kernel.rs#L76-L106) are:
```
sort 2^12 time: [753.58 us 755.43 us 758.14 us]
sort nulls 2^12 time: [633.41 us 635.28 us 637.51 us]
sort 2^12 limit 10 time: [49.246 us 49.820 us 50.667 us]
sort 2^12 limit 100 time: [115.11 us 116.26 us 117.76 us]
sort 2^12 limit 1000 time: [645.91 us 654.36 us 663.78 us]
sort nulls 2^12 limit 10 time: [66.283 us 66.725 us 67.347 us]
sort nulls 2^12 limit 100 time: [76.281 us 77.907 us 79.783 us]
sort nulls 2^12 limit 1000 time: [258.98 us 260.32 us 262.24 us]
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] jorgecarleitao commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r588970908
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -37,10 +37,33 @@ use TimeUnit::*;
/// Returns an `ArrowError::ComputeError(String)` if the array type is either unsupported by `sort_to_indices` or `take`.
///
pub fn sort(values: &ArrayRef, options: Option<SortOptions>) -> Result<ArrayRef> {
- let indices = sort_to_indices(values, options)?;
+ let indices = sort_to_indices(values, options, None)?;
take(values.as_ref(), &indices, None)
}
+/// Sort the `ArrayRef` partially.
+/// Return an sorted `ArrayRef`, discarding the data after limit.
+pub fn partial_sort(
+ values: &ArrayRef,
+ options: Option<SortOptions>,
+ limit: Option<usize>,
+) -> Result<ArrayRef> {
+ let indices = sort_to_indices(values, options, limit)?;
+ take(values.as_ref(), &indices, None)
+}
+
+#[inline]
+fn sort_by<T, F>(array: &mut [T], limit: usize, cmp: F)
+where
+ F: FnMut(&T, &T) -> Ordering,
Review comment:
Why does it need to be `FnMut`?
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -76,118 +99,175 @@ where
// partition indices into valid and null indices
fn partition_validity(array: &ArrayRef) -> (Vec<u32>, Vec<u32>) {
- let indices = 0..(array.len().to_u32().unwrap());
- indices.partition(|index| array.is_valid(*index as usize))
+ match array.null_count() {
+ // faster path
+ 0 => ((0..(array.len() as u32)).collect(), vec![]),
+ _ => {
+ let indices = 0..(array.len() as u32);
+ indices.partition(|index| array.is_valid(*index as usize))
+ }
+ }
}
/// Sort elements from `ArrayRef` into an unsigned integer (`UInt32Array`) of indices.
/// For floating point arrays any NaN values are considered to be greater than any other non-null value
Review comment:
The doc could be updated :)
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li edited a comment on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li edited a comment on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-788550187
I removed `pdqsort`, because the performance didn't show any improvement during the benches in my pc.
Added a separate function `partial_sort `as @jorgecarleitao suggested.
I did not find any other `partial_sort` function in Rust like std::partial_sort in c++. So I created [one](https://github.com/sundy-li/partial_sort).
And the [benche results about partial_sort in arrow](https://github.com/apache/arrow/blob/f19fc1644a07cefcf584d450ad70c5708926c252/rust/arrow/benches/sort_kernel.rs#L76-L106) are:
```
sort 2^12 time: [753.58 us 755.43 us 758.14 us]
sort nulls 2^12 time: [633.41 us 635.28 us 637.51 us]
sort 2^12 limit 10 time: [49.246 us 49.820 us 50.667 us]
sort 2^12 limit 100 time: [115.11 us 116.26 us 117.76 us]
sort 2^12 limit 1000 time: [113.61 us 114.21 us 114.85 us]
sort nulls 2^12 limit 10 time: [66.283 us 66.725 us 67.347 us]
sort nulls 2^12 limit 100 time: [66.455 us 66.932 us 67.504 us]
sort nulls 2^12 limit 1000 time: [65.030 us 65.218 us 65.412 us]
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li edited a comment on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li edited a comment on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-797294233
Hi, all. From other user's advice, partial_sort could be
```
pub fn partial_sort<T, F>(v: &mut [T], limit: usize, mut is_less: F)
where
F: FnMut(&T, &T) -> Ordering,
{
let (before, _mid, _after) = v.select_nth_unstable_by(limit, &mut is_less);
before.sort_unstable_by(is_less);
}
```
So I abandon to use of custom partial_sort. This could fix the issue and risk we are arguing about.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Dandandan commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
Dandandan commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r593904547
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -686,90 +815,124 @@ pub fn lexsort_to_indices(columns: &[SortColumn]) -> Result<UInt32Array> {
};
let mut value_indices = (0..row_count).collect::<Vec<usize>>();
- value_indices.sort_by(lex_comparator);
+ let mut len = value_indices.len();
+
+ if let Some(limit) = limit {
+ len = limit.min(len);
+ }
+ sort_by(&mut value_indices, len, lex_comparator);
Ok(UInt32Array::from(
- value_indices
- .into_iter()
- .map(|i| i as u32)
+ (&value_indices)[0..len]
+ .iter()
+ .map(|i| *i as u32)
.collect::<Vec<u32>>(),
))
}
+pub fn partial_sort<T, F>(v: &mut [T], limit: usize, mut is_less: F)
+where
+ F: FnMut(&T, &T) -> Ordering,
+{
+ let (before, _mid, _after) = v.select_nth_unstable_by(limit, &mut is_less);
+ before.sort_unstable_by(is_less);
Review comment:
Clear, thanks @sundy-li.
Maybe some docs can be added about this?
I think it probably is not really a problem, but might be surprising by someone using the kernel. And we might want to see whether we could use a (faster) unstable version of the normal sort kernel as well (I think that could be used by DataFusion as well I think).
What do you think @alamb
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -686,90 +815,124 @@ pub fn lexsort_to_indices(columns: &[SortColumn]) -> Result<UInt32Array> {
};
let mut value_indices = (0..row_count).collect::<Vec<usize>>();
- value_indices.sort_by(lex_comparator);
+ let mut len = value_indices.len();
+
+ if let Some(limit) = limit {
+ len = limit.min(len);
+ }
+ sort_by(&mut value_indices, len, lex_comparator);
Ok(UInt32Array::from(
- value_indices
- .into_iter()
- .map(|i| i as u32)
+ (&value_indices)[0..len]
+ .iter()
+ .map(|i| *i as u32)
.collect::<Vec<u32>>(),
))
}
+pub fn partial_sort<T, F>(v: &mut [T], limit: usize, mut is_less: F)
+where
+ F: FnMut(&T, &T) -> Ordering,
+{
+ let (before, _mid, _after) = v.select_nth_unstable_by(limit, &mut is_less);
+ before.sort_unstable_by(is_less);
Review comment:
Clear, thanks @sundy-li.
Maybe some docs can be added about this?
I think it probably is not really a problem, but might be surprising by someone using the kernel. And we might want to see whether we could use a (faster) unstable version of the normal sort kernel as well (Ithat could be used by DataFusion as well I think).
What do you think @alamb
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -686,90 +815,124 @@ pub fn lexsort_to_indices(columns: &[SortColumn]) -> Result<UInt32Array> {
};
let mut value_indices = (0..row_count).collect::<Vec<usize>>();
- value_indices.sort_by(lex_comparator);
+ let mut len = value_indices.len();
+
+ if let Some(limit) = limit {
+ len = limit.min(len);
+ }
+ sort_by(&mut value_indices, len, lex_comparator);
Ok(UInt32Array::from(
- value_indices
- .into_iter()
- .map(|i| i as u32)
+ (&value_indices)[0..len]
+ .iter()
+ .map(|i| *i as u32)
.collect::<Vec<u32>>(),
))
}
+pub fn partial_sort<T, F>(v: &mut [T], limit: usize, mut is_less: F)
+where
+ F: FnMut(&T, &T) -> Ordering,
+{
+ let (before, _mid, _after) = v.select_nth_unstable_by(limit, &mut is_less);
+ before.sort_unstable_by(is_less);
Review comment:
Clear, thanks @sundy-li.
Maybe some docs can be added about this?
I think it probably is not really a problem, but might be surprising by someone using the kernel. And we might want to see whether we could use a (faster) unstable version of the normal sort kernel as well (that could be used by DataFusion as well I think).
What do you think @alamb
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] jorgecarleitao commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-792275857
Thanks @sundy-li for the explanation.
> The partial_sort crate seems also quite small (about 60 lines without tests/imports). What about having the code in Arrow for now, and (try to) limit the amount of unsafety @sundy-li and we can have a next review iteration?
I do not follow the argument. Isn't the whole purpose of crates and dependencies to separate concerns and allow code reuse across the ecosystem without dragging dependencies?
From what I've seen, the crate:
* addresses one problem and one alone (unlike our repo or our workspace)
* has CI and CD in place
* it is easy to publish in crates.io (via a github action) which makes CD also easy
* it is duo license, as it is the standard in Rust
* it uses SemVer
If I were @sundy-li, I would keep the setup exactly like it is: it enables the implementation to be used by more projects without having to drag arrow and its dependencies with it, while at the same time maintains the generality that the algorithm itself has. I would be behaving exactly how @sundy-li is behaving: engage with potential "customers" like Arrow to identify value, how it can be adopted, and what are the points to improve.
From our side, I am not sure I agree with copy-pasting code from a crate to our own crate. IMO we should instead actively engage with our "providers" by contributing upstream. In this particular case, one step is to have MIRI run on CI. The other is to either limit or document the usage of `unsafe`. This way, the Rust community as a whole benefits from these, instead of the benefit be limited to users of the Arrow crate. I created a PR upstream to address one of the steps: https://github.com/sundy-li/partial_sort/pull/1 .
Side note: @alamb , what just happened (on which I could trivially PR upstream without any complications, issues, etc.) is a concrete example of what I have been trying to express on the mailing list wrt to the UX that IMO our repo lacks: I can't do that on this repo.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] alamb commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-800171701
BTW when I was writing some doc examples for this functionality, it occured to me that there is another potential way to expose this functionality -- https://github.com/apache/arrow/pull/9722
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r586975774
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -278,12 +347,27 @@ fn sort_boolean(
let valids_len = valids.len();
let nulls_len = nulls.len();
- if !descending {
- valids.sort_by(|a, b| a.1.cmp(&b.1));
- } else {
- valids.sort_by(|a, b| a.1.cmp(&b.1).reverse());
- // reverse to keep a stable ordering
- nulls.reverse();
+ let mut len = values.len();
+ match limit {
+ Some(limit) => {
+ len = limit.min(len);
+ if !descending {
+ valids.partial_sort(len, |a, b| cmp(a.1, b.1));
Review comment:
> Sorry not "fast path" but would the performance be the same as `sort_by`?
I'm afraid not. The sort function in rust is merge sort, it is slightly faster than the heap sort for larger sets.
But it requires twice the memory of the heap sort because of the second array.
```
sort 2^12 time: [799.70 us 806.41 us 814.15 us]
sort 2^12 limit 2^12 time: [1.2848 ms 1.3012 ms 1.3229 ms]
sort nulls 2^12 time: [647.20 us 649.27 us 651.61 us]
sort nulls 2^12 limit 2^12 time: [780.17 us 788.48 us 798.04 us]
```
We can make a new function to reduce the repeated patterns.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li edited a comment on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li edited a comment on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-792216219
> For example, using a priority queue to keep only the top k values in memory.
Yes, but lots of codes may duplicate with sort kernel. partial_sort used priority queue inside. It maybe good to do sorting in pipeline OLAP systems
In ClickHouse, PartialSortingTransform(Each block in each thread) --> MergeSortingTransform(Blocks to one block in each thread) --> MergingSortedTransform(N Block in N Thread to one block) .
```
ββexplainβββββββββββββββββββββββββββββββββ
β (Expression) β
β ExpressionTransform β
β (Limit) β
β Limit β
β (MergingSorted) β
β MergingSortedTransform 16 β 1 β
β (MergeSorting) β
β MergeSortingTransform Γ 16 β
β (PartialSorting) β
β LimitsCheckingTransform Γ 16 β
β PartialSortingTransform Γ 16 β
β (Expression) β
β ExpressionTransform Γ 16 β
β (SettingQuotaAndLimits) β
β (ReadFromStorage) β
β NumbersMt Γ 16 0 β 1 β
ββββββββββββββββββββββββββββββββββββββββββ
```
> I just feel that the sheer amount of uncommented unsafe on it is an indication that there is some opportunity to revisit and further improve the crate, thereby reducing the risks of undefined behavior (UB).
@alamb @jorgecarleitao Thanks for all your reviews. I also have the consideration about unsafe codes in partial_sort may break `arrow`, because it was just created, without any used in production(BTW I am new to rust).
We can keep this MR open currently until you think it's safe enough or must have it.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] github-actions[bot] commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kenerl
Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-787457715
https://issues.apache.org/jira/browse/ARROW-11630
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] alamb commented on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-799412292
Any last thoughts prior to merge? I'll plan to merge this later today unless I hear otherwise
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r585214440
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -36,8 +36,12 @@ use TimeUnit::*;
///
/// Returns an `ArrowError::ComputeError(String)` if the array type is either unsupported by `sort_to_indices` or `take`.
///
-pub fn sort(values: &ArrayRef, options: Option<SortOptions>) -> Result<ArrayRef> {
- let indices = sort_to_indices(values, options)?;
+pub fn sort(
+ values: &ArrayRef,
+ options: Option<SortOptions>,
+ limit: Option<usize>,
Review comment:
Ok, I will introduce `partial_sort` with `limit` parameter.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r586975774
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -278,12 +347,27 @@ fn sort_boolean(
let valids_len = valids.len();
let nulls_len = nulls.len();
- if !descending {
- valids.sort_by(|a, b| a.1.cmp(&b.1));
- } else {
- valids.sort_by(|a, b| a.1.cmp(&b.1).reverse());
- // reverse to keep a stable ordering
- nulls.reverse();
+ let mut len = values.len();
+ match limit {
+ Some(limit) => {
+ len = limit.min(len);
+ if !descending {
+ valids.partial_sort(len, |a, b| cmp(a.1, b.1));
Review comment:
> Sorry not "fast path" but would the performance be the same as `sort_by`?
I'm afraid not. The sort function in rust is merge sort, it is slightly faster than the heap sort for larger sets to sort all elements.
But it requires twice the memory of the heap sort because of the second array.
```
sort 2^12 time: [799.70 us 806.41 us 814.15 us]
sort 2^12 limit 2^12 time: [1.2848 ms 1.3012 ms 1.3229 ms]
sort nulls 2^12 time: [647.20 us 649.27 us 651.61 us]
sort nulls 2^12 limit 2^12 time: [780.17 us 788.48 us 798.04 us]
```
We can make a new function to reduce the repeated patterns or make partial_sort fallback to default sort when limit is the len.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li edited a comment on pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li edited a comment on pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#issuecomment-788550187
I removed `pdqsort`, because the performance didn't show any improvement during the benches in my pc.
Added a separate function `partial_sort `as @jorgecarleitao suggested.
I did not find any other `partial_sort` function in Rust like std::partial_sort in c++. So I created [one](https://github.com/sundy-li/partial_sort).
And the [benche results about partial_sort in arrow](https://github.com/apache/arrow/blob/f19fc1644a07cefcf584d450ad70c5708926c252/rust/arrow/benches/sort_kernel.rs#L76-L106) are:
```
sort 2^12 time: [753.58 us 755.43 us 758.14 us]
sort nulls 2^12 time: [633.41 us 635.28 us 637.51 us]
sort 2^12 limit 10 time: [49.246 us 49.820 us 50.667 us]
sort 2^12 limit 100 time: [115.11 us 116.26 us 117.76 us]
sort 2^12 limit 1000 time: [645.91 us 654.36 us 663.78 us]
sort nulls 2^12 limit 10 time: [66.283 us 66.725 us 67.347 us]
sort nulls 2^12 limit 100 time: [76.281 us 77.907 us 79.783 us]
sort nulls 2^12 limit 1000 time: [258.98 us 260.32 us 262.24 us]
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] sundy-li commented on a change in pull request #9602: ARROW-11630: [Rust] Introduce limit option for sort kernel
Posted by GitBox <gi...@apache.org>.
sundy-li commented on a change in pull request #9602:
URL: https://github.com/apache/arrow/pull/9602#discussion_r586975774
##########
File path: rust/arrow/src/compute/kernels/sort.rs
##########
@@ -278,12 +347,27 @@ fn sort_boolean(
let valids_len = valids.len();
let nulls_len = nulls.len();
- if !descending {
- valids.sort_by(|a, b| a.1.cmp(&b.1));
- } else {
- valids.sort_by(|a, b| a.1.cmp(&b.1).reverse());
- // reverse to keep a stable ordering
- nulls.reverse();
+ let mut len = values.len();
+ match limit {
+ Some(limit) => {
+ len = limit.min(len);
+ if !descending {
+ valids.partial_sort(len, |a, b| cmp(a.1, b.1));
Review comment:
> Sorry not "fast path" but would the performance be the same as `sort_by`?
I'm afraid not. The sort function in rust is merge sort, it is slightly faster than the heap sort for larger sets to sort all elements.
But it requires twice the memory of the heap sort because of the second array.
```
sort 2^12 time: [799.70 us 806.41 us 814.15 us]
sort 2^12 limit 2^12 time: [1.2848 ms 1.3012 ms 1.3229 ms]
sort nulls 2^12 time: [647.20 us 649.27 us 651.61 us]
sort nulls 2^12 limit 2^12 time: [780.17 us 788.48 us 798.04 us]
```
We can make a new function to reduce the repeated patterns.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org