HeartSaVioR commented on code in PR #60669:
URL: https://github.com/apache/spark/pull/60669#discussion_r4625593318
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -362,6 +364,41 @@ case class StreamingSymmetricHashJoinExec(
}
protected override def doExecute(): RDD[InternalRow] = {
+ // Older versions and the legacy mode order the V4 state of an input by
its event time (index
+ // version 0), so their checkpoints cannot evict that state by another
column. Index version 1
+ // orders it by the column that the default mode picks, which the legacy
mode cannot evict.
+ // They keep the rows of an input without an event time out of the index.
Such rows are never
+ // evicted, which only an outer join that emits the unmatched rows of that
input cannot accept.
+ val indexVersion =
conf.getConf(SQLConf.STREAMING_JOIN_STATE_V4_INDEX_VERSION)
+ if (stateFormatVersion == 4 && (indexVersion == 0 ||
!allowMultipleStatefulOperators)) {
+ Seq((LeftSide, stateWatermarkPredicates.left, left.output, right.output),
+ (RightSide, stateWatermarkPredicates.right, right.output,
left.output)).foreach {
+ case (side, predicate, attributes, otherAttributes) =>
+ val eventTime = WatermarkSupport.findEventTimeColumnIndex(
+ attributes, allowMultipleEventTimeColumns = false)
+ val column = if (indexVersion == 0) {
+ stateValueWatermarkColumnOrdinal(predicate, attributes)
+ } else {
+ predicate.collect { case _: JoinStateValueWatermarkPredicate =>
+ StreamingJoinHelper.getStateValueWatermarkColumn(attributes,
+ AttributeSet(otherAttributes), condition.full, Some(0L))
+ }.flatten.map { case (c, _) =>
attributes.indexWhere(_.semanticEquals(c)) }
+ }
+ val emitsUnmatchedRows = joinType match {
+ case LeftOuter => side == LeftSide
+ case RightOuter => side == RightSide
+ case FullOuter => 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.
Added `full_outer` to the one-sided outer test and to the V4 restart test,
which fails naming the left input.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -4399,6 +4399,21 @@ object SQLConf {
.checkValue(v => Set(1, 2, 3, 4).contains(v), "Valid versions are 1, 2,
3, and 4")
.createWithDefault(2)
+ val STREAMING_JOIN_STATE_V4_INDEX_VERSION =
+ buildConf("spark.sql.streaming.join.stateFormatV4.indexVersion")
+ .internal()
+ .withBindingPolicy(ConfigBindingPolicy.NOT_APPLICABLE)
+ .doc("How state format version 4 of stream-stream joins orders the state
of an input that " +
+ "it evicts by a range condition. 0: by the event time of the input, as
in 4.3 and " +
+ "earlier and with spark.sql.streaming.statefulOperator.allowMultiple
set to false. 1: by " +
+ "the column that the condition bounds. A streaming query keeps the
version recorded in " +
+ "its checkpoint, and one restarted from a checkpoint without it uses
0. With 0, a join " +
+ "whose condition does not bound the event time of such an input fails
to restart.")
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.
Reworded the doc to name the cases of an input without an event time for
both versions, including the legacy-mode failure with version 1.
##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingJoinOneSidedWatermarkSuite.scala:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.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 StreamingJoinOneSidedWatermarkSuite extends StateStoreMetricsTest {
+ 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)
+ }
+
+ for {
+ version <- Seq(2, 3, 4)
+ joinType <- Seq("left_outer", "right_outer")
+ } {
+ test(s"range eviction needs only the opposite watermark, V$version
$joinType") {
+ 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") {
+ val leftInput = MemoryStream[(Int, Int)]
+ val rightInput = MemoryStream[(Int, Int)]
+ val leftData = leftInput.toDF().toDF("leftKey", "time")
+ .select($"leftKey", timestamp_seconds($"time").as("leftTime"))
+ val rightData = rightInput.toDF().toDF("rightKey", "time")
+ .select($"rightKey", timestamp_seconds($"time").as("rightTime"))
+ // Only the input whose rows are not preserved has a watermark.
+ val left = if (joinType == "right_outer") {
+ leftData.withWatermark("leftTime", "10 seconds")
+ } else {
+ leftData
+ }
+ val right = if (joinType == "left_outer") {
+ rightData.withWatermark("rightTime", "10 seconds")
+ } else {
+ rightData
+ }
+ val joined = left.join(right, expr("leftKey = rightKey AND " +
+ "leftTime BETWEEN rightTime - INTERVAL 5 SECONDS AND " +
+ "rightTime + INTERVAL 5 SECONDS"), joinType)
+ .select($"leftKey", $"rightKey", $"leftTime".cast("int"),
$"rightTime".cast("int"))
+ val stateInput = if (joinType == "left_outer") leftInput else
rightInput
+ val watermarkInput = if (joinType == "left_outer") rightInput else
leftInput
+ def unmatched(key: Int): Row = {
+ if (joinType == "left_outer") Row(key, null, 5, null) else Row(null,
key, null, 5)
+ }
+ withTempDir { checkpoint =>
+ testStream(joined)(
+ StartStream(checkpointLocation = checkpoint.getCanonicalPath),
+ AddData(stateInput, (1, 5)), CheckNewAnswer(),
+ AddData(watermarkInput, (2, 30)), CheckNewAnswer(),
+ AddData(watermarkInput, (2, 40)), CheckNewAnswer(unmatched(1)),
+ StopStream,
+ StartStream(checkpointLocation = checkpoint.getCanonicalPath),
+ // The input without a watermark can add rows below an earlier
eviction cutoff.
+ AddData(stateInput, (3, 5)), CheckNewAnswer(unmatched(3)),
+ AddData(watermarkInput, (3, 5)), CheckNewAnswer(),
+ AddData(watermarkInput, (4, 50)), CheckNewAnswer(),
+ // The watermarked input keeps its state for arbitrarily old rows
of the other input.
+ AddData(stateInput, (2, 30)), CheckNewAnswer(Row(2, 2, 30, 30)))
+ }
+ }
+ }
+ }
+
+ // The input without a watermark is evicted, and its matches within the
range are kept.
+ for {
+ version <- Seq(2, 4)
+ (joinType, watermarkOnLeft) <- Seq(("inner", false), ("inner", true),
("left_semi", false))
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. The case now adds a row with key 4 that never matches, so the state
count fails if that input is not evicted.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -362,6 +364,41 @@ case class StreamingSymmetricHashJoinExec(
}
protected override def doExecute(): RDD[InternalRow] = {
+ // Older versions and the legacy mode order the V4 state of an input by
its event time (index
+ // version 0), so their checkpoints cannot evict that state by another
column. Index version 1
+ // orders it by the column that the default mode picks, which the legacy
mode cannot evict.
+ // They keep the rows of an input without an event time out of the index.
Such rows are never
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.
Narrowed the comment and the migration note to checkpoints of an earlier
version or of a query started in the legacy mode, and added a TODO for the
unchecked legacy-mode run of a version-1 checkpoint.
##########
docs/streaming/ss-migration-guide.md:
##########
@@ -25,6 +25,40 @@ 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
+ that bounds the state, or a watermark in the join keys, and start it from a
new checkpoint.
+
+- Since Spark 4.4, a stream-stream join also evicts the state of an input
without its own
+ watermark when the other input's watermark and a range condition bound one
of its TIMESTAMP
+ columns, for example `leftTime BETWEEN rightTime - INTERVAL 5 SECONDS AND
rightTime +
+ INTERVAL 5 SECONDS` with a watermark only on `rightTime`. Earlier versions
kept such state
+ forever and never emitted the unmatched rows of an outer join. With state
format version 4, an
+ outer join that emits the unmatched rows of such an input 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. Other joins restart and keep the rows stored
before 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: the note now gives the outcome per state format (format 4 keeps those
rows; formats 1 to 3 evict them once the cutoff passes, and an outer join emits
the unmatched ones then).
--
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]