Dandandan commented on code in PR #2929: URL: https://github.com/apache/arrow-rs/pull/2929#discussion_r1005558281
########## 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 am not sure if the lexsort to indices is correctly doing stable sort. However it's possible to use unstable sort for stable sorting as long as you also sort the indexes: https://rust-lang.github.io/rfcs/1884-unstable-sort.html > Q: Can stable sort be performed using unstable sort? A: Yes. If we transform [T] into [(T, usize)] by pairing every element with its index, then perform unstable sort, and finally remove indices, the result will be equivalent to stable sort. -- 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]
