Skip to content

Commit

Permalink
[SPARK-10437] [SQL] Support aggregation expressions in Order By
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
viirya authored and marmbrus committed Sep 15, 2015
1 parent b42059d commit 841972e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ class Analyzer(
}

case sort @ Sort(sortOrder, global, aggregate: Aggregate)
if aggregate.resolved && !sort.resolved =>
if aggregate.resolved =>

// Try resolving the ordering as though it is in the aggregate clause.
try {
Expand Down Expand Up @@ -598,9 +598,15 @@ class Analyzer(
}
}

Project(aggregate.output,
Sort(evaluatedOrderings, global,
aggregate.copy(aggregateExpressions = originalAggExprs ++ needsPushDown)))
// Since we don't rely on sort.resolved as the stop condition for this rule,
// we need to check this and prevent applying this rule multiple times
if (sortOrder == evaluatedOrderings) {
sort
} else {
Project(aggregate.output,
Sort(evaluatedOrderings, global,
aggregate.copy(aggregateExpressions = originalAggExprs ++ needsPushDown)))
}
} catch {
// Attempting to resolve in the aggregate can result in ambiguity. When this happens,
// just return the original plan.
Expand Down
20 changes: 20 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,26 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
|ORDER BY sum(b) + 1
""".stripMargin),
Row("4", 3) :: Row("1", 7) :: Row("3", 11) :: Row("2", 15) :: Nil)

checkAnswer(
sql(
"""
|SELECT count(*)
|FROM orderByData
|GROUP BY a
|ORDER BY count(*)
""".stripMargin),
Row(2) :: Row(2) :: Row(2) :: Row(2) :: Nil)

checkAnswer(
sql(
"""
|SELECT a
|FROM orderByData
|GROUP BY a
|ORDER BY a, count(*), sum(b)
""".stripMargin),
Row("1") :: Row("2") :: Row("3") :: Row("4") :: Nil)
}

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

0 comments on commit 841972e

Please sign in to comment.