Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -411,14 +411,26 @@ abstract class Optimizer(catalogManager: CatalogManager)
}

/**
* Remove useless DISTINCT for MAX and MIN.
* Remove useless DISTINCT:
* 1. For some aggregate expression, e.g.: MAX and MIN.
* 2. If the distinct semantics is guaranteed by child.
*
* This rule should be applied before RewriteDistinctAggregates.
*/
object EliminateDistinct extends Rule[LogicalPlan] {
override def apply(plan: LogicalPlan): LogicalPlan = plan.transformAllExpressionsWithPruning(
_.containsPattern(AGGREGATE_EXPRESSION)) {
case ae: AggregateExpression if ae.isDistinct && isDuplicateAgnostic(ae.aggregateFunction) =>
ae.copy(isDistinct = false)
override def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning(
_.containsPattern(AGGREGATE)) {
case agg: Aggregate =>
agg.transformExpressionsWithPruning(_.containsPattern(AGGREGATE_EXPRESSION)) {
case ae: AggregateExpression if ae.isDistinct &&
isDuplicateAgnostic(ae.aggregateFunction) =>
ae.copy(isDistinct = false)

case ae: AggregateExpression if ae.isDistinct &&
agg.child.distinctKeys.exists(
_.subsetOf(ExpressionSet(ae.aggregateFunction.children.filterNot(_.foldable)))) =>
Copy link
Contributor

@sigmod sigmod Apr 12, 2022

Choose a reason for hiding this comment

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

Is it correct?

If input plan to this rule is:

SELECT a, count(distinct c) FROM (
   SELECT distinct a, b, c 
   FROM t
)
GROUP BY a

Will the added case branch rewrite the plan to

SELECT a, count(c) FROM (
   SELECT distinct a, b, c 
   FROM t
)
GROUP BY a

agg.child.distinctKeys is {a, b, c}
ExpressionSet(ae.aggregateFunction.children.filterNot(_.foldable)) is {c}.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the distinctKeys of distinct a, b, c is ExpressionSet(a, b, c) not ExpressionSet(a), ExpressionSet(b), ExpressionSet(c)

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right. I forgot distinctKeys is a set of sets.

How about:

agg.child.distinctKeys.exists(
           key =>  !key.isEmpty() && 
                         key.subsetOf(ExpressionSet(ae.aggregateFunction.children.filterNot(_.foldable))))

Alternatively, we can do a require here to make sure that we never return an empty key:
https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/LogicalPlanDistinctKeys.scala#L32

Copy link
Contributor Author

Choose a reason for hiding this comment

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

make sense, I add a require at LogicalPlanDistinctKeys

ae.copy(isDistinct = false)
}
}

def isDuplicateAgnostic(af: AggregateFunction): Boolean = af match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class EliminateDistinctSuite extends PlanTest {
}

val testRelation = LocalRelation($"a".int)
val testRelation2 = LocalRelation($"a".int, $"b".string)

Seq(
Max(_),
Expand Down Expand Up @@ -71,4 +72,21 @@ class EliminateDistinctSuite extends PlanTest {
comparePlans(Optimize.execute(query), answer)
}
}

test("SPARK-38832: Remove unnecessary distinct in aggregate expression by distinctKeys") {
val q1 = testRelation2.groupBy($"a")($"a")
.rebalance().groupBy()(countDistinct($"a") as "x", sumDistinct($"a") as "y").analyze
val r1 = testRelation2.groupBy($"a")($"a")
.rebalance().groupBy()(count($"a") as "x", sum($"a") as "y").analyze
comparePlans(Optimize.execute(q1), r1)

// not a subset of distinct attr
val q2 = testRelation2.groupBy($"a", $"b")($"a", $"b")
.rebalance().groupBy()(countDistinct($"a") as "x", sumDistinct($"a") as "y").analyze
comparePlans(Optimize.execute(q2), q2)

// child distinct key is empty
val q3 = testRelation2.groupBy($"a")(countDistinct($"a") as "x").analyze
comparePlans(Optimize.execute(q3), q3)
}
}