Conversation
…apache#5719) Give Iceberg and parquet native writes a real originalPlan so revertToSpark keeps the commit-message node instead of duplicating or dropping the child.
b0a9862 to
812b06c
Compare
sunchao
left a comment
There was a problem hiding this comment.
Correctness
The previous fallback could discard a native writer because its originalPlan was the writer's input, rather than the Spark write operator. For a unary input, restoring children could also reconstruct the wrong shape. This change keeps the original DataWritingCommandExec or IcebergWriteExec, restores the Parquet WriteFilesExec wrapper when present, and removes stacked row/columnar transitions before rebuilding the stage. It also rejects missing, aliased, or arity-incompatible fallback plans and leaves an unsafe stage unreverted.
The restored Parquet shape agrees with the maintained Spark 3.5 and 4.0 sources: DataWritingCommandExec owns command execution, and FileFormatWriter uses WriteFilesExec.executeWrite for planned writes. Retaining the original command and wrapper preserves their write metadata and save-mode handling. For Iceberg, carrying the original write node and stable commit-message output preserves the relationship with the outer commit operator and its existing commit/abort path. The native writer execution methods are unchanged. I found no additional production correctness issue in the reviewed changes. Spark 3.4 and 4.1 source compatibility remains unverified because the required maintained branches were unavailable.
One [P2] test reliability issue remains, detailed inline: captureDataWritingCommand unregisters its listener before asynchronous callback completion is guaranteed, so three new regression tests can fail after a successful write. Waiting for the callback before unregistering fixes that ordering.
Validation
Reviewed all 10 changed files at 812b06c9 against 4e69a248, including the leaf/unary fallback cases, alias guard, wrapper restoration, and the added AQE-on/off write tests. A compiled Scala probe containing the exact new helper reproduced the delayed-callback failure, with passing immediate-delivery and wait-before-unregister controls. This was an isolated test-double check, not a Spark/JNI run. I did not execute the full Comet suites locally. At September 16, 03:50 UTC, CI and CodeQL were action_required with zero jobs. The successful label job provides no test coverage.
Performance
The added alias validation makes one extra linear walk of an eligible stage during driver-side planning. It runs only when transition-heavy reversion is enabled and the transition threshold is exceeded. That feature remains disabled by default. The transition-stripping recursion removes wrappers, and the changes add no per-row work or additional write execution. I found no material new hot-path overhead requiring a separate benchmark. No runtime speedup, memory improvement, or benchmark result is claimed by this review.
Design
Keeping the actual Spark writer as the fallback source is a sound way to preserve ownership of write and commit behavior. The generic bottom-up reconstruction handles ordinary Comet nodes, while the Parquet override reconstructs the extra WriteFilesExec layer that conversion previously removed. Checking aliases before rewriting children is necessary because child replacement could otherwise hide the old reference relationship. Catching only the dedicated fallback exception keeps this conservative decision local to an unsafe restoration without swallowing unrelated failures. The source changes and the plan-shape tests address the original defect directly. The callback wait is the concrete improvement needed for dependable regression coverage.
Abstraction & complexity
sparkFallback(newChildren) gives restoration a narrow operator-level extension point instead of embedding write-specific reconstruction in the stage rule. Its shared null/alias/arity checks and the single Parquet specialization earn their complexity because these operators have different restoration shapes. The dedicated exception makes the rule's refusal to revert explicit. I found no additional abstraction or simplification issue that warrants a separate finding.
| spark.range(1).toDF("id").write.mode("overwrite").parquet(path) | ||
| } | ||
| } finally { | ||
| spark.listenerManager.unregister(listener) |
There was a problem hiding this comment.
Correctness
[P2] Wait for the write callback before unregistering the listener
A successful parquet(path) call does not guarantee that QueryExecutionListener.onSuccess has run: Spark posts the SQL execution-end event to the shared asynchronous listener queue. If that queue delivers the event after this finally block, the listener has already been removed and captured stays null, so the three new Parquet reversion tests can fail with expected a captured parquet write plan even though the write succeeded. I verified this delivery path in the maintained Spark 3.5 and 4.0 branches and reproduced the failing schedule with the exact helper in isolated Scala test doubles. Immediate delivery and a wait-before-unregister control both pass. Please wait with a bounded latch/future while the listener remains registered, then unregister in finally, so completion and visibility of the captured plan are guaranteed.
There was a problem hiding this comment.
Fixed. The helper now waits on a bounded CountDownLatch from onSuccess while the listener is still registered, then unregisters in finally.
sunchao
left a comment
There was a problem hiding this comment.
Re-reviewed 6d7be018. The previous P2 listener race is addressed: the helper stores the plan before releasing the latch, waits up to ten seconds while the listener remains registered, and unregisters in finally. The production changes are unchanged from the previous review. I found no new or remaining P1/P2 issues.
The exact old helper still reproduces the delayed-callback failure. The exact updated helper passed immediate and delayed delivery, visibility, unrelated-event filtering, write failure, real ten-second timeout, interruption, and cleanup checks in a compiled Scala probe. These are isolated test doubles, not Spark/JNI execution. At September 16, 05:28 UTC, CI and CodeQL remain action_required with zero jobs.
sunchao
left a comment
There was a problem hiding this comment.
Rechecked 76da1090bcab against 58ab5f618e1e, including the full ten-file change and the base merge since 6d7be0184a31. No new or remaining verified P1/P2 findings. Preserving the existing approval.
The authored edits are unchanged. Parquet fallback retains the Spark write command and restores its WriteFilesExec wrapper when present. Iceberg fallback preserves the original writer, output attributes and commit boundary. Null/alias/arity guards, stacked transition removal and AQE stage boundaries remain intact. The inherited plan-cache and scan changes introduce no additional fallback edits.
The listener-race fix is also unchanged: it publishes the captured plan before signaling completion and waits while the listener is registered. The earlier eleven-scenario isolated probe remains historical evidence. I did not rerun it or execute Spark/JNI/write-IO tests on this head. Source checks used the maintained Spark 3.5/4.0 branches. Versions 3.4/4.1 remain unavailable.
At 2026-09-17 04:37:19 UTC, current-head CI and CodeQL require approval with zero jobs. Only labeling passed.
Which issue does this PR close?
Closes #5719.
Rationale for this change
RevertNativeForTransitionHeavyStages.revertToSparktreated everyCometExecas a like-for-like swap oforiginalPlan.CometIcebergWriteExecandCometNativeWriteExecreported their own child asoriginalPlan, so reverting a transition-heavy write stage erased the write node:child.withNewChildren(Seq(child)))After that,
IcebergCommitExecno longer receivediceberg_commit_messagerows. The same aliasing is never a valid Spark restore for any operator.This is pre-existing and independent of #5696. Part of #5649.
What changes are included in this PR?
This implements both fixes from #5719:
CometIcebergWriteExec.originalPlanis theIcebergWriteExecit replacedCometNativeWriteExec.originalPlanis theDataWritingCommandExecit replacedCometExec.sparkFallbackinstead of graftingoriginalPlanonto itself.sparkFallbackso a revertedWriteFilesExec(when present) keeps wrapping the restored input.originalPlanis one of their own children before rewriting. If restore is invalid, skip reversion for the whole stage rather than emitting a broken write plan.transformStageDownafter rewriting a node so stacked transitions such asSparkToColumnar(C2R(...))unwrap fully.How are these changes tested?
Direct
revertToSparkcoverage inRevertNativeForTransitionHeavyStagesSuite:SparkToColumnar, and over a unary Comet child (no duplicatedFilter)SparkToColumnar(C2R)under a native writeWriteFilesExecoriginalPlan == childaliases leave the stage unchangedEnd-to-end, with AQE on and off:
transitionRevert.enabled=trueandmaxTransitions=0restoresIcebergWriteExecand still writes the rowsDataWritingCommandExec→WriteFilesExecand write the expected row counts