HeartSaVioR commented on code in PR #60666:
URL: https://github.com/apache/spark/pull/60666#discussion_r4621771936
##########
docs/streaming/ss-migration-guide.md:
##########
@@ -25,6 +25,16 @@ Please refer [Migration Guide: SQL, Datasets and
DataFrame](../sql-migration-gui
## Upgrading from Structured Streaming 4.3 to 4.4
+- Since Spark 4.4, a stream-stream left outer, left semi, right outer or full
outer join fails
+ analysis unless the watermark of the other input bounds the state of each
input whose rows the
+ join outputs once, as the join support table of the programming guide
requires. For example,
+ a left outer join on `leftTime = rightTime` with a watermark only on
`leftTime` is rejected:
+ a later right row could match a left row that was already output with NULLs.
So is a key
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.
Fixed, thanks: "So are joins on a key shifted backward ... and on a key in
any other form ...".
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -51,6 +51,65 @@ object StreamingJoinHelper extends PredicateHelper with
Logging {
}
}
+ /**
+ * Find the ordinal of the equality key whose watermark evicts the state of
both inputs.
+ *
+ * Join keys of both sides generate rows of the same fields, that is, same
sequence of data
+ * types. If one side (say left side) has a column (say timestamp) that has
a watermark on it,
+ * then it will never consider joining keys that are < state key watermark
(i.e. event time
+ * watermark). On the other side (i.e. right side), even if there is no
watermark defined,
+ * there has to be an equivalent column (i.e., timestamp). And any right
side data that has the
+ * timestamp < watermark will not match will not match with left side data,
as the left side get
+ * filtered with the explicitly defined watermark. So, the watermark in
timestamp column in
+ * 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.
+ */
+ def findJoinKeyOrdinalForWatermark(
+ leftKeys: Seq[Expression],
+ rightKeys: Seq[Expression]): Option[Int] = {
+ leftKeys.zipWithIndex.collectFirst {
+ case (ne: NamedExpression, index) if ne.metadata.contains(delayKey) =>
index
+ } orElse {
+ rightKeys.zipWithIndex.collectFirst {
+ case (ne: NamedExpression, index) if ne.metadata.contains(delayKey) =>
index
+ }
+ }
+ }
+
+ /**
+ * Whether an equality key of one input bounds, by that input's watermark,
the equal key of the
+ * other input's state rows. Later rows of this input pass late-row
filtering only with the
+ * watermarked column, or the end of a watermarked window, above the
watermark. Their keys are
+ * above it too if the key is that column, or that column shifted forward by
constants. With a
+ * backward shift, or a key in another form, they can still match state keys
at or below the
+ * watermark, which eviction removes.
+ */
+ def isKeyBoundedByWatermark(key: Expression, inputAttributes: AttributeSet):
Boolean = {
+ def isWatermarked(a: AttributeReference): Boolean =
+ a.metadata.contains(delayKey) && inputAttributes.contains(a)
+ def isNotBackward(shift: Expression): Boolean = shift.foldable &&
(shift.eval() match {
+ case i: CalendarInterval => i.months >= 0 && i.days >= 0 &&
i.microseconds >= 0
+ case n: java.lang.Number => n.longValue >= 0
+ case _ => false
+ })
+ key match {
+ case a: AttributeReference => isWatermarked(a)
+ // NOTE: We assume this is window struct, as same as
WatermarkSupport.watermarkExpression
+ case GetStructField(a: AttributeReference, 1, _) => isWatermarked(a)
+ case Cast(child, TimestampType, _, _) if child.dataType == TimestampType
=>
+ isKeyBoundedByWatermark(child, inputAttributes)
+ case DatetimeSub(_, _, replacement) =>
isKeyBoundedByWatermark(replacement, inputAttributes)
+ case TimestampAddInterval(start, interval, _) =>
+ isNotBackward(interval) && isKeyBoundedByWatermark(start,
inputAttributes)
+ case TimestampAddYMInterval(start, interval, _) =>
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 four rejection rows: the analyzer's form of `rt - INTERVAL 1 MONTH`, a
calendar interval of -1 day, one of -1 month, and `TIMESTAMPADD` with a
right-input column as the quantity. I checked that each row fails when its
guard is removed.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/UnsupportedOperationChecker.scala:
##########
@@ -733,5 +734,55 @@ object UnsupportedOperationChecker extends Logging {
"is not supported without a watermark in the join keys, or a
watermark on " +
"the nullable side and an appropriate range condition")(join)
}
+
+ if
(SQLConf.get.getConf(SQLConf.STREAMING_JOIN_CHECK_WATERMARK_DIRECTION_ENABLED))
{
+ checkStreamStreamJoinWatermarkDirection(join)
+ }
+ }
+
+ /**
+ * When the join evicts an unmatched row of an input, an outer join outputs
it with NULLs and a
+ * left semi join drops it. Both are final only if later rows of the other
input cannot match
+ * the row. So the other input's watermark must bound this input's state,
through the
+ * equality key that eviction uses or through a range condition.
+ */
+ private def checkStreamStreamJoinWatermarkDirection(join: Join): Unit = {
+ val (leftKeys, rightKeys) = join match {
+ case ExtractEquiJoinKeys(_, leftKeys, rightKeys, _, _, _, _, _) =>
(leftKeys, rightKeys)
+ case _ => (Nil, Nil)
+ }
+ val keyOrdinal =
StreamingJoinHelper.findJoinKeyOrdinalForWatermark(leftKeys, rightKeys)
+
+ def checkInput(
+ stateSide: String,
+ state: LogicalPlan,
+ otherSide: String,
+ other: LogicalPlan,
+ otherKeys: Seq[Expression]): Unit = {
+ val boundedByKey = keyOrdinal.exists { ordinal =>
+ StreamingJoinHelper.isKeyBoundedByWatermark(otherKeys(ordinal),
other.outputSet)
+ }
+ lazy val boundedByRange = StreamingJoinHelper.getStateValueWatermark(
+ state.outputSet, other.outputSet, join.condition,
Some(1000000)).isDefined
+ if (!boundedByKey && !boundedByRange) {
+ val confKey =
SQLConf.STREAMING_JOIN_CHECK_WATERMARK_DIRECTION_ENABLED.key
+ throwError(
+ s"Stream-stream ${join.joinType} join between two streaming
DataFrame/Datasets " +
+ s"is not supported without a watermark on the $otherSide input
that bounds the " +
+ s"state of the $stateSide input, through an equality on the
$otherSide input's " +
+ "event-time column or a range condition. Otherwise, a later row of
the " +
+ s"$otherSide input can match a $stateSide row that the join
already evicted. To " +
+ s"skip this check, set $confKey to false.")(join)
+ }
+ }
+
+ join.joinType match {
+ case LeftOuter | LeftSemi => checkInput("left", join.left, "right",
join.right, rightKeys)
+ case RightOuter => checkInput("right", join.right, "left", join.left,
leftKeys)
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.
Done. Every rejection row of this check now asserts which input the message
names, including the right outer rows and the first full outer check. I checked
that swapping the labels makes them fail.
--
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]