spark-test commented on code in PR #60668:
URL: https://github.com/apache/spark/pull/60668#discussion_r4627124074
##########
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:
**Nit (P3):** This says the flag is true whenever the join condition bounds
the tested column, but solve sets it only for a column the default mode picked
through getStateValueWatermarkColumn. In legacy mode, `rightTime BETWEEN
leftTime AND leftTime + INTERVAL 10 MINUTES` bounds leftTime, yet the value
predicate on leftTime carries false, as StreamingSymmetricHashJoinHelperSuite
asserts. Setting the flag by this description would turn on the arrival-time
NULL check in legacy mode, which the migration guide says keeps the earlier
behavior. Saying that the flag marks a column the default mode picked because
the condition bounds it, and that legacy mode and the whole-input fallback
leave it false, would match the code.
**Verification:**
- **Inspection:** The JoinStateValueWatermarkPredicate scaladoc names the
default mode's column pick as the only case that sets conditionBoundsColumn,
and says that legacy mode and the whole-input fallback leave it false.
##########
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:
**Nit (P3):** This entry does not mention the NULL rule that comes with the
new key choice: a row whose new eviction key is NULL, such as a left row with a
NULL `lb` in this example, can never match, so an outer join now emits it when
it arrives instead of when `la` passes the watermark, and with state format
versions 1 to 3 such rows that a checkpoint of an earlier version holds are
evicted and emitted after the upgrade. The range entry before this one states
the same rule for value columns. A sentence here that names this case, and says
that a NULL in `ra`, the key earlier versions also evicted the right input by,
keeps the earlier handling, would tell users of these outer joins when such
rows now come out.
**Verification:**
- **Inspection:** The equality-key entry of the 4.4-to-4.5 Structured
Streaming migration guide limits the NULL rule to a row whose new eviction key
is NULL, such as a left row with a NULL `lb`, says that a NULL in `ra` keeps
the earlier handling, and describes the rule on arrival and for rows an
upgraded state format 1 to 3 checkpoint holds, consistently with the range
entry.
##########
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:
**Nit (P3):** This sentence predates the later-batch branch below: when the
default mode picked the event time and the batch is not the first of the run,
the scan starts at min(cutoff, lateWatermark), so for `leftTime > rightTime +
INTERVAL 10 SECONDS AND rightTime <= expiry` with a late watermark of 100000 ms
and a cutoff of 110000 ms it starts at 100000 ms, as
StreamingSymmetricHashJoinHelperSuite asserts, not at the beginning. Only the
first batch and every batch of an input bounded only through a cast column
still scan from the beginning when their cutoff is ahead, and a cutoff on
another column always does. Saying that the scan must start at or below the
late-input watermark, and naming those cases, would keep a later reader from
taking the min() branch for a bug.
**Verification:**
- **Inspection:** The comment above prevStateValueWatermark states the start
for a later batch of a run when the default mode picked the event time, for the
first batch, for every batch of an input bounded only through a cast column,
and for a cutoff on another column, each matching the code below it.
##########
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:
**Nit (P3):** Only the left `b` goes through `when($"b" >= 0, ...)`, so a
negative left `a` or any negative right value gives a timestamp before 1970
rather than NULL. A later test that adds `(80, -1)` to the right input to get a
NULL `rb` would join a real timestamp and pass without exercising the NULL
rule. Saying that a negative left `b` gives a NULL `lb` would match the helper.
**Verification:**
- **Inspection:** The crossedKeyJoin comment matches the helper's encoding
of each of its four columns.
--
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]