ulysses-you commented on code in PR #37129:
URL: https://github.com/apache/spark/pull/37129#discussion_r918867434


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PushLocalTopKThroughOuterJoin.scala:
##########
@@ -0,0 +1,118 @@
+/*
+ * 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.{Literal, SortOrder}
+import org.apache.spark.sql.catalyst.planning.{ExtractEquiJoinKeys, 
ExtractTopK}
+import org.apache.spark.sql.catalyst.plans.{JoinType, LeftOuter, RightOuter}
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, Join, LocalLimit, 
LogicalPlan, Project, RebalancePartitions, Repartition, 
RepartitionByExpression, Sort, Union}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{LIMIT, OUTER_JOIN, 
SORT}
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * This rule supports push down local limit and local sort from TopK through 
other join:
+ *   - for a left outer join, the references of ordering of TopK come from the 
left side and
+ *     the limits of TopK is smaller than left side max rows
+ *   - for a right outer join, the references of ordering of TopK come from 
the right side and
+ *     the limits of TopK is smaller than right side max rows
+ */
+object PushLocalTopKThroughOuterJoin extends Rule[LogicalPlan] {
+  private def smallThan(limits: Int, maxRowsOpt: Option[Long]): Boolean = 
maxRowsOpt match {
+    case Some(maxRows) => limits < maxRows
+    case _ => true
+  }
+
+  private def canPushThroughOuterJoin(
+      joinType: JoinType,
+      order: Seq[SortOrder],
+      leftChild: LogicalPlan,
+      rightChild: LogicalPlan,
+      limits: Int): Boolean = joinType match {
+    case LeftOuter =>
+      order.forall(_.references.subsetOf(leftChild.outputSet)) &&
+        smallThan(limits, leftChild.maxRowsPerPartition)
+    case RightOuter =>
+      order.forall(_.references.subsetOf(rightChild.outputSet)) &&
+        smallThan(limits, rightChild.maxRowsPerPartition)
+    case _ => false
+  }
+
+  private def findOuterJoin(limits: Int, order: Seq[SortOrder], child: 
LogicalPlan): Seq[Join] = {
+    child match {
+      case j @ ExtractEquiJoinKeys(joinType, _, _, _, _, leftChild, 
rightChild, _)
+          if canPushThroughOuterJoin(joinType, order, leftChild, rightChild, 
limits) =>
+        // we should find the bottom outer join which can push local topK
+        val childOuterJoin = joinType match {
+          case LeftOuter => findOuterJoin(limits, order, leftChild)
+          case RightOuter => findOuterJoin(limits, order, rightChild)
+          case _ => Seq.empty
+        }
+        if (childOuterJoin.nonEmpty) {
+          childOuterJoin
+        } else {
+          j :: Nil
+        }
+      case u: Union => u.children.flatMap(child => findOuterJoin(limits, 
order, child))
+      case p: Project if p.projectList.forall(_.deterministic) =>
+        findOuterJoin(limits, order, p.child)
+      case f: Filter if f.condition.deterministic =>
+        findOuterJoin(limits, order, f.child)

Review Comment:
   good catch.. some how I put filter here. removed it.
   
   But this rule still can push through filter thanks to the rule 
`PushPredicateThroughJoin`. I added some tests for this. 



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to