siying commented on PR #60068:
URL: https://github.com/apache/spark/pull/60068#issuecomment-6884404506
Follow-up review on recent updates (`016652b8d29` + `cd8b476e4f7`):
### Resolved from previous pass
- **V4 state layout (`SymmetricHashJoinStateManagerV4`):** Dropping
`useEventTimeIndex` completely resolves the [P0] primary-key `[timestamp]` /
`tsWithKey` mismatch across compatible restarts and downgrades.
- **RTM `purgeAfter` recovery (`MicroBatchExecution`):** Reading
`latestOffsetSeqWithRetentionVersion` after `purgeAfter` and inside
`populateStartOffsets` fixes the stale-offset replay failure in
`StreamRealTimeModeWithManualClockSuite`.
- **`OffsetSeqMetadata` precedence (`MicroBatchExecution` /
`StreamMetadata`):** Recorded offset-log values now take precedence over
`StreamMetadata`, with regression coverage for missing and conflicting
`StreamMetadata`.
- **Materialized CTE + `PushDownJoinThroughUnion` + `EXPLAIN` (`stringArgs`)
+ test names:** Addressed. *(Remember to also update the PR description when
pushing, as it still describes the earlier `Join`-annotation design.)*
---
### Remaining / Newly Introduced Issues
#### 1. [P1] `UnsupportedOperationChecker` runs in `StreamingQueryManager`
before checkpoint config restoration, breaking restart of legacy/`version=0`
checkpoints
**Locations:**
- `UnsupportedOperationChecker.scala` (lines 225–230, 718–764)
- `StreamingQueryManager.scala` (lines 287–328)
- `ResolveWriteToStream.scala` (lines 55–66)
`ResolveWriteToStream` invokes
`UnsupportedOperationChecker.checkForStreaming` inside
`StreamingQueryManager.createQuery` **before** `MicroBatchExecution` is
constructed and before checkpoint configs are restored (`StreamingQueryManager`
currently only restores `ALLOW_EXCEPT_ON_STREAMING_DATAFRAME` early):
1. **Upgrade / `version=0` restart rejected before `MicroBatchExecution`:**
An existing Spark <= 4.3 checkpoint (or a Spark 4.4 checkpoint pinned to
`version = 0`) with an outer/left-semi join on `leftKey = rightKey AND
leftJoinTime > rightEventTime` (watermarked on `leftEventTime`) fails with
`AnalysisException` on restart under default session conf (`version = 1`),
because `UnsupportedOperationChecker` reads the session's `1` rather than the
checkpoint's `0`. (`UnsupportedOperationsSuite` calls `checkForStreaming`
directly under `withSQLConf`, bypassing `StreamingQueryManager.createQuery`.)
2. **Reverse mismatch on `version=1` checkpoint:** Restarting an existing
`version = 1` checkpoint when the session has `eventTimeColumnRetentionVersion
= 0` passes `UnsupportedOperationChecker` during `createQuery` (seeing `0`),
then `MicroBatchExecution` restores `1` from the checkpoint and executes the
outer join with `stateWatermarkPredicates = None`, buffering unmatched outer
rows indefinitely.
**Suggestion:** Extend the early read-only checkpoint restoration in
`StreamingQueryManager.createQuery` when `analyzedPlan` contains a stateful
`Join`: check `checkpointMetadata.offsetLog.getLatest()` first, fall back to
`StreamMetadata.read(...)` if `<checkpoint>/metadata` exists (without calling
`checkpointMetadata.streamMetadata`, which writes a new metadata file), default
to `0` if an existing checkpoint predates the key, and keep the session conf
only when neither `offsets` nor `metadata` exists yet. Add an end-to-end
`testStream` restart regression for the `left_outer` case from a `version = 0`
/ legacy checkpoint under default session conf (`version = 1`).
---
#### 2. [P2] `UnsupportedOperationChecker` counts stateful joins before
`InlineCTE`, disagreeing with `ColumnPruning` on CTE plans
**Location:** `UnsupportedOperationChecker.scala` (line 230)
`UnsupportedOperationChecker` runs on `analyzedPlan` before `InlineCTE`,
whereas `ColumnPruning` runs after `InlineCTE`:
- **False negative (silent infinite outer-join buffering):** With an unused
CTE containing a stateful join (`WITH unused AS (SELECT a.key FROM s1 a JOIN s2
b ON a.key = b.key) SELECT ... FROM l LEFT OUTER JOIN r ON l.key = r.key AND
l.leftJoinTime > r.rightEventTime`), `analyzedPlan` has **2** stateful `Join`s
(`WithCTE.children` includes `cteDefs`), so `UnsupportedOperationChecker` sets
`useRetainedJoinEventTimeColumn = false` and accepts the query. `InlineCTE`
then drops `unused`, `ColumnPruning` sees **1** `Join`
(`useRetainedEventTimeColumn = true`), and execution never emits unmatched
outer rows.
- **False positive (spurious rejection):** With a non-materialized CTE
referenced twice (`WITH j AS (SELECT ... FROM l LEFT OUTER JOIN r ON l.key =
r.key AND l.leftJoinTime > r.rightEventTime) SELECT * FROM j UNION ALL SELECT *
FROM j`), `analyzedPlan` has **1** `Join` (`useRetainedJoinEventTimeColumn =
true` -> rejected), even though `InlineCTE` expands `j` into **2** untagged
`Join`s before `ColumnPruning` (`useRetainedEventTimeColumn = false` at
execution).
**Suggestion:** Count stateful joins on `InlineCTE(alwaysInline =
false).apply(plan)` (when `plan.containsPattern(CTE)`) in
`UnsupportedOperationChecker` so its single-join gate matches `ColumnPruning`.
---
#### 3. [P2] `ColumnPruning` mutates `TreeNodeTag` in place on its input
`plan`, and parent-matching optimizer rewrites drop `Join`'s tag
**Locations:**
- `Optimizer.scala` (lines 1520–1525)
- `basicLogicalOperators.scala` (lines 723–724, 870–879)
1. **In-place mutation of `analyzed` plans (plus non-streaming
`plan.collect` overhead):**
- When no rule before `ColumnPruning` clones the `Join`,
`statefulJoins.foreach(_.setUseRetainedEventTimeColumn(true))` mutates the
`Join` node inside the caller's `DataFrame.queryExecution.analyzed` in place.
Calling `j1.explain()` and `j2.explain()` on two DataFrames marks both `Join`s
`useRetainedEventTimeColumn = true`, causing `j1.union(j2).select("key")` to
satisfy `statefulJoins.forall(_.useRetainedEventTimeColumn)` and retain event
time even though `size == 2` (whereas without `.explain()`, `forall` is `false`
and event time is pruned). Similarly, calling `df.explain()` under `version =
1` leaves the tag set on `df.queryExecution.analyzed` if `df` is later started
with `ColumnPruning` excluded.
- Also worth guarding the initial `plan.collect` with `plan.isStreaming`
(or `plan.containsPattern(JOIN)`) so batch queries don't pay a full tree walk
on every fixed-point `ColumnPruning` pass.
2. **Parent-matching optimizer rules rebuild inner `Join`s without
preserving tags:**
`TreeNode` only auto-copies tags from the matched root node
(`afterRule.copyTagsFrom(this)`), not from inner `Join` children rebuilt inside
a parent match. `cd8b476e4f7` handled `PushDownJoinThroughUnion`, but other
rules do the same:
- `ExtractPythonUDFFromJoinCondition` (`joins.scala:275-277`, in
`Batch("Extract UDFs")`): rewrites `j @ Join(...)` to `Filter(udf,
j.copy(condition = newCondition))`, copying `j`'s tag onto `Filter` instead of
`newJoin`. If `PushDownJoinThroughUnion` already split the join into 2
branches, the subsequent `ColumnPruning` at `SparkOptimizer.scala:125` sees
`size == 2` and `forall == false`, clearing the tag on both joins after their
event-time columns were already retained.
- `PushPredicateThroughJoin` (`Optimizer.scala:3277, 3289, 3299`, in
`Batch("RewriteSubquery")` before `ColumnPruning`), `PushDownLeftSemiAntiJoin`
(`PushDownLeftSemiAntiJoin.scala:47, 57, 91, 101`), and `LimitPushDown`
(`Optimizer.scala:974-988, 1024`, which also runs at `SparkOptimizer.scala:126,
136` after the final `ColumnPruning` pass) similarly construct `Join(...)` /
`join.copy(...)` inside parent matches without copying tags.
**Suggestion:** Avoid mutating `Join` nodes on the input `plan` in place,
and either make `useRetainedEventTimeColumn` a defaulted field on `Join`
(excluded from `stringArgs` like `Deduplicate.dedupSpec`, so `join.copy(...)`
preserves it automatically and only explicit `Join(...)` constructors in
join-pushdown rules need forwarding) or use a tag-preserving helper across
those join-rewriting rules.
---
#### 4. [Low / Test] Add V4 restart coverage for newly added eviction range
predicate
In `StreamingJoinV4Suite.scala`, `"SPARK-60850: retention version 0
preserves the legacy secondary index layout"` now checks essentially the same
invariant as `"SPARK-56406: secondary index populated on watermarked side only
when other side has no watermark"` right above it. Adding a V4 restart test
where Run 1 buffers `(1, 20)` under `left.join(right, "key").select("key")`
(`version = 1`) and Run 2 restarts on the same checkpoint with an added
time-interval condition (`leftTime BETWEEN ...`) — verifying `(1, 20)` matches
via `getValuesInRange` and later evicts via `tsWithKey` — would directly guard
against regressions of the V4 primary-key timestamp layout bug fixed in
`cd8b476e4f7`.
---
<!-- GITHUB_MCP_FOOTER: This attribution is automatically appended by GitHub
MCP. -->
_This comment was generated with [GitHub MCP](http://go/mcps)._
--
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]