LukaZdravic commented on code in PR #60403:
URL: https://github.com/apache/spark/pull/60403#discussion_r4578583873


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2315,6 +2321,22 @@ object InferFiltersFromConstraints extends 
Rule[LogicalPlan]
 
         case _ => join
       }
+
+    // A right row that fails the ON condition is never the match of any left 
row, so a filter
+    // that follows from the left constraints and the ON condition can drop it 
before the join.
+    // An inner join also drops the left rows without a match, so its left 
side gets one too.
+    case j: AsOfJoin =>
+      val allConstraints = getAllConstraints(j.left, j.right, j.condition)
+      val newLeft = j.joinType match {
+        case Inner => inferAsOfInputFilter(j.left, allConstraints)
+        case LeftOuter => j.left
+        case other =>
+          throw SparkException.internalError(s"Unexpected as-of join type: 
$other")
+      }
+      // With LeftOuter only the right input gets a filter, so a self join 
loses its shared
+      // exchange. Skipping the filter when both inputs match would keep it, 
but the filter drops
+      // right rows before the shuffle.
+      j.copy(left = newLeft, right = inferAsOfInputFilter(j.right, 
allConstraints))

Review Comment:
   Nice catch, thanks, confirmed with a run, now it's fixed. The right input 
now stays unfiltered when a right ON key, or any expression in the right input, 
is not known to be safe. The second part also covers a failable projection 
above a reused Window.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2264,14 +2264,18 @@ object InferFiltersFromGenerate extends 
Rule[LogicalPlan] {
  * Generate a list of additional filters from an operator's existing 
constraint but remove those
  * that are either already part of the operator's condition or are part of the 
operator's child
  * constraints. These filters are currently inserted to the existing 
conditions in the Filter
- * operators and on either side of Join operators.
+ * operators and on either side of Join and AsOfJoin operators.
  *
  * Note: While this optimization is applicable to a lot of types of join, it 
primarily benefits
  * Inner and LeftSemi joins.
  */
 object InferFiltersFromConstraints extends Rule[LogicalPlan]
   with PredicateHelper with ConstraintHelper {
 
+  // Marks the Filters the AsOfJoin case adds, so the Filter case adds no 
failable or subquery
+  // filter to them.

Review Comment:
   Done!



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