Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-10437][SQL] Support aggregation expressions in Order By #8599

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to set up a stop condition for this rule, or something like SELECT a, SUM(b) FROM t GROUP BY a ORDER BY a will go through this rule again and again until reach the fixed point. How about changing the end of this rule to:

if (evaluatedOrderings == sortOrder) {
  sort
} else {
  Project(aggregate.output,
    Sort(evaluatedOrderings, global,
      aggregate.copy(aggregateExpressions = originalAggExprs ++ needsPushDown)))
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've updated it.


// Try resolving the ordering as though it is in the aggregate clause.
try {
Expand Down
13 changes: 13 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 @@ -1519,6 +1519,19 @@ 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)

Seq("1" -> 3, "2" -> 7, "2" -> 8, "3" -> 5, "3" -> 6, "3" -> 2, "4" -> 1, "4" -> 2,
"4" -> 3, "4" -> 4).toDF("a", "b").registerTempTable("orderByData2")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add orderByData2? I thought orderByData can also reproduce this issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For orderByData the group by result would be ("4", 2), ("3", 2), ("2", 2), ("1", 2), so order by count(*) would be ambiguous.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about SELECT count(*) FROM orderByData GROUP BY a ORDER BY count(*)? The result would be (2), (2), (2), (2).

Actually we don't need to test the correctness here, only need to make sure count(*) works in ORDER BY.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I think it works.


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

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