-
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
Stop most copying LogicalPlan and Exprs in ScalarSubqueryToJoin
#10489
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 |
---|---|---|
|
@@ -29,7 +29,7 @@ use datafusion_common::alias::AliasGenerator; | |
use datafusion_common::tree_node::{ | ||
Transformed, TransformedResult, TreeNode, TreeNodeRecursion, TreeNodeRewriter, | ||
}; | ||
use datafusion_common::{plan_err, Column, Result, ScalarValue}; | ||
use datafusion_common::{internal_err, plan_err, Column, Result, ScalarValue}; | ||
use datafusion_expr::expr_rewriter::create_col_from_scalar_expr; | ||
use datafusion_expr::logical_plan::{JoinType, Subquery}; | ||
use datafusion_expr::utils::conjunction; | ||
|
@@ -50,7 +50,7 @@ impl ScalarSubqueryToJoin { | |
/// # Arguments | ||
/// * `predicate` - A conjunction to split and search | ||
/// | ||
/// Returns a tuple (subqueries, rewrite expression) | ||
/// Returns a tuple (subqueries, alias) | ||
fn extract_subquery_exprs( | ||
&self, | ||
predicate: &Expr, | ||
|
@@ -71,19 +71,36 @@ impl ScalarSubqueryToJoin { | |
impl OptimizerRule for ScalarSubqueryToJoin { | ||
fn try_optimize( | ||
&self, | ||
plan: &LogicalPlan, | ||
config: &dyn OptimizerConfig, | ||
_plan: &LogicalPlan, | ||
_config: &dyn OptimizerConfig, | ||
) -> Result<Option<LogicalPlan>> { | ||
internal_err!("Should have called ScalarSubqueryToJoin::rewrite") | ||
} | ||
|
||
fn supports_rewrite(&self) -> bool { | ||
true | ||
} | ||
|
||
fn rewrite( | ||
&self, | ||
plan: LogicalPlan, | ||
config: &dyn OptimizerConfig, | ||
) -> Result<Transformed<LogicalPlan>> { | ||
match plan { | ||
LogicalPlan::Filter(filter) => { | ||
// Optimization: skip the rest of the rule and its copies if | ||
// there are no scalar subqueries | ||
if !contains_scalar_subquery(&filter.predicate) { | ||
return Ok(Transformed::no(LogicalPlan::Filter(filter))); | ||
} | ||
|
||
let (subqueries, mut rewrite_expr) = self.extract_subquery_exprs( | ||
&filter.predicate, | ||
config.alias_generator(), | ||
)?; | ||
|
||
if subqueries.is_empty() { | ||
// regular filter, no subquery exists clause here | ||
return Ok(None); | ||
return internal_err!("Expected subqueries not found in filter"); | ||
} | ||
|
||
// iterate through all subqueries in predicate, turning each into a left join | ||
|
@@ -94,16 +111,13 @@ impl OptimizerRule for ScalarSubqueryToJoin { | |
{ | ||
if !expr_check_map.is_empty() { | ||
rewrite_expr = rewrite_expr | ||
.clone() | ||
.transform_up(|expr| { | ||
if let Expr::Column(col) = &expr { | ||
if let Some(map_expr) = | ||
expr_check_map.get(&col.name) | ||
{ | ||
Ok(Transformed::yes(map_expr.clone())) | ||
} else { | ||
Ok(Transformed::no(expr)) | ||
} | ||
// replace column references with entry in map, if it exists | ||
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. Remove some indenting using the api added in #10448, but the logic is the same |
||
if let Some(map_expr) = expr | ||
.try_as_col() | ||
.and_then(|col| expr_check_map.get(&col.name)) | ||
{ | ||
Ok(Transformed::yes(map_expr.clone())) | ||
} else { | ||
Ok(Transformed::no(expr)) | ||
} | ||
|
@@ -113,15 +127,21 @@ impl OptimizerRule for ScalarSubqueryToJoin { | |
cur_input = optimized_subquery; | ||
} else { | ||
// if we can't handle all of the subqueries then bail for now | ||
return Ok(None); | ||
return Ok(Transformed::no(LogicalPlan::Filter(filter))); | ||
} | ||
} | ||
let new_plan = LogicalPlanBuilder::from(cur_input) | ||
.filter(rewrite_expr)? | ||
.build()?; | ||
Ok(Some(new_plan)) | ||
Ok(Transformed::yes(new_plan)) | ||
} | ||
LogicalPlan::Projection(projection) => { | ||
// Optimization: skip the rest of the rule and its copies if | ||
// there are no scalar subqueries | ||
if !projection.expr.iter().any(contains_scalar_subquery) { | ||
return Ok(Transformed::no(LogicalPlan::Projection(projection))); | ||
} | ||
|
||
let mut all_subqueryies = vec![]; | ||
let mut expr_to_rewrite_expr_map = HashMap::new(); | ||
let mut subquery_to_expr_map = HashMap::new(); | ||
|
@@ -135,8 +155,7 @@ impl OptimizerRule for ScalarSubqueryToJoin { | |
expr_to_rewrite_expr_map.insert(expr, rewrite_exprs); | ||
} | ||
if all_subqueryies.is_empty() { | ||
// regular projection, no subquery exists clause here | ||
return Ok(None); | ||
return internal_err!("Expected subqueries not found in projection"); | ||
} | ||
// iterate through all subqueries in predicate, turning each into a left join | ||
let mut cur_input = projection.input.as_ref().clone(); | ||
|
@@ -153,14 +172,13 @@ impl OptimizerRule for ScalarSubqueryToJoin { | |
let new_expr = rewrite_expr | ||
.clone() | ||
.transform_up(|expr| { | ||
if let Expr::Column(col) = &expr { | ||
if let Some(map_expr) = | ||
// replace column references with entry in map, if it exists | ||
if let Some(map_expr) = | ||
expr.try_as_col().and_then(|col| { | ||
expr_check_map.get(&col.name) | ||
{ | ||
Ok(Transformed::yes(map_expr.clone())) | ||
} else { | ||
Ok(Transformed::no(expr)) | ||
} | ||
}) | ||
{ | ||
Ok(Transformed::yes(map_expr.clone())) | ||
} else { | ||
Ok(Transformed::no(expr)) | ||
} | ||
|
@@ -172,7 +190,7 @@ impl OptimizerRule for ScalarSubqueryToJoin { | |
} | ||
} else { | ||
// if we can't handle all of the subqueries then bail for now | ||
return Ok(None); | ||
return Ok(Transformed::no(LogicalPlan::Projection(projection))); | ||
} | ||
} | ||
|
||
|
@@ -190,10 +208,10 @@ impl OptimizerRule for ScalarSubqueryToJoin { | |
let new_plan = LogicalPlanBuilder::from(cur_input) | ||
.project(proj_exprs)? | ||
.build()?; | ||
Ok(Some(new_plan)) | ||
Ok(Transformed::yes(new_plan)) | ||
} | ||
|
||
_ => Ok(None), | ||
plan => Ok(Transformed::no(plan)), | ||
} | ||
} | ||
|
||
|
@@ -206,6 +224,13 @@ impl OptimizerRule for ScalarSubqueryToJoin { | |
} | ||
} | ||
|
||
/// Returns true if the expression has a scalar subquery somewhere in it | ||
/// false otherwise | ||
fn contains_scalar_subquery(expr: &Expr) -> bool { | ||
expr.exists(|expr| Ok(matches!(expr, Expr::ScalarSubquery(_)))) | ||
.expect("Inner is always Ok") | ||
} | ||
|
||
struct ExtractScalarSubQuery { | ||
sub_query_info: Vec<(Subquery, String)>, | ||
alias_gen: Arc<AliasGenerator>, | ||
|
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.
Note there is a still a bunch of copying in the actual rewrite logic itself below, but I could not figure out a way to remove it easily as there are several paths that return the original (unmodified) plan for unsupported subqueries
I think we would have to split the check for "is rewrite supported" and the "do the rewrite" logic or something
Given this isn't a tall pole in planning, I don't have any more time to spend on this particular rule so I want to call this good enough