-
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
Fix bugs in SQL planner with GROUP BY scalar function and alias #2457
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
80c7921
Fix issue 2430
andygrove 5f9734f
remove debug println
andygrove a28f1f3
revert some changes that were not needed
andygrove 64200ef
revert some changes that were not needed
andygrove 3ea0f83
revert some changes that were not needed
andygrove 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 |
---|---|---|
|
@@ -37,7 +37,7 @@ use crate::logical_plan::{ | |
use crate::optimizer::utils::exprlist_to_columns; | ||
use crate::prelude::JoinType; | ||
use crate::scalar::ScalarValue; | ||
use crate::sql::utils::{make_decimal_type, normalize_ident}; | ||
use crate::sql::utils::{make_decimal_type, normalize_ident, resolve_columns}; | ||
use crate::{ | ||
error::{DataFusionError, Result}, | ||
physical_plan::aggregates, | ||
|
@@ -1144,30 +1144,45 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
group_by_exprs: Vec<Expr>, | ||
aggr_exprs: Vec<Expr>, | ||
) -> Result<(LogicalPlan, Vec<Expr>, Option<Expr>)> { | ||
// create the aggregate plan | ||
let plan = LogicalPlanBuilder::from(input.clone()) | ||
.aggregate(group_by_exprs.clone(), aggr_exprs.clone())? | ||
.build()?; | ||
|
||
// in this next section of code we are re-writing the projection to refer to columns | ||
// output by the aggregate plan. For example, if the projection contains the expression | ||
// `SUM(a)` then we replace that with a reference to a column `#SUM(a)` produced by | ||
// the aggregate plan. | ||
|
||
// combine the original grouping and aggregate expressions into one list (note that | ||
// we do not add the "having" expression since that is not part of the projection) | ||
let aggr_projection_exprs = group_by_exprs | ||
.iter() | ||
.chain(aggr_exprs.iter()) | ||
.cloned() | ||
.collect::<Vec<Expr>>(); | ||
|
||
let plan = LogicalPlanBuilder::from(input.clone()) | ||
.aggregate(group_by_exprs, aggr_exprs)? | ||
.build()?; | ||
// now attempt to resolve columns and replace with fully-qualified columns | ||
let aggr_projection_exprs = aggr_projection_exprs | ||
.iter() | ||
.map(|expr| resolve_columns(expr, &input)) | ||
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. This is the fix .. we were comparing qualified and unqualified names before this, leading to failures in some cases |
||
.collect::<Result<Vec<Expr>>>()?; | ||
|
||
// After aggregation, these are all of the columns that will be | ||
// available to next phases of planning. | ||
// next we replace any expressions that are not a column with a column referencing | ||
// an output column from the aggregate schema | ||
let column_exprs_post_aggr = aggr_projection_exprs | ||
.iter() | ||
.map(|expr| expr_as_column_expr(expr, &input)) | ||
.collect::<Result<Vec<Expr>>>()?; | ||
|
||
// Rewrite the SELECT expression to use the columns produced by the | ||
// aggregation. | ||
// next we re-write the projection | ||
let select_exprs_post_aggr = select_exprs | ||
.iter() | ||
.map(|expr| rebase_expr(expr, &aggr_projection_exprs, &input)) | ||
.collect::<Result<Vec<Expr>>>()?; | ||
|
||
// finally, we have some validation that the re-written projection can be resolved | ||
// from the aggregate output columns | ||
check_columns_satisfy_exprs( | ||
&column_exprs_post_aggr, | ||
&select_exprs_post_aggr, | ||
|
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 am a huge fan of the comments ❤️ thank you