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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -186,6 +910,48 @@ object StreamingJoinHelper extends PredicateHelper with 
Logging {
       return None
     }
 
+    // The terms count a whole day as 24 hours, although Spark adds days in 
the session time
+    // zone, where a day can be 23 or 25 hours, and read CAST(ts AS BIGINT) as 
ts, although the
+    // cast drops the fraction of a second. Either can put the cutoff above a 
row that can still
+    // match. A column with its own watermark keeps such a cutoff, as existing 
queries rely on
+    // it. With exactForUnwatermarkedColumns, any other column, such as the 
bounded column of an
+    // input without a watermark, gets no cutoff from such a condition.
+    val stateColumnHasWatermark = constraintTerm.exists {
+      case a: AttributeReference =>
+        attributesToFindStateWatermarkFor.contains(a) && 
a.metadata.contains(delayKey)
+      case _ => false
+    }
+    if (exactForUnwatermarkedColumns && !stateColumnHasWatermark &&
+        hasInexactArithmetic(allOnLeftExpr)) {
+      logDebug(s"Inexact arithmetic gives an unwatermarked column no cutoff: 
$allOnLeftExpr")
+      return None
+    }
+
+    // State eviction compares this cutoff in timestamp units against the 
constrained column, or
+    // against its end for a time window; see [[isWindowEvictionSafe]].
+    val stateColumn = constraintTerm.collectFirst {
+      case a: AttributeReference if 
attributesToFindStateWatermarkFor.contains(a) => a
+    }
+    val watermarkColumn = terms.flatMap(_.collect {
+      case a: AttributeReference
+          if attributesWithEventWatermark.contains(a) && 
a.metadata.contains(delayKey) => a
+    }).headOption
+    val canEvictStateColumn = stateColumn.exists { column =>
+      column.dataType == TimestampType || 
watermarkColumn.exists(isWindowEvictionSafe(column, _))
+    }
+    if (!baseAdmissionRules && !canEvictStateColumn) return None
+
+    // A bound needs a term on the opposite input's watermarked event time; a 
conjunct that uses
+    // only constants does not follow the watermark, and gives a cutoff only 
with
+    // keepConstantOnly. The occurrence check above limits each input to one 
attribute, so there
+    // can be at most one such term.
+    // TODO(SPARK-61350): a watermark term with a negative coefficient still 
passes this check.
+    val watermarkTerms = 
terms.filter(_.references.exists(attributesWithEventWatermark.contains))
+    if (!baseAdmissionRules && (watermarkTerms.isEmpty && !keepConstantOnly ||

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 
[d38d50da16e](https://github.com/apache/spark/pull/60284/commits/d38d50da16ec25724332de6ccdf965e7fc6d16dd):
 the left semi and left outer joins on `lk = rk AND lt > rt - INTERVAL 1 SECOND 
*
   5 AND CAST(lt AS BIGINT) > 10` failed at start. The admission check now 
solves the condition in the
   form the optimizer gives eviction: each foldable expression becomes a 
literal and each NOT over a
   comparison becomes the opposite comparison, through De Morgan's laws. 
Current-time functions and
   expressions whose evaluation fails are left as they are, timestampadd is not 
rewritten, and the
   rule of earlier versions still reads the analyzed condition. Eviction, V4 
scans and the restart
   check are unchanged. Tests cover a product, a negated comparison, 
make_dt_interval, an interval
   divided by a number, a string cast to an interval and numeric arithmetic, 
for left outer and left
   semi joins, and keep current_timestamp(), an overflowing interval and 
constant-only bounds
   rejected.
   
   <!-- sp2k-review11 reply 4598373596 -->
   



##########
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).
 The doc now names every check 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 `CAST(lt AS BIGINT) > 10` 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.
   
   <!-- sp2k-review11 reply 4598373613 -->
   



##########
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.
   
   You are right, and our earlier answer was wrong for a build that has this PR 
without #60286. We moved the rule into this PR in 
[d38d50da16e](https://github.com/apache/spark/pull/60284/commits/d38d50da16ec25724332de6ccdf965e7fc6d16dd).
 A bound whose term on the other input's event time is negated, such as 
`CAST(lt AS DOUBLE) > 100 - CAST(rt AS DOUBLE)`, or a sum of both event times, 
now gives no cutoff. The constant no longer caps the minimum here, so without 
the rule such a bound let an outer join emit a row padded with nulls and lose 
its later match on the default state format. The admission rule of earlier 
versions and the restart check's older solve still read such bounds, as before.
   
   Tests: the solver and admission tests for sums and negated terms moved into 
this PR, and the end-to-end tests of #60286's arithmetic suite pass at this 
head and fail without the rule. #60286 keeps those tests and the guides' 
sentence on it, and no longer carries the rule itself.
   
   <!-- sp2k-review11 reply 4598374005 -->
   



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