This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 6ea6c12189 MINOR: add complete lexsort_to_indices usage example
(#11133)
6ea6c12189 is described below
commit 6ea6c12189ed8e5e632fd3280d42f40ac4a7cede
Author: Wilhelm Ă…gren <[email protected]>
AuthorDate: Tue Sep 22 15:37:28 2026 +0200
MINOR: add complete lexsort_to_indices usage example (#11133)
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
-->
No open issue.
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
I had issues finding complete examples of sorting a record batch with
multiple columns, and others might encounter the same issue.
It was not clear to me how to lexicographically sort a record batch
using `lexsort_to_indices`. The docs mention converting to row form as a
potentially faster alternative for sorting over many columns, but the
current docs for `lexsort_to_indices` does not show how the row indices
in the returned `UInt32Array` might be used.
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
Adds an example in the docs for how to use `lexsort_to_indices` together
with `take_record_batch` to sort a `RecordBatch` by multiple cols.
# Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
If this PR claims a performance improvement, please include evidence
such as benchmark results.
-->
Yes, `cargo test -p arrow-ord --doc`
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
If there are any breaking changes to public APIs, please call them out.
-->
Docs only.
---------
Co-authored-by: Andrew Lamb <[email protected]>
---
arrow-ord/src/sort.rs | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/arrow-ord/src/sort.rs b/arrow-ord/src/sort.rs
index c2629245bf..f56c972ae0 100644
--- a/arrow-ord/src/sort.rs
+++ b/arrow-ord/src/sort.rs
@@ -934,6 +934,42 @@ pub fn lexsort(columns: &[SortColumn], limit:
Option<usize>) -> Result<Vec<Array
/// Sort elements lexicographically from a list of `ArrayRef` into an unsigned
integer
/// (`UInt32Array`) of indices.
///
+/// # Example
+///
+/// ```
+/// # use std::sync::Arc;
+/// # use arrow_array::{ArrayRef, UInt32Array, Int32Array, RecordBatch,
StringArray};
+/// # use arrow_ord::sort::{lexsort_to_indices, SortColumn};
+/// # use arrow_select::take::take_record_batch;
+/// // Two columns (a, b). Values (2,x), (1, z), (1(a))
+/// let batch = RecordBatch::try_from_iter(vec![
+/// ("a", Arc::new(Int32Array::from(vec![2, 1, 1])) as ArrayRef),
+/// ("b", Arc::new(StringArray::from(vec!["x", "z", "a"])) as ArrayRef),
+/// ])
+/// .unwrap();
+///
+/// // Configure sort by (a, b)
+/// let sort_columns = vec![
+/// SortColumn {
+/// values: batch.column(0).clone(),
+/// options: None,
+/// },
+/// SortColumn {
+/// values: batch.column(1).clone(),
+/// options: None,
+/// },
+/// ];
+///
+/// // indices of the rows of (a,b), in lexicographic order
+/// let indices = lexsort_to_indices(&sort_columns, None).unwrap();
+/// assert_eq!(&indices, &UInt32Array::from(vec![2, 1, 0]));
+/// // Create new sorted RecordBatch by copying values at indices
+/// let sorted = take_record_batch(&batch, &indices).unwrap();
+///
+/// assert_eq!(sorted.column(0).as_ref(), &Int32Array::from(vec![1, 1, 2]));
+/// assert_eq!(sorted.column(1).as_ref(), &StringArray::from(vec!["a", "z",
"x"]));
+/// ```
+///
/// Note: for multi-column sorts without a limit, using the [row
format](https://docs.rs/arrow-row/latest/arrow_row/)
/// may be significantly faster
pub fn lexsort_to_indices(