viirya commented on code in PR #60699:
URL: https://github.com/apache/spark/pull/60699#discussion_r4656008246


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/CostBasedJoinReorder.scala:
##########
@@ -166,21 +203,47 @@ object JoinReorderDP extends PredicateHelper with Logging 
{
     // Build filters from the join graph to be used by the search algorithm.
     val filters = JoinReorderDPFilters.buildJoinGraphInfo(conf, items, 
conditions, itemIndex)

Review Comment:
   The star filter still sees all `conditions`, while the search joins only by 
`joinConds`. With `spark.sql.cbo.joinReorder.dp.star.filter` on, a dimension 
linked only by an immovable equality can be put in the star, and the filter can 
then block every way to complete it, so `search` falls back to `None`. 
Computing `splitMovable` before this line and passing `joinConds` would keep 
them consistent. It only affects plan quality, and the flag is off by default.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/CostBasedJoinReorder.scala:
##########
@@ -64,38 +64,75 @@ object CostBasedJoinReorder extends Rule[LogicalPlan] with 
PredicateHelper {
           items.forall(_.stats.rowCount.isDefined)) {
         JoinReorderDP.search(conf, items, conditions, output)
       } else {
-        plan
+        Some(plan)
       }
-    // Set consecutive join nodes ordered.
-    replaceWithOrderedJoin(result)
+    result match {

Review Comment:
   Nit: the two filter cases could go away if `replaceWithOrderedJoin` handled 
`Filter(_, j: Join)` when `isTop`, and its project case passed through such a 
filter.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/CostBasedJoinReorder.scala:
##########
@@ -64,38 +64,75 @@ object CostBasedJoinReorder extends Rule[LogicalPlan] with 
PredicateHelper {
           items.forall(_.stats.rowCount.isDefined)) {
         JoinReorderDP.search(conf, items, conditions, output)
       } else {
-        plan
+        Some(plan)
       }
-    // Set consecutive join nodes ordered.
-    replaceWithOrderedJoin(result)
+    result match {
+      // Set consecutive join nodes ordered, also below the filter that 
`JoinReorderDP.search` may
+      // put above them.
+      case Some(p @ Project(_, f @ Filter(_, j: Join))) =>
+        p.copy(child = f.copy(child = replaceWithOrderedJoin(j)))
+      case Some(f @ Filter(_, j: Join)) =>
+        f.copy(child = replaceWithOrderedJoin(j))
+      case Some(reordered) =>
+        replaceWithOrderedJoin(reordered)
+      // The conditions that may join two plans do not connect all items. Only 
the top join is set
+      // ordered, so that the joins of each of its sides are reordered by 
themselves later.
+      case None =>
+        setTopJoinOrdered(plan)
+    }
+  }
+
+  private def setTopJoinOrdered(plan: LogicalPlan): LogicalPlan = plan match {
+    case p @ Project(_, j: Join) => p.copy(child = setTopJoinOrdered(j))
+    case Join(left, right, joinType, condition, _) => OrderedJoin(left, right, 
joinType, condition)
   }
 
   /**
    * Extracts items of consecutive inner joins and join conditions.
    * This method works for bushy trees and left/right deep trees.
+   * A join below the top one whose condition has a conjunct that may not move 
(see
+   * `splitMovable`) is an item, with its condition. It removes a row on which 
its condition is
+   * NULL, so no condition above it is evaluated on the row, but `NULL AND x` 
evaluates `x`.
    */
-  private def extractInnerJoins(plan: LogicalPlan): (Seq[LogicalPlan], 
ExpressionSet) = {
+  private def extractInnerJoins(
+      plan: LogicalPlan,
+      isTop: Boolean = true): (Seq[LogicalPlan], ExpressionSet) = {
     plan match {
+      case j: Join if !isTop && hasImmovableCondition(j) =>
+        (Seq(plan), ExpressionSet())
       case Join(left, right, _: InnerLike, Some(cond), JoinHint.NONE) =>
-        val (leftPlans, leftConditions) = extractInnerJoins(left)
-        val (rightPlans, rightConditions) = extractInnerJoins(right)
+        val (leftPlans, leftConditions) = extractInnerJoins(left, isTop = 
false)
+        val (rightPlans, rightConditions) = extractInnerJoins(right, isTop = 
false)
         (leftPlans ++ rightPlans, leftConditions ++ rightConditions ++
           splitConjunctivePredicates(cond))
       case Project(projectList, j @ Join(_, _, _: InnerLike, Some(cond), 
JoinHint.NONE))
         if projectList.forall(_.isInstanceOf[Attribute]) =>
-        extractInnerJoins(j)
+        extractInnerJoins(j, isTop)
       case _ =>
         (Seq(plan), ExpressionSet())
     }
   }
 
-  private def replaceWithOrderedJoin(plan: LogicalPlan): LogicalPlan = plan 
match {
+  /** Whether `join` is an inner join whose condition has a conjunct that may 
not move. */
+  private def hasImmovableCondition(join: Join): Boolean = join match {

Review Comment:
   This repeats the check in `ExtractFiltersAndInnerJoins.flattenBelow`. A 
shared helper in `PredicateHelper` would keep the two rules from drifting apart.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/CostBasedJoinReorder.scala:
##########
@@ -146,7 +183,7 @@ object JoinReorderDP extends PredicateHelper with Logging {
       conf: SQLConf,
       items: Seq[LogicalPlan],
       conditions: ExpressionSet,
-      output: Seq[Attribute]): LogicalPlan = {
+      output: Seq[Attribute]): Option[LogicalPlan] = {

Review Comment:
   Nit: could the Scaladoc say when this returns `None`? The class doc also 
still describes the result as only the joins.



-- 
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]

Reply via email to