-
Notifications
You must be signed in to change notification settings - Fork 2k
perf: Implement physical execution of uncorrelated scalar subqueries #21240
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
Open
neilconway
wants to merge
34
commits into
apache:main
Choose a base branch
from
neilconway:neilc/scalar-subquery-expr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
4a3e553
Initial implementation
neilconway 6b4f5c0
cargo fmt
neilconway 1412ab1
Properly wait for subquery exec to complete before exec'ing main input
neilconway cedfa5c
Better fix for async exec issue
neilconway d80569f
Fix doc lint error
neilconway 9f606fb
Implement logical plan serialization/deserialization for subqueries
neilconway b07491b
cargo fmt
neilconway 27a1ac2
Refactor logical plan deserialization
neilconway bce0a6d
Merge remote-tracking branch 'origin/main' into neilc/scalar-subquery…
neilconway 7071001
Increase large files size check
neilconway b9bce91
fix clippy
neilconway 7c965aa
Update expected TPC-H plans
neilconway 09f167a
Implement statistics
neilconway 54a9f79
Tweak comments
neilconway b979e3d
Merge branch 'main' into neilc/scalar-subquery-expr
neilconway 2c256e7
Ensure projection pushdown works inside uncorrelated subqueries
neilconway 99d9bcf
Update expected plans
neilconway 9a11d62
Fix overlooked cases for projection pushdown
neilconway 9b217ca
Merge remote-tracking branch 'origin/main' into neilc/scalar-subquery…
neilconway 5aef67e
Fix line numbers in expected EXPLAIN
neilconway 3d0b99f
Evaluate subqueries in parallel
neilconway f99ded5
Merge remote-tracking branch 'origin/main' into neilc/scalar-subquery…
neilconway b02abf8
Don't try to use subquery filters for partition pruning
neilconway 3971312
Raise an error if duplicate subquery eval is detected
neilconway 64e9f34
cargo fmt
neilconway 26d8acb
Update expected plan
neilconway d2af491
Merge remote-tracking branch 'origin/main' into neilc/scalar-subquery…
neilconway f9c9d5d
Remove unnecessary IN/EXISTS serialization code
neilconway 92e6054
Code cleanup
neilconway 6857966
Code cleanup
neilconway 6a4f524
Code cleanup and refactoring
neilconway 7adb788
Merge remote-tracking branch 'origin/main' into neilc/scalar-subquery…
neilconway 670139c
Updates for plan API changes
neilconway 1239e3a
Fix doc build
neilconway 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // 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. | ||
|
|
||
| //! Benchmarks for uncorrelated scalar subquery evaluation. | ||
| //! | ||
| //! Measures the overhead of subquery execution machinery by using simple | ||
| //! arithmetic and comparison operators that don't have specialized scalar | ||
| //! fast paths, keeping the comparison between the old (join-based) and new | ||
| //! (ScalarSubqueryExec-based) approaches apples-to-apples. | ||
|
|
||
| use arrow::array::Int64Array; | ||
| use arrow::datatypes::{DataType, Field, Schema}; | ||
| use arrow::record_batch::RecordBatch; | ||
| use criterion::{Criterion, criterion_group, criterion_main}; | ||
| use datafusion::datasource::MemTable; | ||
| use datafusion::error::Result; | ||
| use datafusion::prelude::SessionContext; | ||
| use std::hint::black_box; | ||
| use std::sync::Arc; | ||
| use tokio::runtime::Runtime; | ||
|
|
||
| fn query(ctx: &SessionContext, rt: &Runtime, sql: &str) { | ||
| let df = rt.block_on(ctx.sql(sql)).unwrap(); | ||
| black_box(rt.block_on(df.collect()).unwrap()); | ||
| } | ||
|
|
||
| fn create_context(num_rows: usize) -> Result<SessionContext> { | ||
| let schema = Arc::new(Schema::new(vec![Field::new("x", DataType::Int64, false)])); | ||
|
|
||
| let batch_size = 4096; | ||
| let batches = (0..num_rows / batch_size) | ||
| .map(|i| { | ||
| let values: Vec<i64> = | ||
| ((i * batch_size) as i64..((i + 1) * batch_size) as i64).collect(); | ||
| RecordBatch::try_new(schema.clone(), vec![Arc::new(Int64Array::from(values))]) | ||
| .unwrap() | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| // Small lookup table for the subquery to read from. | ||
| let sq_schema = Arc::new(Schema::new(vec![Field::new("v", DataType::Int64, false)])); | ||
| let sq_batch = RecordBatch::try_new( | ||
| sq_schema.clone(), | ||
| vec![Arc::new(Int64Array::from(vec![10, 20, 30]))], | ||
| )?; | ||
|
|
||
| let ctx = SessionContext::new(); | ||
| ctx.register_table( | ||
| "main_t", | ||
| Arc::new(MemTable::try_new(schema, vec![batches])?), | ||
| )?; | ||
| ctx.register_table( | ||
| "lookup", | ||
| Arc::new(MemTable::try_new(sq_schema, vec![vec![sq_batch]])?), | ||
| )?; | ||
|
|
||
| Ok(ctx) | ||
| } | ||
|
|
||
| fn criterion_benchmark(c: &mut Criterion) { | ||
| let num_rows = 1_048_576; // 2^20 | ||
| let rt = Runtime::new().unwrap(); | ||
|
|
||
| // Scalar subquery in a filter (WHERE clause). | ||
| c.bench_function("scalar_subquery_filter", |b| { | ||
| let ctx = create_context(num_rows).unwrap(); | ||
| b.iter(|| { | ||
| query( | ||
| &ctx, | ||
| &rt, | ||
| "SELECT x FROM main_t WHERE x > (SELECT max(v) FROM lookup)", | ||
| ) | ||
| }) | ||
| }); | ||
|
|
||
| // Scalar subquery in a projection (SELECT expression). | ||
| c.bench_function("scalar_subquery_projection", |b| { | ||
| let ctx = create_context(num_rows).unwrap(); | ||
| b.iter(|| { | ||
| query( | ||
| &ctx, | ||
| &rt, | ||
| "SELECT x + (SELECT max(v) FROM lookup) AS y FROM main_t", | ||
| ) | ||
| }) | ||
| }); | ||
|
|
||
| // Two scalar subqueries in one query. | ||
| c.bench_function("scalar_subquery_two_subqueries", |b| { | ||
| let ctx = create_context(num_rows).unwrap(); | ||
| b.iter(|| { | ||
| query( | ||
| &ctx, | ||
| &rt, | ||
| "SELECT x FROM main_t \ | ||
| WHERE x > (SELECT min(v) FROM lookup) \ | ||
| AND x < (SELECT max(v) FROM lookup) + 1000000", | ||
| ) | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| criterion_group!(benches, criterion_benchmark); | ||
| criterion_main!(benches); |
Oops, something went wrong.
Oops, something went wrong.
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.
do we really need to up the limit? this repo gets checked out a lot
What is so large that required increasing to 2MB?