sarutak commented on code in PR #60725:
URL: https://github.com/apache/spark/pull/60725#discussion_r4638966708


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala:
##########
@@ -299,11 +299,20 @@ trait PredicateHelper extends AliasHelper with Logging {
    * inputs of the joins or of the filter below, that can fail (see
    * `failableOutput`). It would be evaluated on rows that the conjunct before 
it may have
    * dropped. The conjuncts that may not move are returned in their written 
order.
+   *
+   * If `afterBarrier` is true, the conjuncts are evaluated after a condition 
that they may not
+   * move ahead of, as if after a non-deterministic or throwable conjunct, so 
only the ones that
+   * can not fail may move.

Review Comment:
   Nit: "evaluated after a condition that they may not move ahead of" reads as 
if no conjunct moves ahead of that condition. But the ones that can not fail 
do: `PushPredicateThroughJoin` pushes them below the join, or puts them ahead 
of its condition (`newJoinConditions ++ joinCondition`). Maybe: "If 
`afterBarrier` is true, the conjuncts are treated as written after a 
non-deterministic or throwable condition that is not in `conditions`, such as a 
throwable one in the condition of the join below them, so only the ones that 
can not fail may move."
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -3428,8 +3428,13 @@ object PushPredicateThroughJoin extends 
Rule[LogicalPlan] with PredicateHelper {
     // push the where condition down into join filter
     case f @ Filter(filterCondition, Join(left, right, joinType, 
joinCondition, hint))
         if canPushThrough(joinType) =>
-      val (movable, stayUp) =
-        splitMovable(splitConjunctivePredicates(filterCondition), Seq(left, 
right))
+      // An inner join evaluates its condition before the filter, so the 
filter's conditions are
+      // written after it, as at the end of its ON clause, and a 
non-deterministic or throwable
+      // conjunct of it keeps the ones that can fail in place.
+      val afterBarrier = joinType.isInstanceOf[InnerLike] &&
+        joinCondition.exists(cond => !cond.deterministic || cond.throwable)

Review Comment:
   A left semi or a left anti join also evaluates its condition before the 
filter above it: it drops the left rows without a match (semi) or with one 
(anti). So the failure this PR fixes for inner joins remains there. With the 
tables of the description, both of these fail with `DIVIDE_BY_ZERO` with this 
PR, since the division is pushed into a filter on `tc1` below the join:
   
   ```sql
   -- It should return no row: the CASE WHEN is false, so the semi join outputs 
nothing.
   SELECT t1.k FROM tc1 t1 LEFT SEMI JOIN tc2 t2
     ON t1.k = t2.k AND CASE WHEN t1.f < 0 THEN raise_error('neg') ELSE t1.f > 
t2.g END
   WHERE 10 / (t1.v - 9) < 100;
   
   -- It should return no row: the CASE WHEN (5 < 9) is true, so the anti join 
drops the row.
   SELECT t1.k FROM tc1 t1 LEFT ANTI JOIN tc2 t2
     ON t1.k = t2.k AND CASE WHEN t1.f < 0 THEN raise_error('neg') ELSE t1.f < 
t2.g END
   WHERE 10 / (t1.v - 9) < 100;
   ```
   
   The left semi query with the division at the end of the `ON` clause returns 
no row before and after this PR. The other join types the rule handles (left 
and right outer, left single and existence joins) output every row of the side 
the conditions are pushed into, so they are fine as they are.
   
   Could we set `afterBarrier` for `LeftSemi | LeftAnti` too? For example, with 
a helper that the filters over as-of and lookup joins can also call (see the 
review body):
   
   ```scala
   private def hasBarrier(joinType: JoinType, condition: Option[Expression]): 
Boolean =
     (joinType match {
       case _: InnerLike | LeftSemi | LeftAnti => true
       case _ => false
     }) && condition.exists(cond => !cond.deterministic || cond.throwable)
   ```
   
   and `val afterBarrier = hasBarrier(joinType, joinCondition)` here. I tried 
this, with the helper also called in those two cases: both queries then return 
no row, and `FilterPushdownSuite`, 
`PushPredicateThroughExternalLookupJoinSuite`, `RewriteAsOfJoinSuite`, 
`ExternalLookupJoinSuite`, `AsOfJoinSQLSuite`, `AsOfJoinSortMergeSQLSuite`, 
`DataFrameAsOfJoinSuite`, `SortMergeAsOfJoinSuite` and `DataFrameSuite` still 
pass. The comment above it, and the title and the description, would then need 
to cover left semi and left anti joins too.
   
   The new tests only use inner joins. Could we add left semi and left anti 
cases, plus a `FilterPushdownSuite` plan case for a left outer join whose 
condition has the throwable conjunct, and ideally a right outer one, where a 
division over the preserved side is still pushed below the join? Without such a 
case, `FilterPushdownSuite` and `PushPredicateThroughExternalLookupJoinSuite` 
still pass when I remove `joinType.isInstanceOf[InnerLike] &&`. A `checkAnswer` 
case can not catch that, since keeping the division above an outer join returns 
the same rows.
   



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