@@ -567,32 +567,24 @@ object CollapseProject extends Rule[LogicalPlan] {
567567object CollapseRepartition extends Rule [LogicalPlan ] {
568568 def apply (plan : LogicalPlan ): LogicalPlan = plan transformUp {
569569 // Case 1: When a Repartition has a child of Repartition or RepartitionByExpression,
570- // we can collapse it with the child based on the type of shuffle and the specified number
571- // of partitions.
572- case r @ Repartition (_, _, child : RepartitionOperation ) =>
573- collapseRepartition(r, child)
574- // Case 2: When a RepartitionByExpression has a child of Repartition or RepartitionByExpression
575- // we can remove the child.
576- case r @ RepartitionByExpression (_, child : RepartitionOperation , _) =>
577- r.copy(child = child.child)
578- }
579-
580- /**
581- * Collapses the [[Repartition ]] with its child [[RepartitionOperation ]], if possible.
582- * - Case 1 the top [[Repartition ]] does not enable shuffle (i.e., coalesce API):
583- * If the last numPartitions is bigger, returns the child node; otherwise, keep unchanged.
584- * - Case 2 the top [[Repartition ]] enables shuffle (i.e., repartition API):
585- * returns the child node with the last numPartitions.
586- */
587- private def collapseRepartition (r : Repartition , child : RepartitionOperation ): LogicalPlan = {
588- (r.shuffle, child.shuffle) match {
570+ // 1) When the top node does not enable the shuffle (i.e., coalesce API), but the child
571+ // enables the shuffle. Returns the child node if the last numPartitions is bigger;
572+ // otherwise, keep unchanged.
573+ // 2) In the other cases, returns the child node with the last numPartitions.
574+ case r @ Repartition (_, _, child : RepartitionOperation ) => (r.shuffle, child.shuffle) match {
589575 case (false , true ) =>
590576 if (r.numPartitions >= child.numPartitions) child else r
591577 case _ => child match {
592- case child : Repartition => child.copy(numPartitions = r.numPartitions, shuffle = r.shuffle)
593- case child : RepartitionByExpression => child.copy(numPartitions = r.numPartitions)
578+ case child : Repartition =>
579+ child.copy(numPartitions = r.numPartitions, shuffle = r.shuffle)
580+ case child : RepartitionByExpression =>
581+ child.copy(numPartitions = r.numPartitions)
594582 }
595583 }
584+ // Case 2: When a RepartitionByExpression has a child of Repartition or RepartitionByExpression
585+ // we can remove the child.
586+ case r @ RepartitionByExpression (_, child : RepartitionOperation , _) =>
587+ r.copy(child = child.child)
596588 }
597589}
598590
0 commit comments