Skip to content

Commit 71249cf

Browse files
committed
[SPARK-15020][SQL] GROUP-BY should support Aliases
``` sql("select a x from values 1 T(a) group by x").explain org.apache.spark.sql.AnalysisException: cannot resolve '`x`' given input columns: [a]; line 1 pos 39 ```
1 parent d78fbcc commit 71249cf

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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 = {

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)