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


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -896,7 +979,14 @@ case class StreamingSymmetricHashJoinExec(
         //   is done at this point. That said, input can be skipped to be 
added to the state store
         //   if input is going to be evicted in this batch. Though, input 
should be added to the
         //   state store if it's right outer join or full outer join, as 
unmatched output is
-        //   handled during state eviction.
+        //   handled during state eviction. A left semi join processes the 
right side first, so
+        //   the left input of this batch has not probed it yet: an input this 
batch evicts is
+        //   added, and the eviction at the end of the batch removes it, 
unless a cutoff at the
+        //   late-events watermark that is exact today shows no left row 
passing late filtering
+        //   can match it (see getLeftSemiExactRightCutoffs). The cutoff 
derivation gives no exact
+        //   cutoff otherwise: it can apply a bound on another right column to 
the right event

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 
[c984d5cd38b](https://github.com/apache/spark/pull/60279/commits/c984d5cd38be5c8c24dd2120235af9ceb2db4dfc).
 In this PR, a bound on another right column no longer reaches the right event 
time, so we removed that reason from the left semi storing comment and from the 
`getLeftSemiExactRightCutoffs` doc. Both now keep the whole-day and 
truncating-cast reasons and cite only `TODO(SPARK-61381)`.
   
   <!-- sp2k-review3 reply 4537966948 -->
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinHelper.scala:
##########
@@ -296,6 +292,229 @@ object StreamingSymmetricHashJoinHelper extends Logging {
     JoinStateWatermarkPredicates(leftStateWatermarkPredicate, 
rightStateWatermarkPredicate)
   }
 
+  /** Keep each cutoff attached to the column constrained by the join 
condition. */
+  private def getStateValueWatermarkForInput(
+      inputAttributes: Seq[Attribute],
+      otherInputAttributes: Seq[Attribute],
+      condition: Option[Expression],
+      eventWatermark: Option[Long]): Option[(Attribute, Long)] = {
+    val eventTime = inputAttributes.find(_.metadata.contains(delayKey))
+    // First try the watermarked column. If it has no inferable bound, try the 
other timestamp
+    // columns in input schema order and use the first with a bound. This 
makes the indexed column
+    // deterministic for a fixed input schema and join condition.
+    val candidates = eventTime.toSeq ++ inputAttributes.filter { attribute =>
+      attribute.dataType == TimestampType && 
!eventTime.exists(_.semanticEquals(attribute))
+    }
+    candidates.iterator.flatMap { attribute =>
+      StreamingJoinHelper.getStateValueWatermark(AttributeSet(Seq(attribute)),
+        AttributeSet(otherInputAttributes), condition, 
eventWatermark).map(attribute -> _)
+    }.take(1).toSeq.headOption
+  }
+
+  /**
+   * Conservatively prove that older state can use the current watermark 
semantics. Without
+   * watermarks there is no affected state. An input that neither version 
evicts keeps the same
+   * rows, but its state watermark in the join's output watermark changed, so 
such an input is
+   * accepted only when no stateful operator after the join reads its event 
time:
+   * `consumedSides` names the inputs whose event time such an operator reads, 
both when the
+   * operator cannot be traced and none when no stateful operator follows. A 
key-only join, with
+   * no other condition and no equality key that uses an event time, is 
evicted by neither
+   * version, and V4 reads its rows by key prefix. Otherwise each side must 
have exactly one
+   * event-time column, and either its own event time must be the sole 
watermarked equality key
+   * at the same ordinal, or the join must be a time-interval join on the 
event times (see
+   * [[isOwnEventTimeIntervalJoin]]). This excludes other range bounds.
+   */
+  private[sql] def isWatermarkCheckpointCompatible(
+      leftAttributes: Seq[Attribute],
+      rightAttributes: Seq[Attribute],
+      leftKeys: Seq[Expression],
+      rightKeys: Seq[Expression],
+      condition: Option[Expression],
+      consumedSides: Set[JoinSide]): Boolean = {
+    val leftEventTime = leftAttributes.filter(_.metadata.contains(delayKey))
+    val rightEventTime = rightAttributes.filter(_.metadata.contains(delayKey))
+    val keyOnly = condition.isEmpty && !(leftKeys ++ rightKeys).exists { key =>
+      key.references.exists(_.metadata.contains(delayKey)) || (key match {
+        case named: NamedExpression => named.metadata.contains(delayKey)
+        case _ => false
+      })
+    }
+
+    def watermarkKeyOrdinals(keys: Seq[Expression]): Seq[Int] = {
+      keys.zipWithIndex.collect {
+        case (key: NamedExpression, index) if key.metadata.contains(delayKey) 
=> index
+      }
+    }
+
+    def ownEventTimeKeyOrdinal(keys: Seq[Expression], eventTime: Attribute): 
Option[Int] = {
+      keys.zipWithIndex.collectFirst {
+        case (key: NamedExpression, index)
+            if key.metadata.contains(delayKey) && 
key.semanticEquals(eventTime) => index
+      }
+    }
+
+    val leftOrdinals = watermarkKeyOrdinals(leftKeys)
+    val rightOrdinals = watermarkKeyOrdinals(rightKeys)
+    if (leftEventTime.isEmpty && rightEventTime.isEmpty) {
+      leftOrdinals.isEmpty && rightOrdinals.isEmpty
+    } else if (keyOnly && consumedSides.isEmpty) {
+      true
+    } else if (leftEventTime.size == 1 && rightEventTime.size == 1) {
+      val leftOrdinal = ownEventTimeKeyOrdinal(leftKeys, leftEventTime.head)
+      val rightOrdinal = ownEventTimeKeyOrdinal(rightKeys, rightEventTime.head)
+      val alignedOwnEventTimeKeys = leftOrdinal.isDefined && leftOrdinal == 
rightOrdinal &&
+        leftOrdinals == leftOrdinal.toSeq && rightOrdinals == 
rightOrdinal.toSeq
+      alignedOwnEventTimeKeys || isOwnEventTimeIntervalJoin(leftAttributes, 
rightAttributes,
+        leftKeys, rightKeys, condition, leftEventTime.head, 
rightEventTime.head, consumedSides)
+    } else {
+      false
+    }
+  }
+
+  /**
+   * Whether every ordering conjunct compares the two inputs' TIMESTAMP event 
times, each only
+   * cast or offset by constants (see 
[[StreamingJoinHelper.isConstantOffsetOf]]), and each event
+   * time either gets a range bound or gets none and is not in 
`consumedSides`. Older versions
+   * and this one solve such conjuncts the same way for columns with their own 
watermark, and
+   * older versions solved every column of an input but then found bounds only 
in the same
+   * conjuncts, so they derived the same value predicates, scan starts and V4 
index columns, and
+   * neither evicts an input that gets no bound. Old V4 stored a null event 
time at timestamp 0
+   * under the row's own key; both versions scan that entry the same way. No 
equality key may
+   * use an event time: older versions evicted such keys at the unshifted 
watermark. Time windows
+   * are excluded: older versions derived an ordering bound for them that this 
version removes.

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 
[c984d5cd38b](https://github.com/apache/spark/pull/60279/commits/c984d5cd38be5c8c24dd2120235af9ceb2db4dfc).
 You are right: this commit still derives the older bound for whole windows. 
The restart check's doc and the test comment now say that, and that SPARK-61348 
later removes the bound for windows shorter than the other input's. That change 
is why the check does not accept window orderings.
   
   <!-- sp2k-review3 reply 4537966961 -->
   



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