-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
refactor push_down_filter
to fix dead-loop and use optimizer_recurse.
#5337
Merged
Merged
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
|
||
//! Push Down Filter optimizer rule ensures that filters are applied as early as possible in the plan | ||
|
||
use crate::optimizer::ApplyOrder; | ||
use crate::utils::{conjunction, split_conjunction}; | ||
use crate::{utils, OptimizerConfig, OptimizerRule}; | ||
use datafusion_common::{Column, DFSchema, DataFusionError, Result}; | ||
|
@@ -29,7 +30,6 @@ use datafusion_expr::{ | |
use std::collections::{HashMap, HashSet}; | ||
use std::iter::once; | ||
use std::sync::Arc; | ||
use utils::optimize_children; | ||
|
||
/// Push Down Filter optimizer rule pushes filter clauses down the plan | ||
/// # Introduction | ||
|
@@ -511,25 +511,20 @@ impl OptimizerRule for PushDownFilter { | |
"push_down_filter" | ||
} | ||
|
||
fn apply_order(&self) -> Option<ApplyOrder> { | ||
Some(ApplyOrder::TopDown) | ||
} | ||
|
||
fn try_optimize( | ||
&self, | ||
plan: &LogicalPlan, | ||
config: &dyn OptimizerConfig, | ||
_config: &dyn OptimizerConfig, | ||
) -> Result<Option<LogicalPlan>> { | ||
let filter = match plan { | ||
LogicalPlan::Filter(filter) => filter, | ||
// we also need to pushdown filter in Join. | ||
LogicalPlan::Join(join) => { | ||
let optimized_plan = push_down_join(plan, join, None)?; | ||
return match optimized_plan { | ||
Some(optimized_plan) => Ok(Some( | ||
optimize_children(self, &optimized_plan, config)? | ||
.unwrap_or(optimized_plan), | ||
)), | ||
None => optimize_children(self, plan, config), | ||
}; | ||
} | ||
_ => return optimize_children(self, plan, config), | ||
Comment on lines
-522
to
-532
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. @HaoYang670 Here remove a risk recursion. |
||
LogicalPlan::Join(join) => return push_down_join(plan, join, None), | ||
_ => return Ok(None), | ||
}; | ||
|
||
let child_plan = filter.input.as_ref(); | ||
|
@@ -550,11 +545,12 @@ impl OptimizerRule for PushDownFilter { | |
let new_predicate = conjunction(new_predicates).ok_or_else(|| { | ||
DataFusionError::Plan("at least one expression exists".to_string()) | ||
})?; | ||
let new_plan = LogicalPlan::Filter(Filter::try_new( | ||
let new_filter = LogicalPlan::Filter(Filter::try_new( | ||
new_predicate, | ||
child_filter.input.clone(), | ||
)?); | ||
return self.try_optimize(&new_plan, config); | ||
self.try_optimize(&new_filter, _config)? | ||
.unwrap_or(new_filter) | ||
} | ||
LogicalPlan::Repartition(_) | ||
| LogicalPlan::Distinct(_) | ||
|
@@ -691,7 +687,7 @@ impl OptimizerRule for PushDownFilter { | |
LogicalPlan::Join(join) => { | ||
match push_down_join(&filter.input, join, Some(&filter.predicate))? { | ||
Some(optimized_plan) => optimized_plan, | ||
None => plan.clone(), | ||
None => return Ok(None), | ||
} | ||
} | ||
LogicalPlan::CrossJoin(CrossJoin { left, right, .. }) => { | ||
|
@@ -741,12 +737,9 @@ impl OptimizerRule for PushDownFilter { | |
None => new_scan, | ||
} | ||
} | ||
_ => plan.clone(), | ||
_ => return Ok(None), | ||
}; | ||
|
||
Ok(Some( | ||
optimize_children(self, &new_plan, config)?.unwrap_or(new_plan), | ||
)) | ||
Ok(Some(new_plan)) | ||
} | ||
} | ||
|
||
|
@@ -777,6 +770,7 @@ pub fn replace_cols_by_name( | |
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::optimizer::Optimizer; | ||
use crate::rewrite_disjunctive_predicate::RewriteDisjunctivePredicate; | ||
use crate::test::*; | ||
use crate::OptimizerContext; | ||
|
@@ -791,28 +785,35 @@ mod tests { | |
use std::sync::Arc; | ||
|
||
fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) -> Result<()> { | ||
let optimized_plan = PushDownFilter::new() | ||
.try_optimize(plan, &OptimizerContext::new()) | ||
.unwrap() | ||
.expect("failed to optimize plan"); | ||
let formatted_plan = format!("{optimized_plan:?}"); | ||
assert_eq!(plan.schema(), optimized_plan.schema()); | ||
assert_eq!(expected, formatted_plan); | ||
Ok(()) | ||
crate::test::assert_optimized_plan_eq( | ||
Arc::new(PushDownFilter::new()), | ||
plan, | ||
expected, | ||
) | ||
} | ||
|
||
fn assert_optimized_plan_eq_with_rewrite_predicate( | ||
plan: &LogicalPlan, | ||
expected: &str, | ||
) -> Result<()> { | ||
let mut optimized_plan = RewriteDisjunctivePredicate::new() | ||
.try_optimize(plan, &OptimizerContext::new()) | ||
.unwrap() | ||
.expect("failed to optimize plan"); | ||
optimized_plan = PushDownFilter::new() | ||
.try_optimize(&optimized_plan, &OptimizerContext::new()) | ||
.unwrap() | ||
.expect("failed to optimize plan"); | ||
let optimizer = Optimizer::with_rules(vec![ | ||
Arc::new(RewriteDisjunctivePredicate::new()), | ||
Arc::new(PushDownFilter::new()), | ||
]); | ||
let mut optimized_plan = optimizer | ||
.optimize_recursively( | ||
optimizer.rules.get(0).unwrap(), | ||
plan, | ||
&OptimizerContext::new(), | ||
)? | ||
.unwrap_or_else(|| plan.clone()); | ||
optimized_plan = optimizer | ||
.optimize_recursively( | ||
optimizer.rules.get(1).unwrap(), | ||
&optimized_plan, | ||
&OptimizerContext::new(), | ||
)? | ||
.unwrap_or_else(|| plan.clone()); | ||
let formatted_plan = format!("{optimized_plan:?}"); | ||
assert_eq!(plan.schema(), optimized_plan.schema()); | ||
assert_eq!(expected, formatted_plan); | ||
|
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.
Will it lose information if we disable
config
here?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 didn't disable it, just because it's only used recursively, so it needs to be prefixed with
_
.It's hint by
cargo clippy
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.
In the original code, we pass the
config
to theoptimize_children
, but now, it is just a placeholder for self recursion, which means we never use theconfig
anymore.