Skip to content

Commit b591f2f

Browse files
committed
refactor: simplify grouped metric retrieval and clean up metric recorder initialization
1 parent 39aab04 commit b591f2f

3 files changed

Lines changed: 19 additions & 19 deletions

File tree

‎datafusion/functions-aggregate-common/src/aggregate/groups_accumulator.rs‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ impl GroupsAccumulatorAdapter {
168168
Ok(accumulator)
169169
}
170170

171+
/// Returns the aggregate-owned grouped-update metric, if the first
172+
/// accumulator this adapter created exposed one.
173+
///
174+
/// The cell is seeded by [`Self::create_accumulator`], so this returns
175+
/// `None` until at least one accumulator has been built.
176+
fn grouped_metric(&self) -> Option<Arc<dyn AggregateMetric>> {
177+
self.grouped_update_metric.get().and_then(Clone::clone)
178+
}
179+
171180
/// Ensure that self.accumulators has total_num_groups
172181
fn make_accumulators_if_needed(&mut self, total_num_groups: usize) -> Result<()> {
173182
// can't shrink
@@ -277,8 +286,7 @@ impl GroupsAccumulatorAdapter {
277286
let values = take_arrays(values, &batch_indices, None)?;
278287
let opt_filter = get_filter_at_indices(opt_filter, &batch_indices)?;
279288

280-
let grouped_update_metric =
281-
self.grouped_update_metric.get().and_then(Clone::clone);
289+
let grouped_update_metric = self.grouped_metric();
282290

283291
let mut sizes_pre = 0;
284292
let mut sizes_post = 0;
@@ -570,11 +578,11 @@ impl GroupsAccumulator for GroupsAccumulatorAdapter {
570578
// Create the empty accumulator and prepare adapter-owned input
571579
// outside the aggregate submetric.
572580
let accumulator = self.create_accumulator()?;
573-
if row_idx == 0 {
574-
metric_recorder = Some(AggregateMetricRecorder::new(
575-
self.grouped_update_metric.get().and_then(Clone::clone),
576-
));
577-
}
581+
// `create_accumulator` resolves the metric, so the recorder can
582+
// only be built once the first accumulator exists.
583+
metric_recorder.get_or_insert_with(|| {
584+
AggregateMetricRecorder::new(self.grouped_metric())
585+
});
578586
let values_to_accumulate =
579587
slice_and_maybe_filter(values, opt_filter, &[row_idx, row_idx + 1])?;
580588
prepared.push((accumulator, values_to_accumulate));

‎datafusion/functions-aggregate/src/array_agg.rs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -913,9 +913,7 @@ impl DistinctArrayAggAccumulator {
913913
}
914914
Ok(())
915915
}
916-
}
917916

918-
impl DistinctArrayAggAccumulator {
919917
fn update_batch_impl(
920918
&mut self,
921919
values: &[ArrayRef],
@@ -954,7 +952,7 @@ impl DistinctArrayAggAccumulator {
954952
.then(|| self.distinct_metric.clone())
955953
.flatten();
956954
let mut metric_recorder = AggregateMetricRecorder::new(distinct_metric);
957-
(|| {
955+
{
958956
let _timer = metric_recorder.timer();
959957
self.ensure_state(col.data_type())?;
960958

@@ -1003,11 +1001,9 @@ impl DistinctArrayAggAccumulator {
10031001
}
10041002
}
10051003
Ok(())
1006-
})()
1004+
}
10071005
}
1008-
}
10091006

1010-
impl DistinctArrayAggAccumulator {
10111007
fn merge_batch_impl(
10121008
&mut self,
10131009
states: &[ArrayRef],

‎datafusion/physical-plan/src/aggregates/group_values/metrics.rs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,8 @@ impl AggregateMetric for AggregateSubMetric {
8282

8383
impl AggregateMetrics for AggregateSubMetrics {
8484
fn metric(&self, subphase: &'static str) -> Arc<dyn AggregateMetric> {
85-
if let Some((registered_subphase, metric)) = self.first_subphase_metric.get()
86-
&& *registered_subphase == subphase
87-
{
88-
return Arc::clone(metric);
89-
}
90-
85+
// `get_or_init` already takes a lock-free fast path when the cell is
86+
// initialized, so the common repeat lookup never allocates or locks.
9187
let (registered_subphase, metric) = self
9288
.first_subphase_metric
9389
.get_or_init(|| (subphase, self.new_metric(subphase)));

0 commit comments

Comments
 (0)