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


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/IncrementalExecution.scala:
##########
@@ -167,6 +171,29 @@ class IncrementalExecution(
   private val allowMultipleStatefulOperators: Boolean =
     
sparkSession.sessionState.conf.getConf(SQLConf.STATEFUL_OPERATOR_ALLOW_MULTIPLE)
 
+  // The lags of the planned join inputs, against the columns that they filter 
late rows by. They
+  // read the optimized plan, whose expression IDs the planned joins use. The 
analysis check reads
+  // the analyzed plan with the same rule, against every column that pruning 
can leave first, so
+  // it bounds a column here whenever it bounds one there. The projections 
that build the later
+  // watermarked columns stay in every batch's optimized plan: they sit above 
the input's
+  // watermark node, which the optimizer keeps even for a batch without data.
+  private lazy val eventTimeLags: Option[EventTimeLags] =
+    if (allowMultipleStatefulOperators) {
+      None
+    } else {
+      Some(StreamingJoinHelper.findEventTimeLags(optimizedPlan, planned = 
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.
   
   Thanks, fixed in 
[ab59957f358](https://github.com/apache/spark/pull/60280/commits/ab59957f3582d270164fd563b24f31603ff5e54c)
 on #60280, and it reaches this PR through the next merge. Execution still 
measures
   each lag on the batch's optimized plan. In addition, the columns at each 
union position that some
   input does not watermark are read from the batch's analyzed plan, where an 
idle input is still an
   empty relation under its projections, and a column that reaches one of them 
has no bound. The set
   holds the union's column and every input's column at that position, so a 
rename that the
   optimizer strips, or the re-alias when the first input is removed, still 
lands on a marked ID. A
   column marked this way gets no bound by any path, in analysis and in 
execution alike, which only
   keeps more state. The comment above `eventTimeLags` and the migration guide 
are corrected.
   
   Tests: your range scenario and its equality-key form, on state formats 2 and 
3: the left row
   stays through a batch in which only the watermarked union input has data, 
and a later row of the
   unwatermarked input matches it. I did not add a separate test with a rename 
between the union and
   the join.
   
   <!-- sp2k-review11 reply 4597828365 -->
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -17,36 +17,720 @@
 
 package org.apache.spark.sql.catalyst.analysis
 
+import java.util.Locale
+
+import scala.util.Try
 import scala.util.control.NonFatal
 
 import org.apache.spark.internal.Logging
 import org.apache.spark.internal.LogKeys._
+import org.apache.spark.sql.catalyst.InternalRow
 import org.apache.spark.sql.catalyst.expressions._
-import org.apache.spark.sql.catalyst.planning.ExtractEquiJoinKeys
-import org.apache.spark.sql.catalyst.plans.logical.{EventTimeWatermark, 
LogicalPlan}
+import org.apache.spark.sql.catalyst.plans.{InnerLike, LeftOuter, RightOuter}
+import org.apache.spark.sql.catalyst.plans.logical.{Expand, Join, LogicalPlan, 
Project, UnaryNode, Union}
 import org.apache.spark.sql.catalyst.plans.logical.EventTimeWatermark._
-import org.apache.spark.sql.catalyst.util.DateTimeConstants.MICROS_PER_DAY
+import org.apache.spark.sql.catalyst.util.DateTimeConstants.{MICROS_PER_DAY, 
MICROS_PER_HOUR, MICROS_PER_MILLIS, MICROS_PER_MINUTE, MICROS_PER_SECOND}
 import org.apache.spark.sql.types._
 import org.apache.spark.unsafe.types.CalendarInterval
 
+/**
+ * How far below a column that can filter its stream-stream join input's late 
rows one of the
+ * input's watermarked columns can be, for an equality key or a range 
condition on it: at most
+ * `micros` where the plan relates the two columns; by an amount that the plan 
does not bound when
+ * `unbounded`; and by an amount that it does not show when `unrelated`, 
because it does not relate
+ * the two. A window column is compared by its end, as eviction and late 
filtering compare it.
+ */
+case class EventTimeLag(micros: Long, unrelated: Boolean, unbounded: Boolean) {
+  def max(other: EventTimeLag): EventTimeLag = EventTimeLag(math.max(micros, 
other.micros),
+    unrelated || other.unrelated, unbounded || other.unbounded)
+
+  /**
+   * The cutoff for the other input's equality key when this input's watermark 
is `watermark`.
+   * A key that the plan does not relate keeps the Spark 4.3 cutoff.
+   */
+  def keyWatermark(watermark: Long): Option[Long] =
+    if (unbounded) None else Some(watermark - EventTimeLag.ceilMillis(micros))

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. We keep this behavior on purpose. In legacy mode, a key that the 
plan does not relate to
   the late-filtered column keeps the Spark 4.3 cutoff. We lower or remove a 
key's cutoff only where
   the plan shows that rows can hold the key below that column. Round 4 of 
#60280 (470c0cc6b63)
   tried the rule you suggest, and it removed the bound from many keys that are 
safe in practice:
   keys behind a repartition, a view, a typed filter, `observe`, a hint, a 
stream-static join, a
   self-join or a session window. Legacy mode is a fallback that few queries 
use, so we do not want
   to change released queries where we cannot show a problem. The key form of 
the scenario,
   `l = v`, returns the same result as on the base branch; it does not get 
worse.
   
   Two related cases where the plan does show a problem are now fixed in 
[ab59957f358](https://github.com/apache/spark/pull/60280/commits/ab59957f3582d270164fd563b24f31603ff5e54c)
 on #60280: an unrelated key
   no longer wins over a key with a known lag, and an earlier-join equality 
whose lag has no bound
   now gives no bound instead of being unrelated. The `keyWatermark` doc and 
the migration guide
   already state the Spark 4.3 rule for unrelated keys.
   
   <!-- sp2k-review11 reply 4597828347 -->
   



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