feiniaofeiafei opened a new pull request, #69131:
URL: https://github.com/apache/doris/pull/69131
### What problem does this PR solve?
Merging nested set operations can lose the parent’s input column mapping and
return values from the wrong columns. For example, a query grouping by country
can return campaign names after pruning an unused date column.
Each set operation’s regularChildrenOutputs[i] specifies which child slots
supply its output columns, in output order. This mapping can differ from the
child’s own output order.
In the following plan, the parent reads the nested UNION as [country#63,
campaign#62], while the nested UNION’s own output is [campaign#62, country#63].
Before MERGE_SET_OPERATION:
```sql
Union
outputs: [country#70, tag#71]
child mappings:
[country#46, tag#49]
[country#63, campaign#62]
├─ Project [country#46, campaign#45 AS tag#49]
│ └─ Scan
└─ Union
outputs: [campaign#62, country#63]
child mappings:
[campaign#51, country#52]
[campaign#57, country#58]
├─ Project [campaign#51, country#52]
│ └─ Scan
└─ Project [campaign#57, country#58]
└─ Scan
```
The original rewrite directly copied the nested UNION’s child mappings into
the parent, discarding the parent’s required column permutation.
After the original rewrite — incorrect:
```sql
Union
outputs: [country#70, tag#71]
child mappings:
[country#46, tag#49]
[campaign#51, country#52] ← country#70 receives campaign#51
[campaign#57, country#58] ← country#70 receives campaign#57
├─ Project [country#46, campaign#45 AS tag#49]
│ └─ Scan
├─ Project [campaign#51, country#52]
│ └─ Scan
└─ Project [campaign#57, country#58]
└─ Scan
```
The fix composes the parent’s input mapping with the nested operation’s
output ordinals before flattening its children.
After the fixed rewrite — correct:
```sql
Union
outputs: [country#70, tag#71]
child mappings:
[country#46, tag#49]
[country#52, campaign#51]
[country#58, campaign#57]
├─ Project [country#46, campaign#45 AS tag#49]
│ └─ Scan
├─ Project [campaign#51, country#52]
│ └─ Scan
└─ Project [campaign#57, country#58]
└─ Scan
The child Project nodes keep their original output order. The corrected
mappings ensure that country#70 receives country values from every branch.
```
The same mapping composition is applied to nested INTERSECT and left-deep
EXCEPT operations. UNION constant rows are reordered consistently, and the
parent EXCEPT’s remaining inputs retain their original mappings.
Issue Number: close #xxx
Related PR: #xxx
Problem Summary:
### Release note
None
### Check List (For Author)
- Test <!-- At least one of them must be included. -->
- [ ] Regression test
- [ ] Unit Test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
- [ ] Previous test can cover this change.
- [ ] No code files have been changed.
- [ ] Other reason <!-- Add your reason? -->
- Behavior changed:
- [ ] No.
- [ ] Yes. <!-- Explain the behavior change -->
- Does this need documentation?
- [ ] No.
- [ ] Yes. <!-- Add document PR link here. eg:
https://github.com/apache/doris-website/pull/1214 -->
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label <!-- Add branch pick label that this PR should
merge into -->
--
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]