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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -186,6 +186,15 @@ object StreamingJoinHelper extends PredicateHelper with 
Logging {
       return None
     }
 
+    // The cutoff must rise with the watermark: `watermark + c < leftTime`. 
Without the watermark
+    // term, or with `-watermark`, later rightTime values can still match old 
leftTime values.
+    val watermarkTerms = terms.filter(_.references.exists { a =>
+      attributesWithEventWatermark.contains(a) && a.metadata.contains(delayKey)
+    })
+    if (watermarkTerms.size != 1 || 
watermarkTerms.head.isInstanceOf[UnaryMinus]) {

Review Comment:
   **Blocking (P1):** This check drops a constant-only or negative-sign 
conjunct from the minimum in `getStateValueWatermark`. The solver still accepts 
a constraint term on any column of the state input (lines 146-187), and 
`getStateWatermarkPredicates` applies the cutoff to that input's watermarked 
column (`StreamingSymmetricHashJoinHelper.scala:280-281`). Where the dropped 
conjunct held the minimum on master, a cutoff solved for a different column now 
decides eviction.
   
   By the review's reading of the code, take a left outer join with 1-second 
watermarks on `lt` and `rt`, a left column `le = lt + INTERVAL 100 SECONDS`, 
and the condition `leftKey = rightKey AND le > rt - INTERVAL 5 SECONDS AND 
CAST(lt AS LONG) > 10`. Drive it like the constant-only test in 
`StreamingJoinRangeCutoffSuite`:
   - batch 1: left (1, lt=20), right (9, 20)
   - batch 2: left (8, 40), right (9, 40), which moves the watermark to 39 s
   - batch 3: right (9, 41)
   - batch 4: right (1, 50)
   
   On master the left cutoff is min(34 s, 10 s) = 10 s, the row stays in state, 
and batch 4 emits (1, 20, 120, 50). With this PR the cutoff is 34 s on `lt`. 
Batch 3 evicts the row and emits (1, 20, 120, null), and batch 4 then finds no 
match, although `120 > 50 - 5` holds. Both revisions accept the query at 
analysis. V2 to V4 behave the same, because each evicts by `lt`.
   
   Affected joins: those where, for one input, every cutoff left after this 
check was solved for a non-watermarked column of that input, and a dropped 
conjunct held master's minimum. A remaining cutoff on the watermarked column 
keeps eviction sound, because the minimum never exceeds it. Master already has 
this wrong-column eviction when no such conjunct is present, which the PR 
declares out of scope. This change extends it to joins whose results were 
correct on master.
   
   **Recommended change:** In 
StreamingJoinHelper.getStateWatermarkFromLessThenPredicate, once the constraint 
term is selected, return no cutoff when the state input has a watermarked 
column and the constraint term does not reference it. This mirrors the new 
check on the other input's watermark term, and matches the V4 scan-offset 
caller, which already passes only event-time columns. State inputs without a 
watermarked column keep today's behavior. Add solver unit tests for a state 
input that has both a watermarked column and a second time column, plus 
end-to-end join tests and analysis-check tests for a conjunct on the second 
column combined with a constant-only or negative-sign conjunct. Extend the new 
migration-note entry to say that the bounded column must be the bounded input's 
watermarked column, and that outer and semi joins whose only range condition 
bounds another column now fail analysis. The PR's single commit (ed30aa0c44f) 
added the new sign check and does not change which
  column the solver accepts as the constraint term.
   
   **Why this works:** Once every cutoff the solver returns bounds the column 
it is applied to, every member of the minimum is a valid bound, and so is the 
minimum. Dropping a constant-only or negative-sign conjunct then leaves the 
minimum of the remaining valid bounds, which is still valid. The analysis 
check, eviction and the output watermark take the same answer from the solver, 
so a join left with no sound cutoff for an input is rejected at analysis or 
keeps that input's state, instead of evicting rows that later rows match.
   
   **Compatibility:** Cutoffs from conjuncts on the state input's watermarked 
column, the outer and semi analysis result for joins whose state input has no 
watermark of its own, and the V4 scan offsets stay as they are.
   
   **Risks:** Outer and semi stream-stream joins whose only range condition 
bounds a non-watermarked column of a watermarked input now fail analysis at 
start and at restart. Their results on the base branch were wrong, because rows 
that later rows match were evicted. Inner joins whose only cutoff for an input 
came from such a condition stop evicting that input, so its state grows. There 
is no flag, so rolling back means reverting the check. Most joins whose only 
cutoff for an input comes from a conjunct on a non-watermarked column behave 
the same on the base branch and with this PR alone, so the repair takes on the 
question of which column a cutoff bounds, which the PR declares out of scope. A 
narrower repair leaves them unchanged: return no cutoff only when the new check 
rejects a conjunct and every remaining cutoff was solved for another column. 
Its cost is that a conjunct's cutoff then depends on the conjuncts beside it.
   
   **Constraints:** The check must name the column that 
getStateWatermarkPredicates builds the eviction predicate on, the state input's 
watermarked column, so the solver and the predicate builder agree. State inputs 
without a watermarked column must keep today's behavior, since the PR declares 
joins of an input without its own watermark out of scope. The migration-note 
wording must agree with the corrected statement of the sign rule in the same 
entry.
   
   **Exclusions:**
   - Joins of an input without its own watermark, which the PR declares out of 
scope, keep their current analysis result and eviction.
   - Inexact bounds (SPARK-61381), which the PR declares out of scope.
   - The legacy mode with several watermarked columns on one input, where the 
eviction predicate uses the first such column.
   
   **Verification:**
   - **Regression:** A stream-stream left outer join whose left input has a 
watermarked time column lt and a second time column le, with the condition 
`leftKey = rightKey AND le > rt - INTERVAL 5 SECONDS AND CAST(lt AS LONG) > 
10`, fails analysis with the existing 'not supported without a watermark in the 
join keys' error, and the state watermark predicates built for that condition 
hold no left-input cutoff.
   - **Regression:** On state formats V2 to V4, an inner join whose condition 
combines a range conjunct on the second left time column with a negative-sign 
conjunct emits the match for a left row when the matching right row arrives 
after the watermark has passed that row's watermarked time.
   - **Behavior:** For a state input that has a watermarked column, the solver 
returns no cutoff for a conjunct whose constraint term is another column of 
that input, and the same cutoffs as before for conjuncts on the watermarked 
column, alone and combined with constant-only or negative-sign conjuncts.
   - **Compatibility:** Solver, analysis-check and stream-stream join tests 
whose state inputs have no watermarked column, or whose range conditions use 
the watermarked columns, keep their current results.



##########
docs/streaming/ss-migration-guide.md:
##########
@@ -25,6 +25,13 @@ Please refer [Migration Guide: SQL, Datasets and 
DataFrame](../sql-migration-gui
 
 ## Upgrading from Structured Streaming 4.3 to 4.4
 
+- Since Spark 4.4, a range condition of a stream-stream join bounds the state 
of one input only
+  if the other input's watermarked column appears in it with a positive sign, 
for example

Review Comment:
   **Nit (P3):** Read literally, this rule contradicts the entry's own example. 
`CAST(leftTime AS LONG) + CAST(rightTime AS LONG) > 100` adds `rightTime` and 
is listed as no longer bounding state. `CAST(leftTime AS LONG) - CAST(rightTime 
AS LONG) > 2` subtracts it and still yields a cutoff 
(`StreamingJoinHelperSuite.scala:135`). The check in 
`StreamingJoinHelper.scala:191-196` tests the sign after the condition is 
solved for the bounded column. The rule is that the condition can be rearranged 
to `leftTime > rightTime + c` (or `>=`), so the cutoff rises with the other 
input's watermark.
   
   Two more statements in this entry need the same care:
   - The reason given, that later rows of the other input can still match, fits 
the negative-sign form, whose cutoff fell as the watermark rose. It does not 
fit `CAST(leftTime AS LONG) > 10`. That form's old 10 s cutoff evicted only 
rows that fail the comparison, and those never enter state. The problem with it 
is that its cutoff never rises, so through the minimum it held back the cutoff 
of any other range condition.
   - The sentence at lines 32-33 of this entry says such outer and semi joins 
fail analysis. The check rejects only those without a watermark in the join 
keys (`UnsupportedOperationChecker.scala:730`), so an outer join on a 
watermarked window key still passes.
   
   The new test comments at `StreamingJoinRangeCutoffSuite.scala:27-28`, 
`UnsupportedOperationsSuite.scala:561` and 
`StreamingJoinHelperSuite.scala:170-171` describe the rule by the same literal 
sign.
   
   **Verification:**
   - **Inspection:** Every condition the entry lists as no longer bounding 
state yields no cutoff in the solver, while its bounding example yields one. 
The constant-only and negative-sign forms each carry their own reason. The 
analysis-failure sentence excludes joins with a watermark in the join keys, and 
no new comment describes the rule by the literal sign of a column.



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