datafusion_expr/function.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Function module contains typing and signature for built-in and user defined functions.
6
7use crate::ColumnarValue;
8use crate::{Expr, PartitionEvaluator};
9use arrow::datatypes::DataType;
10use datafusion_common::Result;
11use std::sync::Arc;
12
13pub use datafusion_functions_aggregate_common::accumulator::{
14 AccumulatorArgs, AccumulatorFactoryFunction, StateFieldsArgs,
15};
16
17use crate::expr::{AggregateFunction, WindowFunction};
18use crate::simplify::SimplifyContext;
19pub use datafusion_functions_window_common::expr::ExpressionArgs;
20pub use datafusion_functions_window_common::field::WindowUDFFieldArgs;
21pub use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;
22
23#[derive(Debug, Clone, Copy)]
24pub enum Hint {
25 /// Indicates the argument needs to be padded if it is scalar
26 Pad,
27 /// Indicates the argument can be converted to an array of length 1
28 AcceptsSingular,
29}
30
31/// Scalar function
32///
33/// The Fn param is the wrapped function but be aware that the function will
34/// be passed with the slice / vec of columnar values (either scalar or array)
35/// with the exception of zero param function, where a singular element vec
36/// will be passed. In that case the single element is a null array to indicate
37/// the batch's row count (so that the generative zero-argument function can know
38/// the result array size).
39pub type ScalarFunctionImplementation =
40 Arc<dyn Fn(&[ColumnarValue]) -> Result<ColumnarValue> + Send + Sync>;
41
42/// Factory that returns the functions's return type given the input argument types
43pub type ReturnTypeFunction =
44 Arc<dyn Fn(&[DataType]) -> Result<Arc<DataType>> + Send + Sync>;
45
46/// Factory that creates a PartitionEvaluator for the given window
47/// function
48pub type PartitionEvaluatorFactory =
49 Arc<dyn Fn() -> Result<Box<dyn PartitionEvaluator>> + Send + Sync>;
50
51/// Factory that returns the types used by an aggregator to serialize
52/// its state, given its return datatype.
53pub type StateTypeFunction =
54 Arc<dyn Fn(&DataType) -> Result<Arc<Vec<DataType>>> + Send + Sync>;
55
56/// Type alias for [crate::udaf::AggregateUDFImpl::simplify].
57///
58/// This closure is invoked with:
59/// * `aggregate_function`: [AggregateFunction] with already simplified arguments
60/// * `info`: [SimplifyContext]
61///
62/// It returns a simplified [Expr] or an error.
63pub type AggregateFunctionSimplification =
64 Box<dyn Fn(AggregateFunction, &SimplifyContext) -> Result<Expr>>;
65
66/// Type alias for [crate::udwf::WindowUDFImpl::simplify].
67///
68/// This closure is invoked with:
69/// * `window_function`: [WindowFunction] with already simplified arguments
70/// * `info`: [SimplifyContext]
71///
72/// It returns a simplified [Expr] or an error.
73pub type WindowFunctionSimplification =
74 Box<dyn Fn(WindowFunction, &SimplifyContext) -> Result<Expr>>;