spark-test commented on code in PR #60664:
URL: https://github.com/apache/spark/pull/60664#discussion_r4641869235


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -186,6 +209,16 @@ object StreamingJoinHelper extends PredicateHelper with 
Logging {
       return None
     }
 
+    // The cutoff must rise with the watermark: `watermark + c < leftTime`. 
Without the watermark
+    // term, the cutoff never rises and caps the other conjuncts' cutoffs. 
With `-watermark`, the
+    // cutoff falls, and later rightTime values can still match old leftTime 
values.
+    val watermarkTerms = terms.filter(_.references.exists { a =>
+      attributesWithEventWatermark.contains(a) && a.metadata.contains(delayKey)
+    })
+    if (watermarkTerms.size != 1 || 
watermarkTerms.head.isInstanceOf[UnaryMinus]) {

Review Comment:
   Thanks, a cutoff through a cast to an integral type, such as `CAST(lt AS 
LONG)`, now keeps master's minimum, and the new test covers this example. The 
check still misses casts that round or lose precision, such as `CAST(lt AS 
DECIMAL(10,0))` or `CAST(lt AS FLOAT)`, which the solver also passes through as 
exact, so a constant-only or negative-sign conjunct beside one of them no 
longer holds back the cutoff. This review raises that as a finding.
   
   <!-- SPARK_DEV_REVIEW_REPLY 
{"feedback_id":"inline:4622938130","thread_id":"inline:4622938130","verdict_sha256":"4dcabd6c2a6e2c4863cade8f2db0595976b41581a4d1a1acf71d24acbadd6d7d"}
 -->



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -111,10 +116,47 @@ object StreamingJoinHelper extends PredicateHelper with 
Logging {
       if (stateWatermark.nonEmpty) {
         logInfo(log"Condition ${MDC(JOIN_CONDITION, joinCondition)} generated 
" +
           log"watermark constraint = ${MDC(WATERMARK_CONSTRAINT, 
stateWatermark.get)}")
+        truncatingCutoff ||= predicate.exists {
+          case Cast(c, _: IntegralType, _, _) => 
!c.dataType.isInstanceOf[IntegralType]

Review Comment:
   **Blocking (P1):** This check treats a cutoff as inexact only when a 
conjunct casts a non-integral value to an integral type, but `collectTerms` 
passes every cast to a numeric type through as if it were exact 
(`StreamingJoinHelper.scala:309-311`). A TIMESTAMP cast to DECIMAL with fewer 
than six fractional digits rounds half up, and a cast to FLOAT keeps about 
seven significant digits, so a cutoff solved through either is as inexact as 
one through `CAST(lt AS LONG)`, yet nothing restores the earlier minimum for it.
   
   By the review's reading of the code, take a left outer join with 1-second 
watermarks on `leftKey = rightKey AND CAST(lt AS DECIMAL(10,0)) >= CAST(rt AS 
DECIMAL(10,0)) AND CAST(lt AS LONG) > 10`, fed left (1, 39.3 s) with right (9, 
39.3 s), then left (8, 40.4 s) with right (9, 40.4 s), then right (9, 40.5 s), 
then right (1, 39.45 s). The rule drops the constant, so once the watermark 
reaches 39.4 s the left cutoff is 39.399 s: the row (1, 39.3) is evicted and 
emitted as (1, 39.3, null), and the right row (1, 39.45), which passes late 
filtering, finds no match although both values round to 39. On master the 
constant held the left cutoff at 10 s, the row stayed, and the join emitted (1, 
39.3, 39.45). A negative-sign conjunct beside such a bound behaves the same 
way, and an inner join loses the match.
   
   Treating as inexact every cast that does not keep its input exactly, 
including casts to DECIMAL with fewer fractional digits than the input carries 
and casts to FLOAT from a type that FLOAT cannot hold exactly, on either 
input's column, keeps the earlier minimum for these joins until SPARK-61381 
makes such cutoffs exact.
   
   This extends existing discussions 
[1](https://github.com/apache/spark/pull/60664#issuecomment-7005786499), 
[2](https://github.com/apache/spark/pull/60664#issuecomment-7035636180), 
[3](https://github.com/apache/spark/pull/60664#discussion_r4622938130), 
[4](https://github.com/apache/spark/pull/60664#discussion_r4627046550).
   
   **Verification:**
   - **Regression:** On state formats 2 to 4, a left outer join with 1-second 
watermarks on `leftKey = rightKey AND CAST(lt AS DECIMAL(10,0)) >= CAST(rt AS 
DECIMAL(10,0)) AND CAST(lt AS LONG) > 10`, fed left (1, 39.3 s) with right (9, 
39.3 s), left (8, 40.4 s) with right (9, 40.4 s), right (9, 40.5 s) and right 
(1, 39.45 s), emits (1, 39.3, 39.45) and no (1, 39.3, null).
   - **Behavior:** The solver returns the earlier minimum for DECIMAL(10,0) and 
FLOAT bounds beside a constant-only conjunct and beside a negative-sign 
conjunct, and the rule's cutoff for the same conditions written with a cast to 
DECIMAL with six fractional digits.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -710,17 +740,44 @@ case class StreamingSymmetricHashJoinExec(
         Predicate.create(Literal(false), Seq.empty).eval _ // false = do not 
remove if no predicate
     }
 
+    private[this] var updatedStateRowsCount = 0
+    private[this] var numRemovedFromOtherSideDuringJoinCount = 0
+    private[this] val allowMultipleStatefulOperators: Boolean =
+      conf.getConf(SQLConf.STATEFUL_OPERATOR_ALLOW_MULTIPLE)
+
+    // When the default mode picked the column that the value predicate tests 
because the join
+    // condition bounds it, a row with a NULL in it can never match, and 
`column <= cutoff` never
+    // holds for it. The legacy mode, and the default mode for an input 
bounded only through a
+    // column cast to TIMESTAMP, test the event time, which the condition need 
not bound.
+    private[this] val isEvictionColumnNull: UnsafeRow => Boolean = 
stateWatermarkPredicate match {
+      case Some(JoinStateValueWatermarkPredicate(LessThanOrEqual(column, _), 
_, _, true)) =>
+        Predicate.create(IsNull(column), inputAttributes).eval _
+      case _ => _ => false
+    }
+
+    // Such a row is not stored, but an earlier version or the legacy mode can 
have stored one.
     private[this] val stateValueWatermarkPredicateFunc = 
stateWatermarkPredicate match {
-      case Some(JoinStateValueWatermarkPredicate(expr, _, _)) =>
+      case Some(JoinStateValueWatermarkPredicate(expr @ 
LessThanOrEqual(column, _), _, _, true)) =>
+        Predicate.create(Or(expr, IsNull(column)), inputAttributes).eval _
+      case Some(JoinStateValueWatermarkPredicate(expr, _, _, _)) =>
         Predicate.create(expr, inputAttributes).eval _
       case _ =>
         Predicate.create(Literal(false), Seq.empty).eval _  // false = do not 
remove if no predicate
     }
 
-    private[this] var updatedStateRowsCount = 0
-    private[this] var numRemovedFromOtherSideDuringJoinCount = 0
-    private[this] val allowMultipleStatefulOperators: Boolean =
-      conf.getConf(SQLConf.STATEFUL_OPERATOR_ALLOW_MULTIPLE)
+    // A left semi join processes its right input first, so this batch's left 
rows can still match
+    // a right row that the completion check skips (SPARK-61380). Earlier 
versions tested it against
+    // the cutoff on the event time solved over the whole input, which a 
picked column can exceed.
+    private[this] val skipsEvictingRightRow: UnsafeRow => Boolean = 
stateWatermarkPredicate match {
+      case Some(p: JoinStateValueWatermarkPredicate) if 
p.conditionBoundsColumn &&
+          joinType == LeftSemi && joinSide == RightSide =>
+        val cutoff = 
StreamingJoinHelper.getStateValueWatermark(AttributeSet(inputAttributes),

Review Comment:
   **Blocking (P1):** This call takes the solver's default `risingCutoffsOnly = 
true`. For a left semi join whose condition bounds the right event time with a 
rising range conjunct and also has a negative-sign conjunct across both inputs, 
such as `CAST(lt AS LONG) + CAST(rt AS LONG) > 100`, the skip therefore 
compares the right event time with the rising bound alone, while on master that 
conjunct's falling cutoff kept the minimum below every row and the completion 
check stored every right row. A left semi join processes its right input first, 
so a right row skipped here can no longer be matched by a left row of the same 
batch.
   
   By the review's reading of the code, take `k = rk AND rt BETWEEN lt - 
INTERVAL 10 MINUTES AND lt + INTERVAL 10 MINUTES AND CAST(lt AS LONG) + CAST(rt 
AS LONG) > 100` with 0-second watermarks in the default mode, fed left (9, 1000 
s) with right (9, 1000 s), then left (8, 2000 s) with right (8, 2000 s), then 
right (1, 1200 s) with left (1, 1300 s), with no batch without data between the 
second and third batches, as in a backlog. In the third batch both rows pass 
late filtering at 1000 s, but the right cutoff at the eviction watermark is 
1399.999 s, so the right row is not stored; the left row finds no match and is 
evicted by the left cutoff of 1399.999 s, and (1, 1300) is never emitted. On 
master both cutoffs are -1900 s, the right row is stored, and the join emits 
(1, 1300). The legacy mode filters late rows by the eviction watermark and is 
not affected.
   
   Passing `risingCutoffsOnly = false` in this call restores the test that the 
comment above it says the check keeps, while eviction of stored right rows 
keeps the rule.
   
   **Verification:**
   - **Regression:** In the default mode on state formats 2 to 4, with no-data 
micro-batches disabled so that the third batch directly follows the second, the 
left semi join above with 0-second watermarks, fed left (9, 1000 s) with right 
(9, 1000 s), then left (8, 2000 s) with right (8, 2000 s), then right (1, 1200 
s) with left (1, 1300 s), emits (1, 1300) in the third batch.
   - **Compatibility:** In the same join, a right row whose event time falls 
more than 10 minutes below the watermark is evicted from state at the end of 
the batch, as before the repair.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -66,13 +66,17 @@ 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 risingCutoffsOnly             whether only conjuncts whose cutoff 
rises with the

Review Comment:
   **Nit (P3):** With the default, the method still returns the earlier 
minimum, constant-only and negative-sign conjuncts included, in two cases that 
lines 126-136 handle: a remaining cutoff goes through a truncating cast, or a 
remaining cutoff was solved for a column other than the input's event time and 
the event time has no rising cutoff of its own. When no conjunct gives a rising 
cutoff, it returns no cutoff, even where earlier versions returned one. The new 
StreamingJoinHelperSuite tests assert 10000, the constant's value, for 
`rightTime <= expiry AND CAST(leftTime AS LONG) > 10` over {leftTime, expiry}, 
which this doc says the flag excludes. A caller who reads only this doc would 
assume that non-rising conjuncts never lower the default result; naming both 
cases and the no-cutoff case here keeps it accurate.
   
   **Verification:**
   - **Inspection:** The scaladoc of getStateValueWatermark states that the 
default returns the earlier minimum when a remaining cutoff goes through a cast 
the method treats as inexact, or when a remaining cutoff was solved for a 
column other than the input's event time and the event time has no rising 
cutoff of its own, and that it returns no cutoff when no conjunct gives a 
rising one, matching the fallback in the method body.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -66,13 +66,17 @@ 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 risingCutoffsOnly             whether only conjuncts whose cutoff 
rises with the
+   *                                      watermark give a cutoff. Earlier 
versions also took
+   *                                      constant-only and negative-sign 
conjuncts.
    * @return state value watermark in milliseconds, is possible.
    */
   def getStateValueWatermark(
       attributesToFindStateWatermarkFor: AttributeSet,
       attributesWithEventWatermark: AttributeSet,
       joinCondition: Option[Expression],
-      eventWatermark: Option[Long]): Option[Long] = {
+      eventWatermark: Option[Long],
+      risingCutoffsOnly: Boolean = true): Option[Long] = {

Review Comment:
   Thanks, inputs on the whole-input path now report master's state watermark, 
and so do inputs with a truncating-cast bound. For an input the rule leaves 
without a cutoff, the SPARK-62001 guard checks only Append mode, and a query 
restarted from a checkpoint written before this change only logs a warning, so 
in Update mode and after such a restart a stateful operator after the join 
still drops rows that master passed to it. Separately, the exactness check 
added since recognizes only casts to an integral type, so the DECIMAL and FLOAT 
casts this comment listed still drop the cap. This review raises both as 
findings.
   
   <!-- SPARK_DEV_REVIEW_REPLY 
{"feedback_id":"inline:4627046550","thread_id":"inline:4627046550","verdict_sha256":"4dcabd6c2a6e2c4863cade8f2db0595976b41581a4d1a1acf71d24acbadd6d7d"}
 -->



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