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


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -722,6 +751,21 @@ case class StreamingSymmetricHashJoinExec(
     private[this] val allowMultipleStatefulOperators: Boolean =
       conf.getConf(SQLConf.STATEFUL_OPERATOR_ALLOW_MULTIPLE)
 
+    // The join condition bounds the column that the value predicate tests, so 
a row with a NULL
+    // in it can never match, and the predicate never evicts it. The legacy 
mode tests the event
+    // time even when the condition bounds another column. The same holds for 
an equality key
+    // that the opposite watermark selects; the key that 
findJoinKeyOrdinalForWatermark selects
+    // keeps the previous behavior.
+    private[this] val isEvictionColumnNull: UnsafeRow => Boolean = 
stateWatermarkPredicate match {

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.
   
   Reproduced on state formats 2 and 3: a left row with a NULL `lb` stored in 
legacy mode was never evicted after a restart in the default mode. Fixed: when 
the key that the opposite watermark picks is not the shared key, the key 
predicate also evicts a row whose key is NULL. The `expiry` half is fixed in 
#60667 the same way.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -403,19 +428,21 @@ case class StreamingSymmetricHashJoinExec(
     // Create left and right side hash joiners and store in the joiner manager.
     // Both sides should use the same store generator if we are re-using the 
same store instance.
     val joinStateManagerStoreGenerator = new JoinStateManagerStoreGenerator()
-    val joinKeyOrdinalForWatermark =
-      
StreamingSymmetricHashJoinHelper.findJoinKeyOrdinalForWatermark(leftKeys, 
rightKeys)
+    def joinKeyOrdinalForWatermark(side: JoinSide): Option[Int] =
+      findStateKeyOrdinalForWatermark(side, leftKeys, rightKeys, 
!allowMultipleStatefulOperators)

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.
   
   Reproduced: started in the default mode and restarted with allowMultiple set 
to false, the crossed-key join emitted `(5,50,null,null)` and 
`(null,null,5,50)` instead of `(5,50,5,50)`. Fixed in two places. In #60667, a 
query started in legacy mode records index version 0, so a later default-mode 
restart fails as for an earlier version. Here, a legacy-mode restart of a 
version-1 checkpoint fails when the default mode's key differs from the shared 
one. The error now names allowMultiple as a cause, and a failed restart keeps 
the recorded version.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -4399,6 +4399,20 @@ object SQLConf {
       .checkValue(v => Set(1, 2, 3, 4).contains(v), "Valid versions are 1, 2, 
3, and 4")
       .createWithDefault(2)
 
+  val STREAMING_JOIN_STATE_V4_INDEX_VERSION =
+    buildConf("spark.sql.streaming.join.stateFormatV4.indexVersion")
+      .internal()
+      .withBindingPolicy(ConfigBindingPolicy.NOT_APPLICABLE)
+      .doc("How state format version 4 of stream-stream joins orders the state 
of an input that " +

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.
   
   Updated: the doc now describes both the column and the equality-key ordering 
for versions 0 and 1, and says a restart fails when the recorded version orders 
an input differently.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinHelper.scala:
##########
@@ -309,12 +327,32 @@ object StreamingSymmetricHashJoinHelper extends Logging {
     // left side keys effectively causes the timestamp on the right side to 
have a watermark.
     // We will use the ordinal of the left timestamp in the left keys to find 
the corresponding
     // right timestamp in the right keys.
-    leftKeys.zipWithIndex.collectFirst {
+    watermarkedKeyOrdinal(leftKeys).orElse(watermarkedKeyOrdinal(rightKeys))
+  }
+
+  /**
+   * The ordinal of the equality key that evicts the state of one side: the 
first watermarked key
+   * of the opposite input, whose future rows have that key above the 
watermark. When the
+   * opposite input has no watermarked key, and in the legacy mode with a 
single global
+   * watermark, this is [[findJoinKeyOrdinalForWatermark]].
+   */
+  def findStateKeyOrdinalForWatermark(
+      joinSide: JoinSide,
+      leftKeys: Seq[Expression],
+      rightKeys: Seq[Expression],
+      useFirstEventTimeColumn: Boolean): Option[Int] = {
+    val otherSideKeys = if (joinSide == LeftSide) rightKeys else leftKeys
+    val sharedOrdinal = findJoinKeyOrdinalForWatermark(leftKeys, rightKeys)
+    if (useFirstEventTimeColumn) {
+      sharedOrdinal
+    } else {
+      watermarkedKeyOrdinal(otherSideKeys).orElse(sharedOrdinal)

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.
   
   Added a TODO for 
[SPARK-62002](https://issues.apache.org/jira/browse/SPARK-62002) at 
`findStateKeyOrdinalForWatermark`.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/UnsupportedOperationChecker.scala:
##########
@@ -708,20 +708,23 @@ object UnsupportedOperationChecker extends Logging {
   private def checkForStreamStreamJoinWatermark(join: Join): Unit = {
     val watermarkInJoinKeys = StreamingJoinHelper.isWatermarkInJoinKeys(join)
 
+    // Execution evicts the state of an input with its own watermark only by 
the column that
+    // getStateValueWatermarkColumn picks, such as not by a DATE column cast 
to TIMESTAMP.
+    def hasValidRange(input: LogicalPlan, other: LogicalPlan): Boolean =
+      if 
(input.output.exists(_.metadata.contains(EventTimeWatermark.delayKey))) {
+        StreamingJoinHelper.getStateValueWatermarkColumn(
+          input.output, other.outputSet, join.condition, 
Some(1000000)).isDefined
+      } else {
+        StreamingJoinHelper.getStateValueWatermark(
+          input.outputSet, other.outputSet, join.condition, 
Some(1000000)).isDefined
+      }
+
     // Check if the nullable side has a watermark, and there's a range 
condition which
     // implies a state value watermark on the first side.
     val hasValidWatermarkRange = join.joinType match {
-      case LeftOuter | LeftSemi => StreamingJoinHelper.getStateValueWatermark(
-        join.left.outputSet, join.right.outputSet, join.condition, 
Some(1000000)).isDefined
-      case RightOuter => StreamingJoinHelper.getStateValueWatermark(
-        join.right.outputSet, join.left.outputSet, join.condition, 
Some(1000000)).isDefined
-      case FullOuter =>
-        Seq((join.left.outputSet, join.right.outputSet),
-          (join.right.outputSet, join.left.outputSet)).exists {
-          case (attributesToFindStateWatermarkFor, 
attributesWithEventWatermark) =>
-            
StreamingJoinHelper.getStateValueWatermark(attributesToFindStateWatermarkFor,
-              attributesWithEventWatermark, join.condition, 
Some(1000000)).isDefined
-        }
+      case LeftOuter | LeftSemi => hasValidRange(join.left, join.right)
+      case RightOuter => hasValidRange(join.right, join.left)
+      case FullOuter => hasValidRange(join.left, join.right) || 
hasValidRange(join.right, join.left)

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.
   
   This was #60667's rule, and it is gone: #60667 now keeps master's eviction 
for an input bounded only through a cast column, so such a full outer join 
emits its unmatched rows as master does. Master's early eviction for such 
inputs predates this stack and is 
[SPARK-62044](https://issues.apache.org/jira/browse/SPARK-62044).



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