Repository navigation
Improve planning speed: Fast path for union_schema when all children share a schema
#24389
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| // 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. | ||
|
|
||
| //! Benchmark for `UnionExec` construction cost as a function of child count. | ||
| //! | ||
| //! Scenarios (run against a flat and a nested/struct schema): | ||
| //! - `shared_arc`: every child returns the same `Arc<Schema>` | ||
| //! - `content_equal`: pointer-distinct but identical schemas per child | ||
| //! - `last_differs`: all children equal except extra metadata on the last | ||
| //! child's last top-level field. Worst case for the equality fast path: | ||
| //! the scan runs, fails on the final child, then the full merge runs anyway. | ||
| //! - `names_differ`: the first field is renamed in every child but the first, | ||
| //! the common real-world unequal union where equality fails immediately. | ||
|
|
||
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
|
|
||
| use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; | ||
| use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; | ||
| use datafusion_physical_plan::ExecutionPlan; | ||
| use datafusion_physical_plan::empty::EmptyExec; | ||
| use datafusion_physical_plan::union::UnionExec; | ||
|
|
||
| const NUM_FIELDS: usize = 10; | ||
| const NESTED_CHILDREN: usize = 5; | ||
| const METADATA_PER_FIELD: usize = 2; | ||
|
|
||
| fn metadata(tag: &str, i: usize) -> HashMap<String, String> { | ||
| (0..METADATA_PER_FIELD) | ||
| .map(|m| (format!("key_{m}"), format!("value_{tag}_{i}_{m}"))) | ||
| .collect() | ||
| } | ||
|
|
||
| fn flat_schema() -> Schema { | ||
| let fields: Vec<Field> = (0..NUM_FIELDS) | ||
| .map(|i| { | ||
| Field::new(format!("col_{i}"), DataType::Int64, true) | ||
| .with_metadata(metadata("f", i)) | ||
| }) | ||
| .collect(); | ||
| Schema::new(fields) | ||
| } | ||
|
|
||
| fn nested_schema() -> Schema { | ||
| let fields: Vec<Field> = (0..NUM_FIELDS) | ||
| .map(|i| { | ||
| let children: Vec<Field> = (0..NESTED_CHILDREN) | ||
| .map(|c| { | ||
| Field::new(format!("sub_{i}_{c}"), DataType::Int64, true) | ||
| .with_metadata(metadata("n", i * NESTED_CHILDREN + c)) | ||
| }) | ||
| .collect(); | ||
| Field::new(format!("col_{i}"), DataType::Struct(children.into()), true) | ||
| .with_metadata(metadata("s", i)) | ||
| }) | ||
| .collect(); | ||
| Schema::new(fields) | ||
| } | ||
|
|
||
| /// Clone `schema` with extra metadata on its last field. Divergence must stay | ||
| /// at the top level: differing nested fields change the field `DataType` | ||
| /// itself, which `UnionExec::try_new` rejects ("Schemas have to be aligned"). | ||
| fn divergent(schema: &Schema) -> Schema { | ||
| let mut fields: Vec<Field> = | ||
| schema.fields().iter().map(|f| f.as_ref().clone()).collect(); | ||
| let last = fields.pop().unwrap(); | ||
| let mut md = last.metadata().clone(); | ||
| md.insert("divergent".to_string(), "true".to_string()); | ||
| fields.push(last.with_metadata(md)); | ||
| Schema::new(fields) | ||
| } | ||
|
|
||
| fn child(schema: SchemaRef) -> Arc<dyn ExecutionPlan> { | ||
| Arc::new(EmptyExec::new(schema)) | ||
| } | ||
|
|
||
| fn children_shared_arc(schema: &Schema, n: usize) -> Vec<Arc<dyn ExecutionPlan>> { | ||
| let schema: SchemaRef = Arc::new(schema.clone()); | ||
| (0..n).map(|_| child(Arc::clone(&schema))).collect() | ||
| } | ||
|
|
||
| fn children_content_equal(schema: &Schema, n: usize) -> Vec<Arc<dyn ExecutionPlan>> { | ||
| (0..n).map(|_| child(Arc::new(schema.clone()))).collect() | ||
| } | ||
|
|
||
| /// First child keeps `schema`; the rest rename the first field — the common | ||
| /// real-world unequal union (`SELECT a .. UNION ALL SELECT b ..`), where any | ||
| /// equality check fails immediately. | ||
| fn children_names_differ(schema: &Schema, n: usize) -> Vec<Arc<dyn ExecutionPlan>> { | ||
| let mut fields: Vec<Field> = | ||
| schema.fields().iter().map(|f| f.as_ref().clone()).collect(); | ||
| let first = fields.remove(0); | ||
| let renamed = first.clone().with_name(format!("renamed_{}", first.name())); | ||
| fields.insert(0, renamed); | ||
| let alt = Schema::new(fields); | ||
| let mut children = vec![child(Arc::new(schema.clone()))]; | ||
| children.extend((1..n).map(|_| child(Arc::new(alt.clone())))); | ||
| children | ||
| } | ||
|
|
||
| fn children_last_differs(schema: &Schema, n: usize) -> Vec<Arc<dyn ExecutionPlan>> { | ||
| let mut children = children_content_equal(schema, n - 1); | ||
| children.push(child(Arc::new(divergent(schema)))); | ||
| children | ||
| } | ||
|
|
||
| fn bench_union_construction(c: &mut Criterion) { | ||
| for (suffix, schema, sizes) in [ | ||
| ("", flat_schema(), &[100usize, 1000, 4000][..]), | ||
| ("_nested", nested_schema(), &[1000, 4000][..]), | ||
| ] { | ||
| let mut group = c.benchmark_group(format!("union_exec_try_new{suffix}")); | ||
| for &n in sizes { | ||
| let shared = children_shared_arc(&schema, n); | ||
| let content = children_content_equal(&schema, n); | ||
| let differs = children_last_differs(&schema, n); | ||
|
|
||
| group.bench_with_input( | ||
| BenchmarkId::new("shared_arc", n), | ||
| &shared, | ||
| |b, ch| b.iter(|| UnionExec::try_new(ch.clone()).unwrap()), | ||
| ); | ||
| group.bench_with_input( | ||
| BenchmarkId::new("content_equal", n), | ||
| &content, | ||
| |b, ch| b.iter(|| UnionExec::try_new(ch.clone()).unwrap()), | ||
| ); | ||
| group.bench_with_input( | ||
| BenchmarkId::new("last_differs", n), | ||
| &differs, | ||
| |b, ch| b.iter(|| UnionExec::try_new(ch.clone()).unwrap()), | ||
| ); | ||
| let names = children_names_differ(&schema, n); | ||
| group.bench_with_input( | ||
| BenchmarkId::new("names_differ", n), | ||
| &names, | ||
| |b, ch| b.iter(|| UnionExec::try_new(ch.clone()).unwrap()), | ||
| ); | ||
| } | ||
| group.finish(); | ||
| } | ||
| } | ||
|
|
||
| criterion_group!(benches, bench_union_construction); | ||
| criterion_main!(benches); |
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
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.
I think we should slim this comment down - the first sentence is probably enough. The last sentence is unecessary as it is restating in english what the code right below it clearly does so it is redundant
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.
@reidkaufmann would you be willing to make a follow on PR to slim down the comment?
Uh oh!
There was an error while loading. Please reload this page.
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.
Just noticed this request. Yes -- it's on my todo list now.