HeartSaVioR commented on code in PR #60664:
URL: https://github.com/apache/spark/pull/60664#discussion_r4624876554


##########
docs/streaming/ss-migration-guide.md:
##########
@@ -25,6 +25,29 @@ 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 stream-stream join whose condition bounds an input with 
its own watermark
+  only through another TIMESTAMP column, for example `rightTime BETWEEN 
leftTime AND expiry`,
+  evicts the state of that input by the bounded column instead of its event 
time. Earlier versions
+  could emit outer join rows too early and lose matches. Such a join can now 
emit rows below its
+  output watermark, which still assumes eviction by the event time, so a 
stateful operator after
+  it can drop them (SPARK-62001). With state format version 4, such a join 
cannot restart from a
+  checkpoint of an earlier version, or after a change of
+  `spark.sql.streaming.statefulOperator.allowMultiple`; start it with a new 
checkpoint. With
+  state format versions 1 to 3, it restarts, and rows that an earlier version 
evicted are not
+  restored. With `spark.sql.streaming.statefulOperator.allowMultiple` set to 
false, joins keep the
+  earlier behavior.
+
+- Since Spark 4.4, a range condition of a stream-stream join bounds the state 
of one input only
+  if it can be rearranged to `leftTime > rightTime + c` (or `>=`), where 
`rightTime` is the other
+  input's watermarked column, so that the cutoff rises with that watermark. 
For example,
+  `leftTime > rightTime - INTERVAL 5 SECONDS` bounds the state of the left 
input.
+  `CAST(leftTime AS LONG) > 10` no longer gives a cutoff, so it no longer 
holds back the cutoff of
+  other range conditions. `CAST(leftTime AS LONG) + CAST(rightTime AS LONG) > 
100` no longer
+  evicts state that later rows of the other input can still match. As a 
result, outer and semi
+  joins without a watermark in the join keys, whose only range condition has 
one of these forms,
+  fail analysis, also when an existing query restarts. To run such a query, 
add a range condition

Review Comment:
   > [!NOTE]
   > **DISCLAIMER:** This comment was written by an LLM: Claude Opus 5.5 
(`claude-opus-5-5`, 1M context), running as an SP2K agent on behalf of 
@HeartSaVioR.
   
   You're right, this PR caused it: master accepts such a left semi join and 
its results are correct. The analysis check now keeps master's rule for left 
semi joins, so new and existing queries are accepted again, while eviction 
keeps the new rule, so such a join evicts nothing and stays correct. Outer 
joins of these shapes still fail, because they could not give correct results. 
A new test runs a constant-only left semi join across a restart.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -186,6 +209,16 @@ object StreamingJoinHelper extends PredicateHelper with 
Logging {
       return None
     }
 
+    // The cutoff must rise with the watermark: `watermark + c < leftTime`. 
Without the watermark
+    // term, the cutoff never rises and caps the other conjuncts' cutoffs. 
With `-watermark`, the
+    // cutoff falls, and 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:
   > [!NOTE]
   > **DISCLAIMER:** This comment was written by an LLM: Claude Opus 5.5 
(`claude-opus-5-5`, 1M context), running as an SP2K agent on behalf of 
@HeartSaVioR.
   
   Thanks, your reading is right for the legacy mode and for the default-mode 
fallback: there a cutoff solved for another column still evicts by the event 
time, which the constant used to cap on master. Going back to master's rule on 
that path would bring back this PR's own sum example, `CAST(lt AS LONG) + 
CAST(rt AS LONG) > 100`, because it picks no column and falls back there too. 
In the default mode, the cast case is 
[SPARK-62044](https://issues.apache.org/jira/browse/SPARK-62044), fixed by 
#60705, which evicts such an input by the cast. For the legacy mode, the 
migration note now says that this rule also applies there and what it changes.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -186,6 +209,16 @@ object StreamingJoinHelper extends PredicateHelper with 
Logging {
       return None
     }
 
+    // The cutoff must rise with the watermark: `watermark + c < leftTime`. 
Without the watermark
+    // term, the cutoff never rises and caps the other conjuncts' cutoffs. 
With `-watermark`, the
+    // cutoff falls, and 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:
   > [!NOTE]
   > **DISCLAIMER:** This comment was written by an LLM: Claude Opus 5.5 
(`claude-opus-5-5`, 1M context), running as an SP2K agent on behalf of 
@HeartSaVioR.
   
   Right, this is 
[SPARK-61381](https://issues.apache.org/jira/browse/SPARK-61381), the inexact 
cutoff of a truncating cast, which the constant used to hide. It is the known 
follow-up and will be fixed there, not here.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -710,18 +740,30 @@ case class StreamingSymmetricHashJoinExec(
         Predicate.create(Literal(false), Seq.empty).eval _ // false = do not 
remove if no predicate
     }
 
+    private[this] var updatedStateRowsCount = 0
+    private[this] var numRemovedFromOtherSideDuringJoinCount = 0
+    private[this] val allowMultipleStatefulOperators: Boolean =
+      conf.getConf(SQLConf.STATEFUL_OPERATOR_ALLOW_MULTIPLE)
+
+    // The join condition bounds the column that the value predicate tests, so 
a row with a NULL
+    // in it can never match, and `column <= cutoff` never holds for it. The 
legacy mode tests the
+    // event time even when the condition bounds another column.
+    private[this] val isEvictionColumnNull: UnsafeRow => Boolean = 
stateWatermarkPredicate match {

Review Comment:
   > [!NOTE]
   > **DISCLAIMER:** This comment was written by an LLM: Claude Opus 5.5 
(`claude-opus-5-5`, 1M context), running as an SP2K agent on behalf of 
@HeartSaVioR.
   
   Thanks, fixed in #60667, which owns the NULL rule: it now applies only to a 
column that the default mode picked, so the cast fallback keeps master's 
handling.



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