-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Benchmark: Add micro-benchmark for Nested Loop Join operator #16819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
90c96f4
Micro-benchmark for NLJ
2010YOUY01 305826c
clippy
2010YOUY01 a3f5d05
review
2010YOUY01 25ffcf3
small clean-up
2010YOUY01 5690078
Update benchmarks/src/nlj.rs
2010YOUY01 fe02cc3
add doc to ./benchmark/README.md
2010YOUY01 97415fb
don't buffer result in benchmark runner
2010YOUY01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
// 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 crate::util::{BenchmarkRun, CommonOpt, QueryResult}; | ||
use datafusion::{error::Result, prelude::SessionContext}; | ||
use datafusion_common::instant::Instant; | ||
use datafusion_common::{exec_datafusion_err, exec_err, DataFusionError}; | ||
use structopt::StructOpt; | ||
|
||
/// Run the Nested Loop Join (NLJ) benchmark | ||
/// | ||
/// This micro-benchmark focuses on the performance characteristics of NLJs. | ||
/// | ||
/// It always tries to use fast scanners (without decoding overhead) and | ||
/// efficient predicate expressions to ensure it can reflect the performance | ||
/// of the NLJ operator itself. | ||
/// | ||
/// In this micro-benchmark, the following workload characteristics will be | ||
/// varied: | ||
/// - Join type: Inner/Left/Right/Full (all for the NestedLoopJoin physical | ||
/// operator) | ||
/// TODO: Include special join types (Semi/Anti/Mark joins) | ||
/// - Input size: Different combinations of left (build) side and right (probe) | ||
/// side sizes | ||
/// - Selectivity of join filters | ||
#[derive(Debug, StructOpt, Clone)] | ||
#[structopt(verbatim_doc_comment)] | ||
pub struct RunOpt { | ||
/// Query number (between 1 and 10). If not specified, runs all queries | ||
#[structopt(short, long)] | ||
query: Option<usize>, | ||
|
||
/// Common options | ||
#[structopt(flatten)] | ||
common: CommonOpt, | ||
|
||
/// If present, write results json here | ||
#[structopt(parse(from_os_str), short = "o", long = "output")] | ||
output_path: Option<std::path::PathBuf>, | ||
} | ||
|
||
/// Inline SQL queries for NLJ benchmarks | ||
/// | ||
/// Each query's comment includes: | ||
/// - Left (build) side row count × Right (probe) side row count | ||
/// - Join predicate selectivity (1% means the output size is 1% * input size) | ||
const NLJ_QUERIES: &[&str] = &[ | ||
// Q1: INNER 10K x 10K | LOW 0.1% | ||
r#" | ||
SELECT * | ||
FROM range(10000) AS t1 | ||
JOIN range(10000) AS t2 | ||
ON (t1.value + t2.value) % 1000 = 0; | ||
"#, | ||
// Q2: INNER 10K x 10K | Medium 20% | ||
r#" | ||
SELECT * | ||
FROM range(10000) AS t1 | ||
JOIN range(10000) AS t2 | ||
ON (t1.value + t2.value) % 5 = 0; | ||
"#, | ||
// Q3: INNER 10K x 10K | High 90% | ||
r#" | ||
SELECT * | ||
FROM range(10000) AS t1 | ||
JOIN range(10000) AS t2 | ||
ON (t1.value + t2.value) % 10 <> 0; | ||
"#, | ||
// Q4: INNER 30K x 30K | Medium 20% | ||
r#" | ||
SELECT * | ||
FROM range(30000) AS t1 | ||
JOIN range(30000) AS t2 | ||
ON (t1.value + t2.value) % 5 = 0; | ||
"#, | ||
// Q5: INNER 10K x 200K | LOW 0.1% (small to large) | ||
r#" | ||
SELECT * | ||
FROM range(10000) AS t1 | ||
JOIN range(200000) AS t2 | ||
ON (t1.value + t2.value) % 1000 = 0; | ||
"#, | ||
// Q6: INNER 200K x 10K | LOW 0.1% (large to small) | ||
r#" | ||
SELECT * | ||
FROM range(200000) AS t1 | ||
JOIN range(10000) AS t2 | ||
ON (t1.value + t2.value) % 1000 = 0; | ||
"#, | ||
// Q7: RIGHT OUTER 10K x 200K | LOW 0.1% | ||
r#" | ||
SELECT * | ||
FROM range(10000) AS t1 | ||
RIGHT JOIN range(200000) AS t2 | ||
ON (t1.value + t2.value) % 1000 = 0; | ||
"#, | ||
// Q8: LEFT OUTER 200K x 10K | LOW 0.1% | ||
r#" | ||
SELECT * | ||
FROM range(200000) AS t1 | ||
LEFT JOIN range(10000) AS t2 | ||
ON (t1.value + t2.value) % 1000 = 0; | ||
"#, | ||
// Q9: FULL OUTER 30K x 30K | LOW 0.1% | ||
r#" | ||
SELECT * | ||
FROM range(30000) AS t1 | ||
FULL JOIN range(30000) AS t2 | ||
ON (t1.value + t2.value) % 1000 = 0; | ||
"#, | ||
// Q10: FULL OUTER 30K x 30K | High 90% | ||
r#" | ||
SELECT * | ||
FROM range(30000) AS t1 | ||
FULL JOIN range(30000) AS t2 | ||
ON (t1.value + t2.value) % 10 <> 0; | ||
"#, | ||
]; | ||
|
||
impl RunOpt { | ||
pub async fn run(self) -> Result<()> { | ||
println!("Running NLJ benchmarks with the following options: {self:#?}\n"); | ||
|
||
// Define query range | ||
let query_range = match self.query { | ||
Some(query_id) => { | ||
if query_id >= 1 && query_id <= NLJ_QUERIES.len() { | ||
query_id..=query_id | ||
} else { | ||
return exec_err!( | ||
"Query {query_id} not found. Available queries: 1 to {}", | ||
NLJ_QUERIES.len() | ||
); | ||
// return Err(exec_datafusion_err!( | ||
// "Query {} not found. Available queries: 1 to {}", | ||
// query_id, | ||
// NLJ_QUERIES.len() | ||
// )); | ||
2010YOUY01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
None => 1..=NLJ_QUERIES.len(), | ||
}; | ||
|
||
let config = self.common.config()?; | ||
let rt_builder = self.common.runtime_env_builder()?; | ||
let ctx = SessionContext::new_with_config_rt(config, rt_builder.build_arc()?); | ||
|
||
let mut benchmark_run = BenchmarkRun::new(); | ||
for query_id in query_range { | ||
let query_index = query_id - 1; // Convert 1-based to 0-based index | ||
|
||
let sql = NLJ_QUERIES[query_index]; | ||
benchmark_run.start_new_case(&format!("Query {query_id}")); | ||
let query_run = self.benchmark_query(sql, &query_id.to_string(), &ctx).await; | ||
match query_run { | ||
Ok(query_results) => { | ||
for iter in query_results { | ||
benchmark_run.write_iter(iter.elapsed, iter.row_count); | ||
} | ||
} | ||
Err(e) => { | ||
return Err(DataFusionError::Context( | ||
"NLJ benchmark Q{query_id} failed with error:".to_string(), | ||
Box::new(e), | ||
)); | ||
} | ||
} | ||
} | ||
|
||
benchmark_run.maybe_write_json(self.output_path.as_ref())?; | ||
Ok(()) | ||
} | ||
|
||
/// Validates that the query's physical plan uses a NestedLoopJoin (NLJ), | ||
/// then executes the query and collects execution times. | ||
/// | ||
/// TODO: ensure the optimizer won't change the join order (it's not at | ||
/// v48.0.0). | ||
async fn benchmark_query( | ||
&self, | ||
sql: &str, | ||
query_name: &str, | ||
ctx: &SessionContext, | ||
) -> Result<Vec<QueryResult>> { | ||
let mut query_results = vec![]; | ||
|
||
// Validate that the query plan includes a Nested Loop Join | ||
let df = ctx.sql(sql).await?; | ||
let physical_plan = df.create_physical_plan().await?; | ||
let plan_string = format!("{physical_plan:#?}"); | ||
|
||
if !plan_string.contains("NestedLoopJoinExec") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍👍 |
||
return Err(exec_datafusion_err!( | ||
"Query {query_name} does not use Nested Loop Join. Physical plan: {plan_string}" | ||
)); | ||
} | ||
|
||
for i in 0..self.common.iterations { | ||
let start = Instant::now(); | ||
let df = ctx.sql(sql).await?; | ||
let batches = df.collect().await?; | ||
let elapsed = start.elapsed(); | ||
|
||
let row_count = batches.iter().map(|b| b.num_rows()).sum(); | ||
println!( | ||
"Query {query_name} iteration {i} returned {row_count} rows in {elapsed:?}" | ||
); | ||
|
||
query_results.push(QueryResult { elapsed, row_count }); | ||
} | ||
|
||
Ok(query_results) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can remove this ?