-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-12063][SQL] Use number in group by clause to refer to columns #10052
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 all commits
74c42ae
a179e6f
b4cfcbf
8a5a4f6
bd453d5
e4edc31
09b3f77
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 |
|---|---|---|
|
|
@@ -70,6 +70,7 @@ class Analyzer( | |
| Batch("Resolution", fixedPoint, | ||
| ResolveRelations :: | ||
| ResolveReferences :: | ||
| ResolveNumericReferences :: | ||
| ResolveGroupingAnalytics :: | ||
| ResolvePivot :: | ||
| ResolveSortReferences :: | ||
|
|
@@ -178,6 +179,46 @@ class Analyzer( | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Replaces queries of the form "SELECT expr FROM A GROUP BY 1 ORDER BY 1" | ||
| * with a query of the form "SELECT expr FROM A GROUP BY expr ORDER BY expr" | ||
| */ | ||
| object ResolveNumericReferences extends Rule[LogicalPlan] { | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators { | ||
| case Aggregate(groups, aggs, child) => | ||
| val newGroups = groups.map { | ||
| case group if group.isInstanceOf[Literal] && group.dataType.isInstanceOf[IntegralType] => | ||
| aggs(group.toString.toInt - 1) match { | ||
| case u: UnresolvedAlias => | ||
| u.child match { | ||
| case UnresolvedStar(_) => // Can't replace literal with column yet | ||
| group | ||
| case _ => u.child | ||
| } | ||
| case a: Alias => a.child | ||
| case a: AttributeReference => a | ||
| } | ||
| case group => group | ||
| } | ||
| Aggregate(newGroups, aggs, child) | ||
| case Sort(ordering, global, child) => | ||
| val newOrdering = ordering.map { | ||
| case o if o.child.isInstanceOf[Literal] && o.dataType.isInstanceOf[IntegralType] => | ||
|
Contributor
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. Same comment as above. |
||
| val newExpr = child.asInstanceOf[Project].projectList(o.child.toString.toInt - 1) | ||
|
Contributor
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. Tests are currently failing to due this cast. A |
||
| match { | ||
| case u: UnresolvedAlias => | ||
| u.child | ||
| case a: Alias => | ||
| a.child | ||
| } | ||
| SortOrder(newExpr, o.direction) | ||
| case other => other | ||
| } | ||
| Sort(newOrdering, global, child) | ||
| } | ||
| } | ||
|
|
||
| object ResolveGroupingAnalytics extends Rule[LogicalPlan] { | ||
| /* | ||
| * GROUP BY a, b, c WITH ROLLUP | ||
|
|
||
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.
case Literal(index: Int) =>is easier. It also eliminates the need forgroup.toString.toInt