-
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
Enhance table scan unparsing to avoid unnamed subqueries. #13006
Merged
alamb
merged 1 commit into
apache:main
from
goldmedal:feature/enahce-pushdown-unparsing
Oct 21, 2024
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 | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -617,9 +617,10 @@ impl Unparser<'_> { | |||||||||||||||||||||
if !Self::is_scan_with_pushdown(table_scan) { | ||||||||||||||||||||||
return Ok(None); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
let table_schema = table_scan.source.schema(); | ||||||||||||||||||||||
let mut filter_alias_rewriter = | ||||||||||||||||||||||
alias.as_ref().map(|alias_name| TableAliasRewriter { | ||||||||||||||||||||||
table_schema: table_scan.source.schema(), | ||||||||||||||||||||||
table_schema: &table_schema, | ||||||||||||||||||||||
alias_name: alias_name.clone(), | ||||||||||||||||||||||
}); | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -628,6 +629,17 @@ impl Unparser<'_> { | |||||||||||||||||||||
Arc::clone(&table_scan.source), | ||||||||||||||||||||||
None, | ||||||||||||||||||||||
)?; | ||||||||||||||||||||||
// We will rebase the column references to the new alias if it exists. | ||||||||||||||||||||||
// If the projection or filters are empty, we will append alias to the table scan. | ||||||||||||||||||||||
// | ||||||||||||||||||||||
// Example: | ||||||||||||||||||||||
// select t1.c1 from t1 where t1.c1 > 1 -> select a.c1 from t1 as a where a.c1 > 1 | ||||||||||||||||||||||
if alias.is_some() | ||||||||||||||||||||||
&& (table_scan.projection.is_some() || !table_scan.filters.is_empty()) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
builder = builder.alias(alias.clone().unwrap())?; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
if let Some(project_vec) = &table_scan.projection { | ||||||||||||||||||||||
let project_columns = project_vec | ||||||||||||||||||||||
.iter() | ||||||||||||||||||||||
|
@@ -645,9 +657,6 @@ impl Unparser<'_> { | |||||||||||||||||||||
} | ||||||||||||||||||||||
}) | ||||||||||||||||||||||
.collect::<Vec<_>>(); | ||||||||||||||||||||||
if let Some(alias) = alias { | ||||||||||||||||||||||
builder = builder.alias(alias)?; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
builder = builder.project(project_columns)?; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -677,6 +686,16 @@ impl Unparser<'_> { | |||||||||||||||||||||
builder = builder.limit(0, Some(fetch))?; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// If the table scan has an alias but no projection or filters, it means no column references are rebased. | ||||||||||||||||||||||
// So we will append the alias to this subquery. | ||||||||||||||||||||||
// Example: | ||||||||||||||||||||||
// select * from t1 limit 10 -> (select * from t1 limit 10) as a | ||||||||||||||||||||||
if alias.is_some() | ||||||||||||||||||||||
&& (table_scan.projection.is_none() && table_scan.filters.is_empty()) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
builder = builder.alias(alias.clone().unwrap())?; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
Comment on lines
+693
to
+697
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.
Suggested change
No need for a clone here. |
||||||||||||||||||||||
|
||||||||||||||||||||||
Ok(Some(builder.build()?)) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
LogicalPlan::SubqueryAlias(subquery_alias) => { | ||||||||||||||||||||||
|
@@ -685,6 +704,40 @@ impl Unparser<'_> { | |||||||||||||||||||||
Some(subquery_alias.alias.clone()), | ||||||||||||||||||||||
) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
// SubqueryAlias could be rewritten to a plan with a projection as the top node by [rewrite::subquery_alias_inner_query_and_columns]. | ||||||||||||||||||||||
// The inner table scan could be a scan with pushdown operations. | ||||||||||||||||||||||
LogicalPlan::Projection(projection) => { | ||||||||||||||||||||||
if let Some(plan) = | ||||||||||||||||||||||
Self::unparse_table_scan_pushdown(&projection.input, alias.clone())? | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
let exprs = if alias.is_some() { | ||||||||||||||||||||||
let mut alias_rewriter = | ||||||||||||||||||||||
alias.as_ref().map(|alias_name| TableAliasRewriter { | ||||||||||||||||||||||
table_schema: plan.schema().as_arrow(), | ||||||||||||||||||||||
alias_name: alias_name.clone(), | ||||||||||||||||||||||
}); | ||||||||||||||||||||||
projection | ||||||||||||||||||||||
.expr | ||||||||||||||||||||||
.iter() | ||||||||||||||||||||||
.cloned() | ||||||||||||||||||||||
.map(|expr| { | ||||||||||||||||||||||
if let Some(ref mut rewriter) = alias_rewriter { | ||||||||||||||||||||||
expr.rewrite(rewriter).data() | ||||||||||||||||||||||
} else { | ||||||||||||||||||||||
Ok(expr) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
}) | ||||||||||||||||||||||
.collect::<Result<Vec<_>>>()? | ||||||||||||||||||||||
} else { | ||||||||||||||||||||||
projection.expr.clone() | ||||||||||||||||||||||
}; | ||||||||||||||||||||||
Ok(Some( | ||||||||||||||||||||||
LogicalPlanBuilder::from(plan).project(exprs)?.build()?, | ||||||||||||||||||||||
)) | ||||||||||||||||||||||
} else { | ||||||||||||||||||||||
Ok(None) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
_ => Ok(None), | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
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
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
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 find this to be slightly cleaner