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 @@ -1002,12 +1002,11 @@ object EliminateSorts extends Rule[LogicalPlan] {

private def isOrderIrrelevantAggs(aggs: Seq[NamedExpression]): Boolean = {
def isOrderIrrelevantAggFunction(func: AggregateFunction): Boolean = func match {
case _: Sum => true
case _: Min => true
case _: Max => true
case _: Count => true
case _: Average => true
case _: CentralMomentAgg => true
case _: Min | _: Max | _: Count => true
// Arithmetic operations for floating-point values are order-sensitive
// (they are not associative).
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for adding more description.

case _: Sum | _: Average | _: CentralMomentAgg =>
!Seq(FloatType, DoubleType).exists(_.sameType(func.children.head.dataType))
case _ => false
}

Expand Down
17 changes: 17 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,23 @@ class SubquerySuite extends QueryTest with SharedSparkSession {
}
}

test("Cannot remove sort for floating-point order-sensitive aggregates from subquery") {
Seq("float", "double").foreach { typeName =>
Seq("SUM", "AVG", "KURTOSIS", "SKEWNESS", "STDDEV_POP", "STDDEV_SAMP",
"VAR_POP", "VAR_SAMP").foreach { aggName =>
val query =
s"""
|SELECT k, $aggName(v) FROM (
| SELECT k, v
| FROM VALUES (1, $typeName(2.0)), (2, $typeName(1.0)) t(k, v)
| ORDER BY v)
|GROUP BY k
""".stripMargin
assert(getNumSortsInQuery(query) == 1)
}
}
}

test("SPARK-25482: Forbid pushdown to datasources of filters containing subqueries") {
withTempView("t1", "t2") {
sql("create temporary view t1(a int) using parquet")
Expand Down