Constructs an `AggregateFunctionExpr` from the builder Note that an [`Self::alias`] must be provided before calling this method. # Example: Create an [`AggregateUDF`] In the following example, [`AggregateFunctionExpr`] will be built using [`AggregateExprBuilder`] which provides a build function. Full example could be accessed from the source file. ``` # use std::any::Any; # use std::sync::Arc;
(self)
| 248 | /// This creates a physical expression equivalent to SQL: |
| 249 | /// `first_value(a ORDER BY x) IGNORE NULLS AS first_a_by_x` |
| 250 | pub fn build(self) -> Result<AggregateFunctionExpr> { |
| 251 | let Self { |
| 252 | fun, |
| 253 | args, |
| 254 | alias, |
| 255 | output_metadata, |
| 256 | human_display, |
| 257 | human_display_alias, |
| 258 | schema, |
| 259 | order_bys, |
| 260 | ignore_nulls, |
| 261 | is_distinct, |
| 262 | is_reversed, |
| 263 | } = self; |
| 264 | assert_or_internal_err!(!args.is_empty(), "args should not be empty"); |
| 265 | |
| 266 | let ordering_types = order_bys |
| 267 | .iter() |
| 268 | .map(|e| e.expr.data_type(&schema)) |
| 269 | .collect::<Result<Vec<_>>>()?; |
| 270 | |
| 271 | let ordering_fields = utils::ordering_fields(&order_bys, &ordering_types); |
| 272 | |
| 273 | let input_exprs_fields = args |
| 274 | .iter() |
| 275 | .map(|arg| arg.return_field(&schema)) |
| 276 | .collect::<Result<Vec<_>>>()?; |
| 277 | |
| 278 | check_arg_count( |
| 279 | fun.name(), |
| 280 | &input_exprs_fields, |
| 281 | &fun.signature().type_signature, |
| 282 | )?; |
| 283 | |
| 284 | let mut return_field = fun.return_field(&input_exprs_fields)?; |
| 285 | if let Some(output_metadata) = output_metadata { |
| 286 | return_field = output_metadata.add_to_field_ref(return_field); |
| 287 | } |
| 288 | let is_nullable = fun.is_nullable(); |
| 289 | let name = match alias { |
| 290 | None => { |
| 291 | return internal_err!( |
| 292 | "AggregateExprBuilder::alias must be provided prior to calling build" |
| 293 | ); |
| 294 | } |
| 295 | Some(alias) => alias, |
| 296 | }; |
| 297 | |
| 298 | let human_display = |
| 299 | AggregateHumanDisplay::try_new(human_display, human_display_alias, &name)?; |
| 300 | |
| 301 | let arg_fields = args |
| 302 | .iter() |
| 303 | .map(|e| e.return_field(schema.as_ref())) |
| 304 | .collect::<Result<Vec<_>>>()?; |
| 305 | |
| 306 | Ok(AggregateFunctionExpr { |
| 307 | fun: Arc::unwrap_or_clone(fun), |