singhpk234 commented on a change in pull request #36005: URL: https://github.com/apache/spark/pull/36005#discussion_r838096257
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PushPartialAggregationThroughJoin.scala ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.catalyst.optimizer + +import org.apache.spark.sql.catalyst.expressions._ +import org.apache.spark.sql.catalyst.planning.ExtractEquiJoinKeys +import org.apache.spark.sql.catalyst.plans.Inner +import org.apache.spark.sql.catalyst.plans.logical._ +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.trees.TreePattern._ + +/** + * Push down the partial aggregation through join if it cannot be planned as broadcast hash join. + */ +object PushPartialAggregationThroughJoin extends Rule[LogicalPlan] + with PredicateHelper + with JoinSelectionHelper { + + private def isSupportPushdown(aggregateExpressions: Seq[NamedExpression]): Boolean = { + aggregateExpressions.forall { + case _: Attribute => true + case Alias(_: Attribute, _) => true + case Alias(_: Literal, _) => true + case _ => false + } + } + + private def split(expressions: Seq[NamedExpression], left: LogicalPlan, right: LogicalPlan) = { + val (leftExpressions, rest) = + expressions.partition(e => e.references.nonEmpty && e.references.subsetOf(left.outputSet)) + val (rightExpressions, remainingExpressions) = + rest.partition(e => e.references.nonEmpty && e.references.subsetOf(right.outputSet)) + + (leftExpressions, rightExpressions, remainingExpressions) + } + + private def pushdown( + agg: Aggregate, + leftKeys: Seq[Expression], + rightKeys: Seq[Expression], + join: Join): LogicalPlan = { + val (leftGroupExps, rightGroupExps, remainingGroupExps) = + split(agg.groupingExpressions.map(_.asInstanceOf[NamedExpression]), join.left, join.right) + + val (leftAggExps, rightAggExps, remainingAggExps) = + split(agg.aggregateExpressions, join.left, join.right) + + val newLeftGroup = leftGroupExps ++ leftKeys.flatMap(_.references) + val newLeftAgg: Seq[NamedExpression] = newLeftGroup ++ leftAggExps.flatMap(_.references) + + val newRightGroup = rightGroupExps ++ rightKeys.flatMap(_.references) + val newRightAgg: Seq[NamedExpression] = newRightGroup ++ rightAggExps.flatMap(_.references) + + val newJoin = join.copy( + left = Aggregate(newLeftGroup.distinct, newLeftAgg.distinct, true, join.left), + right = Aggregate(newRightGroup.distinct, newRightAgg.distinct, true, join.right)) + + if (agg.isPartialOnly) { + Project(agg.aggregateExpressions, newJoin) + } else { + agg.copy(child = newJoin) + } + } + + def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning( + _.containsAllPatterns(AGGREGATE, JOIN), ruleId) { + case agg @ Aggregate(_, _, _, join: Join) + if join.left.isInstanceOf[Aggregate] || join.right.isInstanceOf[Aggregate] => + agg + case agg @ Aggregate(_, _, _, Project(_, join: Join)) + if join.left.isInstanceOf[Aggregate] || join.right.isInstanceOf[Aggregate] => + agg + + case agg @ Aggregate(_, aggExprs, _, + join @ ExtractEquiJoinKeys(Inner, leftKeys, rightKeys, None, _, left, right, _)) + if isSupportPushdown(aggExprs) && (leftKeys ++ rightKeys).nonEmpty && + !canPlanAsBroadcastHashJoin(join, conf) => + pushdown(agg, leftKeys, rightKeys, join) + + case agg @ Aggregate(_, aggExprs, _, Project(projectList, + join @ ExtractEquiJoinKeys(Inner, leftKeys, rightKeys, None, _, left, right, _))) Review comment: [question] can we extend this to LeftSemi / LeftAnti as well, as we can very well have a parent agg on non-join key, then in this case we could push partialAgg of aggExpr, left join-key down in the left side of join to reduce our join input. As per my understanding it might be possible, Am I am missing something here ? ########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala ########## @@ -88,6 +88,7 @@ abstract class Optimizer(catalogManager: CatalogManager) LimitPushDownThroughWindow, ColumnPruning, GenerateOptimization, + PushPartialAggregationThroughJoin, Review comment: should we do this post Collpase project rule ? As we could miss this case : Agg -> Proj -> Proj -> Join (inner / non-broadcastable) -- 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]
