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 @@ -105,12 +105,22 @@ case class AggregateExpression(
}

// We compute the same thing regardless of our final result.
override lazy val canonicalized: Expression =
override lazy val canonicalized: Expression = {
val normalizedAggFunc = mode match {
// For PartialMerge or Final mode, the input to the `aggregateFunction` is aggregate buffers,
// and the actual children of `aggregateFunction` is not used, here we normalize the expr id.
case PartialMerge | Final => aggregateFunction.transform {
case a: AttributeReference => a.withExprId(ExprId(0))
}
case Partial | Complete => aggregateFunction
}

AggregateExpression(
aggregateFunction.canonicalized.asInstanceOf[AggregateFunction],
normalizedAggFunc.canonicalized.asInstanceOf[AggregateFunction],
mode,
isDistinct,
ExprId(0))
}

override def children: Seq[Expression] = aggregateFunction :: Nil
override def dataType: DataType = aggregateFunction.dataType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] extends TreeNode[PlanT

def recursiveTransform(arg: Any): AnyRef = arg match {
case e: Expression => transformExpression(e)
case Some(e: Expression) => Some(transformExpression(e))
case Some(value) => Some(recursiveTransform(value))
case m: Map[_, _] => m
case d: DataType => d // Avoid unpacking Structs
case seq: Traversable[_] => seq.map(recursiveTransform)
Expand Down Expand Up @@ -320,7 +320,7 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] extends TreeNode[PlanT

productIterator.flatMap {
case e: Expression => e :: Nil
case Some(e: Expression) => e :: Nil
case s: Some[_] => seqToExpressions(s.toSeq)
case seq: Traversable[_] => seqToExpressions(seq)
case other => Nil
}.toSeq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
package org.apache.spark.sql.execution

import org.apache.spark.sql.{DataFrame, QueryTest}
import org.apache.spark.sql.functions._
import org.apache.spark.sql.test.SharedSQLContext

/**
* Tests for the sameResult function for [[SparkPlan]]s.
*/
class SameResultSuite extends QueryTest with SharedSQLContext {
import testImplicits._

test("FileSourceScanExec: different orders of data filters and partition filters") {
withTempPath { path =>
Expand All @@ -46,4 +48,14 @@ class SameResultSuite extends QueryTest with SharedSQLContext {
df.queryExecution.sparkPlan.find(_.isInstanceOf[FileSourceScanExec]).get
.asInstanceOf[FileSourceScanExec]
}

test("SPARK-20725: partial aggregate should behave correctly for sameResult") {
val df1 = spark.range(10).agg(sum($"id"))
val df2 = spark.range(10).agg(sum($"id"))
Copy link
Member

Choose a reason for hiding this comment

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

    val df1 = spark.range(10).agg(sumDistinct($"id"))
    val df2 = spark.range(10).agg(sumDistinct($"id"))

They will not match?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! The reason is, HashAggregateExec.requiredChildDistributionExpressions is a Option[Seq[Expression]], which is not treated as expressions of HashAggregateExec, and thus not touched by QueryPlan.mapExpressions.

I have fixed it in QueryPlan

assert(df1.queryExecution.executedPlan.sameResult(df2.queryExecution.executedPlan))

val df3 = spark.range(10).agg(sumDistinct($"id"))
val df4 = spark.range(10).agg(sumDistinct($"id"))
assert(df3.queryExecution.executedPlan.sameResult(df4.queryExecution.executedPlan))
}
}