HeartSaVioR commented on code in PR #60667:
URL: https://github.com/apache/spark/pull/60667#discussion_r4662093816
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/StreamingJoinHelper.scala:
##########
@@ -117,6 +117,29 @@ object StreamingJoinHelper extends PredicateHelper with
Logging {
allStateWatermarks.reduceOption((x, y) => Math.min(x, y))
}
+ /**
+ * Get the column that evicts the state of an input with its own watermark,
with its state value
+ * watermark. That is the event time if the condition bounds it, and
otherwise the first other
+ * TIMESTAMP column in input order that the condition bounds. Each column is
solved on its own,
+ * so the cutoff applies to the column that the condition bounds.
+ */
+ def getStateValueWatermarkColumn(
+ inputAttributes: Seq[Attribute],
+ attributesWithEventWatermark: AttributeSet,
+ joinCondition: Option[Expression],
+ eventWatermark: Option[Long]): Option[(Attribute, Long)] = {
+ val eventTime = inputAttributes.find(_.metadata.contains(delayKey))
+ val references =
joinCondition.map(_.references).getOrElse(AttributeSet.empty)
+ val candidates = eventTime.toSeq ++ inputAttributes.filter { a =>
+ a.dataType == TimestampType && !eventTime.exists(_.semanticEquals(a)) &&
+ references.contains(a)
+ }
+ candidates.iterator.flatMap { a =>
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 helper test with validFrom and validTo: the pick skips validFrom,
which the condition bounds only from above, and returns validTo with its cutoff.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/UnsupportedOperationChecker.scala:
##########
@@ -156,6 +156,73 @@ object UnsupportedOperationChecker extends Logging {
}
}
+ /**
+ * Checks, in append mode, for an operator that drops late rows after a
stream-stream join whose
+ * state of an input has no cutoff, as
[[checkStreamingQueryGlobalWatermarkLimit]] does for other
+ * operators. It takes the optimized plan, whose join conditions eviction
uses. A query from a
+ * checkpoint without `STATEFUL_OPERATOR_CHECK_JOIN_OUTPUT_WATERMARK` only
logs a warning.
+ */
+ def checkStreamStreamJoinOutputWatermark(plan: LogicalPlan, outputMode:
OutputMode): Unit = {
+ val failWhenDetected = SQLConf.get.statefulOperatorCorrectnessCheckEnabled
&&
+
SQLConf.get.getConf(SQLConf.STATEFUL_OPERATOR_CHECK_JOIN_OUTPUT_WATERMARK)
+ try {
+ plan.foreach { subPlan =>
+ // TODO: Update mode is not checked, although its aggregations also
drop late rows.
+ if (outputMode == InternalOutputModes.Append &&
dropsLateRows(subPlan)) {
+ subPlan.foreach {
+ case j: Join if j ne subPlan =>
+ joinInputWithoutStateCutoff(j).foreach { side =>
+ throwError("Detected pattern of possible 'correctness' issue:
the state of the " +
+ s"$side input of a stream-stream join has no cutoff, so the
join can emit its " +
+ "old rows below its output watermark, and a later stateful
operator drops them " +
+ "as late. Add a watermark in the join keys or a range
condition that bounds " +
+ "that input by the other input's watermark, or set " +
+
"`spark.sql.streaming.statefulOperator.checkCorrectness.enabled` to false.")(j)
+ }
+ case _ =>
+ }
+ }
+ }
+ } catch {
+ case e: AnalysisException if !failWhenDetected =>
+ logWarning(log"${MDC(ANALYSIS_ERROR, e.message)};\n${MDC(QUERY_PLAN,
plan)}", e)
+ }
+ }
+
+ // An input of a stream-stream join with an event time but no state cutoff,
as eviction derives
+ // it: no watermark in the join keys, and no range condition that bounds the
input by the
+ // watermark of the other input. With allowMultiple, an input that eviction
picks another column
+ // for counts too, since its state keeps rows of any event time. A left semi
join only emits
+ // left rows.
+ private def joinInputWithoutStateCutoff(join: Join): Option[String] = join
match {
+ case Join(left, right, joinType, condition, _) if left.isStreaming &&
right.isStreaming &&
+ !StreamingJoinHelper.isWatermarkInJoinKeys(join) =>
+ Seq(("left", left, right), ("right", right, left)).take(if (joinType ==
LeftSemi) 1 else 2)
+ .collectFirst {
+ case (side, input, other) if input.output.exists(hasEventTimeCol) &&
(
+ StreamingJoinHelper.getStateValueWatermark(
+ input.outputSet, other.outputSet, condition,
Some(1000000)).isEmpty ||
+ SQLConf.get.getConf(SQLConf.STATEFUL_OPERATOR_ALLOW_MULTIPLE) &&
+ StreamingJoinHelper.getStateValueWatermarkColumn(input.output,
other.outputSet,
+ condition, Some(1000000)).exists { case (column, _) =>
!hasEventTimeCol(column) }
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 window count after a join bounded only through `CAST(leftDate AS
TIMESTAMP)` is accepted and runs, on state formats 2 and 4.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/join/StreamingSymmetricHashJoinExec.scala:
##########
@@ -710,17 +740,44 @@ 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.
+ private[this] val isEvictionColumnNull: UnsafeRow => Boolean =
stateWatermarkPredicate match {
+ case Some(JoinStateValueWatermarkPredicate(LessThanOrEqual(column, _),
_, _, true)) =>
+ Predicate.create(IsNull(column), inputAttributes).eval _
+ case _ => _ => false
+ }
+
+ // Such a row is not stored, but an earlier version or the legacy mode can
have stored one.
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 naming
[SPARK-62038](https://issues.apache.org/jira/browse/SPARK-62038): state format
4 still keeps restored rows with a NULL event time.
--
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]