ulysses-you commented on code in PR #36117:
URL: https://github.com/apache/spark/pull/36117#discussion_r848173398
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -411,14 +412,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))))
=>
Review Comment:
make sense, I add a require at `LogicalPlanDistinctKeys`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]