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 @@ -400,13 +400,14 @@ object RewriteDistinctAggregates extends Rule[LogicalPlan] {
(distinctAggOperatorMap.flatMap(_._2) ++
regularAggOperatorMap.map(e => (e._1, e._3))).toMap

val groupByMapNonFoldable = groupByMap.filter(!_._1.foldable)
val patchedAggExpressions = a.aggregateExpressions.map { e =>
e.transformDown {
case e: Expression =>
// The same GROUP BY clauses can have different forms (different names for instance) in
// the groupBy and aggregate expressions of an aggregate. This makes a map lookup
// tricky. So we do a linear search for a semantically equal group by expression.
groupByMap
groupByMapNonFoldable
.find(ge => e.semanticEquals(ge._1))
.map(_._2)
.getOrElse(transformations.getOrElse(e, e))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package org.apache.spark.sql.catalyst.optimizer

import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.expressions.Literal
import org.apache.spark.sql.catalyst.expressions.{Literal, Round}
import org.apache.spark.sql.catalyst.expressions.aggregate.CollectSet
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Expand, LocalRelation, LogicalPlan}
Expand Down Expand Up @@ -109,4 +109,20 @@ class RewriteDistinctAggregatesSuite extends PlanTest {
case _ => fail(s"Plan is not rewritten:\n$rewrite")
}
}

test("SPARK-49261: Literals in grouping expressions shouldn't result in unresolved aggregation") {
val relation = testRelation2
.select(Literal(6).as("gb"), $"a", $"b", $"c", $"d")
val input = relation
.groupBy($"a", $"gb")(
countDistinct($"b").as("agg1"),
countDistinct($"d").as("agg2"),
Round(sum($"c").as("sum1"), 6)).analyze
val rewriteFold = FoldablePropagation(input)
// without the fix, the below produces an unresolved plan
val rewrite = RewriteDistinctAggregates(rewriteFold)
if (!rewrite.resolved) {
fail(s"Plan is not as expected:\n$rewrite")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,27 @@ class DataFrameAggregateSuite extends QueryTest
})
}
}

test("SPARK-49261: Literals in grouping expressions shouldn't result in unresolved aggregation") {
val data = Seq((1, 1.001d, 2), (2, 3.001d, 4), (2, 3.001, 4)).toDF("a", "b", "c")
withTempView("v1") {
data.createOrReplaceTempView("v1")
val df =
sql("""SELECT
| ROUND(SUM(b), 6) AS sum1,
| COUNT(DISTINCT a) AS count1,
| COUNT(DISTINCT c) AS count2
|FROM (
| SELECT
| 6 AS gb,
| *
| FROM v1
|)
|GROUP BY a, gb
|""".stripMargin)
checkAnswer(df, Row(1.001d, 1, 1) :: Row(6.002d, 1, 1) :: Nil)
}
}
}

case class B(c: Option[Double])
Expand Down