@@ -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) ) ;
0 commit comments