spark-test commented on code in PR #60307:
URL: https://github.com/apache/spark/pull/60307#discussion_r4529610417
##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingJoinSuite.scala:
##########
@@ -2425,6 +2425,45 @@ 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(
Review Comment:
**Nit (P3):** Both rows equate event times, so the right side always gets a
key cutoff. A time-interval condition gives it a value cutoff instead, and
there the right row this fix stores is found and removed by different code:
format 4's range-scan probe and `evictByTimestamp` from
`prevStateValueWatermark`, or `evictByValueCondition` in formats 2 and 3. No
existing left semi test sends a right row that is above the late-events
watermark and at or below its value cutoff (the range-condition test never
does, and the format 4 lower-bound test is a left outer join), so a regression
on this path would drop these matches again without a failing test.
A row like this would cover it under formats 2, 3 and 4:
```scala
("a time interval",
"leftTime BETWEEN rightTime - INTERVAL 10 SECONDS AND rightTime + INTERVAL
10 SECONDS",
125, 130, 150, 4L, 2L)
```
With a late-events watermark of 100 s and an eviction watermark of 150 s,
the right cutoff is 140 s minus 1 ms, so the right row (1, 130) passes late
filtering but is stored only because of this fix, and the left row (1, 125)
matches it. By the review's reading of the code, the base branch emits nothing
in that batch, while with the fix it emits (1, 125) and leaves 4 state rows
after the second batch and 2 after the third.
**Verification:**
- **Regression:** The time-interval case fails on the base branch, which
drops the left row, and passes with the fix, asserting the emitted row and the
total and updated state rows after the batch under state formats 2, 3 and 4.
##########
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:
**Non-blocking (P2):** For a left semi join, this clause makes the right
side append every right row that passes late filtering, including rows that no
left row of this or a later batch can match. A left row passes late filtering
only above the late-events watermark, so a right row below what such a left row
can reach was correctly skipped before; now it is appended, counted in
`numRowsUpdated`, and deleted again by `removeOldState` at the end of the
batch. Output and committed state stay the same, but each such row costs a
state-store append and delete plus changelog or delta-file records, and in
formats 2 and 3 one more value for every same-key left probe.
Reading the code, cases include:
- Time-interval conditions with a positive minimum gap, such as `rightTime
BETWEEN leftTime + INTERVAL 1 MINUTE AND leftTime + INTERVAL 1 HOUR`. With both
watermarks at 100 s, the right cutoff is 160 s minus 1 ms, so a right row at
130 s is appended, although a left row matching it would need `leftTime` of at
most 70 s. With in-order data, nearly every right row falls in this band.
- Key conditions whose right input's late filter does not bound the key,
such as `leftTime = rightTime` with a watermark on the left input only: late
right rows at or below the late-events watermark are appended, although every
left row of the batch has a larger key.
- Equality shifted so that the right event time is later, written on the
left key as in the second new test case (`leftTime + INTERVAL 10 SECONDS =
rightTime`): with a late-events watermark of 100 s and an eviction watermark of
150 s, a right row at 105 s is appended, although it can only match `leftTime =
95 s`, which is late.
When the two watermarks are equal, as in every batch under
`spark.sql.streaming.statefulOperator.allowMultiple=false` and in default-mode
data batches that follow a no-data batch, every right row the batch evicts is
of this kind. Unshifted equality on event times, as in the first new test case,
gains no such rows. The cost has not been measured, and its size depends on the
event-time distribution.
**Recommended change:** For a left semi join, store a right row the batch
evicts only when it lies above the right side's cutoff computed from the
late-events watermark, instead of storing every such row. Derive that cutoff
with the helper that already derives the eviction cutoff
(StreamingSymmetricHashJoinHelper.getStateWatermarkPredicates), applied to
eventTimeWatermarkForLateEvents, build key and value predicates from it in the
right joiner the same way as the eviction predicates, and change the left semi
clause in AddingProcessedRowToStateCompletionIterator.completion to admit a row
only when it is not at or below that cutoff. Update the comment above the check
to say which evicted rows are kept and why the others can be skipped, and add a
TODO there noting that for equality whose shift is written on the left key,
such as `leftTime + INTERVAL 10 SECONDS = rightTime`, the key cutoff ignores
the shift, so right rows within the shift above the late-events watermark are
still stor
ed. Extend the stream-stream left semi join tests with a time-interval
condition whose right cutoff leads the watermark and with an equality condition
whose right input has no watermark, asserting output and state-row counts.
**Why this works:** A left row of the batch passes late filtering only above
the late-events watermark, so it can match only right rows above the cutoff
that the eviction derivation gives for that watermark; this is the same
guarantee eviction relies on, applied to the late-events watermark. Rows at or
below that cutoff are skipped as on the base branch, because no left row of
this or a later batch can match them, while every evicted row above it is still
stored for the batch's left rows and removed by the end-of-batch eviction. When
the two watermarks are equal the two cutoffs coincide and the check skips
exactly what the base branch skips; without a late-events watermark there is no
late filter and no such cutoff, so the row is stored as it is now. The cutoff
is only as tight as the eviction derivation makes it: a key cutoff compares the
right key with the watermark, so for equality whose shift is written on the
left key, such as `leftTime + INTERVAL 10 SECONDS = rightTime`, no
non-late right row reaches it, and the right rows within the shift above the
late-events watermark, which no left row of the batch can match either, stay
stored and evicted as with the PR's current change.
**Scope:** Narrow the left semi clause of the right side's add-to-state
check to rows above the right cutoff computed from the late-events watermark,
using the existing cutoff derivation, and cover both cutoff kinds in the left
semi join tests.
**Compatibility:** Every right row that a left row of the same batch can
match is still stored before the left input is processed, so the same-batch
matches this PR restores keep being emitted, and committed state after each
batch stays as on the base branch.
**Risks:** If the late-events cutoff were derived from a different watermark
or column than the late filter uses, the check could skip a right row a left
row of the batch can match and silently drop the output this PR restores;
deriving it with the eviction helper and testing both cutoff kinds guards
against that. Each right row a left semi batch evicts also evaluates the
late-events cutoff, a comparison against a constant, which is small next to the
state-store append it avoids; the change is confined to left semi joins and
needs no state or checkpoint migration to roll back.
**Constraints:** Derive the late-events cutoff from
eventTimeWatermarkForLateEvents, the watermark the late filter applies, not
from the format 4 lower-bound argument of getStateWatermarkPredicates, which is
None under allowMultiple=false and in the first batch. Treat a missing
late-events watermark or a missing cutoff as no cutoff, so the row is stored as
it is now. Leave inner and outer joins, the left side's decision, eviction, and
format 4's eviction lower bound unchanged.
**Exclusions:**
- The cost of storing and evicting the right rows a left row of the batch
can match stays, because restoring the dropped output needs it.
- Format 4's eviction lower bound and its full index scans for these
condition shapes are not changed.
- Right rows within the shift above the late-events watermark for equality
whose shift is written on the left key, such as `leftTime + INTERVAL 10 SECONDS
= rightTime` in the PR's second test case: the key cutoff compares the right
key with the watermark and ignores the shift, so in batches whose late-events
watermark is below the eviction watermark these rows, which no left row of the
batch can match, stay stored and evicted in the same batch. Removing them needs
a bound that accounts for the shift between the two sides' keys, which the
eviction derivation does not provide, so the TODO at the check names them
instead.
**Success:** A stream-stream left semi join still emits a left row whose
matching right row arrives in the same micro-batch between the late-events and
eviction watermarks, for key and time-interval conditions, under state formats
2, 3 and 4. A right row at or below the right cutoff computed from the
late-events watermark is not written to state, so a batch whose two watermarks
are equal writes the same right rows as the base branch. Output and committed
state are the same as with the PR's current change for every input, and
numRowsUpdated and numRowsRemoved no longer count right rows at or below the
right cutoff computed from the late-events watermark. Inner and outer joins and
the left side's add-to-state decision behave as before.
**Verification:**
- **Behavior:** A left semi join with a time-interval condition whose right
cutoff leads the watermark, such as `rightTime BETWEEN leftTime + INTERVAL 1
MINUTE AND leftTime + INTERVAL 1 HOUR`, still emits a left row whose matching
right row arrives in the same batch above the right cutoff computed from the
late-events watermark and at or below the eviction cutoff, under state formats
2, 3 and 4.
- **Regression:** In that query, a right row that passes late filtering but
lies at or below the right cutoff computed from the late-events watermark, such
as 130 s when that watermark is 100 s, is not counted as an updated or removed
state row, both in a batch whose two watermarks differ and in a batch whose two
watermarks are equal, and the output matches the base branch.
- **Regression:** A left semi join that equates event times with a watermark
on the left input only does not store a late 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.
- **Compatibility:** Under
spark.sql.streaming.statefulOperator.allowMultiple=false a left semi join
writes the same right rows as the base branch and returns the same output, and
the existing inner, outer and left semi join tests 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]