perf: don't re-inline CSE'd expensive expressions in projection pushdown#23459
Open
fordN wants to merge 2 commits into
Open
perf: don't re-inline CSE'd expensive expressions in projection pushdown#23459fordN wants to merge 2 commits into
fordN wants to merge 2 commits into
Conversation
Contributor
Author
|
@xudong963 here's a follow up to #23395 that expands the set of expressions to not be "re-inline'd" to include performance considerations along with the correctness considerations. In practice, this change has shown to have a significant effect on the performance of queries where outer statement references expensive computation results from inner. |
Add a benchmark measuring queries that repeat a scalar function call, which the logical CSE pass extracts into a single intermediate projection referenced by column. It exercises the interaction between CSE and projection pushdown on parquet sources; a follow-up will use it to demonstrate reducing redundant re-evaluation of the extracted expression.
Extend the projection-pushdown guard so it also declines to merge a projection into a file scan when the merge would duplicate a non-trivial expression (e.g. `power(a, 2)`, `a + b`, `c2 >= 2`) that CSE extracted into a shared intermediate projection. Without this the expression is re-inlined at every reference site and re-evaluated per row, undoing the deduplication. "Non-trivial" is decided by expression placement (`KeepInPlace`) — the same signal `try_collapse_projection_chain` uses — so leaf-pushable expressions (columns, `get_field`, `input_file_name`) still merge. References are counted with multiplicity (reused from the volatile guard), so an expression is blocked only when referenced more than once; volatile (correctness, apache#23220) and non-trivial (performance) share the same threshold.
80096e0 to
8b7250a
Compare
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.
Which issue does this PR close?
Follow-up to #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'sDataSourceExec, 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 #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:EXPLAIN, after —
power(a, 2)evaluated once, kept in aProjectionExec:What changes are included in this PR?
FileScanConfig::try_swapping_with_projectionso 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 signaltry_collapse_projection_chainuses — 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.cse_projection_pushdownbenchmark used to measure the change.Are these changes tested?
Yes:
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-sqllogictestsuite passes. One existing plan inwindow.sltimproves: a repeatedc2 >= 2comparison over a CSV scan is now computed once instead of being inlined twice into theDataSourceExec.cse_projection_pushdown(sample-size 50, 5s), with theno_repeated_exprscontrol confirming no change on unaffected queries:repeated_power(power(a,2)×3)repeated_nested_fn(ln(abs(a))×3)repeated_sqrt(sqrt(a)×3)mixed_repeated_uniquerepeated_cheap_abs(abs(a)×3)no_repeated_exprs(control)The gain scales with expression cost. For a single-instruction function like
abs, caching the value in aProjectionExecversus recomputing it is a wash (run-to-run noise), since the extra plan node roughly offsets the recomputation it saves.Reproduce with:
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
ProjectionExecabove the scan (the expression evaluated once) instead of inlining it into theDataSourceExecprojection.