HeartSaVioR opened a new pull request, #60704:
URL: https://github.com/apache/spark/pull/60704
### What changes were proposed in this pull request?
A stream-stream join row whose watermarked equality key is NULL can never
match, because SQL
`=` never matches NULL. This PR handles such a row like one that fails the
pre-join filter: an
outer join outputs it at once, padded with NULLs, and every join drops it
before it reaches the
state. The change adds `IsNotNull` on the watermarked key to the pre-join
filter of each side, for
every state format.
This fixes two problems:
- SPARK-61986: state format V4 fails with `NullPointerException` for a NULL
window key.
- SPARK-62046: a NULL-key row stays in state for good, and an outer join
never outputs it.
### Why are the changes needed?
**1. V4 fails the query (SPARK-61986).** With
`spark.sql.streaming.join.stateFormatVersion=4`:
```scala
val left = leftInput.toDF().toDF("lk", "t")
.select($"lk", timestamp_seconds($"t").as("lt"))
.withWatermark("lt", "10 seconds")
.select($"lk", window($"lt", "10 seconds").as("lw"))
val rightTime = when($"s" >= 0, timestamp_seconds($"s"))
val right = rightInput.toDF().toDF("rk", "s") // no watermark; rw is NULL
for a negative s
.select($"rk", when($"s" >= 0, struct(rightTime.as("start"),
(rightTime + expr("INTERVAL 10 SECONDS")).as("end"))).as("rw"))
left.join(right, expr("lk = rk AND lw = rw"), "full_outer")
.select($"lk", $"lw.end".cast("long"), $"rk", $"rw.end".cast("long"))
// one batch: left (1, 5); right (1, -1) and (1, 0)
```
- Before: the query fails with `NullPointerException` when V4 reads the
event time of the NULL
window key of the right row `(1, -1)`
(`SymmetricHashJoinStateManagerV4.getJoinedRows`).
- After: `(1, 10, 1, 10)` and `(null, null, 1, null)` in that batch.
**2. The row is never output (SPARK-62046).** On every state format, a row
with a NULL
watermarked key passes the late-row filter, but eviction never removes it:
V2 and V3 never
evict it, because their eviction predicate is NULL for it, and V4 evicts it
only in the first
micro-batch. For example:
```scala
val windowed = leftInput.toDF().toDF("lk", "t")
.select($"lk", timestamp_seconds($"t").as("lt"))
.withWatermark("lt", "10 seconds")
.select($"lk", window($"lt", "10 seconds").as("lw"))
val nulls = nullInput.toDF().toDF("lk")
.select($"lk", lit(null).cast(windowed.schema("lw").dataType).as("lw"))
val right = rightInput.toDF().toDF("rk", "s")
.select($"rk", window(timestamp_seconds($"s"), "10 seconds").as("rw"))
windowed.union(nulls)
.join(right, expr("lk = rk AND lw = rw"), "left_outer")
.select($"lk", $"lw.end".cast("long"), $"rk")
// batch 0: leftInput (1, 5); batch 1: nullInput 7;
// batch 2: leftInput (2, 100), so the watermark moves to 90 seconds; batch
3: no data
```
- Before: on V2 and V3, `(1, 10, null)` only. The row `(7, null, null)` is
never output and
stays in state. V4 fails as in 1; with a timestamp key instead of the
window, V4 also never
outputs the row.
- After: `(7, null, null)` in batch 1, then `(1, 10, null)`. No state is
kept for the NULL row.
Also, in a full outer join a NULL watermarked key on each input no longer
joins as
`(7, null, 7, null)`; each row comes out padded with NULLs.
### Does this PR introduce _any_ user-facing change?
Yes. Stream-stream joins output rows whose watermarked equality key is NULL
in the micro-batch
they arrive in (outer joins, padded with NULLs) and keep no state for them.
State format V4 no
longer fails for a NULL window key. No state layout change. Such rows
already in a checkpoint's
state stay there, as before.
### How was this patch tested?
New tests in `StreamingJoinSuite`, on state formats V2, V3 and V4:
- `StreamingFullOuterJoinBase`: a full outer join with a NULL window key and
a NULL timestamp key
on the input without a watermark, as in example 1.
- `StreamingOuterJoinBase`: inner, left outer and full outer joins, window
and timestamp keys,
with the NULL keys in the first or a later micro-batch, with constraint
propagation off so that
no inferred `IS NOT NULL` filter removes them first. They check the output
of each batch, and
that no NULL-key row stays in state.
Without the fix, all of them fail: V4 with the `NullPointerException` for
window keys, the others
with missing or extra rows, or NULL-key rows left in state.
Out of scope: NULLs in equality keys that carry no watermark (SPARK-61382),
and NULL value-side
event times in V4 (SPARK-62022, #60701).
### Note for reviewers, including AI review
Please review only this PR's own commit(s). For each finding, give a
runnable reproducer and
say whether master has the same behavior.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
This pull request and its description were written by Isaac.
--
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]