Repository navigation
Commit b006c63
perf: don't re-inline CSE'd expensive expressions in projection pushdown (apache#23459)
## Which issue does this PR close?
- Closes apache#23425.
Follow-up to apache#23395 (the volatile correctness fix, now merged),
extending the same projection-pushdown guard to the performance case.
## Rationale for this change
The logical CSE pass extracts a repeated scalar-function call (e.g.
`power(a, 2)`) into a single intermediate projection referenced by
column, so it is evaluated once per row. For file sources that can
absorb computed projections (e.g. Parquet), the physical
projection-pushdown rule then merges that projection into the scan's
`DataSourceExec`, re-inlining the expression at every reference site and
re-evaluating it N times per row — undoing the deduplication.
This is the performance sibling of apache#23220 (which fixed the *correctness*
case for volatile expressions). It reproduces on file scans
(Parquet/CSV), not in-memory tables.
**EXPLAIN, before** — `power(a, 2)` evaluated 3× per row:
```
DataSourceExec: projection=[power(a,2)+b as x, power(a,2)-b as y, power(a,2)*c as z]
```
**EXPLAIN, after** — `power(a, 2)` evaluated once, kept in a
`ProjectionExec`:
```
ProjectionExec: [__common_expr_1+b as x, __common_expr_1-b as y, __common_expr_1*c as z]
DataSourceExec: projection=[power(a,2) as __common_expr_1, b, c]
```
## What changes are included in this PR?
- Extends the projection-pushdown guard in
`FileScanConfig::try_swapping_with_projection` so it also declines the
merge when it would duplicate a **non-trivial** expression.
"Non-trivial" is decided by the existing expression-placement signal
(`KeepInPlace`) — the same signal `try_collapse_projection_chain` uses —
covering arithmetic, casts, and most scalar functions. Cheap
leaf-pushable expressions (columns, `get_field`, `input_file_name`)
still merge, so struct-field pushdown is unaffected. It reuses the
volatile guard's multiplicity-aware reference counting, so an expression
is blocked only when referenced more than once.
- Adds the `cse_projection_pushdown` benchmark used to measure the
change.
## Are these changes tested?
Yes:
- Unit tests in `file_scan_config.rs`: a non-trivial expr (arithmetic /
scalar function) referenced ≥2 → blocked, once → allowed; a
leaf-pushable scalar function (`get_field`) → allowed; volatile
referenced ≥2 → blocked, once → allowed.
- `datafusion-sqllogictest` suite passes. One existing plan in
`window.slt` improves: a repeated `c2 >= 2` comparison over a CSV scan
is now computed once instead of being inlined twice into the
`DataSourceExec`.
- Benchmark A/B on `cse_projection_pushdown` (sample-size 50, 5s), with
the `no_repeated_exprs` control confirming no change on unaffected
queries:
| Benchmark | Change |
|---|---|
| `repeated_power` (`power(a,2)` ×3) | −40% |
| `repeated_nested_fn` (`ln(abs(a))` ×3) | −38% |
| `repeated_sqrt` (`sqrt(a)` ×3) | −37% |
| `mixed_repeated_unique` | −35% |
| `repeated_cheap_abs` (`abs(a)` ×3) | within noise |
| `no_repeated_exprs` (control) | no change |
The gain scales with expression cost. For a single-instruction function
like `abs`, caching the value in a `ProjectionExec` versus recomputing
it is a wash (run-to-run noise), since the extra plan node roughly
offsets the recomputation it saves.
Reproduce with:
```
cargo bench -p datafusion --bench cse_projection_pushdown --features parquet
```
## Are there any user-facing changes?
No API changes. Queries that repeat an expensive expression over a file
scan run faster; their physical plans retain a `ProjectionExec` above
the scan (the expression evaluated once) instead of inlining it into the
`DataSourceExec` projection.1 parent 7181da7 commit b006c63
5 files changed
Lines changed: 368 additions & 37 deletions
File tree
- datafusion
- core
- benches
- datasource/src/file_scan_config
- sqllogictest/test_files
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
255 | 260 | | |
256 | 261 | | |
257 | 262 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
0 commit comments