alamb commented on a change in pull request #691: URL: https://github.com/apache/arrow-datafusion/pull/691#discussion_r664755107
########## File path: datafusion/benches/physical_plan.rs ########## @@ -0,0 +1,176 @@ +// 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. + +#[macro_use] +extern crate criterion; +use criterion::{BatchSize, Criterion}; +extern crate arrow; +extern crate datafusion; + +use std::{iter::FromIterator, sync::Arc}; + +use arrow::{ + array::{ArrayRef, Int64Array, StringArray}, + record_batch::RecordBatch, +}; +use tokio::runtime::Runtime; + +use datafusion::physical_plan::{ + collect, + expressions::{col, PhysicalSortExpr}, + memory::MemoryExec, + sort_preserving_merge::SortPreservingMergeExec, +}; + +// Initialise the operator using the provided record batches and the sort key +// as inputs. All record batches must have the same schema. +fn sort_preserving_merge_operator(batches: Vec<RecordBatch>, sort: &[&str]) { + let schema = batches[0].schema(); + + let sort = sort + .iter() + .map(|name| PhysicalSortExpr { + expr: col(name, &schema).unwrap(), + options: Default::default(), + }) + .collect::<Vec<_>>(); + + let exec = MemoryExec::try_new( + &batches.into_iter().map(|rb| vec![rb]).collect::<Vec<_>>(), + schema, + None, + ) + .unwrap(); + let merge = Arc::new(SortPreservingMergeExec::new(sort, Arc::new(exec), 10000)); + + let rt = Runtime::new().unwrap(); + rt.block_on(collect(merge)).unwrap(); +} + +// Produces `n` record batches of row size `m`. Each record batch will have +// identical contents except for if the `batch_offset` is set. In that case the +// values for column "d" in each subsequent record batch will be offset in +// value. +// +// The `rows_per_key` value controls how many rows are generated per "key", +// which is defined as columns a, b and c. +fn batches( + n: usize, + m: usize, + rows_per_sort_key: usize, + batch_offset: usize, +) -> Vec<RecordBatch> { + let mut rbs = Vec::with_capacity(n); + let mut curr_batch_offset = 0; + + for _ in 0..n { + let mut col_a = Vec::with_capacity(m); + let mut col_b = Vec::with_capacity(m); + let mut col_c = Vec::with_capacity(m); + let mut col_d = Vec::with_capacity(m); + + let mut j = 0; + let mut current_rows_per_sort_key = 0; + + for i in 0..m { + if current_rows_per_sort_key == rows_per_sort_key { + current_rows_per_sort_key = 0; + j = i; + } + + col_a.push(Some(format!("a-{:?}", j))); + col_b.push(Some(format!("b-{:?}", j))); + col_c.push(Some(format!("c-{:?}", j))); + col_d.push(Some((i + curr_batch_offset) as i64)); + + current_rows_per_sort_key += 1; + } + + col_a.sort(); Review comment: This data is only sorted on `a` correct? In other words, the `SortPreservingMerge` operator will only see sorted input data if the sort key is `["a"]`, but the benchmark is using `["a", "b", "c", "d"]` as the sort key. Or perhaps I am mis reading 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
