Skip to content

Commit 3acb73b

Browse files
Shreyaskr1409alamb
authored and
Nirnay Roy
committed
Add documentation example for AggregateExprBuilder (apache#15504)
* Add formatting changes and fix the merge issue * Fix formatting * Fixed a faulty statement * Folded irrelevant sections from example * Fix formatting * Add unimplemented!() --------- Co-authored-by: Andrew Lamb <[email protected]>
1 parent eef461f commit 3acb73b

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

datafusion/physical-expr/src/aggregate.rs

+88
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,94 @@ impl AggregateExprBuilder {
9797
/// Constructs an `AggregateFunctionExpr` from the builder
9898
///
9999
/// Note that an [`Self::alias`] must be provided before calling this method.
100+
///
101+
/// # Example: Create an [`AggregateUDF`]
102+
///
103+
/// In the following example, [`AggregateFunctionExpr`] will be built using [`AggregateExprBuilder`]
104+
/// which provides a build function. Full example could be accessed from the source file.
105+
///
106+
/// ```
107+
/// # use std::any::Any;
108+
/// # use std::sync::Arc;
109+
/// # use arrow::datatypes::DataType;
110+
/// # use datafusion_common::{Result, ScalarValue};
111+
/// # use datafusion_expr::{col, ColumnarValue, Documentation, Signature, Volatility, Expr};
112+
/// # use datafusion_expr::{AggregateUDFImpl, AggregateUDF, Accumulator, function::{AccumulatorArgs, StateFieldsArgs}};
113+
/// # use arrow::datatypes::Field;
114+
/// #
115+
/// # #[derive(Debug, Clone)]
116+
/// # struct FirstValueUdf {
117+
/// # signature: Signature,
118+
/// # }
119+
/// #
120+
/// # impl FirstValueUdf {
121+
/// # fn new() -> Self {
122+
/// # Self {
123+
/// # signature: Signature::any(1, Volatility::Immutable),
124+
/// # }
125+
/// # }
126+
/// # }
127+
/// #
128+
/// # impl AggregateUDFImpl for FirstValueUdf {
129+
/// # fn as_any(&self) -> &dyn Any {
130+
/// # unimplemented!()
131+
/// # }
132+
/// # fn name(&self) -> &str {
133+
/// # unimplemented!()
134+
/// }
135+
/// # fn signature(&self) -> &Signature {
136+
/// # unimplemented!()
137+
/// # }
138+
/// # fn return_type(&self, args: &[DataType]) -> Result<DataType> {
139+
/// # unimplemented!()
140+
/// # }
141+
/// #
142+
/// # fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
143+
/// # unimplemented!()
144+
/// # }
145+
/// #
146+
/// # fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<Field>> {
147+
/// # unimplemented!()
148+
/// # }
149+
/// #
150+
/// # fn documentation(&self) -> Option<&Documentation> {
151+
/// # unimplemented!()
152+
/// # }
153+
/// # }
154+
/// #
155+
/// # let first_value = AggregateUDF::from(FirstValueUdf::new());
156+
/// # let expr = first_value.call(vec![col("a")]);
157+
/// #
158+
/// # use datafusion_physical_expr::expressions::Column;
159+
/// # use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
160+
/// # use datafusion_physical_expr::aggregate::AggregateExprBuilder;
161+
/// # use datafusion_physical_expr::expressions::PhysicalSortExpr;
162+
/// # use datafusion_physical_expr::PhysicalSortRequirement;
163+
/// #
164+
/// fn build_aggregate_expr() -> Result<()> {
165+
/// let args = vec![Arc::new(Column::new("a", 0)) as Arc<dyn PhysicalExpr>];
166+
/// let order_by = vec![PhysicalSortExpr {
167+
/// expr: Arc::new(Column::new("x", 1)) as Arc<dyn PhysicalExpr>,
168+
/// options: Default::default(),
169+
/// }];
170+
///
171+
/// let first_value = AggregateUDF::from(FirstValueUdf::new());
172+
///
173+
/// let aggregate_expr = AggregateExprBuilder::new(
174+
/// Arc::new(first_value),
175+
/// args
176+
/// )
177+
/// .order_by(order_by.into())
178+
/// .alias("first_a_by_x")
179+
/// .ignore_nulls()
180+
/// .build()?;
181+
///
182+
/// Ok(())
183+
/// }
184+
/// ```
185+
///
186+
/// This creates a physical expression equivalent to SQL:
187+
/// `first_value(a ORDER BY x) IGNORE NULLS AS first_a_by_x`
100188
pub fn build(self) -> Result<AggregateFunctionExpr> {
101189
let Self {
102190
fun,

0 commit comments

Comments
 (0)