-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-20710][SQL] Support aliases in CUBE/ROLLUP/GROUPING SETS #17948
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1003,18 +1003,30 @@ class Analyzer( | |
| */ | ||
| object ResolveAggAliasInGroupBy extends Rule[LogicalPlan] { | ||
|
|
||
| // This is a strict check though, we put this to apply the rule only in alias expressions | ||
| private def notResolvableByChild(attrName: String, child: LogicalPlan): Boolean = | ||
| !child.output.exists(a => resolver(a.name, attrName)) | ||
|
Member
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. Nit: style private def notResolvableByChild(attrName: String, child: LogicalPlan): Boolean = {
!child.output.exists(a => resolver(a.name, attrName))
}
Member
Author
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. Thanks! Fixed. |
||
|
|
||
| override def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperators { | ||
| case agg @ Aggregate(groups, aggs, child) | ||
| if conf.groupByAliases && child.resolved && aggs.forall(_.resolved) && | ||
| groups.exists(_.isInstanceOf[UnresolvedAttribute]) => | ||
| // This is a strict check though, we put this to apply the rule only in alias expressions | ||
| def notResolvableByChild(attrName: String): Boolean = | ||
| !child.output.exists(a => resolver(a.name, attrName)) | ||
| agg.copy(groupingExpressions = groups.map { | ||
| case u: UnresolvedAttribute if notResolvableByChild(u.name) => | ||
| groups.exists(!_.resolved) => | ||
| agg.copy(groupingExpressions = groups.map { _.transform { | ||
| case u: UnresolvedAttribute if notResolvableByChild(u.name, child) => | ||
| aggs.find(ne => resolver(ne.name, u.name)).getOrElse(u) | ||
| } | ||
| }) | ||
|
|
||
| case gs @ GroupingSets(selectedGroups, groups, child, aggs) | ||
| if conf.groupByAliases && child.resolved && aggs.forall(_.resolved) && | ||
| (selectedGroups :+ groups).exists(_.exists(_.isInstanceOf[UnresolvedAttribute])) => | ||
|
||
| def mayResolveAttrByAggregateExprs(exprs: Seq[Expression]): Seq[Expression] = exprs.map { | ||
|
||
| case u: UnresolvedAttribute if notResolvableByChild(u.name, child) => | ||
| aggs.find(ne => resolver(ne.name, u.name)).getOrElse(u) | ||
| case e => e | ||
| }) | ||
| } | ||
| gs.copy(selectedGroupByExprs = selectedGroups.map(mayResolveAttrByAggregateExprs), | ||
| groupByExprs = mayResolveAttrByAggregateExprs(groups)) | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
... only if the expression is not resolvable by childThere 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.
ok