-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Optimize planning for projected nested union #18713
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
7 commits
Select commit
Hold shift + click to select a range
ea59584
It works ig
logan-keede 466ca17
Okay, this one maybe, wrapped projection nicely
logan-keede c516035
clippy
logan-keede f2e5d0a
Merge branch 'main' into optimize_projected_union
logan-keede 91b6c9a
Merge branch 'main' of https://github.com/logan-keede/datafusion into…
logan-keede d9acde5
remove clone
logan-keede 681a173
add inline comment for explanation
logan-keede 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ use crate::{OptimizerConfig, OptimizerRule}; | |
| use datafusion_common::tree_node::Transformed; | ||
| use datafusion_common::Result; | ||
| use datafusion_expr::expr_rewriter::coerce_plan_expr_for_schema; | ||
| use datafusion_expr::{Distinct, LogicalPlan, Union}; | ||
| use datafusion_expr::{Distinct, LogicalPlan, Projection, Union}; | ||
| use itertools::Itertools; | ||
| use std::sync::Arc; | ||
|
|
||
|
|
@@ -105,6 +105,38 @@ fn extract_plans_from_union(plan: Arc<LogicalPlan>) -> Vec<LogicalPlan> { | |
| .into_iter() | ||
| .map(Arc::unwrap_or_clone) | ||
| .collect::<Vec<_>>(), | ||
| // While unnesting, unwrap a Projection whose input is a nested Union, | ||
| // flatten the inner Union, and push the same Projection down onto | ||
| // each of the nested Union’s children. | ||
| // | ||
| // Example: | ||
| // Union { Projection { Union { plan1, plan2 } }, plan3 } | ||
| // => Union { Projection { plan1 }, Projection { plan2 }, plan3 } | ||
| LogicalPlan::Projection(Projection { | ||
| expr, | ||
| input, | ||
| schema, | ||
| .. | ||
| }) => match Arc::unwrap_or_clone(input) { | ||
| LogicalPlan::Union(Union { inputs, .. }) => inputs | ||
| .into_iter() | ||
| .map(Arc::unwrap_or_clone) | ||
| .map(|plan| { | ||
| LogicalPlan::Projection( | ||
| Projection::try_new_with_schema( | ||
| expr.clone(), | ||
| Arc::new(plan), | ||
| Arc::clone(&schema), | ||
| ) | ||
| .unwrap(), | ||
| ) | ||
| }) | ||
| .collect::<Vec<_>>(), | ||
|
|
||
| plan => vec![LogicalPlan::Projection( | ||
| Projection::try_new_with_schema(expr, Arc::new(plan), schema).unwrap(), | ||
| )], | ||
| }, | ||
| plan => vec![plan], | ||
| } | ||
| } | ||
|
|
@@ -331,6 +363,27 @@ mod tests { | |
| ") | ||
| } | ||
|
|
||
| #[test] | ||
| fn eliminate_nested_union_in_projection() -> Result<()> { | ||
|
Contributor
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. Without the change in this PR the test fails like In other words, the plan is left as |
||
| let plan_builder = table_scan(Some("table"), &schema(), None)?; | ||
|
|
||
| let plan = plan_builder | ||
| .clone() | ||
| .union(plan_builder.clone().build()?)? | ||
| .project(vec![col("id").alias("table_id"), col("key"), col("value")])? | ||
| .union(plan_builder.build()?)? | ||
| .build()?; | ||
|
|
||
| assert_optimized_plan_equal!(plan, @r" | ||
| Union | ||
| Projection: id AS table_id, key, value | ||
| TableScan: table | ||
| Projection: id AS table_id, key, value | ||
| TableScan: table | ||
| TableScan: table | ||
| ") | ||
| } | ||
|
|
||
| #[test] | ||
| fn eliminate_nested_union_with_type_cast_projection() -> Result<()> { | ||
| let table_1 = table_scan( | ||
|
|
||
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.
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.
Am I right in understanding this change that it transforms
to
(aka it pushes the projection to each input of the union?)
Maybe some comments could help future readers