alamb commented on code in PR #5881:
URL: https://github.com/apache/arrow-datafusion/pull/5881#discussion_r1158964935
##########
datafusion/core/benches/sort.rs:
##########
@@ -210,58 +178,61 @@ impl SortBenchCase {
}
}
- /// runs the specified plan to completion, draining all input and
- /// panic'ing on error
- fn run(&self) {
- let plan = Arc::clone(&self.plan);
- let task_ctx = Arc::clone(&self.task_ctx);
+ fn sort_merge(partitions: &[Vec<RecordBatch>]) -> Self {
+ let runtime =
tokio::runtime::Builder::new_multi_thread().build().unwrap();
+ let session_ctx = SessionContext::new();
+ let task_ctx = session_ctx.task_ctx();
- assert_eq!(plan.output_partitioning().partition_count(), 1);
+ let schema = partitions[0][0].schema();
+ let sort = make_sort_exprs(schema.as_ref());
- self.runtime.block_on(async move {
- let mut stream = plan.execute(0, task_ctx).unwrap();
- while let Some(b) = stream.next().await {
- b.expect("unexpected execution error");
- }
- })
+ let exec = MemoryExec::try_new(partitions, schema, None).unwrap();
+ let exec =
+ SortExec::new_with_partitioning(sort.clone(), Arc::new(exec),
true, None);
+ let plan = Arc::new(SortPreservingMergeExec::new(sort,
Arc::new(exec)));
+
+ Self {
+ runtime,
+ task_ctx,
+ plan,
+ }
}
-}
-/// Encapsulates running a test case where input partitioning is not preserved.
-struct SortBenchCasePreservePartitioning {
- runtime: Runtime,
- task_ctx: Arc<TaskContext>,
- // The plan to run
- plan: Arc<dyn ExecutionPlan>,
- partition_count: usize,
-}
+ fn sort(partitions: &[Vec<RecordBatch>]) -> Self {
Review Comment:
```suggestion
/// Test SortExec in "non partitioned" mode which sorts the input streams
/// into a single sorted output stream
fn sort(partitions: &[Vec<RecordBatch>]) -> Self {
```
##########
datafusion/core/benches/sort.rs:
##########
@@ -210,58 +178,61 @@ impl SortBenchCase {
}
}
- /// runs the specified plan to completion, draining all input and
- /// panic'ing on error
- fn run(&self) {
- let plan = Arc::clone(&self.plan);
- let task_ctx = Arc::clone(&self.task_ctx);
+ fn sort_merge(partitions: &[Vec<RecordBatch>]) -> Self {
+ let runtime =
tokio::runtime::Builder::new_multi_thread().build().unwrap();
+ let session_ctx = SessionContext::new();
+ let task_ctx = session_ctx.task_ctx();
- assert_eq!(plan.output_partitioning().partition_count(), 1);
+ let schema = partitions[0][0].schema();
+ let sort = make_sort_exprs(schema.as_ref());
- self.runtime.block_on(async move {
- let mut stream = plan.execute(0, task_ctx).unwrap();
- while let Some(b) = stream.next().await {
- b.expect("unexpected execution error");
- }
- })
+ let exec = MemoryExec::try_new(partitions, schema, None).unwrap();
+ let exec =
+ SortExec::new_with_partitioning(sort.clone(), Arc::new(exec),
true, None);
+ let plan = Arc::new(SortPreservingMergeExec::new(sort,
Arc::new(exec)));
Review Comment:
I would expect this to behave the same, performance wise, as `sort` -- is
that your expectation too?
##########
datafusion/core/benches/sort.rs:
##########
@@ -210,58 +178,61 @@ impl SortBenchCase {
}
}
- /// runs the specified plan to completion, draining all input and
- /// panic'ing on error
- fn run(&self) {
- let plan = Arc::clone(&self.plan);
- let task_ctx = Arc::clone(&self.task_ctx);
+ fn sort_merge(partitions: &[Vec<RecordBatch>]) -> Self {
+ let runtime =
tokio::runtime::Builder::new_multi_thread().build().unwrap();
+ let session_ctx = SessionContext::new();
+ let task_ctx = session_ctx.task_ctx();
- assert_eq!(plan.output_partitioning().partition_count(), 1);
+ let schema = partitions[0][0].schema();
+ let sort = make_sort_exprs(schema.as_ref());
- self.runtime.block_on(async move {
- let mut stream = plan.execute(0, task_ctx).unwrap();
- while let Some(b) = stream.next().await {
- b.expect("unexpected execution error");
- }
- })
+ let exec = MemoryExec::try_new(partitions, schema, None).unwrap();
+ let exec =
+ SortExec::new_with_partitioning(sort.clone(), Arc::new(exec),
true, None);
+ let plan = Arc::new(SortPreservingMergeExec::new(sort,
Arc::new(exec)));
+
+ Self {
+ runtime,
+ task_ctx,
+ plan,
+ }
}
-}
-/// Encapsulates running a test case where input partitioning is not preserved.
-struct SortBenchCasePreservePartitioning {
- runtime: Runtime,
- task_ctx: Arc<TaskContext>,
- // The plan to run
- plan: Arc<dyn ExecutionPlan>,
- partition_count: usize,
-}
+ fn sort(partitions: &[Vec<RecordBatch>]) -> Self {
+ let runtime =
tokio::runtime::Builder::new_multi_thread().build().unwrap();
+ let session_ctx = SessionContext::new();
+ let task_ctx = session_ctx.task_ctx();
-impl SortBenchCasePreservePartitioning {
- /// Prepare to run a benchmark that merges the specified
- /// partitions (streams) together using all keyes
- fn new(partitions: &[Vec<RecordBatch>]) -> Self {
- let partition_count = partitions.len();
+ let schema = partitions[0][0].schema();
+ let sort = make_sort_exprs(schema.as_ref());
+
+ let exec = MemoryExec::try_new(partitions, schema, None).unwrap();
+ let exec = Arc::new(CoalescePartitionsExec::new(Arc::new(exec)));
+ let plan = Arc::new(SortExec::try_new(sort, exec, None).unwrap());
+
+ Self {
+ runtime,
+ task_ctx,
+ plan,
+ }
+ }
+
+ fn sort_partitioned(partitions: &[Vec<RecordBatch>]) -> Self {
Review Comment:
```suggestion
/// Test SortExec in "partitioned" mode which sorts the input streams
/// individually into some number of output streams
fn sort_partitioned(partitions: &[Vec<RecordBatch>]) -> Self {
```
##########
datafusion/core/benches/sort.rs:
##########
@@ -41,167 +94,82 @@ use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use tokio::runtime::Runtime;
-use lazy_static::lazy_static;
+use datafusion::physical_plan::coalesce_partitions::CoalescePartitionsExec;
/// Total number of streams to divide each input into
/// models 8 partition plan (should it be 16??)
-const NUM_STREAMS: u64 = 8;
+const NUM_STREAMS: usize = 8;
+
+/// The size of each batch within each stream
+const BATCH_SIZE: usize = 1024;
/// Total number of input rows to generate
const INPUT_SIZE: u64 = 100000;
-// cases:
-// * physical sort expr (X, Y Z, NULLS FIRST, ASC) (not parameterized)
-//
-// streams of distinct values
-// streams with 10% duplicated values (within each stream, and across streams)
-// These cases are intended to model important usecases in TPCH
-// parameters:
-//
-// Input schemas
-lazy_static! {
- static ref I64_STREAMS: Vec<Vec<RecordBatch>> = i64_streams();
- static ref F64_STREAMS: Vec<Vec<RecordBatch>> = f64_streams();
-
- static ref UTF8_LOW_CARDINALITY_STREAMS: Vec<Vec<RecordBatch>> =
utf8_low_cardinality_streams();
- static ref UTF8_HIGH_CARDINALITY_STREAMS: Vec<Vec<RecordBatch>> =
utf8_high_cardinality_streams();
-
- static ref DICTIONARY_STREAMS: Vec<Vec<RecordBatch>> =
dictionary_streams();
- static ref DICTIONARY_TUPLE_STREAMS: Vec<Vec<RecordBatch>> =
dictionary_tuple_streams();
- static ref MIXED_DICTIONARY_TUPLE_STREAMS: Vec<Vec<RecordBatch>> =
mixed_dictionary_tuple_streams();
- // * (string(low), string(low), string(high)) -- tpch q1 + iox
- static ref UTF8_TUPLE_STREAMS: Vec<Vec<RecordBatch>> =
utf8_tuple_streams();
- // * (f64, string, string, int) -- tpch q2
- static ref MIXED_TUPLE_STREAMS: Vec<Vec<RecordBatch>> =
mixed_tuple_streams();
-
-}
+type PartitionedBatches = Vec<Vec<RecordBatch>>;
fn criterion_benchmark(c: &mut Criterion) {
- c.bench_function("sort i64", |b| {
- let case = SortBenchCase::new(&I64_STREAMS);
-
- b.iter(move || case.run())
- });
- c.bench_function("sort i64 preserve partitioning", |b| {
- let case = SortBenchCasePreservePartitioning::new(&I64_STREAMS);
-
- b.iter(move || case.run())
- });
-
- c.bench_function("sort f64", |b| {
- let case = SortBenchCase::new(&F64_STREAMS);
-
- b.iter(move || case.run())
- });
- c.bench_function("sort f64 preserve partitioning", |b| {
- let case = SortBenchCasePreservePartitioning::new(&F64_STREAMS);
-
- b.iter(move || case.run())
- });
-
- c.bench_function("sort utf8 low cardinality", |b| {
- let case = SortBenchCase::new(&UTF8_LOW_CARDINALITY_STREAMS);
-
- b.iter(move || case.run())
- });
- c.bench_function("sort utf8 low cardinality preserve partitioning", |b| {
- let case =
SortBenchCasePreservePartitioning::new(&UTF8_LOW_CARDINALITY_STREAMS);
-
- b.iter(move || case.run())
- });
-
- c.bench_function("sort utf8 high cardinality", |b| {
- let case = SortBenchCase::new(&UTF8_HIGH_CARDINALITY_STREAMS);
-
- b.iter(move || case.run())
- });
- c.bench_function("sort utf8 high cardinality preserve partitioning", |b| {
- let case =
SortBenchCasePreservePartitioning::new(&UTF8_HIGH_CARDINALITY_STREAMS);
-
- b.iter(move || case.run())
- });
-
- c.bench_function("sort utf8 tuple", |b| {
- let case = SortBenchCase::new(&UTF8_TUPLE_STREAMS);
-
- b.iter(move || case.run())
- });
- c.bench_function("sort utf8 tuple preserve partitioning", |b| {
- let case = SortBenchCasePreservePartitioning::new(&UTF8_TUPLE_STREAMS);
-
- b.iter(move || case.run())
- });
-
- c.bench_function("sort utf8 dictionary", |b| {
- let case = SortBenchCase::new(&DICTIONARY_STREAMS);
-
- b.iter(move || case.run())
- });
- c.bench_function("sort utf8 dictionary preserve partitioning", |b| {
- let case = SortBenchCasePreservePartitioning::new(&DICTIONARY_STREAMS);
-
- b.iter(move || case.run())
- });
-
- c.bench_function("sort utf8 dictionary tuple", |b| {
- let case = SortBenchCase::new(&DICTIONARY_TUPLE_STREAMS);
- b.iter(move || case.run())
- });
- c.bench_function("sort utf8 dictionary tuple preserve partitioning", |b| {
- let case =
SortBenchCasePreservePartitioning::new(&DICTIONARY_TUPLE_STREAMS);
- b.iter(move || case.run())
- });
-
- c.bench_function("sort mixed utf8 dictionary tuple", |b| {
- let case = SortBenchCase::new(&MIXED_DICTIONARY_TUPLE_STREAMS);
- b.iter(move || case.run())
- });
-
- c.bench_function(
- "sort mixed utf8 dictionary tuple preserve partitioning",
- |b| {
- let case =
-
SortBenchCasePreservePartitioning::new(&MIXED_DICTIONARY_TUPLE_STREAMS);
+ let cases: Vec<(&str, &dyn Fn(bool) -> PartitionedBatches)> = vec![
+ ("i64", &i64_streams),
+ ("f64", &f64_streams),
+ ("utf8 low cardinality", &utf8_low_cardinality_streams),
+ ("utf8 high cardinality", &utf8_high_cardinality_streams),
+ ("utf8 tuple", &utf8_tuple_streams),
+ ("utf8 dictionary", &dictionary_streams),
+ ("utf8 dictionary tuple", &dictionary_tuple_streams),
+ ("mixed dictionary tuple", &mixed_dictionary_tuple_streams),
+ ("mixed tuple", &mixed_tuple_streams),
+ ];
+
+ for (name, f) in cases {
+ c.bench_function(&format!("merge sorted {name}"), |b| {
+ let data = f(true);
+ let case = BenchCase::merge_sorted(&data);
b.iter(move || case.run())
- },
- );
+ });
- c.bench_function("sort mixed tuple", |b| {
- let case = SortBenchCase::new(&MIXED_TUPLE_STREAMS);
+ c.bench_function(&format!("sort merge {name}"), |b| {
+ let data = f(false);
+ let case = BenchCase::sort_merge(&data);
+ b.iter(move || case.run())
+ });
- b.iter(move || case.run())
- });
- c.bench_function("sort mixed tuple preserve partitioning", |b| {
- let case =
SortBenchCasePreservePartitioning::new(&MIXED_TUPLE_STREAMS);
+ c.bench_function(&format!("sort {name}"), |b| {
+ let data = f(false);
+ let case = BenchCase::sort(&data);
+ b.iter(move || case.run())
+ });
- b.iter(move || case.run())
- });
+ c.bench_function(&format!("sort partitioned {name}"), |b| {
+ let data = f(false);
+ let case = BenchCase::sort_partitioned(&data);
+ b.iter(move || case.run())
+ });
+ }
}
-/// Encapsulates running a test case where input partitioning is not preserved.
-struct SortBenchCase {
+/// Encapsulates running each test case
+struct BenchCase {
runtime: Runtime,
task_ctx: Arc<TaskContext>,
// The plan to run
plan: Arc<dyn ExecutionPlan>,
}
-impl SortBenchCase {
+impl BenchCase {
/// Prepare to run a benchmark that merges the specified
/// partitions (streams) together using all keyes
Review Comment:
```suggestion
/// pre-sorted partitions (streams) together using all keys
```
##########
datafusion/core/benches/sort.rs:
##########
@@ -210,58 +178,61 @@ impl SortBenchCase {
}
}
- /// runs the specified plan to completion, draining all input and
- /// panic'ing on error
- fn run(&self) {
- let plan = Arc::clone(&self.plan);
- let task_ctx = Arc::clone(&self.task_ctx);
+ fn sort_merge(partitions: &[Vec<RecordBatch>]) -> Self {
Review Comment:
```suggestion
/// Test SortExec in "partitioned" mode followed by a
SortPreservingMerge
fn sort_merge(partitions: &[Vec<RecordBatch>]) -> Self {
```
--
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]