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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2315,6 +2315,19 @@ 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 => inferNewFilter(j.left, allConstraints)
+        case LeftOuter => j.left
+        case other =>
+          throw SparkException.internalError(s"Unexpected as-of join type: 
$other")
+      }
+      j.copy(left = newLeft, right = inferNewFilter(j.right, allConstraints))

Review Comment:
   Thanks, good catch. Fixed in 3dd1097. The as-of case now drops inferred 
predicates that match `SubqueryExpression.hasInOrCorrelatedExistsSubquery`, for 
both inputs. Plain filters, IS NOT NULL on the ON keys, and scalar subquery 
comparisons are still inferred.
   
   I checked the plans. IN gave the sort-merge semi join plus a second sort, as 
you said. NOT IN was planned as a broadcast null-aware anti join by default, 
not a nested-loop join, but it still adds a join over every row of that input, 
so it is left out too. Added plan tests (IN, NOT IN, IN inside OR, IN kept in a 
LEFT ON, scalar subquery) and a SQL test that checks the rows and that no join 
appears below the other input.
   



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