alamb commented on code in PR #2929:
URL: https://github.com/apache/arrow-rs/pull/2929#discussion_r1005540154


##########
arrow/src/row/mod.rs:
##########
@@ -73,6 +73,24 @@
 //! assert_eq!(&c2_values, &["a", "f", "c", "e"]);
 //! ```
 //!
+//! It can also be used to implement a fast lexicographic sort

Review Comment:
   ```suggestion
   //! It can also be used to implement a fast multi-column / lexicographic sort
   ```



##########
arrow/benches/lexsort.rs:
##########
@@ -0,0 +1,171 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::compute::{lexsort_to_indices, SortColumn};
+use arrow::row::{RowConverter, SortField};
+use arrow::util::bench_util::{
+    create_dict_from_values, create_primitive_array, 
create_string_array_with_len,
+};
+use arrow_array::types::Int32Type;
+use arrow_array::{Array, ArrayRef, UInt32Array};
+use criterion::{criterion_group, criterion_main, Criterion};
+use std::sync::Arc;
+
+#[derive(Copy, Clone)]
+enum Column {
+    RequiredI32,
+    OptionalI32,
+    Required16CharString,
+    Optional16CharString,
+    Optional50CharString,
+    Optional100Value50CharStringDict,
+}
+
+impl std::fmt::Debug for Column {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        let s = match self {
+            Column::RequiredI32 => "i32",
+            Column::OptionalI32 => "i32_opt",
+            Column::Required16CharString => "str(16)",
+            Column::Optional16CharString => "str_opt(16)",
+            Column::Optional50CharString => "str_opt(50)",
+            Column::Optional100Value50CharStringDict => 
"dict(100,str_opt(50))",
+        };
+        f.write_str(s)
+    }
+}
+
+impl Column {
+    fn generate(self, size: usize) -> ArrayRef {
+        match self {
+            Column::RequiredI32 => {
+                Arc::new(create_primitive_array::<Int32Type>(size, 0.))
+            }
+            Column::OptionalI32 => {
+                Arc::new(create_primitive_array::<Int32Type>(size, 0.2))
+            }
+            Column::Required16CharString => {
+                Arc::new(create_string_array_with_len::<i32>(size, 0., 16))
+            }
+            Column::Optional16CharString => {
+                Arc::new(create_string_array_with_len::<i32>(size, 0.2, 16))
+            }
+            Column::Optional50CharString => {
+                Arc::new(create_string_array_with_len::<i32>(size, 0., 50))
+            }
+            Column::Optional100Value50CharStringDict => {
+                Arc::new(create_dict_from_values::<Int32Type>(
+                    size,
+                    0.1,
+                    &create_string_array_with_len::<i32>(100, 0., 50),
+                ))
+            }
+        }
+    }
+}
+
+fn do_bench(c: &mut Criterion, columns: &[Column], len: usize) {
+    let arrays: Vec<_> = columns.iter().map(|x| x.generate(len)).collect();
+    let sort_columns: Vec<_> = arrays
+        .iter()
+        .cloned()
+        .map(|values| SortColumn {
+            values,
+            options: None,
+        })
+        .collect();
+
+    c.bench_function(
+        &format!("lexsort_to_indices({:?}): {}", columns, len),
+        |b| {
+            b.iter(|| {
+                criterion::black_box(lexsort_to_indices(&sort_columns, 
None).unwrap())
+            })
+        },
+    );
+
+    c.bench_function(&format!("lexsort_rows({:?}): {}", columns, len), |b| {

Review Comment:
   ```suggestion
       c.bench_function(&format!("RowFormat: lexsort_rows({:?}): {}", columns, 
len), |b| {
   ```



##########
arrow/benches/lexsort.rs:
##########
@@ -0,0 +1,166 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::compute::{lexsort_to_indices, SortColumn};
+use arrow::row::{RowConverter, SortField};
+use arrow::util::bench_util::{
+    create_dict_from_values, create_primitive_array, 
create_string_array_with_len,
+};
+use arrow_array::types::Int32Type;
+use arrow_array::{Array, ArrayRef, UInt32Array};
+use criterion::{criterion_group, criterion_main, Criterion};
+use std::sync::Arc;
+
+#[derive(Copy, Clone)]
+enum Column {
+    RequiredI32,
+    OptionalI32,
+    Required16CharString,
+    Optional16CharString,
+    Optional50CharString,
+    Optional100Value50CharStringDict,
+}
+
+impl std::fmt::Debug for Column {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        let s = match self {
+            Column::RequiredI32 => "i32",
+            Column::OptionalI32 => "i32_opt",
+            Column::Required16CharString => "str(16)",
+            Column::Optional16CharString => "str_opt(16)",
+            Column::Optional50CharString => "str_opt(50)",
+            Column::Optional100Value50CharStringDict => 
"dict(100,str_opt(50))",
+        };
+        f.write_str(s)
+    }
+}
+
+impl Column {
+    fn generate(self, size: usize) -> ArrayRef {
+        match self {
+            Column::RequiredI32 => {
+                Arc::new(create_primitive_array::<Int32Type>(size, 0.))
+            }
+            Column::OptionalI32 => {
+                Arc::new(create_primitive_array::<Int32Type>(size, 0.2))
+            }
+            Column::Required16CharString => {
+                Arc::new(create_string_array_with_len::<i32>(size, 0., 16))
+            }
+            Column::Optional16CharString => {
+                Arc::new(create_string_array_with_len::<i32>(size, 0.2, 16))
+            }
+            Column::Optional50CharString => {
+                Arc::new(create_string_array_with_len::<i32>(size, 0., 50))
+            }
+            Column::Optional100Value50CharStringDict => {
+                Arc::new(create_dict_from_values::<Int32Type>(
+                    size,
+                    0.1,
+                    &create_string_array_with_len::<i32>(100, 0., 50),
+                ))
+            }
+        }
+    }
+}
+
+fn do_bench(c: &mut Criterion, columns: &[Column], len: usize) {
+    let arrays: Vec<_> = columns.iter().map(|x| x.generate(len)).collect();
+    let sort_columns: Vec<_> = arrays
+        .iter()
+        .cloned()
+        .map(|values| SortColumn {
+            values,
+            options: None,
+        })
+        .collect();
+
+    c.bench_function(&format!("lexsort_to_indices({:?}): {}", columns, len), 
|b| {
+        b.iter(|| criterion::black_box(lexsort_to_indices(&sort_columns, 
None).unwrap()))
+    });
+
+    c.bench_function(&format!("lexsort_rows({:?}): {}", columns, len), |b| {
+        b.iter(|| {
+            criterion::black_box({
+                let fields = arrays
+                    .iter()
+                    .map(|a| SortField::new(a.data_type().clone()))
+                    .collect();
+                let mut converter = RowConverter::new(fields);
+                let rows = converter.convert_columns(&arrays).unwrap();
+                let mut sort: Vec<_> = rows.iter().enumerate().collect();
+                sort.sort_unstable_by(|(_, a), (_, b)| a.cmp(b));

Review Comment:
   I thought we changed lexsort_to_indices to be unstable in the name of 
performance. As in it shouldn't be claiming to be stable



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