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


##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingJoinArithmeticWatermarkSuite.scala:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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, 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 StreamingJoinArithmeticWatermarkSuite extends StreamTest {
+  import testImplicits._
+
+  for (version <- Seq(2, 3, 4)) {
+    test(s"positive opposite watermark term keeps old matching state, 
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("leftTime"))
+          .withWatermark("leftTime", "1 second")
+        val right = rightInput.toDF().toDF("rightKey", "time")
+          .select($"rightKey", timestamp_seconds($"time").as("rightTime"))
+          .withWatermark("rightTime", "1 second")
+        val condition = expr("leftKey = rightKey AND " +
+          "CAST(leftTime AS LONG) + CAST(rightTime AS LONG) > 100")
+        val joined = left.join(right, condition)
+          .select($"leftKey", $"leftTime".cast("long"), 
$"rightTime".cast("long"))
+
+        testStream(joined)(
+          StreamProgressLockedActions(Seq(
+            AddData(leftInput, (1, 5L)), AddData(rightInput, (9, 5L)))),
+          CheckNewAnswer(),
+          StreamProgressLockedActions(Seq(
+            AddData(leftInput, (8, 21L)), AddData(rightInput, (9, 21L)))),
+          CheckNewAnswer(),
+          AddData(rightInput, (9, 22L)),
+          CheckNewAnswer(),
+          // The global watermark passes 5, but a later rightTime can still 
match leftTime 5.
+          AddData(rightInput, (1, 100L)),
+          CheckNewAnswer(Row(1, 5L, 100L)))
+      }
+    }
+  }
+
+  // The negative term's cutoff falls as the watermark rises, so it cannot 
finalize left rows,
+  // and the constant-only conjunct does not count as a bound either. The left 
watermark keeps
+  // the integral casts from deciding: a column with its own watermark is not 
held to the
+  // inexact-arithmetic rule, so only those two rules reject the conjuncts. 
Analysis rejects the
+  // plan before any state format is used.
+  test("left outer join cannot finalize with a negative opposite watermark 
term") {
+    val leftInput = MemoryStream[(Int, Long)]
+    val rightInput = MemoryStream[(Int, Long)]
+    val left = leftInput.toDF().toDF("leftKey", "time")
+      .select($"leftKey", timestamp_seconds($"time").as("leftTime"))
+      .withWatermark("leftTime", "0 seconds")
+    val right = rightInput.toDF().toDF("rightKey", "time")
+      .select($"rightKey", timestamp_seconds($"time").as("rightTime"))
+      .withWatermark("rightTime", "0 seconds")
+    val condition = expr("leftKey = rightKey AND " +
+      "CAST(leftTime AS LONG) > 100 - CAST(rightTime AS LONG) AND 
CAST(leftTime AS LONG) > 10")
+    val joined = left.join(right, condition, "left_outer")
+      .select($"leftKey", $"leftTime".cast("long"), $"rightTime".cast("long"))
+
+    val error = intercept[AnalysisException] {
+      testStream(joined)(
+        AddData(leftInput, (1, 60L)),
+        CheckNewAnswer())
+    }
+    assert(error.getMessage.contains("a watermark on the right input and a 
time constraint"))
+  }
+
+  // Earlier versions admitted a bound through a negative opposite watermark 
term, so disabling
+  // the finalization check runs this join. The left state is never evicted, 
so the matched left

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 
[37eebbfe08d](https://github.com/apache/spark/pull/60286/commits/37eebbfe08d6c07269007d66e813fdb5bcab3a1d).
 The test now puts a watermark on `leftTime`, so the truncating casts no longer 
decide eviction, and keeps an unmatched left row at 30 seconds that the 
watermark has passed. A later right row at 80 seconds must match it, and the 
comment says so. Evicting the left state at the old `100 - W` cutoff, by 
turning the negative-term rule off, makes the test fail, which I checked.
   
   <!-- sp2k-review11 reply 4599498453 -->
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -66,24 +752,42 @@ object StreamingJoinHelper extends PredicateHelper with 
Logging {
    * @param attributesWithEventWatermark  attributes of the other side which 
has a watermark column
    * @param joinCondition                 join condition
    * @param eventWatermark                watermark defined on the input event 
data
+   * @param exactForUnwatermarkedColumns  whether a column without its own 
watermark gets no
+   *                                      cutoff from a condition that the 
solver counts
+   *                                      inexactly. State eviction and the 
admission check set
+   *                                      it, so they agree.
+   * @param baseAdmissionRules            whether to skip the check that the 
bounded column is a

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 
[d38d50da16e](https://github.com/apache/spark/pull/60284/commits/d38d50da16ec25724332de6ccdf965e7fc6d16dd)
 on #60284. The `baseAdmissionRules` doc now lists every rule the flag skips: 
the TIMESTAMP-or-window column rule, the rule that a bound needs a term on the 
other input's watermarked event time, so a constant-only conjunct still gives a 
cutoff, and the sign rule, so a negated term still gives one. It names both 
callers: the admission rule kept with the finalization check disabled, and the 
restart check's solve of the older bound. The sign rule itself now lives in 
#60284.
   
   <!-- sp2k-review11 reply 4599498378 -->
   



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