-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Consolidate EliminateNestedUnion and EliminateOneUnion optimizer rules'
#18678
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
Changes from all commits
70b34d9
4e62ef8
9041a17
4ed0786
7006f19
39b186c
161abcb
e972f47
9f6c6df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ | |
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! [`EliminateNestedUnion`]: flattens nested `Union` to a single `Union` | ||
| //! [`OptimizeUnions`]: removes `Union` nodes in the logical plan. | ||
| use crate::optimizer::ApplyOrder; | ||
| use crate::{OptimizerConfig, OptimizerRule}; | ||
| use datafusion_common::tree_node::Transformed; | ||
|
|
@@ -26,19 +26,21 @@ use itertools::Itertools; | |
| use std::sync::Arc; | ||
|
|
||
| #[derive(Default, Debug)] | ||
| /// An optimization rule that replaces nested unions with a single union. | ||
| pub struct EliminateNestedUnion; | ||
| /// An optimization rule that | ||
| /// 1. replaces nested unions with a single union. | ||
| /// 2. removes unions with a single input. | ||
| pub struct OptimizeUnions; | ||
|
|
||
| impl EliminateNestedUnion { | ||
| impl OptimizeUnions { | ||
| #[allow(missing_docs)] | ||
| pub fn new() -> Self { | ||
| Self {} | ||
| } | ||
| } | ||
|
|
||
| impl OptimizerRule for EliminateNestedUnion { | ||
| impl OptimizerRule for OptimizeUnions { | ||
| fn name(&self) -> &str { | ||
| "eliminate_nested_union" | ||
| "optimize_unions" | ||
| } | ||
|
|
||
| fn apply_order(&self) -> Option<ApplyOrder> { | ||
|
|
@@ -55,6 +57,9 @@ impl OptimizerRule for EliminateNestedUnion { | |
| _config: &dyn OptimizerConfig, | ||
| ) -> Result<Transformed<LogicalPlan>> { | ||
| match plan { | ||
| LogicalPlan::Union(Union { mut inputs, .. }) if inputs.len() == 1 => Ok( | ||
| Transformed::yes(Arc::unwrap_or_clone(inputs.pop().unwrap())), | ||
| ), | ||
|
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. Wow, that's it? Nice find, will be curious if this shows up in planning benchmark |
||
| LogicalPlan::Union(Union { inputs, schema }) => { | ||
| let inputs = inputs | ||
| .into_iter() | ||
|
|
@@ -139,7 +144,7 @@ mod tests { | |
| let analyzed_plan = Analyzer::with_rules(vec![Arc::new(TypeCoercion::new())]) | ||
| .execute_and_check($plan, &options, |_, _| {})?; | ||
| let optimizer_ctx = OptimizerContext::new().with_max_passes(1); | ||
| let rules: Vec<Arc<dyn crate::OptimizerRule + Send + Sync>> = vec![Arc::new(EliminateNestedUnion::new())]; | ||
| let rules: Vec<Arc<dyn crate::OptimizerRule + Send + Sync>> = vec![Arc::new(OptimizeUnions::new())]; | ||
| assert_optimized_plan_eq_snapshot!( | ||
| optimizer_ctx, | ||
| rules, | ||
|
|
@@ -420,4 +425,28 @@ mod tests { | |
| TableScan: table_1 | ||
| ") | ||
| } | ||
|
|
||
| #[test] | ||
| fn eliminate_one_union() -> Result<()> { | ||
|
Contributor
Author
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. single input unions can't be created easily, so this test looks a little different |
||
| let plan = table_scan(Some("table"), &schema(), None)?.build()?; | ||
| let schema = Arc::clone(plan.schema()); | ||
| // note it is not possible to create a single input union via | ||
| // LogicalPlanBuilder so create it manually here | ||
| let plan = LogicalPlan::Union(Union { | ||
| inputs: vec![Arc::new(plan)], | ||
| schema, | ||
| }); | ||
|
|
||
| // Note we can't use the same assert_optimized_plan_equal as creating a | ||
| // single input union is not possible via LogicalPlanBuilder and other passes | ||
| // throw errors / don't handle the schema correctly. | ||
| assert_optimized_plan_eq_snapshot!( | ||
| OptimizerContext::new().with_max_passes(1), | ||
| vec![Arc::new(OptimizeUnions::new())], | ||
| plan, | ||
| @r" | ||
| TableScan: table | ||
| " | ||
| ) | ||
| } | ||
| } | ||
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.
Worth adding a minor comment here, considering its not exactly related to nested unions?
(Or could rename away from
EliminateNestedUnionsto something likeSimplifyUnionsto be all encompassing)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 like this idea -- done in 9041a17