HeartSaVioR commented on code in PR #60284: URL: https://github.com/apache/spark/pull/60284#discussion_r4547004563
########## sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingJoinConstantWatermarkSuite.scala: ########## @@ -0,0 +1,132 @@ +/* + * 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.Row +import org.apache.spark.sql.execution.streaming.operators.stateful.join.StreamingSymmetricHashJoinExec +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 StreamingJoinConstantWatermarkSuite extends StreamTest { + import testImplicits._ + + for (version <- Seq(2, 3, 4)) { + test(s"static conjunct cannot stall left outer finalization, V$version") { + 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, Long)] + val rightInput = MemoryStream[(Int, Long)] + val left = leftInput.toDF().toDF("leftKey", "time") + .select($"leftKey", 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 condition = expr("leftKey = rightKey AND lt > rt - INTERVAL 5 SECONDS AND " + + "CAST(lt AS BIGINT) > 10") + val joined = left.join(right, condition, "left_outer") + .select($"leftKey", $"lt".cast("long"), $"rt".cast("long")) + + testStream(joined)( + StreamProgressLockedActions(Seq( + AddData(leftInput, (1, 20L)), AddData(rightInput, (9, 20L)))), + CheckNewAnswer(), + StreamProgressLockedActions(Seq( + AddData(leftInput, (8, 40L)), AddData(rightInput, (9, 40L)))), + CheckNewAnswer(), + AddData(rightInput, (9, 41L)), + CheckNewAnswer(Row(1, 20L, null)), + Execute { query => + val join = query.lastExecution.executedPlan.collectFirst { + case j: StreamingSymmetricHashJoinExec => j + }.get + assert(join.eventTimeWatermarkForEviction.contains(39000L)) + }) + } + } + + test(s"static conjunct still caps an inexact bound in a left outer join, V$version") { + 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, Long)] + val rightInput = MemoryStream[(Int, Long)] + val left = leftInput.toDF().toDF("leftKey", "ms") + .select($"leftKey", timestamp_millis($"ms").as("lt")) + .withWatermark("lt", "0 seconds") + val right = rightInput.toDF().toDF("rightKey", "ms") + .select($"rightKey", timestamp_millis($"ms").as("rt")) + .withWatermark("rt", "0 seconds") + // The truncating casts make the solver put the left cutoff at the watermark plus 5 + // seconds, above rows that can still match. The constant keeps capping the cutoff. + val condition = expr("leftKey = rightKey AND " + + "CAST(lt AS BIGINT) >= CAST(rt AS BIGINT) + 5 AND CAST(lt AS BIGINT) > 10") + val joined = left.join(right, condition, "left_outer") + .select($"leftKey", unix_millis($"lt"), unix_millis($"rt")) + + testStream(joined)( + // The watermark becomes 10.9 seconds. + StreamProgressLockedActions(Seq( + AddData(leftInput, (1, 15500L)), AddData(rightInput, (9, 10900L)))), + CheckNewAnswer(), + AddData(rightInput, (8, 10910L)), + CheckNewAnswer(), + // 10.95 seconds is not late, and CAST(15.5 s) = 15 >= CAST(10.95 s) + 5 = 15. + AddData(rightInput, (1, 10950L)), + CheckNewAnswer(Row(1, 15500L, 10950L))) + } + } + } + + // Earlier versions admitted a bound that uses only constants, so disabling the finalization + // check runs this join. The left state is never evicted, so the matched left row emits no Review Comment: > [!NOTE] > **DISCLAIMER:** This comment was written by an LLM: Claude Sonnet 5.5 (`claude-sonnet-5-5`, 1M context), running as an SP2K agent on behalf of @HeartSaVioR. Fixed in [c2b4a1f003c](https://github.com/apache/spark/pull/60284/commits/c2b4a1f003c5812f9e0e5869e692f30592950584). The comment now names the unmatched row `(2, 100 s)`, which is the row that never-evicted state withholds. The test also moves the watermark to 199 seconds and runs one more batch before the last empty check, so the check would see that row if the state were evicted. <!-- sp2k-review3 reply 4537904310 --> ########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala: ########## @@ -114,7 +364,23 @@ object StreamingJoinHelper extends PredicateHelper with Logging { } stateWatermark } - allStateWatermarks.reduceOption((x, y) => Math.min(x, y)) + + val conjuncts = splitConjunctivePredicates(joinCondition.get) + val boundingConjuncts = conjuncts.flatMap { predicate => + stateWatermarkOf(predicate, keepConstantOnly = false).map(predicate -> _) + } + // A constant-only conjunct is left out when the other bounds follow the watermark exactly. + // The solver can over-estimate an inexact bound, such as a truncating cast or whole days, + // above a row that can still match. A constant's cutoff keeps that row, as in earlier + // versions, so keep the constants in the minimum then. + val constantBounds = + if (boundingConjuncts.exists { case (predicate, _) => hasInexactArithmetic(predicate) }) { 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. The gap is real at this step, but the final stack does not have it. #60286 (SPARK-61350), the next PR in the stack, rejects any bound whose watermark term is negative: `getStateWatermarkFromLessThenPredicate` gives no cutoff when that term is a `UnaryMinus`. The solver's terms wrap every numeric cast the same way, so DOUBLE and DECIMAL casts reach that check just as BIGINT casts do, and so does the sum `CAST(lt AS DOUBLE) + CAST(rt AS DOUBLE) > 200`. In your example, the first conjunct then gives no cutoff. The constant conjunct stays out, because no bound has inexact arithmetic, so the left input gets no cutoff at all. The left outer join fails analysis by default. With the finalization check disabled, the left row stays in state and the join returns `(1, 60, 50)`. #60286 tests this shape with BIGINT casts in `StreamingJoinArithmeticWatermarkSuite` ("left outer join cannot finalize with a negative opposite watermark term"), and the sum form in `StreamingJoinHelperSuite`. We plan to merge this PR and #60286 one after the other, so we would rather not add a sign rule here that the next PR replaces. <!-- sp2k-review3 reply 4537904293 --> -- 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]
