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


##########
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:
   **Nit (P3):** This comment covers NULL handling on the key side only. 
`extractEventTimeFn` just above (lines 275-294), which `append` still calls at 
line 451 when no equality key carries the watermark, has no NULL check: a NULL 
window-struct watermark column, which `withWatermark` accepts for any struct 
with a timestamp `end` field, still throws `NullPointerException` at line 282. 
The PR description puts NULL value-side event times out of scope, which is 
fine, but nothing in the code says so, so a reader sees a guarded key-side 
extractor next to an unguarded value-side one and cannot tell a deliberate 
deferral from an oversight. A TODO at `extractEventTimeFn` naming the deferred 
case, with a JIRA if you file one, would match how this file marks SPARK-55729, 
SPARK-56536 and SPARK-55732.
   
   **Verification:**
   - **Inspection:** The value-side event-time extraction of the V4 
stream-stream join state manager has a TODO naming the deferred handling of 
NULL value-side event times, including the NullPointerException for a NULL 
window-struct watermark column, and no code behavior changes.



##########
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:
   **Non-blocking (P2):** With this guard a NULL watermarked key gets no event 
time, so `append` puts the row in a random bucket and skips its `TsWithKey` 
entry (lines 448-455). Watermark eviction reaches rows only through that index 
(`evictByTimestamp` and `evictAndReturnByTimestamp`, lines 611-648), and the 
operator emits outer-join rows only from what eviction returns, so on V4 such a 
row is now never emitted and never leaves state.
   
   On the base branch V4 reads a NULL timestamp key as time 0 and indexes it. 
On a side whose own input has no watermark on that key the eviction scan starts 
from the beginning (`StreamingSymmetricHashJoinHelper.scala:250-256`), as it 
does on every side in the query's first batch or with 
`spark.sql.streaming.statefulOperator.allowMultiple` off, so the first eviction 
removes the row and an outer join emits it null-padded. The PR description says 
the same ("evicts the row at the first eviction and a full outer join emits it 
at once"), and Spark 4.3.0 ships this same V4 state manager, so V4 queries on 
that release return these rows for NULL timestamp keys today. A NULL equality 
key can never match, and 
`docs/streaming/apis-on-dataframes-and-datasets.md:1303-1305` promises the 
outer NULL result once the watermark rules out matches, so this drops output 
that V4 currently gets right. For a NULL window key it turns the old 
`NullPointerException` into the same silent omission instead of the 
 correct row.
   
   Keeping these rows also has costs. They stay in state for the life of the 
query, so state grows with every NULL-key row. Every lookup for a NULL 
timestamp key becomes a prefix scan over all of that key's buckets instead of 
one point lookup at time 0. In a full outer join they keep pairing with later 
rows from the other side that have the same NULL key, through the join's 
existing binary match of identical key rows. The new test pins the omission on 
V4: it expects `Row(null, null, 1, null)` never to appear and still counts the 
row in state (`assertNumStateRows(total = 2, updated = 5)`).
   
   This only applies to state format V4 
(`spark.sql.streaming.join.stateFormatVersion=4`, an internal setting); the 
default V2 and V3 are unchanged. On the side whose own input carries the key 
watermark, once the first batch has passed, the base branch already kept these 
rows, as V2 and V3 do (SPARK-61382).
   
   **Recommended change:** Make extractEventTimeFnFromKey in 
SymmetricHashJoinStateManager.scala (lines 296-307) read a NULL watermarked key 
as event time 0, the value the base branch reads for a NULL timestamp key, so a 
NULL window key no longer throws; return append (lines 444-458) to the base 
branch form, in which every row of a join with an event time gets its index 
entry; and replace the comments at lines 296 and 446-447 with one saying that a 
NULL event-time key is read as time 0 and is removed by the first eviction that 
covers time 0. In the stream-stream join tests, change the new full outer join 
test so that on state format 4 it expects the null-padded row for the NULL key 
in the first batch and no remaining state for it, while on formats 2 and 3 it 
keeps the current expectation with a comment naming SPARK-61382, branching on 
the configured state format version as other tests in that file already do. Add 
the same NULL-key case for a left or right outer join whose preserved i
 nput has no watermark on that key, and for a NULL key on the watermarked side 
that arrives in the query's first batch.
   
   **Why this works:** With time 0 and an index entry, a NULL-key row falls 
inside every eviction scan that has no lower bound, so on a side whose own 
input has no watermark on the key the first eviction removes it and the outer 
join emits it null-padded, as the base branch and Spark 4.3.0 already do for 
timestamp keys; its state stays bounded, and a probe for a NULL key goes back 
to one point lookup at time 0. A non-null window whose end is NULL already 
takes this path, so every NULL form of the key is handled the same way.
   
   **Compatibility:** Rows whose watermarked key is not NULL keep their bucket, 
index entry, lookups, and eviction, and a non-null window key whose end is NULL 
keeps being read as time 0.
   
   **Risks:** On a side whose own input has the watermark on the key, NULL-key 
rows again get an index entry at time 0, as on the base branch; once the first 
batch has passed, with spark.sql.streaming.statefulOperator.allowMultiple at 
its default, that side's eviction scan starts above time 0, so those rows and 
their index entries stay in state, as the rows alone do with the current change 
and as V2 and V3 keep them, until SPARK-61382 fixes emission for every state 
format. V4 output for NULL watermarked keys again differs from V2 and V3, which 
keep never emitting those rows until SPARK-61382 is fixed; an existing 
checkpoint cannot switch formats, so the difference shows only between queries 
started on different formats. If this pull request merged without the repair 
and a release shipped it, NULL-key rows that release stores in random buckets 
without index entries would be neither evicted nor found by the time-0 point 
lookup after a later fix; making the repair in this pull request a
 voids that.
   
   **Constraints:** Keep the V4 state layout unchanged, with no new column 
family, schema, or encoder, so state written by the base branch, by Spark 
4.3.0, and by this change stays readable in both directions. Leave the V2 and 
V3 state managers and the join operator unchanged; emitting NULL-key rows on 
every format stays with SPARK-61382.
   
   **Exclusions:**
   - Emitting the null-padded row for NULL-key rows on V2 and V3, and on the V4 
side whose own input has the watermark on the key once the first batch has 
passed (with spark.sql.streaming.statefulOperator.allowMultiple at its 
default), stays with SPARK-61382, as the pull request description defers it.
   - Handling NULL value-side event times stays deferred, as the pull request 
description states; a separate comment marks that path.
   
   **Verification:**
   - **Regression:** On state format 4, a left, right, or full outer join whose 
watermark is on an equality key runs without error when a preserved-side row's 
window or timestamp join key is NULL, emits that row null-padded exactly once 
at the first eviction that covers time 0, and then holds no state for it; the 
cases include a side whose input has no watermark on that key and the 
watermarked side in the query's first batch.
   - **Compatibility:** On state formats 2 and 3 the same query keeps its 
current outcome, with the NULL-key row neither emitted nor evicted, and the 
test says this is the gap tracked as SPARK-61382 rather than intended behavior.
   - **Inspection:** The V4 key-side event-time extraction returns time 0 for a 
NULL watermarked key, and no branch in append sends a row of a join with an 
event time to a random bucket or skips its index entry.



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