Skip to content

Commit 04d7714

Browse files
author
B Vadlamani
committed
groups_acc_more_robust_checks
1 parent a9cdec8 commit 04d7714

1 file changed

Lines changed: 62 additions & 65 deletions

File tree

datafusion/functions-aggregate/benches/count_distinct.rs

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -200,77 +200,74 @@ fn create_skewed_groups(num_groups: usize) -> Vec<usize> {
200200
fn count_distinct_groups_benchmark(c: &mut Criterion) {
201201
let count_fn = Count::new();
202202

203-
// bench different scenarios
204-
let scenarios = [
205-
// (name, num_groups, distinct_pct, group_fn)
206-
("sparse_uniform", 10, 80, "uniform"),
207-
("moderate_uniform", 100, 80, "uniform"),
208-
("dense_uniform", 1000, 80, "uniform"),
209-
("sparse_skewed", 10, 80, "skewed"),
210-
("dense_skewed", 1000, 80, "skewed"),
211-
("sparse_high_cardinality", 10, 99, "uniform"),
212-
("dense_low_cardinality", 1000, 20, "uniform"),
213-
];
203+
let group_counts = [100, 1000, 10000];
204+
let cardinalities = [("low", 20), ("mid", 80), ("high", 99)];
205+
let distributions = ["uniform", "skewed"];
214206

215-
for (name, num_groups, distinct_pct, group_type) in scenarios {
216-
let n_distinct = BATCH_SIZE * distinct_pct / 100;
217-
let values = Arc::new(create_i64_array(n_distinct)) as ArrayRef;
218-
let group_indices = if group_type == "uniform" {
219-
create_uniform_groups(num_groups)
220-
} else {
221-
create_skewed_groups(num_groups)
222-
};
223-
224-
let (_schema, args) = prepare_args(DataType::Int64);
225-
226-
if count_fn.groups_accumulator_supported(args.clone()) {
227-
c.bench_function(&format!("count_distinct_groups {name}"), |b| {
228-
b.iter(|| {
229-
let mut acc =
230-
count_fn.create_groups_accumulator(args.clone()).unwrap();
231-
acc.update_batch(
232-
std::slice::from_ref(&values),
233-
&group_indices,
234-
None,
235-
num_groups,
236-
)
237-
.unwrap();
238-
acc.evaluate(EmitTo::All).unwrap()
239-
})
240-
});
241-
} else {
242-
let arr = values.as_any().downcast_ref::<Int64Array>().unwrap();
243-
let mut group_rows: Vec<Vec<i64>> = vec![Vec::new(); num_groups];
244-
for (idx, &group_idx) in group_indices.iter().enumerate() {
245-
if arr.is_valid(idx) {
246-
group_rows[group_idx].push(arr.value(idx));
247-
}
248-
}
249-
let group_arrays: Vec<ArrayRef> = group_rows
250-
.iter()
251-
.map(|rows| Arc::new(Int64Array::from(rows.clone())) as ArrayRef)
252-
.collect();
207+
for num_groups in group_counts {
208+
for (card_name, distinct_pct) in cardinalities {
209+
for dist in distributions {
210+
let name = format!("g{num_groups}_{card_name}_{dist}");
211+
let n_distinct = BATCH_SIZE * distinct_pct / 100;
212+
let values = Arc::new(create_i64_array(n_distinct)) as ArrayRef;
213+
let group_indices = if dist == "uniform" {
214+
create_uniform_groups(num_groups)
215+
} else {
216+
create_skewed_groups(num_groups)
217+
};
253218

254-
c.bench_function(&format!("count_distinct_groups {name}"), |b| {
255-
b.iter(|| {
256-
let mut accumulators: Vec<_> = (0..num_groups)
257-
.map(|_| prepare_accumulator(DataType::Int64))
258-
.collect();
219+
let (_schema, args) = prepare_args(DataType::Int64);
259220

260-
for (group_idx, batch) in group_arrays.iter().enumerate() {
261-
if !batch.is_empty() {
262-
accumulators[group_idx]
263-
.update_batch(std::slice::from_ref(batch))
264-
.unwrap();
221+
if count_fn.groups_accumulator_supported(args.clone()) {
222+
c.bench_function(&format!("count_distinct_groups {name}"), |b| {
223+
b.iter(|| {
224+
let mut acc =
225+
count_fn.create_groups_accumulator(args.clone()).unwrap();
226+
acc.update_batch(
227+
std::slice::from_ref(&values),
228+
&group_indices,
229+
None,
230+
num_groups,
231+
)
232+
.unwrap();
233+
acc.evaluate(EmitTo::All).unwrap()
234+
})
235+
});
236+
} else {
237+
let arr = values.as_any().downcast_ref::<Int64Array>().unwrap();
238+
let mut group_rows: Vec<Vec<i64>> = vec![Vec::new(); num_groups];
239+
for (idx, &group_idx) in group_indices.iter().enumerate() {
240+
if arr.is_valid(idx) {
241+
group_rows[group_idx].push(arr.value(idx));
265242
}
266243
}
267-
268-
let _results: Vec<_> = accumulators
269-
.iter_mut()
270-
.map(|acc| acc.evaluate().unwrap())
244+
let group_arrays: Vec<ArrayRef> = group_rows
245+
.iter()
246+
.map(|rows| Arc::new(Int64Array::from(rows.clone())) as ArrayRef)
271247
.collect();
272-
})
273-
});
248+
249+
c.bench_function(&format!("count_distinct_groups {name}"), |b| {
250+
b.iter(|| {
251+
let mut accumulators: Vec<_> = (0..num_groups)
252+
.map(|_| prepare_accumulator(DataType::Int64))
253+
.collect();
254+
255+
for (group_idx, batch) in group_arrays.iter().enumerate() {
256+
if !batch.is_empty() {
257+
accumulators[group_idx]
258+
.update_batch(std::slice::from_ref(batch))
259+
.unwrap();
260+
}
261+
}
262+
263+
let _results: Vec<_> = accumulators
264+
.iter_mut()
265+
.map(|acc| acc.evaluate().unwrap())
266+
.collect();
267+
})
268+
});
269+
}
270+
}
274271
}
275272
}
276273
}

0 commit comments

Comments
 (0)