Skip to content

[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
nookcreed:fix/SPARK-54724-dropDuplicates-exceptAll
Open

[SPARK-54724][SQL] Fix dropDuplicates(columns) followed by exceptAll causing INTERNAL_ERROR_ATTRIBUTE_NOT_FOUND#54814
nookcreed wants to merge 3 commits intoapache:masterfrom
nookcreed:fix/SPARK-54724-dropDuplicates-exceptAll

Conversation

@nookcreed
Copy link

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.

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?

Varun Srinivas 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant