Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -465,12 +465,13 @@ class Analyzer(

// Find aggregate expressions and evaluate them early, since they can't be evaluated in a
// Sort.
val (withAggsRemoved, aliasedAggregateList) = newOrdering.map {
case aggOrdering if aggOrdering.collect { case a: AggregateExpression => a }.nonEmpty =>
val aliased = Alias(aggOrdering.child, "_aggOrdering")()
val (withAggsRemoved, aliasedAggregateList) = newOrdering.zipWithIndex.map {
case (aggOrdering, idx)
if aggOrdering.collect { case a: AggregateExpression => a }.nonEmpty =>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This causes exception with ambiguous references for _aggOrdering

val aliased = Alias(aggOrdering.child, s"${aggOrdering.toString}_$idx")()
(aggOrdering.copy(child = aliased.toAttribute), Some(aliased))

case other => (other, None)
case (other, _) => (other, None)
}.unzip

val missing = missingAttr ++ aliasedAggregateList.flatten
Expand Down
18 changes: 18 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 @@ -23,6 +23,7 @@ import org.apache.spark.AccumulatorSuite
import org.apache.spark.sql.catalyst.analysis.FunctionRegistry
import org.apache.spark.sql.catalyst.DefaultParserDialect
import org.apache.spark.sql.catalyst.errors.DialectException
import org.apache.spark.sql.catalyst.expressions.Literal
import org.apache.spark.sql.execution.aggregate
import org.apache.spark.sql.functions._
import org.apache.spark.sql.test.SharedSQLContext
Expand Down Expand Up @@ -1625,4 +1626,21 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
checkAnswer(sql("select count(num) from 1one"), Row(10))
}
}

test("SPARK-10044: resolving reference for sorting with aggregation") {
withTempTable("mytable") {
sqlContext.sparkContext.parallelize(1 to 10).map(i => (i, i.toString))
.toDF("key", "value")
.registerTempTable("mytable")
checkAnswer(sql(
"""select max(value) as _aggOrdering_0 from mytable group by key % 2
|order by max(concat(value,",", key)), min(substr(value, 0, 4))
|""".stripMargin), Row("8") :: Row("9") :: Nil)

checkAnswer(
sqlContext.table("mytable").groupBy($"key" % 2).agg(max($"value"))
.orderBy(max(concat($"value", lit(","), $"key")), min(substring($"value", 0, 4))),
Row(0, "8") :: Row(1, "9") :: Nil)
}
}
}