File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed
main/scala/org/apache/spark/sql/catalyst/analysis
test/scala/org/apache/spark/sql/catalyst/analysis Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -514,6 +514,13 @@ class Analyzer(
514514 } else {
515515 a.copy(aggregateExpressions = buildExpandedProjectList(a.aggregateExpressions, a.child))
516516 }
517+
518+ // When resolve grouping expressions in Aggregate, we need to consider aliases in
519+ // aggregationExprs; e.g. SELECT 1 a GROUP BY a
520+ case a @ Aggregate (ge, ae, child) if child.resolved && ae.forall(_.resolved) && ! a.resolved =>
521+ val newGroupingExprs = ge.map(expandGroupingExpr(_, ae))
522+ a.copy(groupingExpressions = newGroupingExprs)
523+
517524 // If the script transformation input contains Stars, expand it.
518525 case t : ScriptTransformation if containsStar(t.input) =>
519526 t.copy(
@@ -633,6 +640,17 @@ class Analyzer(
633640 failAnalysis(s " Invalid usage of '*' in expression ' ${o.prettyName}' " )
634641 }
635642 }
643+
644+ /**
645+ * Expands the matching attribute in aggregation expression aliases.
646+ */
647+ private def expandGroupingExpr (expr : Expression , aggregationExprs : Seq [NamedExpression ]) =
648+ if (! expr.resolved && expr.isInstanceOf [UnresolvedAttribute ]) {
649+ val name = expr.asInstanceOf [UnresolvedAttribute ].name
650+ aggregationExprs.filter(x => x.resolved && x.name == name).headOption.getOrElse(expr)
651+ } else {
652+ expr
653+ }
636654 }
637655
638656 private def containsDeserializer (exprs : Seq [Expression ]): Boolean = {
Original file line number Diff line number Diff line change @@ -346,4 +346,10 @@ class AnalysisSuite extends AnalysisTest {
346346
347347 assertAnalysisSuccess(query)
348348 }
349+
350+ test(" SPARK-15020: GROUP-BY should support Aliases" ) {
351+ val input = LocalRelation (' a .int)
352+ val query = input.groupBy(' x )(' a .as(' x ))
353+ assertAnalysisSuccess(query)
354+ }
349355}
You can’t perform that action at this time.
0 commit comments