Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions datafusion/physical-plan/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ name = "partial_ordering"
[[bench]]
harness = false
name = "spill_io"

[[bench]]
harness = false
name = "sort_merge_join"
117 changes: 117 additions & 0 deletions datafusion/physical-plan/benches/sort_merge_join.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// 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_schema::SortOptions;
use criterion::{criterion_group, criterion_main, Criterion};
use datafusion_common::JoinType::Inner;
use datafusion_execution::config::SessionConfig;
use datafusion_execution::disk_manager::DiskManagerConfig;
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
use datafusion_execution::TaskContext;
use datafusion_physical_expr::expressions::Column;
use datafusion_physical_plan::common::collect;
use datafusion_physical_plan::joins::SortMergeJoinExec;
use datafusion_physical_plan::test::{build_table_i32, TestMemoryExec};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry that using test only structures like this will means the benchmark is not measuring performance that will map directly to query performance. I think you could move the file from

  • datafusion/physical-plan/benches/sort_merge_join.rs

to

  • datafusion/core/benches/sort_merge_join.rs

And use a SessionContext and actual query to run to be closer.

Here is an example that does something similar: https://github.com/apache/datafusion/blob/main/datafusion/core/benches/filter_query_sql.rs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I moved file to /core/benches/
  2. try to using actual query by SessionContext, but it is hard to simulate.
    Because It satisfied SMJExec+spill execution, but securing enough memory for operations like RepartitionExec was challenging.

use datafusion_physical_plan::ExecutionPlan;
use std::sync::Arc;
use tokio::runtime::Runtime;

fn create_test_data() -> SortMergeJoinExec {
let left_batch = build_table_i32(
("a1", &vec![0, 1, 2, 3, 4, 5]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a bencmark that has only 5 rows is likely going to only measure the overhead of plan setup rather than the actual performance of a large join that needs to spill

Perhaps we can increase this size to 1M rows or something (is important that b1 and b2 remain sorted)

Copy link
Contributor Author

@getChan getChan May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for review!
now I run benchmark with 1_048_576 rows. with all row spill.
but benchmark result is little performance improvement...

SortMergeJoinExec_spill time:   [79.761 s 79.858 s 79.974 s]
                        change: [-0.7912% -0.5805% -0.3386%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 1 outliers among 10 measurements (10.00%)
  1 (10.00%) high mild

It seems that skip validation is small impact on the overall execution.

("b1", &vec![1, 2, 3, 4, 5, 6]),
("c1", &vec![4, 5, 6, 7, 8, 9]),
);
let left_schema = left_batch.schema();
let left =
TestMemoryExec::try_new_exec(&[vec![left_batch]], left_schema, None).unwrap();
let right_batch = build_table_i32(
("a2", &vec![0, 10, 20, 30, 40]),
("b2", &vec![1, 3, 4, 6, 8]),
("c2", &vec![50, 60, 70, 80, 90]),
);
let right_schema = right_batch.schema();
let right =
TestMemoryExec::try_new_exec(&[vec![right_batch]], right_schema, None).unwrap();
let on = vec![(
Arc::new(Column::new_with_schema("b1", &left.schema()).unwrap()) as _,
Arc::new(Column::new_with_schema("b2", &right.schema()).unwrap()) as _,
)];
let sort_options = vec![SortOptions::default(); on.len()];

SortMergeJoinExec::try_new(left, right, on, None, Inner, sort_options, false).unwrap()
}

// `cargo bench --bench sort_merge_join`
fn bench_spill(c: &mut Criterion) {
let sort_merge_join_exec = create_test_data();

let mut group = c.benchmark_group("sort_merge_join_spill");
let rt = Runtime::new().unwrap();

let runtime = RuntimeEnvBuilder::new()
.with_memory_limit(100, 1.0) // Set memory limit to 100 bytes
.with_disk_manager(DiskManagerConfig::NewOs) // Enable DiskManager to allow spilling
.build_arc()
.unwrap();
let session_config = SessionConfig::default();
let task_ctx = Arc::new(
TaskContext::default()
.with_session_config(session_config.clone())
.with_runtime(Arc::clone(&runtime)),
);

group.bench_function("SortMergeJoinExec_spill", |b| {
b.iter(|| {
criterion::black_box(rt.block_on(async {
let stream = sort_merge_join_exec
.execute(0, Arc::clone(&task_ctx))
.unwrap();
collect(stream).await.unwrap()
}))
})
});
group.finish();

assert!(
sort_merge_join_exec
.metrics()
.unwrap()
.spill_count()
.unwrap()
> 0
);
assert!(
sort_merge_join_exec
.metrics()
.unwrap()
.spilled_bytes()
.unwrap()
> 0
);
assert!(
sort_merge_join_exec
.metrics()
.unwrap()
.spilled_rows()
.unwrap()
> 0
);
}

criterion_group!(benches, bench_spill);
criterion_main!(benches);
2 changes: 1 addition & 1 deletion datafusion/physical-plan/src/joins/sort_merge_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2313,7 +2313,7 @@ fn fetch_right_columns_from_batch_by_idxs(
Vec::with_capacity(buffered_indices.len());

let file = BufReader::new(File::open(spill_file.path())?);
let reader = StreamReader::try_new(file, None)?;
let reader = unsafe {StreamReader::try_new(file, None)?.with_skip_validation(true)};

for batch in reader {
batch?.columns().iter().for_each(|column| {
Expand Down
1 change: 0 additions & 1 deletion datafusion/physical-plan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,4 @@ pub mod udaf {
}

pub mod coalesce;
#[cfg(test)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use test utilities in the bench as well. Is it okay?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could avoid doing this by using the actual operators -- see comment above

pub mod test;
Loading