spark-test commented on code in PR #60665:
URL: https://github.com/apache/spark/pull/60665#discussion_r4619435145
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/SymmetricHashJoinStateManager.scala:
##########
@@ -293,10 +293,14 @@ class SymmetricHashJoinStateManagerV4(
}
}
+ // A NULL event-time key is read as time 0, as a NULL timestamp key always
was, so the first
+ // eviction that covers time 0 removes the row.
private val extractEventTimeFnFromKey: UnsafeRow => Option[Long] = { row =>
joinKeyOrdinalForWatermark.map { idx =>
val attr = keyAttributes(idx)
- if (attr.dataType.isInstanceOf[StructType]) {
+ if (row.isNullAt(idx)) {
+ 0L
Review Comment:
**Non-blocking (P2):** Reading a NULL window key as time 0 fixes the input
without its own watermark on the key, which the new test covers, but not the
input whose own watermark column is the key. There, from the second micro-batch
on (with `spark.sql.streaming.statefulOperator.allowMultiple` at its default),
`getStateWatermarkPredicates` passes the previous late-event watermark as
`prevStateWatermark` (`StreamingSymmetricHashJoinHelper.scala:250-256`,
`IncrementalExecution.scala:606-618`), and `scanEvictedKeys` starts at that
bound plus one (`SymmetricHashJoinStateManager.scala:935-948`), never at 0. The
late-input filter keeps the row, because `lw.end <= watermark` is NULL for it
(`statefulOperators.scala:564-572`). So a NULL-window row admitted after the
first micro-batch stays at time 0 for good: a left, right or full outer join
that preserves that input never emits it null-padded, and its two state entries
stay for the life of the query. On master the same row fails the query
with the `NullPointerException`, so this turns a loud failure into silently
missing output and growing state; NULL timestamp keys already behave this way
on master.
A NULL window can reach that input through, for example, a streaming union
or the null-padded side of an upstream outer join, both of which keep the
watermark metadata. For instance, from reading the code (not run), on state
format 4 with `spark.sql.streaming.noDataMicroBatches.enabled=false`:
```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 nullWindows = 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(nullWindows)
.join(right, expr("lk = rk AND lw = rw"), "left_outer")
.select($"lk", $"lw.end".cast("long"), $"rk")
```
Batch 0 adds `(1, 5)` to `leftInput`, batch 1 adds `7` to `nullInput`, batch
2 adds `(2, 100)`, which moves the watermark to 90 seconds, and batch 3 adds
nothing. Batch 3's left eviction scans only (0, 90 s], so it emits `(1, 10,
null)` but never `(7, null, null)`. On master the batch-1 probe throws instead.
This extends existing discussions
[1](https://github.com/apache/spark/pull/60665#issuecomment-6986163118),
[2](https://github.com/apache/spark/pull/60665#discussion_r4615003979).
**Recommended change:** In SymmetricHashJoinStateManagerV4, when
evictByTimestamp or evictAndReturnByTimestamp gets a lower bound and the event
time comes from the watermarked join key, also evict the entries stored at time
0, where a NULL watermarked key, or a window key whose end is NULL, is filed,
besides the range above the bound. Update the lower-bound documentation on
SupportsEvictByTimestamp and JoinStateKeyWatermarkPredicate and the comment at
SymmetricHashJoinStateManager.scala:296-297 to say so, and add stream-stream
join tests on state format 4 with a NULL window key and a NULL timestamp key on
the input whose own watermark is the key, arriving after the first micro-batch,
for a left outer and a full outer join.
**Why this works:** A row at time 0 is always at or below the current
eviction watermark, and after the first micro-batch the only rows admitted
there are ones whose watermarked key is NULL or is a window with a NULL end,
because the late-input filter drops every other row at or below the previous
watermark. Evicting that one slot on every eviction therefore removes exactly
the rows the lower bound would skip, so the outer join emits the unmatched ones
null-padded once, while the rest of the range keeps the narrowed scan.
**Compatibility:** Rows whose watermarked key carries an event time, a
non-NULL timestamp or a window with a non-NULL end, keep their eviction
micro-batch and output, the narrowed scan from the lower bound stays for the
rest of the range, and NULL keys on an input without its own key watermark keep
being evicted and emitted at the first eviction, as the PR's new test asserts.
**Risks:** Each eviction with a lower bound on state format 4 key-watermark
state does one more seek, over the time-0 slot of the secondary index, on every
partition and micro-batch, including queries that never see a NULL key; after
NULL-key rows are evicted their tombstones stay in that slot until RocksDB
compaction, so the seek passes over them. The cost is fixed per eviction, and a
query can avoid it only by starting on another state format. Queries started on
Spark 4.3.0 or the base branch that kept NULL timestamp-key rows at time 0 on
the input whose own watermark is the key evict those rows at their first
eviction after upgrading and emit the unmatched ones null-padded, which is the
output those versions should have produced. On the input whose own watermark is
the key, rows whose window key has a NULL end, which state format 4 also reads
as time 0, are evicted at the next eviction too instead of staying in state for
good as on the base branch, so a row from the other input
with the same window that arrives in a later micro-batch no longer joins with
them; the input without its own key watermark already evicts such rows at its
first eviction.
**Constraints:** Keep the state format 4 layout, schemas and encoders
unchanged, so checkpoints from the base branch and Spark 4.3.0 restart with the
fix and stay readable by code without it. Apply the extra eviction only to
state whose event time comes from the watermarked join key, leaving value-side
event times, deferred as SPARK-62022, as they are. Visit the time-0 slot
exactly once per eviction call that has a lower bound: skip the extra pass when
the bounded scan already covers time 0, and keep it when the early return for a
Long.MaxValue bound skips the bounded scan, so no row is evicted or emitted
twice. Keep the narrowed scan from the lower bound for the rest of the range.
**Exclusions:**
- Emitting NULL-key rows of outer joins on state formats 2 and 3 stays with
SPARK-61382, as the PR description defers it.
- Handling NULL value-side event times stays deferred as SPARK-62022.
- Stopping NULL window keys from the two inputs of a full outer join from
matching each other is a separate fix in the key lookups.
**Verification:**
- **Regression:** On state format 4, a left outer join and a full outer join
keyed on a window column whose watermark is on the left input, with a NULL
window added on the left through a streaming union after the first micro-batch,
each emit that row null-padded exactly once when the next eviction runs and
then hold no state for it; the same holds for a NULL timestamp key on a
timestamp-keyed join whose watermark is on that key.
- **Behavior:** On state format 4, in a left outer join whose left input
carries the watermark on its struct-typed join key, a left row that arrives
after the first micro-batch with a non-NULL key whose end field is NULL is
evicted at the next eviction, emitted null-padded when no right row matched it,
and leaves no state behind.
- **Inspection:** The documented contract for the eviction lower bound, and
the comment on how state format 4 reads a NULL watermarked key, say that rows
stored at time 0 are evicted even when the eviction scan has a lower bound.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/SymmetricHashJoinStateManager.scala:
##########
@@ -293,10 +293,14 @@ class SymmetricHashJoinStateManagerV4(
}
}
+ // A NULL event-time key is read as time 0, as a NULL timestamp key always
was, so the first
+ // eviction that covers time 0 removes the row.
private val extractEventTimeFnFromKey: UnsafeRow => Option[Long] = { row =>
joinKeyOrdinalForWatermark.map { idx =>
val attr = keyAttributes(idx)
- if (attr.dataType.isInstanceOf[StructType]) {
+ if (row.isNullAt(idx)) {
Review Comment:
**Non-blocking (P2):** With this check a NULL window key no longer throws,
but `getJoinedRows` then reads the other input's state at `(key, 0)`
(`SymmetricHashJoinStateManager.scala:508-510`), and two key rows whose
watermarked field is NULL and whose other fields are equal have identical
bytes, so they match. Nothing on the stream-stream join path skips a NULL key,
and a full outer join gets no inferred `IsNotNull` filter, so when the input
that carries the key watermark can produce a NULL window and the other input
has a NULL key too, V4 emits a NULL = NULL joined row instead of two
null-padded rows. That contradicts the PR description's statement that a NULL
key cannot match, and because such rows on the watermarked input are also never
evicted after the first micro-batch, each later NULL-key row from the other
input joins with all of them. On master the probe throws the
`NullPointerException`; V4 timestamp keys and V2/V3 already match NULL keys
this way there.
For example, from reading the code (not run), on state format 4, with the
right input from the new test:
```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 nullWindows = nullInput.toDF().toDF("lk")
.select($"lk", lit(null).cast(windowed.schema("lw").dataType).as("lw"))
val rightTime = when($"s" >= 0, timestamp_seconds($"s"))
val right = rightInput.toDF().toDF("rk", "s")
.select($"rk", when($"s" >= 0, struct(rightTime.as("start"),
(rightTime + expr("INTERVAL 10 SECONDS")).as("end"))).as("rw"))
windowed.union(nullWindows)
.join(right, expr("lk = rk AND lw = rw"), "full_outer")
.select($"lk", $"lw.end".cast("long"), $"rk", $"rw.end".cast("long"))
```
Adding `7` to `nullInput` and `(7, -1)` to `rightInput` in one micro-batch
emits `(7, null, 7, null)`, while a batch full outer join returns `(7, null,
null, null)` and `(null, null, 7, null)`.
This extends an [existing
discussion](https://github.com/apache/spark/pull/60665#discussion_r4615003979).
**Verification:**
- **Regression:** On state format 4, a full outer join keyed on a window
column whose watermark is on one input, with a NULL window key on each input in
the same micro-batch and the watermarked input's NULL window supplied through a
streaming union, emits both rows null-padded and no joined row for them; the
same holds for NULL timestamp keys on a timestamp-keyed join.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/SymmetricHashJoinStateManager.scala:
##########
@@ -293,8 +293,9 @@ class SymmetricHashJoinStateManagerV4(
}
}
+ // A NULL event-time key has no event time.
private val extractEventTimeFnFromKey: UnsafeRow => Option[Long] = { row =>
- joinKeyOrdinalForWatermark.map { idx =>
+ joinKeyOrdinalForWatermark.filterNot(row.isNullAt).map { idx =>
Review Comment:
Thanks, the new commit resolves the issue I raised about NULL watermarked
keys losing their index entry: such a key is now read as time 0 and keeps its
entry, and the new test shows V4 emitting the NULL-key row from the input
without a watermark at the first eviction. One correction to the risk note in
my earlier comment on the NULL-key guard: it said NULL-key rows on the input
whose own watermark is the join key stay in state as on the base branch, which
holds for timestamp keys, but for window keys the base branch fails with the
NullPointerException instead. This review raises two remaining issues for NULL
window keys on that input: after the first micro-batch they are never evicted
or emitted, and in a full outer join they match NULL keys from the other input.
<!-- SPARK_DEV_REVIEW_REPLY
{"feedback_id":"inline:4615003979","thread_id":"inline:4615003979","verdict_sha256":"7d96b5a3a93a441f3a8f9188750272ed3fa8ff3601dd228bd0e8bc4ef9ddd6f1"}
-->
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/SymmetricHashJoinStateManager.scala:
##########
@@ -293,8 +293,9 @@ class SymmetricHashJoinStateManagerV4(
}
}
+ // A NULL event-time key has no event time.
Review Comment:
Thanks for checking and for filing SPARK-62022; keeping the value-side fix
out of this PR is fine. The TODO I suggested would not widen the change: a
one-line comment at `extractEventTimeFn` naming SPARK-62022 lets readers tell
that its unguarded window-struct read is a known gap rather than an oversight,
now that the key-side extractor has a NULL check. Could you add it?
<!-- SPARK_DEV_REVIEW_REPLY
{"feedback_id":"inline:4615003990","thread_id":"inline:4615003990","verdict_sha256":"7d96b5a3a93a441f3a8f9188750272ed3fa8ff3601dd228bd0e8bc4ef9ddd6f1"}
-->
--
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]