Skip to content

Commit d09ff92

Browse files
authored
feat: Reduce allocations for aggregating Statistics (#20768)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #15809. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? Vectorize aggregations for combining statistics by gathering all values then calling kernels once <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? Unit tests + existing tests <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? Removed `merge_iter`
1 parent 10d8bcb commit d09ff92

File tree

5 files changed

+769
-153
lines changed

5 files changed

+769
-153
lines changed

datafusion/common/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ name = "with_hashes"
6161
harness = false
6262
name = "scalar_to_array"
6363

64+
[[bench]]
65+
harness = false
66+
name = "stats_merge"
67+
6468
[dependencies]
6569
ahash = { workspace = true }
6670
apache-avro = { workspace = true, features = [
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
//! Benchmark for `Statistics::try_merge_iter`.
19+
20+
use std::sync::Arc;
21+
22+
use arrow::datatypes::{DataType, Field, Schema};
23+
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
24+
use datafusion_common::stats::Precision;
25+
use datafusion_common::{ColumnStatistics, ScalarValue, Statistics};
26+
27+
/// Build a vector of `n` with `num_cols` columns
28+
fn make_stats(n: usize, num_cols: usize) -> Vec<Statistics> {
29+
(0..n)
30+
.map(|i| {
31+
let mut stats = Statistics::default()
32+
.with_num_rows(Precision::Exact(100 + i))
33+
.with_total_byte_size(Precision::Exact(8000 + i * 80));
34+
for c in 0..num_cols {
35+
let base = (i * num_cols + c) as i64;
36+
stats = stats.add_column_statistics(
37+
ColumnStatistics::new_unknown()
38+
.with_null_count(Precision::Exact(i))
39+
.with_min_value(Precision::Exact(ScalarValue::Int64(Some(base))))
40+
.with_max_value(Precision::Exact(ScalarValue::Int64(Some(
41+
base + 1000,
42+
))))
43+
.with_sum_value(Precision::Exact(ScalarValue::Int64(Some(
44+
base * 100,
45+
)))),
46+
);
47+
}
48+
stats
49+
})
50+
.collect()
51+
}
52+
53+
fn bench_stats_merge(c: &mut Criterion) {
54+
let mut group = c.benchmark_group("stats_merge");
55+
56+
for &num_partitions in &[10, 100, 500] {
57+
for &num_cols in &[1, 5, 20] {
58+
let items = make_stats(num_partitions, num_cols);
59+
let schema = Arc::new(Schema::new(
60+
(0..num_cols)
61+
.map(|i| Field::new(format!("col{i}"), DataType::Int64, true))
62+
.collect::<Vec<_>>(),
63+
));
64+
65+
let param = format!("{num_partitions}parts_{num_cols}cols");
66+
67+
group.bench_with_input(
68+
BenchmarkId::new("try_merge_iter", &param),
69+
&(&items, &schema),
70+
|b, (items, schema)| {
71+
b.iter(|| {
72+
std::hint::black_box(
73+
Statistics::try_merge_iter(*items, schema).unwrap(),
74+
);
75+
});
76+
},
77+
);
78+
}
79+
}
80+
81+
group.finish();
82+
}
83+
84+
criterion_group!(benches, bench_stats_merge);
85+
criterion_main!(benches);

0 commit comments

Comments
 (0)