-
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 group by aliased expression in LogicalPLanBuilder::aggregate #8629
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
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 |
---|---|---|
|
@@ -904,27 +904,11 @@ impl LogicalPlanBuilder { | |
group_expr: impl IntoIterator<Item = impl Into<Expr>>, | ||
aggr_expr: impl IntoIterator<Item = impl Into<Expr>>, | ||
) -> Result<Self> { | ||
let mut group_expr = normalize_cols(group_expr, &self.plan)?; | ||
let group_expr = normalize_cols(group_expr, &self.plan)?; | ||
let aggr_expr = normalize_cols(aggr_expr, &self.plan)?; | ||
|
||
// Rewrite groupby exprs according to functional dependencies | ||
let group_by_expr_names = group_expr | ||
.iter() | ||
.map(|group_by_expr| group_by_expr.display_name()) | ||
.collect::<Result<Vec<_>>>()?; | ||
let schema = self.plan.schema(); | ||
if let Some(target_indices) = | ||
get_target_functional_dependencies(schema, &group_by_expr_names) | ||
{ | ||
for idx in target_indices { | ||
let field = schema.field(idx); | ||
let expr = | ||
Expr::Column(Column::new(field.qualifier().cloned(), field.name())); | ||
if !group_expr.contains(&expr) { | ||
group_expr.push(expr); | ||
} | ||
} | ||
} | ||
let group_expr = | ||
add_group_by_exprs_from_dependencies(group_expr, self.plan.schema())?; | ||
Aggregate::try_new(Arc::new(self.plan), group_expr, aggr_expr) | ||
.map(LogicalPlan::Aggregate) | ||
.map(Self::from) | ||
|
@@ -1189,6 +1173,42 @@ pub fn build_join_schema( | |
schema.with_functional_dependencies(func_dependencies) | ||
} | ||
|
||
/// Add additional "synthetic" group by expressions based on functional | ||
/// dependencies. | ||
/// | ||
/// For example, if we are grouping on `[c1]`, and we know from | ||
/// functional dependencies that column `c1` determines `c2`, this function | ||
/// adds `c2` to the group by list. | ||
/// | ||
/// This allows MySQL style selects like | ||
/// `SELECT col FROM t WHERE pk = 5` if col is unique | ||
fn add_group_by_exprs_from_dependencies( | ||
mut group_expr: Vec<Expr>, | ||
schema: &DFSchemaRef, | ||
) -> Result<Vec<Expr>> { | ||
// Names of the fields produced by the GROUP BY exprs for example, `GROUP BY | ||
// c1 + 1` produces an output field named `"c1 + 1"` | ||
let mut group_by_field_names = group_expr | ||
.iter() | ||
.map(|e| e.display_name()) | ||
.collect::<Result<Vec<_>>>()?; | ||
|
||
if let Some(target_indices) = | ||
get_target_functional_dependencies(schema, &group_by_field_names) | ||
{ | ||
for idx in target_indices { | ||
let field = schema.field(idx); | ||
let expr = | ||
Expr::Column(Column::new(field.qualifier().cloned(), field.name())); | ||
let expr_name = expr.display_name()?; | ||
if !group_by_field_names.contains(&expr_name) { | ||
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 change -- rather than comparing the |
||
group_by_field_names.push(expr_name); | ||
group_expr.push(expr); | ||
} | ||
} | ||
} | ||
Ok(group_expr) | ||
} | ||
/// Errors if one or more expressions have equal names. | ||
pub(crate) fn validate_unique_names<'a>( | ||
node_name: &str, | ||
|
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 pulled the logic into its own function so I could document it better