Repository navigation
Commit 80e9788
fix: map remaining union input by position in
## Which issue does this PR close?
- Closes apache#26516.
## Rationale for this change
A `UNION ALL` fails to plan with `Schema error: No field named ...` when
the optimizer removes its first input and a later input remains. For
example:
```sql
SELECT 1 WHERE false UNION ALL (SELECT 2 LIMIT 1);
```
`propagate_empty_relation` puts a projection over the remaining input
with `Projection::new_from_schema`. That function makes column
references from the union's field names, which come from the first
input. When the remaining input has other names, the projection refers
to columns that do not exist, and `optimize_projections` fails.
## What changes are included in this PR?
- When one input remains and its schema is different from the union's
schema, map its columns to the union's output fields **by position**,
alias each one to the union's name, and build the projection with
`Projection::try_new` (which also checks the result).
- Three existing snapshots change their text from `Projection: a, b, c`
to `Projection: test.a AS a, test.b AS b, test.c AS c`. In those tests
the remaining input is the first input, so the old by-name references
worked. The output schema is the same.
- This also changes the `EXPLAIN` output of the `struct.slt` test added
in apache#25412. That PR made `push_down_leaf_projections` handle the
bare-name projection that `propagate_empty_relation` left after it
removed an empty union input. With this change, the projection aliases
the qualified input columns (`leaf_merge_source.s AS s`). The test still
plans and returns the same rows. apache#25412 also handles this same-name
alias form, and its unit tests still build the bare-name shape directly.
## What is the testing strategy for this PR?
- New unit test `propagate_union_remaining_input_with_other_names`: the
first input is empty and the second input has other column names.
Without the fix, the rule produces `Projection: a, b, c` over an input
that only has `x, y, z`.
- New `union.slt` cases with the two queries from the issue.
## Are there any user-facing changes?
No. The queries that failed now return results. There are no public API
changes.
Co-authored-by: Jorge Morais <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Gabriel <[email protected]>propagate_empty_relation (apache#26557)1 parent 209d852 commit 80e9788
3 files changed
Lines changed: 62 additions & 10 deletions
File tree
- datafusion
- optimizer/src
- sqllogictest/test_files
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
213 | 226 | | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
| 227 | + | |
218 | 228 | | |
219 | 229 | | |
220 | 230 | | |
| |||
439 | 449 | | |
440 | 450 | | |
441 | 451 | | |
442 | | - | |
| 452 | + | |
| 453 | + | |
443 | 454 | | |
444 | 455 | | |
445 | 456 | | |
| |||
529 | 540 | | |
530 | 541 | | |
531 | 542 | | |
532 | | - | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
533 | 571 | | |
534 | 572 | | |
535 | 573 | | |
| |||
813 | 851 | | |
814 | 852 | | |
815 | 853 | | |
816 | | - | |
| 854 | + | |
817 | 855 | | |
818 | 856 | | |
819 | 857 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1982 | 1982 | | |
1983 | 1983 | | |
1984 | 1984 | | |
1985 | | - | |
1986 | | - | |
| 1985 | + | |
| 1986 | + | |
1987 | 1987 | | |
1988 | 1988 | | |
1989 | 1989 | | |
| |||
2009 | 2009 | | |
2010 | 2010 | | |
2011 | 2011 | | |
2012 | | - | |
| 2012 | + | |
2013 | 2013 | | |
2014 | 2014 | | |
2015 | 2015 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1144 | 1144 | | |
1145 | 1145 | | |
1146 | 1146 | | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
1147 | 1161 | | |
1148 | 1162 | | |
1149 | 1163 | | |
0 commit comments