Copilot commented on code in PR #7530:
URL: https://github.com/apache/arrow-rs/pull/7530#discussion_r2097318426


##########
arrow-ord/src/sort.rs:
##########
@@ -781,6 +813,50 @@ impl LexicographicalComparator {
     }
 }
 
+/// A lexicographical comparator that wraps given array data (columns) and can 
lexicographically compare data
+/// at given two indices. This version of the comparator is for compile-time 
constant number of columns.
+/// The lifetime is the same at the data wrapped.
+pub struct FixedLexicographicalComparator<const N: usize> {
+    compare_items: [DynComparator; N],
+}
+
+impl<const N: usize> FixedLexicographicalComparator<N> {
+    /// lexicographically compare values at the wrapped columns with given 
indices.
+    pub fn compare(&self, a_idx: usize, b_idx: usize) -> Ordering {
+        for comparator in &self.compare_items {
+            match comparator(a_idx, b_idx) {
+                Ordering::Equal => continue,
+                r => return r,
+            }
+        }
+        Ordering::Equal
+    }
+
+    /// Create a new lex comparator that will wrap the given sort columns and 
give comparison
+    /// results with two indices.
+    /// The number of columns should be equal to the compile-time constant N.
+    pub fn try_new(

Review Comment:
   Consider adding an explicit check that 'columns.len() == N' before mapping 
to comparators to provide a clearer and more detailed error message. This can 
ensure that users get a specific error if the number of sort columns does not 
match the fixed-size array expectation.



##########
arrow-ord/src/sort.rs:
##########
@@ -724,15 +725,46 @@ pub fn lexsort_to_indices(
         len = limit.min(len);
     }
 
-    let lexicographical_comparator = 
LexicographicalComparator::try_new(columns)?;
-    // uint32 can be sorted unstably
-    sort_unstable_by(&mut value_indices, len, |a, b| {
+    match columns.len() {
+        2 => {
+            sort_fixed_column::<2>(columns, &mut value_indices, len)?;
+        }
+        3 => {
+            sort_fixed_column::<3>(columns, &mut value_indices, len)?;
+        }
+        4 => {
+            sort_fixed_column::<4>(columns, &mut value_indices, len)?;
+        }
+        5 => {
+            sort_fixed_column::<5>(columns, &mut value_indices, len)?;
+        }
+        _ => {

Review Comment:
   [nitpick] Consider adding a comment explaining why fixed-column sorting is 
only implemented for 2 to 5 columns and what the intended behavior is when more 
or fewer columns are provided. This will help future maintainers understand the 
design decision.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to