Skip to content

Commit 452cfb5

Browse files
committed
Support aggregation expressions in Order By.
1 parent cf42138 commit 452cfb5

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ class Analyzer(
561561
}
562562

563563
case sort @ Sort(sortOrder, global, aggregate: Aggregate)
564-
if aggregate.resolved && !sort.resolved =>
564+
if aggregate.resolved =>
565565

566566
// Try resolving the ordering as though it is in the aggregate clause.
567567
try {

sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,19 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
15191519
|ORDER BY sum(b) + 1
15201520
""".stripMargin),
15211521
Row("4", 3) :: Row("1", 7) :: Row("3", 11) :: Row("2", 15) :: Nil)
1522+
1523+
Seq("1" -> 3, "2" -> 7, "2" -> 8, "3" -> 5, "3" -> 6, "3" -> 2, "4" -> 1, "4" -> 2,
1524+
"4" -> 3, "4" -> 4).toDF("a", "b").registerTempTable("orderByData2")
1525+
1526+
checkAnswer(
1527+
sql(
1528+
"""
1529+
|SELECT a, count(*)
1530+
|FROM orderByData2
1531+
|GROUP BY a
1532+
|ORDER BY count(*)
1533+
""".stripMargin),
1534+
Row("1", 1) :: Row("2", 2) :: Row("3", 3) :: Row("4", 4) :: Nil)
15221535
}
15231536

15241537
test("SPARK-7952: fix the equality check between boolean and numeric types") {

0 commit comments

Comments
 (0)