HeartSaVioR commented on code in PR #60280:
URL: https://github.com/apache/spark/pull/60280#discussion_r4578642808
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/IncrementalExecution.scala:
##########
@@ -630,6 +671,231 @@ class IncrementalExecution(
watermarkPropagator.propagate(currentBatchId, plan,
watermarkForCurrBatch)
}
+ /**
+ * Find, for each stateful operator that applies its input watermark to an
event-time column,
+ * which stream-stream join inputs' event times that column carries. The
walk follows the
+ * column down through nodes that pass it on unchanged, to the same
position in every input
+ * of a union, and through windows built from it, which end after it. It
stops at a join, at
+ * another operator that writes state or sets an event time, and at any
node it does not
+ * recognize; a join that no traced column reaches holds back for both
inputs.
+ */
+ private def joinWatermarkConsumers(plan: SparkPlan):
Seq[JoinWatermarkConsumer] = {
+ def eventTime(output: Seq[Attribute]): Option[Attribute] =
+ output.find(_.metadata.contains(EventTimeWatermark.delayKey))
+
+ // A time or session window built from one column, plus constants, ends
after that column.
+ def windowSource(window: Attribute, expression: Expression):
Option[Attribute] = {
+ val isWindow = window.metadata.contains(TimeWindow.marker) ||
+ window.metadata.contains(SessionWindow.marker)
+ expression.references.toSeq match {
+ case Seq(source) if isWindow => Some(source)
+ case _ => None
+ }
+ }
+
+ def passThrough(child: SparkPlan, attribute: Attribute): Seq[(Long,
JoinSide, Attribute)] =
+ child.output.find(_.exprId ==
attribute.exprId).toSeq.flatMap(findJoinSides(child, _))
+
+ def findJoinSides(
+ node: SparkPlan,
+ attribute: Attribute): Seq[(Long, JoinSide, Attribute)] = {
+ node match {
+ case j: StreamingSymmetricHashJoinExec =>
+ val side = if (eventTime(j.left.output).exists(_.exprId ==
attribute.exprId)) {
+ Some(LeftSide)
+ } else if (eventTime(j.right.output).exists(_.exprId ==
attribute.exprId)) {
+ Some(RightSide)
+ } else {
+ None
+ }
+ j.stateInfo.map(_.operatorId).zip(side).map { case (id, s) => (id,
s, attribute) }
+ .toSeq
+ case p: ProjectExec =>
+ p.projectList.find(_.exprId == attribute.exprId).toSeq.flatMap {
+ case a: Attribute => passThrough(p.child, a)
+ case Alias(a: Attribute, _) => passThrough(p.child, a)
+ case alias: Alias =>
+ windowSource(alias.toAttribute,
alias.child).toSeq.flatMap(passThrough(p.child, _))
+ case _ => Nil
+ }
+ case e: ExpandExec =>
+ // A sliding window is one projection per window; each must carry
the same column.
+ val index = e.output.indexWhere(_.exprId == attribute.exprId)
+ val traced = e.projections.map { projection =>
+ projection.lift(index).toSeq.flatMap {
+ case a: Attribute => passThrough(e.child, a)
+ case expression =>
+ windowSource(e.output(index),
expression).toSeq.flatMap(passThrough(e.child, _))
+ }
+ }
+ if (traced.nonEmpty && traced.forall(_.nonEmpty) &&
traced.distinct.size == 1) {
+ traced.head
+ } else {
+ Nil
+ }
+ case u: UnionExec =>
+ val index = u.output.indexWhere(_.exprId == attribute.exprId)
+ if (index < 0) Nil else u.children.flatMap(c => findJoinSides(c,
c.output(index)))
+ case _: StateStoreWriter | _: EventTimeWatermarkExec | _:
UpdateEventTimeColumnExec =>
+ Nil
+ case u: UnaryExecNode if u.output.exists(_.exprId ==
attribute.exprId) =>
+ passThrough(u.child, attribute)
+ case _ => Nil
+ }
+ }
+
+ // The inputs whose event-time column each operator compares with its
input watermark.
+ def watermarkedInputs(node: SparkPlan): Seq[SparkPlan] = node match {
+ case _: StateStoreSaveExec | _: StatefulStreamlineAggregateExec |
+ _: SessionWindowStateStoreSaveExec | _: StreamingDeduplicateExec |
+ _: StreamingDeduplicateWithinWatermarkExec | _:
StreamingSymmetricHashJoinExec =>
+ node.children
+ case f: FlatMapGroupsWithStateExec if f.timeoutConf ==
EventTimeTimeout => Seq(f.child)
+ case f: FlatMapGroupsInPandasWithStateExec if f.timeoutConf ==
EventTimeTimeout =>
+ Seq(f.child)
+ case t: TransformWithStateExec if t.timeMode == TimeMode.EventTime()
=> Seq(t.child)
+ case t: TransformWithStateInPySparkExec if t.timeMode ==
TimeMode.EventTime() =>
+ Seq(t.child)
+ case _ => Nil
+ }
+
+ plan.flatMap { consumer =>
+ watermarkedInputs(consumer).flatMap { input =>
+ Try(WatermarkSupport.findEventTimeColumn(input.output,
+ allowMultipleEventTimeColumns =
false)).toOption.flatten.toSeq.flatMap { column =>
+ findJoinSides(input, column).map { case (joinOperatorId, side,
joinEventTime) =>
+ JoinWatermarkConsumer(consumer, joinOperatorId, side,
joinEventTime)
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Tell each stream-stream join which inputs' event times reach the
stateful operator that
+ * consumes its output watermark, so that the join holds back that
watermark only for those
+ * inputs' state. A join without a traced consumer holds back for both
inputs.
+ */
+ private def withJoinOutputWatermarkSides(plan: SparkPlan): SparkPlan = {
+ val sides =
joinWatermarkConsumers(plan).groupMap(_.joinOperatorId)(_.side)
+ if (sides.isEmpty) {
+ plan
+ } else {
+ plan transform {
+ case j: StreamingSymmetricHashJoinExec
+ if j.stateInfo.exists(info => sides.contains(info.operatorId)) =>
+ j.copy(outputWatermarkSides =
Some(sides(j.stateInfo.get.operatorId).toSet))
+ }
+ }
+ }
+
+ /**
+ * Reject an append-mode aggregation whose event time comes from a
stream-stream join input
+ * that the join cannot bound: the join holds back its output watermark
for that input, so
+ * the aggregation would never emit a result. Only an aggregation that the
tracer of
+ * [[joinWatermarkConsumers]] reaches fails; with the finalization check
disabled, it is only
+ * logged.
+ */
+ private def checkJoinOutputWatermarkConsumers(plan: SparkPlan): Unit = {
+ val joins = plan.collect {
+ case j: StreamingSymmetricHashJoinExec if j.stateInfo.isDefined =>
+ j.stateInfo.get.operatorId -> j
+ }.toMap
+ // A bounded input's state watermark advances with the input watermark;
an input the join
+ // cannot bound contributes 0 at every input watermark.
+ def holdsOutputWatermark(join: StreamingSymmetricHashJoinExec, side:
JoinSide): Boolean = {
+ Seq(1000000L, 2000000L).forall { inputWatermark =>
+ val (left, right) =
StreamingSymmetricHashJoinHelper.getStateWatermark(
+ join.left.output, join.right.output, join.leftKeys, join.rightKeys,
+ join.condition.full, Some(inputWatermark),
!allowMultipleStatefulOperators)
+ (if (side == LeftSide) left else right).contains(0L)
+ }
+ }
+ def isAppendAggregation(consumer: SparkPlan): Boolean = consumer match {
+ case s: StateStoreSaveExec =>
s.outputMode.contains(OutputMode.Append())
+ case s: SessionWindowStateStoreSaveExec =>
s.outputMode.contains(OutputMode.Append())
+ case s: StatefulStreamlineAggregateExec =>
s.outputMode.contains(OutputMode.Append())
+ case _ => false
+ }
+ joinWatermarkConsumers(plan).distinct.foreach {
+ case JoinWatermarkConsumer(consumer, joinOperatorId, side, eventTime)
+ if isAppendAggregation(consumer) &&
+ joins.get(joinOperatorId).exists(holdsOutputWatermark(_, side))
=>
+ val join = joins(joinOperatorId)
+ val (other, otherOutput) =
+ if (side == LeftSide) ("right", join.right.output) else ("left",
join.left.output)
+ val constraint = s"a time constraint that limits how late a matching
$other row can " +
+ "arrive"
+ val requirement =
+ if
(otherOutput.exists(_.metadata.contains(EventTimeWatermark.delayKey))) {
+ constraint
+ } else {
+ s"a watermark on the $other input and $constraint"
+ }
+ val error = new AnalysisException(
+ errorClass = "STREAMING_JOIN_UNBOUNDED_DOWNSTREAM_EVENT_TIME",
+ messageParameters = Map(
+ "eventTime" -> eventTime.name,
+ "side" -> side.toString,
+ "requirement" -> requirement,
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
[e03f9977b87](https://github.com/apache/spark/pull/60280/commits/e03f9977b8722bd5783107c6c4cb0ba933e10e52).
`STREAMING_JOIN_UNBOUNDED_DOWNSTREAM_EVENT_TIME` now has two sub-conditions,
`TIME_CONSTRAINT` for an other input that has a watermark and
`WATERMARK_AND_TIME_CONSTRAINT` for one
that has none. Their wording lives in `error-conditions.json`, and the only
parameters are the event
time, the side, the other input's side and the config name. The message text
is the same as before.
The tests check the sub-condition and the parameters of each case.
<!-- sp2k-review8 reply 4577114413 -->
--
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]