spark-test commented on code in PR #60307:
URL: https://github.com/apache/spark/pull/60307#discussion_r4531790798


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -910,6 +912,8 @@ case class StreamingSymmetricHashJoinExec(
             !stateKeyWatermarkPredicateFunc(key) && 
!stateValueWatermarkPredicateFunc(thisRow)
 
           isNotEvictingInThisBatch ||
+            // if the left input of this batch still has to probe the input
+            joinType == LeftSemi ||

Review Comment:
   Agreed. At a late-events watermark of 100 s, the cutoff derivation applies 
the `rightExpiry` bound to `rightTime` and gives `rightTime <= 129.999 s`, so 
the narrowing I proposed would skip right (1, 110) and drop its match with left 
(1, 120) again; the new test pins that case. This review separately raises 
that, where the cutoff at the late-events watermark is exact, storing every 
evicted right row slows down joins whose results were already correct on the 
base branch. Whether to accept the remaining cost for conditions without an 
exact cutoff is left open in the Decision challenges section.
   
   <!-- SPARK_DEV_REVIEW_REPLY 
{"feedback_id":"inline:4529610402","thread_id":"inline:4529610402","verdict_sha256":"30fe454a3cdb6ea5245ca25338611eccc2328a15829d595ebe09222b730a6881"}
 -->



##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingJoinSuite.scala:
##########
@@ -2425,6 +2425,82 @@ abstract class StreamingLeftSemiJoinBase extends 
StreamingJoinSuite {
     }
   }
 
+  // A left semi join processes the right input before the left input. A right 
row at or below
+  // its eviction cutoff passes late filtering when it is above the 
late-events watermark, and
+  // the left rows of the same batch must still find it. Each case runs a 
batch whose
+  // late-events watermark is 100 seconds and whose eviction watermark is 
`watermark` seconds.
+  // (name, condition, left time, right time, watermark, state rows after the 
second batch and
+  // after the third)
+  for ((name, condition, leftTime, rightTime, watermark, rowsBefore, 
rowsAfter) <- Seq(
+      ("equal event times", "leftTime = rightTime", 102, 102, 105, 2L, 0L),
+      // The left key leftTime + 10 seconds of (7, 100) stays above the 
100-second cutoff.
+      ("a key shifted by less than the watermark step",
+        "leftTime + INTERVAL 10 SECONDS = rightTime", 120, 130, 150, 3L, 1L),
+      // A value cutoff of the watermark less 10 seconds: (1, 130) is at or 
below 140 seconds.

Review Comment:
   **Nit (P3):** For `leftTime BETWEEN rightTime - INTERVAL 10 SECONDS AND 
rightTime + INTERVAL 10 SECONDS`, the right cutoff comes from the inclusive 
bound `leftTime <= rightTime + INTERVAL 10 SECONDS`, which 
`StreamingJoinHelper.getStateValueWatermark` lowers by 1 ms. At the 150-second 
eviction watermark the right predicate is therefore `rightTime <= 139.999 s`, 
not 140 s: a right row at exactly 140 s stays in state, while this comment says 
it is evicted. The `rightExpiry` comment below counts the 1 ms ("130 seconds 
less 1 ms"). The test still passes, because 130 s is below both values.
   
   ```suggestion
         // A value cutoff of the watermark less 10 seconds and 1 ms: (1, 130) 
is below 139.999 s.
   ```
   
   **Verification:**
   - **Inspection:** The comment for the time-interval case states the same 
right cutoff that the join's state-watermark derivation computes for its 
condition at a 150-second eviction watermark, 139.999 seconds, and no longer 
describes the cutoff as 140 seconds.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -910,6 +917,8 @@ case class StreamingSymmetricHashJoinExec(
             !stateKeyWatermarkPredicateFunc(key) && 
!stateValueWatermarkPredicateFunc(thisRow)
 
           isNotEvictingInThisBatch ||
+            // if the left input of this batch still has to probe the input
+            joinType == LeftSemi ||

Review Comment:
   **Blocking (P1):** The `rightExpiry` counterexample in the earlier thread 
here shows that the cutoff the derivation gives at the late-events watermark 
cannot decide which evicted rows to skip in general. For some conditions, 
though, that cutoff is exact: no left row that passes late filtering can match 
an evicted right row at or below it, and on inputs whose results were already 
correct on the base branch, storing such rows only adds work.
   
   Reading the code: with 
`spark.sql.streaming.statefulOperator.allowMultiple=false` both watermarks are 
the same W. For `leftKey = rightKey AND rightTime BETWEEN leftTime + INTERVAL 1 
MINUTE AND leftTime + INTERVAL 1 HOUR`, the right cutoff is W + 60 s less 1 ms, 
so with W = 100 s a right row at 130 s passes late filtering and is evicted in 
the same batch. A left row matching it would need `leftTime` of at most 70 s, 
which is late. The base branch skipped that row and returned the same output; 
this clause appends it and `removeOldState` deletes it again. With in-order 
data and batches spanning less than a minute of event time, that is nearly 
every right row, so right-side state writes, deletes and changelog or 
delta-file records per batch go from about zero to the size of the right input. 
The same happens in the default mode for in-order data when the minimum gap 
exceeds the watermark delay plus the event-time advance of a batch, and for 
`leftTime = rightTime` with a watermark only 
 on `leftTime`, where right rows at or below the late-events watermark are 
stored although every left row that passes late filtering has a later key.
   
   In these cases the cutoff is exact, because the bounding conjunct constrains 
`rightTime` itself with a fixed-length interval and no cast, or the watermark 
is the left input's equality key. Skipping evicted right rows at or below it 
therefore cannot drop a match. The `rightExpiry` condition, whose cutoff comes 
from another right column, has no such cutoff and keeps storing every evicted 
row.
   
   This extends existing discussions 
[1](https://github.com/apache/spark/pull/60307#issuecomment-6779932033), 
[2](https://github.com/apache/spark/pull/60307#discussion_r4529610402).
   
   **Recommended change:** For a stream-stream left semi join, store a right 
row that the batch evicts only when no cutoff at the late-events watermark that 
is exact today shows that every left row passing late filtering misses it, and 
skip the row otherwise. Derive that cutoff in two cases: a key cutoff, when the 
left input's late-filtered watermark column is the left join key at the 
watermark ordinal, comparing the right key at that ordinal with the late-events 
watermark; and a value cutoff on the right event-time column, solved at the 
late-events watermark only from conjuncts that bound that column itself by the 
left input's late-filtered watermark column plus a fixed-length offset, with no 
cast and no interval that has a day or month component. In every other case, 
keep storing every evicted right row, and keep the TODO at the check naming 
SPARK-61344 and SPARK-61381 for those conditions.
   
   **Why this works:** A left row passes late filtering only when its watermark 
column is above the late-events watermark. If that column is the left key at 
the watermark ordinal, an equality match needs the same right key, so a right 
row whose key is at or below the late-events watermark matches no such left 
row. If a conjunct requires the right event time to be at least that column 
plus a fixed-length offset, solving it at the late-events watermark gives a 
cutoff below which no left row passing late filtering satisfies it, and every 
conjunct must hold for a match. A bound on another right column, an interval 
with a day component, or a cast can make the derived value too high, so such 
conjuncts contribute no cutoff and the row is stored as the PR does now. The 
skip applies only to rows the batch evicts, so a row that stays in state is 
stored as before, and under allowMultiple=false every evicted row of such a 
condition is skipped, as on the base branch.
   
   **Scope:** Skip storing an evicted right row of a stream-stream left semi 
join when a cutoff at the late-events watermark that is exact today shows no 
left row passing late filtering can match it.
   
   **Risks:** If the exactness test admitted a conjunct whose derived value is 
above the true bound, such as one on another right column, with a day interval, 
or through a cast, the check would skip a right row that a left row of the same 
batch matches and silently drop output again; testing the rightExpiry, 
shifted-key and day-interval conditions alongside the exact ones guards against 
that. Conditions without an exact cutoff keep the extra state work until 
SPARK-61344 and SPARK-61381, including on inputs whose results were already 
correct on the base branch, such as a day-based time interval under 
allowMultiple=false. Each right row a left semi batch evicts evaluates one more 
comparison with a constant, which is small next to the state-store append and 
removal it avoids; the change is confined to left semi joins and needs no state 
or checkpoint migration to roll back.
   
   **Constraints:** Derive the cutoff from eventTimeWatermarkForLateEvents, the 
watermark the left late filter applies, not from the format 4 lower-bound 
argument of getStateWatermarkPredicates, which is None under 
allowMultiple=false and in the first batch. Accept a bound only when its left 
term is the column the left late filter compares with the late-events 
watermark; under allowMultiple=false a left input can carry several event-time 
columns, and only the first is late-filtered. Leave 
StreamingJoinHelper.getStateValueWatermark and its other callers, eviction, the 
format 4 range-scan offsets and UnsupportedOperationChecker, unchanged. Leave 
inner and outer joins, the left side's add-to-state decision, eviction, and 
format 4's eviction lower bound unchanged.
   
   **Exclusions:**
   - A cutoff that comes only from a bound on another right column, such as the 
rightExpiry condition of the new test, gives no exact cutoff on the right event 
time, so every evicted right row is still stored and the TODO at the check 
keeps naming SPARK-61344.
   - Intervals with a day or month component and casts in the bounding conjunct 
give no exact cutoff, so every evicted right row is still stored and the TODO 
at the check keeps naming SPARK-61381.
   - Equality keys shifted on the left side, such as `leftTime + INTERVAL 10 
SECONDS = rightTime`, give no key cutoff that accounts for the shift, so every 
evicted right row is still stored.
   
   **Success:** Under spark.sql.streaming.statefulOperator.allowMultiple=false, 
a stream-stream left semi join with a value cutoff from a fixed-length bound on 
the right event-time column, or a key cutoff on the left input's watermark key, 
writes the same right rows to state as the base branch and returns the same 
output. In the default mode, a right row at or below the exact cutoff at the 
late-events watermark is not written to state, while every evicted right row 
above it, or under a condition without an exact cutoff, is still stored for the 
batch's left rows and removed by the end-of-batch eviction. The four same-batch 
tests the PR adds keep their output and state-row assertions, so the 
rightExpiry and shifted-key matches are still emitted. Inner and outer joins, 
the left side's add-to-state decision, eviction and committed state behave as 
on the base branch.
   
   **Verification:**
   - **Behavior:** For left semi joins whose cutoff at the late-events 
watermark is exact, such as a time interval with a positive lower offset on the 
right event time and an equality on a watermarked left time key with an 
unfiltered right input, a run under 
spark.sql.streaming.statefulOperator.allowMultiple=false reports the same 
updated and removed right state rows and the same output as the base branch, in 
state formats 2, 3 and 4.
   - **Regression:** In the default mode, `rightTime BETWEEN leftTime + 
INTERVAL 1 MINUTE AND leftTime + INTERVAL 1 HOUR` still emits a left row whose 
same-batch matching right row lies above the exact cutoff at the late-events 
watermark and at or below the eviction cutoff, while a right row at or below 
the exact cutoff is not counted as an updated state row.
   - **Regression:** A left semi join on `leftTime = rightTime` with a 
watermark only on leftTime does not store a right row whose key is at or below 
the late-events watermark and still emits a same-batch match whose key lies 
between the two watermarks.
   - **Regression:** The PR's four same-batch tests pass unchanged, and a 
condition without an exact cutoff, such as a day-based time interval or the 
rightExpiry condition, still stores every evicted right row and emits its 
same-batch matches.
   - **Compatibility:** The existing inner, outer and left semi join suites 
keep their output and state-row counts.



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