[SPARK-54724][SQL] Fix dropDuplicates(columns) followed by exceptAll causing INTERNAL_ERROR_ATTRIBUTE_NOT_FOUND#54814
Open
nookcreed wants to merge 3 commits intoapache:masterfrom
Conversation
added 3 commits
March 15, 2026 10:05
…causing INTERNAL_ERROR_ATTRIBUTE_NOT_FOUND ### What changes were proposed in this pull request? Preserve original expression IDs in `ReplaceDeduplicateWithAggregate` when wrapping non-key columns in `First()` aliases. ### Why are the changes needed? When `dropDuplicates(columns)` is followed by `exceptAll`, the query fails with `INTERNAL_ERROR_ATTRIBUTE_NOT_FOUND` because `ReplaceDeduplicateWithAggregate` generates new expression IDs for non-key columns via `Alias(...)()`, but `RewriteExceptAll` (which runs in the same optimizer batch) has already captured references to the original expression IDs. The stale references cannot be resolved against the new Aggregate output. The fix passes `exprId = attr.exprId` to the `Alias` constructor so the aggregate output preserves the same expression IDs as the original `Deduplicate` output. ### Does this PR introduce _any_ user-facing change? Yes. `df.dropDuplicates(columns).exceptAll(other)` and `.intersectAll(other)` no longer throw an internal error. ### How was this patch tested? - Unit test in `ReplaceOperatorSuite` verifying expression IDs are preserved after optimization. - End-to-end test in `DataFrameSuite` for `dropDuplicates(columns)` followed by `exceptAll` and `intersectAll`. ### Was this patch authored or co-authored using generative AI tooling? Yes.
The previous approach of preserving exprId on the Alias (exprId = attr.exprId) violated the plan integrity check: an Alias cannot reuse the same expression ID as one of its child references (e.g., first(name#2) AS name#2). Root cause: In the "Replace Operators" batch, RewriteExceptAll ran before ReplaceDeduplicateWithAggregate. When RewriteExceptAll transformed the Except first, it captured left.output exprIds from the Deduplicate. Then ReplaceDeduplicateWithAggregate changed the non-key column exprIds, and transformUpWithNewOutput failed to propagate through the Aggregate node created by RewriteExceptAll (because plan.references excludes passthrough attributes). Fix: Move ReplaceDeduplicateWithAggregate before RewriteExceptAll and RewriteIntersectAll so Deduplicates are already replaced when set operation rewrite rules run.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Preserve original expression IDs in
ReplaceDeduplicateWithAggregatewhen wrapping non-key columns inFirst()aliases.Why are the changes needed?
When
dropDuplicates(columns)is followed byexceptAll, the query fails withINTERNAL_ERROR_ATTRIBUTE_NOT_FOUNDbecauseReplaceDeduplicateWithAggregategenerates new expression IDs for non-key columns viaAlias(...)(), butRewriteExceptAll(which runs in the same optimizer batch) has already captured references to the original expression IDs. The stale references cannot be resolved against the new Aggregate output.The fix passes
exprId = attr.exprIdto theAliasconstructor so the aggregate output preserves the same expression IDs as the originalDeduplicateoutput.Does this PR introduce any user-facing change?
Yes.
df.dropDuplicates(columns).exceptAll(other)and.intersectAll(other)no longer throw an internal error.How was this patch tested?
ReplaceOperatorSuiteverifying expression IDs are preserved after optimization.DataFrameSuitefordropDuplicates(columns)followed byexceptAllandintersectAll.Was this patch authored or co-authored using generative AI tooling?
Yes.
What changes were proposed in this pull request?
Why are the changes needed?
Does this PR introduce any user-facing change?
How was this patch tested?
Was this patch authored or co-authored using generative AI tooling?