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


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -710,17 +753,49 @@ 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. A NULL in
+    // an equality key that the opposite watermark selects can never match 
either; the key that
+    // findJoinKeyOrdinalForWatermark selects keeps the previous behavior.
+    private[this] val isEvictionColumnNull: UnsafeRow => Boolean = 
stateWatermarkPredicate match {
+      case Some(JoinStateValueWatermarkPredicate(LessThanOrEqual(column, _), 
_, _, true)) =>
+        Predicate.create(IsNull(column), inputAttributes).eval _
+      case Some(_: JoinStateKeyWatermarkPredicate) if 
joinKeyOrdinalForWatermark !=
+          findJoinKeyOrdinalForWatermark(leftKeys, rightKeys) =>
+        Predicate.create(IsNull(joinKeys(joinKeyOrdinalForWatermark.get)), 
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 &&

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.
   
   Master loses these matches too: it tests the right event time against the 
same whole-input cutoff. This PR keeps master's admission for left semi joins, 
so it is not worse; storing every row that the picked column keeps is tracked 
with [SPARK-61380](https://issues.apache.org/jira/browse/SPARK-61380) (#60307).



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -710,17 +753,49 @@ 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. A NULL in
+    // an equality key that the opposite watermark selects can never match 
either; the key that
+    // findJoinKeyOrdinalForWatermark selects keeps the previous behavior.
+    private[this] val isEvictionColumnNull: UnsafeRow => Boolean = 
stateWatermarkPredicate match {
+      case Some(JoinStateValueWatermarkPredicate(LessThanOrEqual(column, _), 
_, _, true)) =>
+        Predicate.create(IsNull(column), inputAttributes).eval _
+      case Some(_: JoinStateKeyWatermarkPredicate) if 
joinKeyOrdinalForWatermark !=
+          findJoinKeyOrdinalForWatermark(leftKeys, rightKeys) =>
+        Predicate.create(IsNull(joinKeys(joinKeyOrdinalForWatermark.get)), 
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),
+          AttributeSet(left.output), condition.full, 
eventTimeWatermarkForEviction)
+        
WatermarkSupport.watermarkExpression(WatermarkSupport.findEventTimeColumn(inputAttributes,
+          allowMultipleEventTimeColumns = false), cutoff)
+          .map(Predicate.create(_, inputAttributes).eval _).getOrElse((_: 
UnsafeRow) => false)

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.
   
   #60667, which this PR is stacked on, now reuses the value predicate where it 
gives the same answer. For a right input evicted by another column the extra 
predicate stays: it costs one compile per batch and does not change results.



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