HeartSaVioR opened a new pull request, #60279:
URL: https://github.com/apache/spark/pull/60279

   ### What changes were proposed in this pull request?
   
   This PR fixes when a stream-stream join removes rows from its state and 
which watermark it forwards downstream.
   
   **Stack.** This is PR 1 of 7. Each PR fixes a separate bug in stream-stream 
join state eviction and builds on the one before it. This PR has no dependency, 
so all of it is up for review. Please merge in this order:
   
   1. (PR to follow) 
[SPARK-61344](https://issues.apache.org/jira/browse/SPARK-61344): Fix 
stream-stream join range eviction column and output watermark (this PR)
   2. (PR to follow) 
[SPARK-61345](https://issues.apache.org/jira/browse/SPARK-61345): Fix 
stream-stream join watermark direction and equality-key eviction
   3. (PR to follow) 
[SPARK-61346](https://issues.apache.org/jira/browse/SPARK-61346): Derive 
stream-stream join range eviction from the late-filtering event time
   4. (PR to follow) 
[SPARK-61347](https://issues.apache.org/jira/browse/SPARK-61347): Evict 
stream-stream join range state without a watermark on the stored side
   5. (PR to follow) 
[SPARK-61348](https://issues.apache.org/jira/browse/SPARK-61348): Reject 
numeric state bounds in stream-stream outer joins
   6. (PR to follow) 
[SPARK-61349](https://issues.apache.org/jira/browse/SPARK-61349): Ignore 
constant-only bounds in stream-stream join state eviction
   7. (PR to follow) 
[SPARK-61350](https://issues.apache.org/jira/browse/SPARK-61350): Reject 
negative watermark terms in stream-stream join eviction bounds
   
   - **Range eviction on the bounded column.** Attach each range-eviction 
cutoff to the timestamp column that the join condition actually bounds, and use 
that column for value eviction and for the V4 timestamp index. The input's 
event-time column is tried first. If it has no bound, the first other timestamp 
column in schema order with a bound is used. The V4 matching scan applies its 
event-time window only when both indexes store event-time columns. As in V2 and 
V3, a row whose indexed timestamp is null is never removed by timestamp 
eviction. V4 stores such a row at the very start of the timestamp range, which 
only a real event time in the first millisecond of ISO year -290308 could share.
   - **Conservative output watermark.** Derive each side's output bound from 
its actual eviction predicate. A side contributes its cutoff only when the 
predicate constrains its own event-time column or event-time key. Otherwise it 
contributes 0 and holds the output watermark back, because buffered rows can 
still emit older event times: the bound is on a different column, there is no 
bound, or the equality keys are crossed.
   - **Checkpoint validation.** Add the internal conf 
`spark.sql.streaming.join.checkpointWatermarkSemanticsVersion` (version 1) and 
pin it in the offset log. An older checkpoint is accepted only for joins 
without watermarks or with an unambiguous aligned event-time equality key. 
Other shapes, including range joins, fail with the new error condition 
`STREAMING_JOIN_CHECKPOINT_INCOMPATIBLE` before any state is opened. The 
version only validates compatibility; it does not select a legacy execution 
path. Later PRs in this stack raise it when they change which rows are kept.
   - **Docs.** The Structured Streaming migration guide explains the rejection 
and how to recover. The join guide notes that the join condition and the 
watermarks decide which column is used to remove rows.
   
   ### Why are the changes needed?
   
   - For `rightTime BETWEEN leftTime AND expiry`, a left row can still match 
after `leftTime` passes the watermark. Applying the `expiry` cutoff to 
`leftTime` removes the row too early, loses later matches, and can emit a 
premature outer null row.
   - An inner join on a non-time key can keep a left row at time 5 while the 
input watermark advances to 20, and then match a right row at time 100. 
Forwarding watermark 20 makes a downstream deduplication drop the valid output 
at time 5.
   - For `la = ra AND lb = rb` with watermarks on `la` and `rb`, a buffered 
right row `(100, 5)` can match a new left row after watermark 40. The join 
emits the match, but a downstream window aggregate has already advanced past it 
and drops it.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. These are corrections relative to all released versions.
   
   - Range eviction and unmatched outer output are corrected in state formats 
V2, V3 and V4.
   - Join output that carries old event times now reaches downstream stateful 
operators. Those operators may keep more state and finalize later while the 
join keeps old event times without an eviction bound.
   - Older checkpoints of affected join shapes fail with 
`STREAMING_JOIN_CHECKPOINT_INCOMPATIBLE`, even if the query never produced 
incorrect results. The migration guide describes the recovery: start a new 
checkpoint, replay the required input, and account for previously written 
output.
   
   ### How was this patch tested?
   
   All commands below passed on this branch, rebased on Apache Spark master: 
958 tests, 0 failures.
   
   ```sh
   build/sbt 'catalyst/testOnly *StreamingJoinHelperSuite 
*UnsupportedOperationsSuite'  # 236
   build/sbt 'sql/testOnly *StreamingJoinEqualityOutputWatermarkSuite 
*StreamingJoinOutputWatermarkSuite *StreamingJoinRangeEvictionSuite 
*StreamingSymmetricHashJoinHelperSuite *MultiStatefulOperatorsSuite 
*OffsetSeqLogSuite *SymmetricHashJoinStateManagerSuite 
*SymmetricHashJoinStateManagerEventTimeInKeySuite'  # 145
   build/sbt 'sql/testOnly *StreamingJoinCheckpointCompatibilitySuite 
*StreamingJoinRangeEvictionCheckpointSuite'  # 169
   build/sbt 'sql/testOnly *StreamingInnerJoinV4Suite 
*StreamingOuterJoinV4Suite *StreamingFullOuterJoinV4Suite 
*StreamingLeftSemiJoinV4Suite *StreamingInnerJoinWithVCFSuite 
*StreamingOuterJoinWithVCFSuite *StreamingFullOuterJoinWithVCFSuite 
*StreamingLeftSemiJoinWithVCFSuite'  # 367
   build/sbt 'hive/testOnly *SparkConfigBindingPolicySuite'  # 3
   build/sbt 'core/testOnly *SparkThrowableSuite'  # 38
   ```
   
   Scalastyle for catalyst and sql, main and test sources: 0 errors, 0 warnings.
   
   The new regression tests failed before their fixes: the delayed range-expiry 
match, the downstream deduplication of an old match, and the downstream window 
aggregate after crossed keys. The V4 null indexed-timestamp tests also failed 
before their fix: V4 removed the null row (2 rows evicted instead of 1), and a 
null window struct threw a `NullPointerException`. The collision test failed 
before its fix: evicting real rows stamped in the first millisecond after the 
epoch also removed the null-timestamp row.
   
   Both bundled historical join checkpoints (Spark 2.4.0 and 3.5.1) are range 
joins, which are now rejected, so they cover rejection instead of resuming. No 
historical equality-join checkpoint is bundled.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: OpenAI Codex (GPT-6) and Anthropic 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]

Reply via email to