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


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -230,6 +233,53 @@ case class StreamingSymmetricHashJoinExec(
 
   private val useVirtualColumnFamilies = stateFormatVersion >= 3
 
+  // Only the V4 state manager reads these ordinals. They depend only on plan 
fields, so
+  // doExecute computes them once on the driver instead of in every task.
+  private lazy val stateKeyOrdinalsForLookup: (Option[Int], Option[Int]) =
+    if (stateFormatVersion >= 4) {
+      (getStateKeyOrdinalForLookup(
+        left.output, leftKeys, right.output, rightKeys, 
stateWatermarkPredicates.left),
+        getStateKeyOrdinalForLookup(
+          right.output, rightKeys, left.output, leftKeys, 
stateWatermarkPredicates.right))
+    } else {
+      (None, None)
+    }
+
+  /**
+   * The window of the other side's state that a row of `joinSide` reads in 
V4, or None to read
+   * every row for the key. V4 orders a side's state by a timestamp, unless 
the side has a key
+   * ordinal (see getStateKeyOrdinalForLookup), whose lookups read the key's 
exact timestamp.
+   * That timestamp is the column the side's value-eviction predicate 
constrains, or its event
+   * time if it has no such predicate. The window bounds that column with the 
join condition
+   * evaluated on the row; see [[MatchingScanWindow]]. For rightTime BETWEEN 
leftTime AND expiry,
+   * the left side evicts on expiry: a left row reads right rows with 
rightTime in
+   * [leftTime, expiry], and a right row reads left rows with expiry at or 
above rightTime.
+   */
+  private[sql] def matchingScanWindow(joinSide: JoinSide): 
Option[MatchingScanWindow] = {
+    val (inputAttributes, otherAttributes, otherKeyOrdinal, otherPredicate) = 
joinSide match {
+      case LeftSide =>
+        (left.output, right.output, stateKeyOrdinalsForLookup._2, 
stateWatermarkPredicates.right)
+      case RightSide =>
+        (right.output, left.output, stateKeyOrdinalsForLookup._1, 
stateWatermarkPredicates.left)
+    }
+    if (stateFormatVersion < 4 || otherKeyOrdinal.isDefined) {
+      None
+    } else {
+      
SymmetricHashJoinStateManager.valueTimestampColumnOrdinal(otherAttributes,

Review Comment:
   **Blocking (P1):** `matchingScanWindow` takes the other side's ordering 
column from the current plan, but V4 records nothing about how the rows already 
in the state store were ordered when they were written. A restart that gives a 
side an ordering column, or moves it to another column, therefore leaves the 
restored rows at their old timestamps, and the window built on this column 
skips those it does not cover.
   
   Reading the code for state format 4 and an inner join on `leftKey = rightKey 
AND leftTime >= rightOther`, where only the right input has a watermark and 
`rightOther` is another TIMESTAMP column of the right input: without a left 
watermark the left side has no ordering column, so `append` stores each left 
row at a random bucket timestamp in [0, 1024), and right rows read every left 
row of their key. After a restart from the same checkpoint that adds 
`withWatermark` on `leftTime`, the state schema check accepts the change, 
because it ignores field metadata. The left side is then ordered by `leftTime`, 
and each right row's window is `[rightOther, Long.MaxValue]`, so the range scan 
starts above every bucket timestamp. No later right row reads a left row stored 
before the restart, although those rows still satisfy the condition, and no 
cutoff ever evicts them. On the base branch this condition built no range scan, 
so the same restart kept every stored row reachable.
   
   The base branch's own range scan for constant-offset event-time intervals, 
and its exact equality-key lookups, already have this hazard; this PR extends 
it to every join it newly windows, such as one-sided bounds and bounds on 
columns other than the two event times. The stacked commits' choice of ordering 
column adds triggers too, such as the q9 auction side moving from `expires` to 
`dateTime` when the bid watermark is dropped. Adding a missing watermark to 
bound a join's state is a natural change to a running query, and nothing 
signals the lost matches.
   
   **Recommended change:** Record, in the join's operator properties, how each 
side's V4 state is ordered: the ordering column's name and type, a lookup key 
ordinal, or no ordering, plus a flag that the side may hold rows stored under 
another ordering. On the first batch of a run, compare the previous run's 
record with the orderings the current plan derives. When a side's ordering 
differs, or the side is already flagged, keep the flag set in the new record 
and give that side no window, so its probes read every row of the key as the 
base branch did for the joins this PR newly windows. Treat a checkpoint without 
a record as ordered by its current plan, as V4 assumes today, and write the 
record from then on. Add V4 restart tests that change a side's ordering column.
   
   **Why this works:** IncrementalExecution already reads the previous run's 
operator metadata on the first batch of a run, so the join can read its 
recorded orderings there and receive the result with its other planning fields. 
matchingScanWindow then returns no window for a flagged side. A window is built 
only when every stored row of that side follows the ordering it bounds, so it 
skips only rows the condition cannot match. The flag stays set for the rest of 
the checkpoint's life, because rows stored under the old ordering remain until 
they are evicted and V4 cannot tell when the last one is gone.
   
   **Scope:** Record each side's V4 ordering in the join's operator metadata, 
and read every row of the key for a side whose stored rows may follow another 
ordering.
   
   **Compatibility:** Joins whose sides keep their ordering across restarts 
read the same windows as before, and state formats 1 to 3 are unaffected.
   
   **Risks:** A side flagged after a restart that changed its ordering reads 
every row of its key for the rest of the checkpoint's life, which gives up the 
window there; the base branch read those joins in full too. A checkpoint 
without a record, such as one written by released V4, is assumed to follow its 
current plan, so a restart that upgrades Spark and changes the layout at once 
can still skip rows. The join's operator metadata gains fields, although the PR 
describes its checkpoints as unchanged; nothing outside tests parses these 
properties in older versions.
   
   **Constraints:** A side gets a window only when every stored row of that 
side follows the ordering the window bounds; otherwise it reads every row of 
the key. Record and compare orderings without changing how V4 stores, looks up 
or evicts rows.
   
   **Exclusions:**
   - Exact key-timestamp lookups at a lookup ordinal, and eviction keyed by an 
ordering column the plan no longer uses, already depend on the plan-derived 
layout on the base branch; this change leaves them as they are.
   
   **Success:** After a restart that gives a V4 side an ordering column or 
moves it, probes of that side read every row of the key and return every stored 
match, in that run and every later run of the checkpoint. Without such a 
restart, V4 probes keep the windows this PR builds.
   
   **Verification:**
   - **Behavior:** For every restart that gives a V4 side an ordering column or 
moves it, such as adding a left watermark to a join on leftKey = rightKey AND 
leftTime >= rightOther, or moving a side's watermark to another column, later 
probes of that side return every match with rows stored before the restart, in 
that run and after a further restart.
   - **Regression:** A V4 join restarted with an unchanged query, and a 
checkpoint written before the record exists, keep the windows their current 
plan derives.
   - **Compatibility:** State formats 1 to 3, and V4 joins without windows, 
restart and return the same rows as before the change.



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