Skip to content

Commit b006c63

Browse files
fordNkosiew
authored andcommitted
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/Cargo.toml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ harness = false
252252
name = "parquet_struct_projection"
253253
required-features = ["parquet"]
254254

255+
[[bench]]
256+
harness = false
257+
name = "cse_projection_pushdown"
258+
required-features = ["parquet"]
259+
255260
[[bench]]
256261
harness = false
257262
name = "range_and_generate_series"
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//! Benchmarks for the interaction between Common Subexpression Elimination
19+
//! (CSE) and projection pushdown on parquet sources.
20+
//!
21+
//! Each query repeats a scalar function call several times, which the logical
22+
//! CSE pass extracts into a single intermediate projection referenced by
23+
//! column. These benchmarks measure the end-to-end cost of such queries, which
24+
//! is dominated by how many times the extracted expression is ultimately
25+
//! evaluated per row.
26+
27+
use arrow::array::{Float64Array, Int64Array};
28+
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
29+
use arrow::record_batch::RecordBatch;
30+
use criterion::{Criterion, criterion_group, criterion_main};
31+
use datafusion::prelude::{SessionConfig, SessionContext};
32+
use datafusion_common::instant::Instant;
33+
use futures::stream::StreamExt;
34+
use parquet::arrow::ArrowWriter;
35+
use parquet::file::properties::{WriterProperties, WriterVersion};
36+
use rand::prelude::*;
37+
use rand::rng;
38+
use std::sync::Arc;
39+
use tempfile::NamedTempFile;
40+
41+
const NUM_BATCHES: usize = 1024;
42+
const BATCH_SIZE: usize = 1024;
43+
44+
fn schema() -> SchemaRef {
45+
Arc::new(Schema::new(vec![
46+
Field::new("a", DataType::Float64, false),
47+
Field::new("b", DataType::Float64, false),
48+
Field::new("c", DataType::Int64, false),
49+
]))
50+
}
51+
52+
fn generate_batch() -> RecordBatch {
53+
let mut rng = rng();
54+
let len = BATCH_SIZE;
55+
56+
let a: Float64Array = (0..len)
57+
.map(|_| Some(rng.random_range(1.0..1000.0)))
58+
.collect();
59+
let b: Float64Array = (0..len)
60+
.map(|_| Some(rng.random_range(1.0..1000.0)))
61+
.collect();
62+
let c: Int64Array = (0..len)
63+
.map(|_| Some(rng.random_range(1i64..1000)))
64+
.collect();
65+
66+
RecordBatch::try_new(schema(), vec![Arc::new(a), Arc::new(b), Arc::new(c)]).unwrap()
67+
}
68+
69+
fn generate_file() -> NamedTempFile {
70+
let now = Instant::now();
71+
let mut named_file = tempfile::Builder::new()
72+
.prefix("cse_projection_pushdown")
73+
.suffix(".parquet")
74+
.tempfile()
75+
.unwrap();
76+
77+
println!("Generating parquet file - {}", named_file.path().display());
78+
79+
let props = WriterProperties::builder()
80+
.set_writer_version(WriterVersion::PARQUET_2_0)
81+
.set_max_row_group_row_count(Some(1024 * 1024))
82+
.build();
83+
84+
let mut writer =
85+
ArrowWriter::try_new(&mut named_file, schema(), Some(props)).unwrap();
86+
87+
for _ in 0..NUM_BATCHES {
88+
let batch = generate_batch();
89+
writer.write(&batch).unwrap();
90+
}
91+
writer.close().unwrap();
92+
93+
println!(
94+
"Generated parquet file in {} seconds",
95+
now.elapsed().as_secs_f32()
96+
);
97+
98+
named_file
99+
}
100+
101+
fn criterion_benchmark(c: &mut Criterion) {
102+
let temp_file = generate_file();
103+
let file_path = temp_file.path().display().to_string();
104+
105+
let partitions = 4;
106+
let config = SessionConfig::new().with_target_partitions(partitions);
107+
let context = SessionContext::new_with_config(config);
108+
109+
let local_rt = tokio::runtime::Builder::new_current_thread()
110+
.build()
111+
.unwrap();
112+
113+
let query_rt = tokio::runtime::Builder::new_multi_thread()
114+
.worker_threads(partitions)
115+
.build()
116+
.unwrap();
117+
118+
local_rt
119+
.block_on(context.register_parquet("t", file_path.as_str(), Default::default()))
120+
.unwrap();
121+
122+
// Queries that repeat a scalar function call, which CSE extracts into a
123+
// single intermediate projection referenced by column.
124+
let queries = vec![
125+
// Same sqrt(a) appears 3 times.
126+
(
127+
"repeated_sqrt",
128+
"SELECT sqrt(a) + 1, sqrt(a) * 2, sqrt(a) / b FROM t",
129+
),
130+
// power(a, 2) appears in multiple places.
131+
(
132+
"repeated_power",
133+
"SELECT power(a, 2) + b, power(a, 2) - b, power(a, 2) * c FROM t",
134+
),
135+
// Deeper nesting: ln(abs(a)) repeated.
136+
(
137+
"repeated_nested_fn",
138+
"SELECT ln(abs(a)) + 1, ln(abs(a)) * b, ln(abs(a)) + c FROM t",
139+
),
140+
// Mixed: some repeated, some unique.
141+
(
142+
"mixed_repeated_unique",
143+
"SELECT sqrt(a) + sqrt(a), abs(b), sqrt(a) * c FROM t",
144+
),
145+
// A trivial function (abs) repeated.
146+
(
147+
"repeated_cheap_abs",
148+
"SELECT abs(a) + 1, abs(a) * 2, abs(a) / b FROM t",
149+
),
150+
// Baseline: no repeated expressions (CSE does not fire).
151+
(
152+
"no_repeated_exprs",
153+
"SELECT sqrt(a), abs(b), power(a, 2) FROM t",
154+
),
155+
];
156+
157+
for (name, query) in queries {
158+
c.bench_function(&format!("cse_pushdown: {name}"), |b| {
159+
b.iter(|| {
160+
let query = query.to_string();
161+
let context = context.clone();
162+
let (sender, mut receiver) = futures::channel::mpsc::unbounded();
163+
164+
query_rt.spawn(async move {
165+
let query = context.sql(&query).await.unwrap();
166+
let mut stream = query.execute_stream().await.unwrap();
167+
168+
while let Some(next) = stream.next().await {
169+
sender.unbounded_send(next).unwrap();
170+
}
171+
});
172+
173+
local_rt.block_on(async {
174+
while receiver.next().await.transpose().unwrap().is_some() {}
175+
})
176+
});
177+
});
178+
}
179+
180+
drop(temp_file);
181+
}
182+
183+
criterion_group!(benches, criterion_benchmark);
184+
criterion_main!(benches);

0 commit comments

Comments
 (0)