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


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinHelper.scala:
##########
@@ -73,12 +73,15 @@ object StreamingSymmetricHashJoinHelper extends Logging {
   }
   /**
    * Predicate for watermark on state values. See 
[[JoinStateKeyWatermarkPredicate]] for
-   * the semantics of `stateWatermark` and `prevStateWatermark`.
+   * the semantics of `stateWatermark` and `prevStateWatermark`. 
`conditionBoundsColumn` is true

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.
   
   Fixed in #60667 and merged here: the doc says the flag is set only when the 
default mode picked the column because the condition bounds it.



##########
docs/streaming/ss-migration-guide.md:
##########
@@ -23,6 +23,34 @@ Note that this migration guide describes the items specific 
to Structured Stream
 Many items of SQL migration can be applied when migrating Structured Streaming 
to higher versions.
 Please refer [Migration Guide: SQL, Datasets and 
DataFrame](../sql-migration-guide.html).
 
+## Upgrading from Structured Streaming 4.4 to 4.5
+
+- Since Spark 4.5, a stream-stream join evicts the state of an input with its 
own watermark by
+  another TIMESTAMP column of that input when the join condition limits how 
long a row of that
+  input can match only through that column, for example through `expiry` in
+  `rightTime BETWEEN leftTime AND expiry`. Earlier versions evicted by the 
event time, and 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). In such a join, and in a time-interval join such as
+  `rightTime BETWEEN leftTime AND leftTime + INTERVAL 10 MINUTES`, a row whose 
eviction column is
+  NULL can never match: an outer join now emits it when it arrives, and no 
join keeps it in state.
+  With state format versions 1 to 3, such rows that a checkpoint of an earlier 
version holds are
+  evicted, and emitted by an outer join, after the upgrade. With state format 
version 4, a join
+  evicted by another column 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.5, a stream-stream join whose inputs have watermarks on 
different equality keys,

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.
   
   Added: a row whose new eviction key is NULL, such as a NULL `lb`, is emitted 
on arrival, state formats 1 to 3 evict such stored rows after the upgrade, and 
a NULL in `ra` keeps the earlier handling.



##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingJoinEqualityWatermarkSuite.scala:
##########
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.streaming
+
+import org.apache.spark.sql.{DataFrame, Row}
+import org.apache.spark.sql.execution.streaming.runtime.MemoryStream
+import org.apache.spark.sql.execution.streaming.state.RocksDBStateStoreProvider
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.tags.SlowSQLTest
+
+@SlowSQLTest
+class StreamingJoinEqualityWatermarkSuite extends StreamTest {
+  import testImplicits._
+
+  private def withJoinConf(version: Int)(f: => Unit): Unit = {
+    withSQLConf(
+      SQLConf.STREAMING_JOIN_STATE_FORMAT_VERSION.key -> version.toString,
+      SQLConf.STATE_STORE_PROVIDER_CLASS.key -> 
classOf[RocksDBStateStoreProvider].getName,
+      SQLConf.STREAMING_NO_DATA_MICRO_BATCHES_ENABLED.key -> "false")(f)
+  }
+
+  // The watermarks are on different equality keys: la on the left and rb on 
the right. A
+  // negative time gives a NULL key.

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.
   
   Fixed: the comment now says that a negative left `b` gives a NULL `lb`.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinHelper.scala:
##########
@@ -262,40 +266,77 @@ object StreamingSymmetricHashJoinHelper extends Logging {
             prevStateKeyWatermark)
         }
       } else if (isWatermarkDefinedOnInput) { // case 2 in the 
StreamingSymmetricHashJoinExec docs
-        val stateValueWatermark = StreamingJoinHelper.getStateValueWatermark(
-          attributesToFindStateWatermarkFor = 
AttributeSet(oneSideInputAttributes),
-          attributesWithEventWatermark = 
AttributeSet(otherSideInputAttributes),
-          condition,
-          eventTimeWatermarkForEviction)
-        // A row can pass late-input filtering but already be eligible for 
eviction in this batch.
-        // When the previous join-derived state cutoff is ahead of the 
late-input watermark, a new
-        // row can fall at or below that cutoff. V4 must scan from the 
beginning to find such rows.
-        val prevStateValueWatermark = eventTimeWatermarkForLateEvents.flatMap 
{ lateWatermark =>
+        val eventTime = 
oneSideInputAttributes.find(_.metadata.contains(delayKey))
+        def wholeInput(watermark: Option[Long]): Option[Long] =
           StreamingJoinHelper.getStateValueWatermark(
             attributesToFindStateWatermarkFor = 
AttributeSet(oneSideInputAttributes),
             attributesWithEventWatermark = 
AttributeSet(otherSideInputAttributes),
             condition,
-            eventTimeWatermarkForLateEvents).filter(_ <= lateWatermark)
+            watermark)
+        // The column that evicts this input, its cutoff, and whether the 
condition bounds that
+        // column. The legacy mode with a single global watermark keeps 
applying a cutoff on any
+        // column of this input to the event time. So does the default mode 
when the condition
+        // bounds no TIMESTAMP column, such as through a DATE column cast to 
TIMESTAMP only.
+        // TODO: [SPARK-62044] An input bounded only through a column cast to 
TIMESTAMP is still
+        //  evicted by its event time with the cast column's cutoff, which can 
be too early.
+        def solve(watermark: Option[Long]): Option[(Attribute, Long, Boolean)] 
= {
+          val picked = if (useFirstEventTimeColumn) None else {
+            
StreamingJoinHelper.getStateValueWatermarkColumn(oneSideInputAttributes,
+              AttributeSet(otherSideInputAttributes), condition, watermark)
+          }
+          picked.map { case (c, w) => (c, w, true) }.orElse {
+            for (e <- eventTime; w <- wholeInput(watermark)) yield (e, w, 
false)
+          }
         }
-        val inputAttributeWithWatermark = 
oneSideInputAttributes.find(_.metadata.contains(delayKey))
-        val expr = watermarkExpression(inputAttributeWithWatermark, 
stateValueWatermark)
-        expr.map { e =>
-          // watermarkExpression only provides the expression when 
eventTimeWatermarkForEviction
-          // is defined
-          JoinStateValueWatermarkPredicate(e, stateValueWatermark.get, 
prevStateValueWatermark)
+        solve(eventTimeWatermarkForEviction).flatMap { case (column, 
stateValueWatermark, bounds) =>
+          // A row can pass late-input filtering but already be eligible for 
eviction in this
+          // batch. When the previous join-derived state cutoff is ahead of 
the late-input
+          // watermark, a new row can fall at or below that cutoff. V4 must 
scan from the

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.
   
   Fixed in #60667 and merged here: the comment names the scan start of each 
case, including the later-batch one.



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