branch-4.1: [fix](nereids) Allocate fresh ExprId for constants when pushing project into Union #62296#63018
Merged
Merged
Conversation
…ct into Union (#62296) ### What problem does this PR solve? Issue Number: close #62294 Problem Summary: When `PushProjectIntoUnion` folds a parent `Project`'s expression into a `LogicalUnion`'s `constantExprsList`, the outer `Alias` of a non-`SlotReference` project expression is preserved by `ExpressionUtils.replaceNameExpression`. Its `ExprId` then collides with the new UNION output `ExprId` (which comes from the parent project's output `Alias`) and is reused across every constant row of the same column. Downstream rules such as `PushDownFilterThroughSetOperation` rely on the invariant that each `constantExprsList` row carries `NamedExpression`s whose `ExprId`s are distinct from the UNION output and from each other across rows; the collision causes them to mis-rewrite the plan and return wrong results, e.g. ```sql WITH tbl0(n) AS (SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT NULL), tbl1(n) AS (SELECT 2 UNION ALL SELECT NULL UNION ALL SELECT 1) SELECT (n*2) AS n FROM tbl0 INTERSECT SELECT (n*2) AS n FROM tbl1; ``` `PushProjectThroughUnion` has the same class of bug in its mixed-union constant-row branch: when `MergeOneRowRelationIntoUnion` folds a `LogicalOneRowRelation` into the union's `constantExprsList`, an outer `Alias(Cast(slot))` project survives `outerProject.rewriteUp` with its parent `ExprId` intact, and then the same `ExprId` is reused as the new UNION output via `project.toSlot()`. The plan-level invariant is broken even though no current downstream rule appears to mis-handle the specific shape today. Fix: - `PushProjectIntoUnion`: after folding the substituted expression, re-wrap the result in a fresh `Alias` so each constant cell receives a new unique `ExprId`. Qualifier and name from the folded `Alias` are preserved. The `SlotReference` branch is left unchanged because it already returns the original `NamedExpression` from `constExprs` whose `ExprId`s are row-distinct and not equal to the UNION output `ExprId`. - `PushProjectThroughUnion`: same treatment for the constant-row non-Slot branch — re-wrap the rewritten `Alias` to allocate a fresh `ExprId`. Introduced by #39450 (closest commit touching the relevant lines; the file `PushProjectIntoUnion` was originally added by #27947). ### Release note Fix wrong results for INTERSECT/EXCEPT/UNION over constant rows when the projection contains expressions such as `((col*2) AS col)`, and harden the parallel `PushProjectThroughUnion` rule against the same class of ExprId collision. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Thank you for your contribution to Apache Doris. Please clearly describe your PR:
|
Contributor
|
run buildall |
yiguolei
approved these changes
May 7, 2026
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.
Cherry-picked from #62296