HeartSaVioR commented on code in PR #60379: URL: https://github.com/apache/spark/pull/60379#discussion_r4598135506
########## sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingJoinNumericWatermarkSuite.scala: ########## @@ -0,0 +1,431 @@ +/* + * 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.logging.log4j.Level + +import org.apache.spark.sql.{AnalysisException, 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 + +class StreamingJoinNumericWatermarkSuite extends StreamTest { + import testImplicits._ + + private val timestampRule = "A time range condition must bound a TIMESTAMP column" + + // Analysis rejects the plan before any state format is used. Each bound below is exact, with + // no truncating cast and no whole days, so only the TIMESTAMP-column rule rejects it. + test("left outer join cannot use a numeric state field for range eviction") { + val leftInput = MemoryStream[(Int, Long, Long)] + val rightInput = MemoryStream[(Int, Long)] + val left = leftInput.toDF().toDF("leftKey", "n", "time") + .select($"leftKey", $"n", timestamp_seconds($"time").as("lt")) + .withWatermark("lt", "1 second") + val right = rightInput.toDF().toDF("rightKey", "time") + .select($"rightKey", timestamp_seconds($"time").as("rt")) + .withWatermark("rt", "1 second") + val joined = left.join(right, + expr("leftKey = rightKey AND n > CAST(rt AS DOUBLE)"), "left_outer") + .select($"leftKey", $"n", $"lt") + + val error = intercept[AnalysisException] { + testStream(joined)( + AddData(leftInput, (1, 20L, 20L)), + CheckNewAnswer()) + } + assert(error.getMessage.contains("a watermark on the right input and a time constraint")) + assert(error.getMessage.contains(timestampRule)) + } + + // Earlier versions admitted this join, so disabling the finalization check runs it. The left + // state is never evicted, so the unmatched left row (2, 50) is never output with nulls, which + // evicting it by the cutoff solved for the numeric field would do. + test("left outer join on a numeric state field runs with the finalization check disabled") { + val leftInput = MemoryStream[(Int, Long, Long)] + val rightInput = MemoryStream[(Int, Long)] + val left = leftInput.toDF().toDF("leftKey", "n", "time") + .select($"leftKey", $"n", timestamp_seconds($"time").as("lt")) + .withWatermark("lt", "1 second") + val right = rightInput.toDF().toDF("rightKey", "time") + .select($"rightKey", timestamp_seconds($"time").as("rt")) + .withWatermark("rt", "1 second") + val joined = left.join(right, + expr("leftKey = rightKey AND n > CAST(rt AS DOUBLE)"), "left_outer") + .select($"leftKey", $"n", $"rt".cast("long")) + + val appender = new LogAppender("finalization check disabled") + withLogAppender(appender, level = Some(Level.WARN)) { + withSQLConf(SQLConf.STREAMING_JOIN_CHECK_FINALIZATION_ENABLED.key -> "false") { + testStream(joined)( + StreamProgressLockedActions(Seq( + AddData(leftInput, (1, 20L, 20L)), AddData(rightInput, (1, 10L)))), + CheckNewAnswer(Row(1, 20L, 10L)), + AddData(rightInput, (2, 100L)), + CheckNewAnswer(), + // The left row (2, 50) does not match (2, 100). Its time is below the eviction + // watermark of 99 seconds in the next batch, yet the left state keeps the row. + AddData(leftInput, (2, 50L, 50L), (3, 200L, 200L)), + CheckNewAnswer(), + AddData(rightInput, (4, 120L)), + CheckNewAnswer()) + } + } + assert(appender.loggingEvents.exists { event => + val message = event.getMessage.getFormattedMessage + message.contains("allow unmatched state to be finalized") && + message.contains("keeps that state without bound") + }) + } + + // Comparing with the TIMESTAMP event time casts these columns, but eviction compares the + // cutoff with the column itself. + for (columnType <- Seq("date", "timestamp_ntz")) { + test(s"left outer join cannot use a $columnType state field for range eviction") { + val leftInput = MemoryStream[(Int, Long)] + val rightInput = MemoryStream[(Int, Long)] + val left = leftInput.toDF().toDF("leftKey", "time") + .select($"leftKey", timestamp_seconds($"time").cast(columnType).as("orderDate")) + val right = rightInput.toDF().toDF("rightKey", "time") + .select($"rightKey", timestamp_seconds($"time").as("rt")) + .withWatermark("rt", "1 second") + val joined = left.join(right, + expr("leftKey = rightKey AND orderDate >= rt - INTERVAL 7 HOURS"), "left_outer") + + val error = intercept[AnalysisException] { + testStream(joined)( + AddData(leftInput, (1, 20L)), + CheckNewAnswer()) + } + assert(error.getMessage.contains(timestampRule)) + } + } + + 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) + } + + // window() over a watermarked time column. The window is the input's only event-time column. + private def windowed( + input: MemoryStream[(Int, Long)], + key: String, + windowName: String, + duration: String, + slide: String): DataFrame = { + input.toDF().toDF(key, "time") + .select(col(key), timestamp_seconds($"time").as("t")) + .withWatermark("t", "0 seconds") + .select(col(key), window($"t", duration, slide).as(windowName)) + } + + // An evicted left window starts at least its own duration before the watermark, and a later + // right window starts less than its own duration before it, so with left windows at least as + // long a later right window orders above every evicted left window. + for { + version <- Seq(2, 3, 4) + leftDuration <- Seq("10 seconds", "20 seconds") + } { + test(s"left outer join finalizes $leftDuration windows by a whole-window bound, V$version") { + withJoinConf(version) { + val leftInput = MemoryStream[(Int, Long)] + val rightInput = MemoryStream[(Int, Long)] + val left = windowed(leftInput, "leftKey", "lw", leftDuration, leftDuration) + val right = windowed(rightInput, "rightKey", "rw", "10 seconds", "10 seconds") + val joined = left.join(right, $"leftKey" === $"rightKey" && $"lw" > $"rw", "left_outer") + .select($"leftKey", $"lw.start".cast("long"), $"rw.start".cast("long")) + val matchedStart = if (leftDuration == "10 seconds") 10L else 0L + + testStream(joined)( + StreamProgressLockedActions(Seq( + AddData(leftInput, (1, 5L), (5, 15L)), AddData(rightInput, (2, 5L), (5, 5L)))), + CheckNewAnswer(Row(5, matchedStart, 0L)), + StreamProgressLockedActions(Seq( + AddData(leftInput, (3, 45L)), AddData(rightInput, (2, 45L)))), + CheckNewAnswer(), + // The eviction watermark is 45 seconds, past the end of key 1's left window. + AddData(rightInput, (2, 46L)), + CheckNewAnswer(Row(1, 0L, null))) + } + } + } + + // The left input is watermarked on its event time lt, which the join condition does not use, + // and holds a window over the unwatermarked column ot. Eviction must try the window after lt + // finds no bound, or the left state is never finalized. + for (version <- Seq(2, 4)) { + test(s"left outer join finalizes a window over an unwatermarked column, V$version") { + withJoinConf(version) { + val leftInput = MemoryStream[(Int, Long)] + val rightInput = MemoryStream[(Int, Long)] + val left = leftInput.toDF().toDF("leftKey", "time") + .select($"leftKey", timestamp_seconds($"time").as("lt"), + timestamp_seconds($"time").as("ot")) + .withWatermark("lt", "0 seconds") + .select($"leftKey", $"lt", window($"ot", "10 seconds").as("lw")) + val right = windowed(rightInput, "rightKey", "rw", "10 seconds", "10 seconds") + val joined = left.join(right, $"leftKey" === $"rightKey" && $"lw" > $"rw", "left_outer") + .select($"leftKey", $"lw.start".cast("long"), $"rw.start".cast("long")) + + testStream(joined)( + StreamProgressLockedActions(Seq( + AddData(leftInput, (1, 5L), (5, 15L)), AddData(rightInput, (2, 5L), (5, 5L)))), + CheckNewAnswer(Row(5, 10L, 0L)), + StreamProgressLockedActions(Seq( + AddData(leftInput, (3, 45L)), AddData(rightInput, (2, 45L)))), + CheckNewAnswer(), + // The eviction watermark is 45 seconds, past the end of key 1's left window. + AddData(rightInput, (2, 46L)), + CheckNewAnswer(Row(1, 0L, null))) + } + } + } + + test("right outer and left semi joins finalize windows by a whole-window bound") { + Seq("right_outer", "left_semi").foreach { joinType => + withClue(joinType) { + withJoinConf(2) { + val leftInput = MemoryStream[(Int, Long)] + val rightInput = MemoryStream[(Int, Long)] + val left = windowed(leftInput, "leftKey", "lw", "10 seconds", "10 seconds") + val right = windowed(rightInput, "rightKey", "rw", "10 seconds", "10 seconds") + val condition = $"leftKey" === $"rightKey" && $"lw" > $"rw" + // The right outer join finalizes the same left windows. + val (joined, unmatched) = if (joinType == "right_outer") { + (right.join(left, condition, joinType) + .select($"leftKey", $"lw.start".cast("long"), $"rw.start".cast("long")), + Seq(Row(1, 0L, null))) + } else { + (left.join(right, condition, joinType) + .select($"leftKey", $"lw.start".cast("long")), Seq.empty) + } + val matched = if (joinType == "right_outer") Row(5, 10L, 0L) else Row(5, 10L) + + testStream(joined)( + StreamProgressLockedActions(Seq( + AddData(leftInput, (1, 5L), (5, 15L)), AddData(rightInput, (2, 5L), (5, 5L)))), + CheckNewAnswer(matched), + StreamProgressLockedActions(Seq( + AddData(leftInput, (3, 25L)), AddData(rightInput, (2, 25L)))), + CheckNewAnswer(), + AddData(rightInput, (2, 26L)), + CheckNewAnswer(unmatched: _*)) + } + } + } + } + + // A 20-second right window that ends above the watermark can start before a 10-second left + // window that ends at or below it, so these left windows cannot be finalized. + test("outer and semi joins cannot finalize windows shorter than the other input's") { + Seq("left_outer", "left_semi", "right_outer").foreach { joinType => + withClue(joinType) { + val leftInput = MemoryStream[(Int, Long)] + val rightInput = MemoryStream[(Int, Long)] + val left = windowed(leftInput, "leftKey", "lw", "10 seconds", "10 seconds") + val right = windowed(rightInput, "rightKey", "rw", "20 seconds", "1 second") + val condition = $"leftKey" === $"rightKey" && $"lw" > $"rw" + val joined = if (joinType == "right_outer") { + right.join(left, condition, joinType) + } else { + left.join(right, condition, joinType) + } + + val error = intercept[AnalysisException] { + testStream(joined)( + AddData(leftInput, (1, 5L)), + CheckNewAnswer()) + } + assert(error.getMessage.contains(timestampRule)) + } + } + } + + for (version <- Seq(2, 3, 4)) { + test(s"inner join evicts windows by a whole-window bound, V$version") { + withJoinConf(version) { + val leftInput = MemoryStream[(Int, Long)] + val rightInput = MemoryStream[(Int, Long)] + val left = windowed(leftInput, "leftKey", "lw", "10 seconds", "10 seconds") + val right = windowed(rightInput, "rightKey", "rw", "10 seconds", "10 seconds") + val joined = left.join(right, $"leftKey" === $"rightKey" && $"lw" > $"rw") + .select($"leftKey", $"lw.start".cast("long"), $"rw.start".cast("long")) + + testStream(joined)( + StreamProgressLockedActions(Seq( + AddData(leftInput, (1, 5L)), AddData(rightInput, (2, 5L)))), + CheckNewAnswer(), + StreamProgressLockedActions(Seq( + AddData(leftInput, (3, 25L)), AddData(rightInput, (4, 25L)))), + CheckNewAnswer(), + // Distinct keys: V4 groups rows with the same key and time into one entry. + AddData(rightInput, (6, 26L)), + CheckNewAnswer(), + Execute { query => + // Left window [0, 10) is evicted. Left [20, 30) and the three right rows remain, + // because the bound gives right windows no cutoff. + val total = query.lastProgress.stateOperators.head.numRowsTotal + assert(total == 4, s"numRowsTotal = $total") + }) + } + } + } + + // Ordering windows compares their starts first. A 20-second right window that ends above the + // watermark can start before a 10-second left window that ends at or below it, so a bound that + // compares whole windows must not evict left windows. + for (version <- Seq(2, 3, 4)) { + test(s"inner join keeps windows that a longer opposite window can still match, V$version") { + withJoinConf(version) { + val leftInput = MemoryStream[(Int, Long)] + val rightInput = MemoryStream[(Int, Long)] + val left = windowed(leftInput, "leftKey", "lw", "10 seconds", "10 seconds") + val right = windowed(rightInput, "rightKey", "rw", "20 seconds", "1 second") + val joined = left.join(right, $"leftKey" === $"rightKey" && $"lw" > $"rw") + .select($"leftKey", $"lw.start".cast("long"), $"rw.start".cast("long")) + + testStream(joined)( + StreamProgressLockedActions(Seq( + AddData(leftInput, (1, 5L)), AddData(rightInput, (9, 5L)))), + CheckNewAnswer(), + StreamProgressLockedActions(Seq( + AddData(leftInput, (8, 15L)), AddData(rightInput, (9, 15L)))), + CheckNewAnswer(), + // The watermark reaches 15 seconds, past the end of left window [0, 10). + AddData(rightInput, (9, 16L)), + CheckNewAnswer(), + // Time 18 is in right window [-1, 19), which is not late and orders below [0, 10). + AddData(rightInput, (1, 18L)), + CheckNewAnswer(Row(1, 0L, -1L))) + } + } + } + + // A union's column keeps its first child's metadata, so with children of different window + // lengths it records none, and a whole-window bound evicts neither union's windows nor the + // windows compared with them: a 20-second window can start before a 10-second window that ends + // at or below the watermark. + test("outer joins cannot finalize windows compared with a union of window lengths") { + Seq("right", "left").foreach { unionSide => + withClue(s"$unionSide union") { + val leftInput = MemoryStream[(Int, Long)] + val otherLeftInput = MemoryStream[(Int, Long)] + val rightInput = MemoryStream[(Int, Long)] + val otherRightInput = MemoryStream[(Int, Long)] + val (left, right) = if (unionSide == "right") { + (windowed(leftInput, "leftKey", "lw", "10 seconds", "10 seconds"), + windowed(rightInput, "rightKey", "rw", "10 seconds", "10 seconds") + .union(windowed(otherRightInput, "rightKey", "rw", "20 seconds", "1 second"))) + } else { + (windowed(leftInput, "leftKey", "lw", "20 seconds", "20 seconds") + .union(windowed(otherLeftInput, "leftKey", "lw", "10 seconds", "10 seconds")), + windowed(rightInput, "rightKey", "rw", "20 seconds", "1 second")) + } + val joined = left.join(right, $"leftKey" === $"rightKey" && $"lw" > $"rw", "left_outer") + + val error = intercept[AnalysisException] { + testStream(joined)( + AddData(leftInput, (1, 5L)), + CheckNewAnswer()) + } + assert(error.getMessage.contains(timestampRule)) + } + } + } + + for (version <- Seq(2, 3, 4)) { + test(s"inner join keeps windows compared with a union of window lengths, V$version") { + withJoinConf(version) { + val leftInput = MemoryStream[(Int, Long)] + val tumblingInput = MemoryStream[(Int, Long)] + val slidingInput = MemoryStream[(Int, Long)] + val left = windowed(leftInput, "leftKey", "lw", "10 seconds", "10 seconds") + val right = windowed(tumblingInput, "rightKey", "rw", "10 seconds", "10 seconds") + .union(windowed(slidingInput, "rightKey", "rw", "20 seconds", "1 second")) + val joined = left.join(right, $"leftKey" === $"rightKey" && $"lw" > $"rw") + .select($"leftKey", $"lw.start".cast("long"), $"rw.start".cast("long")) + + testStream(joined)( + StreamProgressLockedActions(Seq(AddData(leftInput, (1, 5L)), + AddData(tumblingInput, (9, 5L)), AddData(slidingInput, (9, 5L)))), + CheckNewAnswer(), + StreamProgressLockedActions(Seq(AddData(leftInput, (8, 15L)), + AddData(tumblingInput, (9, 15L)), AddData(slidingInput, (9, 15L)))), + CheckNewAnswer(), + // The watermark reaches 15 seconds, past the end of left window [0, 10). + AddData(tumblingInput, (9, 16L)), + CheckNewAnswer(), + // Time 18 is in sliding right window [-1, 19), which is not late and orders below + // [0, 10). + AddData(slidingInput, (1, 18L)), + CheckNewAnswer(Row(1, 0L, -1L))) + } + } + } + + // Both directions give a cutoff, so V4 would compute range-scan offsets, which read the event + // time as a long. A time-window event time must not use them. 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, updated in [da03c49f74c](https://github.com/apache/spark/pull/60379/commits/da03c49f74c55ca2b5ab23f6f5a56734b308d1dd) with your wording. <!-- sp2k-review10 reply 4549328922 --> ########## sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala: ########## @@ -230,6 +233,53 @@ case class StreamingSymmetricHashJoinExec( private val useVirtualColumnFamilies = stateFormatVersion >= 3 + // Only the V4 state manager reads these ordinals. They depend only on plan fields, so + // doExecute computes them once on the driver instead of in every task. + private lazy val stateKeyOrdinalsForLookup: (Option[Int], Option[Int]) = + if (stateFormatVersion >= 4) { + (getStateKeyOrdinalForLookup( + left.output, leftKeys, right.output, rightKeys, stateWatermarkPredicates.left), + getStateKeyOrdinalForLookup( + right.output, rightKeys, left.output, leftKeys, stateWatermarkPredicates.right)) + } else { + (None, None) + } + + /** + * The window of the other side's state that a row of `joinSide` reads in V4, or None to read + * every row for the key. V4 orders a side's state by a timestamp, unless the side has a key + * ordinal (see getStateKeyOrdinalForLookup), whose lookups read the key's exact timestamp. + * That timestamp is the column the side's value-eviction predicate constrains, or its event + * time if it has no such predicate. The window bounds that column with the join condition + * evaluated on the row; see [[MatchingScanWindow]]. For rightTime BETWEEN leftTime AND expiry, + * the left side evicts on expiry: a left row reads right rows with rightTime in + * [leftTime, expiry], and a right row reads left rows with expiry at or above rightTime. + */ + private[sql] def matchingScanWindow(joinSide: JoinSide): Option[MatchingScanWindow] = { + val (inputAttributes, otherAttributes, otherKeyOrdinal, otherPredicate) = joinSide match { + case LeftSide => + (left.output, right.output, stateKeyOrdinalsForLookup._2, stateWatermarkPredicates.right) + case RightSide => + (right.output, left.output, stateKeyOrdinalsForLookup._1, stateWatermarkPredicates.left) + } + if (stateFormatVersion < 4 || otherKeyOrdinal.isDefined) { + None + } else { + SymmetricHashJoinStateManager.valueTimestampColumnOrdinal(otherAttributes, 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 for the careful example. We agree it can happen, but we do not plan to fix it in this PR. The problem is not the window itself. V4 builds its whole state layout from the current plan: the ordering timestamp of each row, which rows eviction removes, and the exact key lookups. None of it is recorded in the checkpoint. So a restart that changes how a side is ordered is already unsafe on the base branch. Take the q9 shape from your comment the other way around: a query without a bid watermark orders the auction side by `dateTime`, and a restart that adds the bid watermark orders it by `expires`. Eviction then compares the stored `dateTime` with the cutoff on `expires`, so it removes auctions that started before the cutoff although they have not expired, and later bids miss them. The window adds one more way the same restart goes wrong. The right fix is to record each side's ordering in the checkpoint and to check it on restart, for eviction, lookups and windows together. That is a change to V4's checkpoint format, so we would rather do it separately than inside this PR. A side flag that only turns off windows would also leave eviction exposed. For now, [da03c49f74c](https://github.com/apache/spark/pull/60379/commits/da03c49f74c55ca2b5ab23f6f5a56734b308d1dd) adds one sentence to the guide, next to the existing note that changing the join condition or the watermarks can remove rows that could still match: they can also decide which stored rows a new row reads, so changing either can skip stored rows that would match. The guide already calls such changes ill-defined. <!-- sp2k-review10 reply 4582134210 --> -- 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]
