Repository navigation
Commit 5134a1a
feat: add projection support to SortMergeJoinExec (#24517)
## Which issue does this PR close?
- Closes #24518
## Rationale for this change
`SortMergeJoinExec` emits every column of both inputs.
We can add projection support to only produce the output columns (saving
some compute / copy).
```sql
select * from t1 right join t2 on t1.c3 = t2.c3 -- c3 needs a cast
```
```text
ProjectionExec: expr=[c1@0 as c1, ..., c4@8 as c4]
SortMergeJoinExec: join_type=Right, on=[(CAST(t1.c3 AS Decimal128(10, 2))@4, c3@2)]
```
The cast column is only there to be joined on, and every operator above
the join
carries it until the projection removes it.
## What changes are included in this PR?
`SortMergeJoinExec` takes an optional projection, set with
`with_projection`, the same
shape as `HashJoinExec`'s.
`try_swapping_with_projection` embeds the projection into the join when
it cannot push
it into the children, so the query above becomes:
```text
SortMergeJoinExec: join_type=Right, on=[(CAST(t1.c3 AS Decimal128(10, 2))@4, c3@2)], projection=[c1@0, c2@1, c3@2, c4@3, c1@5, c2@6, c3@7, c4@8]
```
The change doesn't bring a big speedup but helps aligning with other
join types and helping simplify join optimization in other areas (e.g.
join enumeration).
## Are these changes tested?
Yes:
- a projection pushdown test for a projection that interleaves the two
sides, which
the existing pushdown cannot handle
- a serialization round trip
- existing sqllogictests, whose plans lose a `ProjectionExec` in four
places
## Are there any user-facing changes?
Explain / proto changes.
---------
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>1 parent 3931ab4 commit 5134a1a
10 files changed
Lines changed: 373 additions & 67 deletions
File tree
- datafusion
- core/tests/physical_optimizer
- physical-plan/src/joins/sort_merge_join
- proto-models
- proto
- src/generated
- proto/tests/cases/plans
- sqllogictest/test_files
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
53 | | - | |
| 52 | + | |
| 53 | + | |
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| |||
1875 | 1875 | | |
1876 | 1876 | | |
1877 | 1877 | | |
| 1878 | + | |
| 1879 | + | |
| 1880 | + | |
| 1881 | + | |
| 1882 | + | |
| 1883 | + | |
| 1884 | + | |
| 1885 | + | |
| 1886 | + | |
| 1887 | + | |
| 1888 | + | |
| 1889 | + | |
| 1890 | + | |
| 1891 | + | |
| 1892 | + | |
| 1893 | + | |
| 1894 | + | |
| 1895 | + | |
| 1896 | + | |
| 1897 | + | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
| 1901 | + | |
| 1902 | + | |
| 1903 | + | |
| 1904 | + | |
| 1905 | + | |
| 1906 | + | |
| 1907 | + | |
| 1908 | + | |
| 1909 | + | |
| 1910 | + | |
| 1911 | + | |
| 1912 | + | |
| 1913 | + | |
| 1914 | + | |
| 1915 | + | |
| 1916 | + | |
| 1917 | + | |
| 1918 | + | |
| 1919 | + | |
| 1920 | + | |
0 commit comments