Skip to content

Commit 841972e

Browse files
viiryamarmbrus
authored andcommitted
[SPARK-10437] [SQL] Support aggregation expressions in Order By
JIRA: https://issues.apache.org/jira/browse/SPARK-10437 If an expression in `SortOrder` is a resolved one, such as `count(1)`, the corresponding rule in `Analyzer` to make it work in order by will not be applied. Author: Liang-Chi Hsieh <[email protected]> Closes #8599 from viirya/orderby-agg.
1 parent b42059d commit 841972e

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
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 {
@@ -598,9 +598,15 @@ class Analyzer(
598598
}
599599
}
600600

601-
Project(aggregate.output,
602-
Sort(evaluatedOrderings, global,
603-
aggregate.copy(aggregateExpressions = originalAggExprs ++ needsPushDown)))
601+
// Since we don't rely on sort.resolved as the stop condition for this rule,
602+
// we need to check this and prevent applying this rule multiple times
603+
if (sortOrder == evaluatedOrderings) {
604+
sort
605+
} else {
606+
Project(aggregate.output,
607+
Sort(evaluatedOrderings, global,
608+
aggregate.copy(aggregateExpressions = originalAggExprs ++ needsPushDown)))
609+
}
604610
} catch {
605611
// Attempting to resolve in the aggregate can result in ambiguity. When this happens,
606612
// just return the original plan.

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,26 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
15621562
|ORDER BY sum(b) + 1
15631563
""".stripMargin),
15641564
Row("4", 3) :: Row("1", 7) :: Row("3", 11) :: Row("2", 15) :: Nil)
1565+
1566+
checkAnswer(
1567+
sql(
1568+
"""
1569+
|SELECT count(*)
1570+
|FROM orderByData
1571+
|GROUP BY a
1572+
|ORDER BY count(*)
1573+
""".stripMargin),
1574+
Row(2) :: Row(2) :: Row(2) :: Row(2) :: Nil)
1575+
1576+
checkAnswer(
1577+
sql(
1578+
"""
1579+
|SELECT a
1580+
|FROM orderByData
1581+
|GROUP BY a
1582+
|ORDER BY a, count(*), sum(b)
1583+
""".stripMargin),
1584+
Row("1") :: Row("2") :: Row("3") :: Row("4") :: Nil)
15651585
}
15661586

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

0 commit comments

Comments
 (0)