Skip to content

Commit 63888e8

Browse files
authored
Improve AggregateUDFImpl::state_fields documentation (#9919)
1 parent 24fc99c commit 63888e8

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

datafusion/expr/src/udaf.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ impl AggregateUDF {
171171
self.inner.accumulator(acc_args)
172172
}
173173

174-
/// Return the fields of the intermediate state used by this aggregator, given
175-
/// its state name, value type and ordering fields. See [`AggregateUDFImpl::state_fields`]
176-
/// for more details. Supports multi-phase aggregations
174+
/// Return the fields used to store the intermediate state for this aggregator, given
175+
/// the name of the aggregate, value type and ordering fields. See [`AggregateUDFImpl::state_fields`]
176+
/// for more details.
177+
///
178+
/// This is used to support multi-phase aggregations
177179
pub fn state_fields(
178180
&self,
179181
name: &str,
@@ -283,13 +285,28 @@ pub trait AggregateUDFImpl: Debug + Send + Sync {
283285
/// `acc_args`: the arguments to the accumulator. See [`AccumulatorArgs`] for more details.
284286
fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>>;
285287

286-
/// Return the fields of the intermediate state.
288+
/// Return the fields used to store the intermediate state of this accumulator.
289+
///
290+
/// # Arguments:
291+
/// 1. `name`: the name of the expression (e.g. AVG, SUM, etc)
292+
/// 2. `value_type`: Aggregate's aggregate's output (returned by [`Self::return_type`])
293+
/// 3. `ordering_fields`: the fields used to order the input arguments, if any.
294+
/// Empty if no ordering expression is provided.
295+
///
296+
/// # Notes:
287297
///
288-
/// name: the name of the state
298+
/// The default implementation returns a single state field named `name`
299+
/// with the same type as `value_type`. This is suitable for aggregates such
300+
/// as `SUM` or `MIN` where partial state can be combined by applying the
301+
/// same aggregate.
289302
///
290-
/// value_type: the type of the value, it should be the result of the `return_type`
303+
/// For aggregates such as `AVG` where the partial state is more complex
304+
/// (e.g. a COUNT and a SUM), this method is used to define the additional
305+
/// fields.
291306
///
292-
/// ordering_fields: the fields used for ordering, empty if no ordering expression is provided
307+
/// The name of the fields must be unique within the query and thus should
308+
/// be derived from `name`. See [`format_state_name`] for a utility function
309+
/// to generate a unique name.
293310
fn state_fields(
294311
&self,
295312
name: &str,

0 commit comments

Comments
 (0)