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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TimeWindow.scala:
##########
@@ -187,6 +187,25 @@ case class TimeWindow(
 
 object TimeWindow {
   val marker = "spark.timeWindow"
+  // Window length in microseconds, recorded next to the marker. A 
stream-stream join compares
+  // it to decide whether an ordering of two window columns can evict state.
+  val durationKey = "spark.timeWindow.durationUs"
+
+  /**
+   * The metadata of a union's output column, given the children's columns at 
its position: the
+   * first child's, which a union keeps, but without the window length unless 
every child records
+   * the same one. Otherwise a join would read the first child's length for 
every child's rows.
+   */
+  def unionOutputMetadata(columns: Seq[Attribute]): Metadata = {
+    val first = columns.head.metadata
+    def sameDuration(metadata: Metadata): Boolean =
+      metadata.contains(durationKey) && metadata.getLong(durationKey) == 
first.getLong(durationKey)
+    if (first.contains(durationKey) && !columns.tail.forall(c => 
sameDuration(c.metadata))) {

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, reproduced and fixed in 
[1ee972a3d0a](https://github.com/apache/spark/pull/60283/commits/1ee972a3d0a69925484a5ab4ae268c0bb129561a):
 the left outer join of 20-second left windows with a union of 10- and
   5-second right windows failed analysis. A window column now records its 
shortest and longest window
   length. window() records its length as both, window_time() removes both, and 
a union records the
   smallest shortest and the largest longest of its children, or none when a 
child records none. A
   whole-window comparison gives a cutoff when the stored column's shortest 
window lasts at least as
   long as the other column's longest. That join now passes analysis and 
outputs its unmatched left
   windows, an inner join with a watermarked left input evicts them, and the 
existing union tests
   whose children fail the rule still give no cutoff. The window function tests 
check the lengths a
   union records in the analyzed and the physical plan.
   
   <!-- sp2k-review11 reply 4598327146 -->
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -198,13 +932,80 @@ object StreamingJoinHelper extends PredicateHelper with 
Logging {
       }
     }.reduceLeft(Add(_, _))
 
+    // Callers can pass only the constrained column and the opposite event 
time, so a term on any
+    // other column passes the occurrence check above. Its value is unknown 
here.
+    if (exprWithWatermarkSubstituted.references.nonEmpty) {
+      logDebug(s"Failed to extract state constraint: unresolved 
$exprWithWatermarkSubstituted")
+      return None
+    }
+
     // Calculate the constraint value
     logInfo(log"Final expression to evaluate " +
       log"constraint:\t${MDC(WATERMARK_CONSTRAINT, 
exprWithWatermarkSubstituted)}")
     val constraintValue = 
exprWithWatermarkSubstituted.eval().asInstanceOf[java.lang.Double]
     Some((Double2double(constraintValue) / 1000.0).toLong)
   }
 
+  /** Whether a column of a join input can take a range cutoff: TIMESTAMP, or 
a time window. */
+  def canTakeRangeCutoff(column: Attribute): Boolean =
+    column.dataType == TimestampType || windowDuration(column).isDefined
+
+  /** The length of the windows in a column built by window(), when the column 
records it. */
+  private def windowDuration(column: Attribute): Option[Long] = {
+    val metadata = column.metadata
+    if (metadata.contains(TimeWindow.marker) && 
metadata.contains(TimeWindow.durationKey)) {

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, reproduced and fixed in 
[1ee972a3d0a](https://github.com/apache/spark/pull/60283/commits/1ee972a3d0a69925484a5ab4ae268c0bb129561a):
 a stream of a table created from 20-second windows and appended with
   10-second windows lost all four matches of the window [90 s, 100 s). 
Recorded window lengths now
   stay with the query whose window() builds the column. The shared conversion 
from a schema to a
   relation's columns removes them, so columns read from tables, files, 
`readStream.schema` or
   `createDataFrame` keep the window marker but no length; a nested field of 
such a schema gives no
   length either; and `Dataset.to` keeps the column's own length rather than 
the supplied schema's.
   That join now keeps the 10-second window and outputs its matches, and its 
left outer form fails
   analysis. I left view resolution as it is: its column is not resolved where 
the stored schema is
   applied, and a persisted view's stored schema can only hold lengths that 
window() in the view text
   produced, which re-analysis reproduces, since columns read from tables no 
longer carry them. The
   guide and the migration note say that such window columns do not count.
   
   <!-- sp2k-review11 reply 4598327168 -->
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -66,13 +752,27 @@ 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
+   *                                      TIMESTAMP column or a time window, 
which Spark 4.4
+   *                                      added to the solver. It does not 
skip the inexact
+   *                                      arithmetic check for columns without 
their own
+   *                                      watermark, which 
exactForUnwatermarkedColumns alone
+   *                                      controls; leave that false too for 
the result of
+   *                                      earlier versions. Only the admission 
rule that stays
+   *                                      in force with the finalization check 
disabled sets it.

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 
[1ee972a3d0a](https://github.com/apache/spark/pull/60283/commits/1ee972a3d0a69925484a5ab4ae268c0bb129561a).
 The doc now says that two callers set the flag: the admission rule that stays 
in force with the finalization check disabled, and the restart compatibility 
check's solve of the bound that older versions derived.
   
   <!-- sp2k-review11 reply 4598327179 -->
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -198,13 +932,80 @@ object StreamingJoinHelper extends PredicateHelper with 
Logging {
       }
     }.reduceLeft(Add(_, _))
 
+    // Callers can pass only the constrained column and the opposite event 
time, so a term on any
+    // other column passes the occurrence check above. Its value is unknown 
here.
+    if (exprWithWatermarkSubstituted.references.nonEmpty) {
+      logDebug(s"Failed to extract state constraint: unresolved 
$exprWithWatermarkSubstituted")
+      return None
+    }
+
     // Calculate the constraint value
     logInfo(log"Final expression to evaluate " +
       log"constraint:\t${MDC(WATERMARK_CONSTRAINT, 
exprWithWatermarkSubstituted)}")
     val constraintValue = 
exprWithWatermarkSubstituted.eval().asInstanceOf[java.lang.Double]
     Some((Double2double(constraintValue) / 1000.0).toLong)
   }
 
+  /** Whether a column of a join input can take a range cutoff: TIMESTAMP, or 
a time window. */
+  def canTakeRangeCutoff(column: Attribute): Boolean =
+    column.dataType == TimestampType || windowDuration(column).isDefined
+
+  /** The length of the windows in a column built by window(), when the column 
records it. */
+  private def windowDuration(column: Attribute): Option[Long] = {
+    val metadata = column.metadata
+    if (metadata.contains(TimeWindow.marker) && 
metadata.contains(TimeWindow.durationKey)) {
+      Some(metadata.getLong(TimeWindow.durationKey))
+    } else {
+      None
+    }
+  }
+
+  /**
+   * Whether evicting `stateWindow` by its end is safe when a range condition 
compares it, as a
+   * whole, with `watermarkWindow`, the other input's watermarked time window. 
Ordering windows
+   * compares their starts first. A window evicted at a cutoff no higher than 
the watermark
+   * starts at least its own duration before the watermark, while a later 
window of the other
+   * input ends above the watermark, so it starts less than its own duration 
before it. When the
+   * stored windows last at least as long as the other input's, every later 
window therefore
+   * starts after every evicted window and orders above it. Shorter stored 
windows get no cutoff.

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.
   
   Added in 
[1ee972a3d0a](https://github.com/apache/spark/pull/60283/commits/1ee972a3d0a69925484a5ab4ae268c0bb129561a).
 `TODO(SPARK-61510)` sits at the window rule and says that shorter stored 
windows get no cutoff until then: an inner join keeps their state, and an outer 
or left semi join bounded only that way fails analysis.
   
   <!-- sp2k-review11 reply 4598327220 -->
   



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