datafusion_expr/function.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Function module contains typing and signature for built-in and user defined functions.
19
20use crate::ColumnarValue;
21use crate::{Expr, PartitionEvaluator};
22use arrow::datatypes::DataType;
23use datafusion_common::Result;
24use std::sync::Arc;
25
26pub use datafusion_functions_aggregate_common::accumulator::{
27 AccumulatorArgs, AccumulatorFactoryFunction, StateFieldsArgs,
28};
29
30pub use datafusion_functions_window_common::expr::ExpressionArgs;
31pub use datafusion_functions_window_common::field::WindowUDFFieldArgs;
32pub use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;
33
34#[derive(Debug, Clone, Copy)]
35pub enum Hint {
36 /// Indicates the argument needs to be padded if it is scalar
37 Pad,
38 /// Indicates the argument can be converted to an array of length 1
39 AcceptsSingular,
40}
41
42/// Scalar function
43///
44/// The Fn param is the wrapped function but be aware that the function will
45/// be passed with the slice / vec of columnar values (either scalar or array)
46/// with the exception of zero param function, where a singular element vec
47/// will be passed. In that case the single element is a null array to indicate
48/// the batch's row count (so that the generative zero-argument function can know
49/// the result array size).
50pub type ScalarFunctionImplementation =
51 Arc<dyn Fn(&[ColumnarValue]) -> Result<ColumnarValue> + Send + Sync>;
52
53/// Factory that returns the functions's return type given the input argument types
54pub type ReturnTypeFunction =
55 Arc<dyn Fn(&[DataType]) -> Result<Arc<DataType>> + Send + Sync>;
56
57/// Factory that creates a PartitionEvaluator for the given window
58/// function
59pub type PartitionEvaluatorFactory =
60 Arc<dyn Fn() -> Result<Box<dyn PartitionEvaluator>> + Send + Sync>;
61
62/// Factory that returns the types used by an aggregator to serialize
63/// its state, given its return datatype.
64pub type StateTypeFunction =
65 Arc<dyn Fn(&DataType) -> Result<Arc<Vec<DataType>>> + Send + Sync>;
66
67/// [crate::udaf::AggregateUDFImpl::simplify] simplifier closure
68/// A closure with two arguments:
69/// * 'aggregate_function': [crate::expr::AggregateFunction] for which simplified has been invoked
70/// * 'info': [crate::simplify::SimplifyInfo]
71///
72/// Closure returns simplified [Expr] or an error.
73pub type AggregateFunctionSimplification = Box<
74 dyn Fn(
75 crate::expr::AggregateFunction,
76 &dyn crate::simplify::SimplifyInfo,
77 ) -> Result<Expr>,
78>;
79
80/// [crate::udwf::WindowUDFImpl::simplify] simplifier closure
81/// A closure with two arguments:
82/// * 'window_function': [crate::expr::WindowFunction] for which simplified has been invoked
83/// * 'info': [crate::simplify::SimplifyInfo]
84///
85/// Closure returns simplified [Expr] or an error.
86pub type WindowFunctionSimplification = Box<
87 dyn Fn(
88 crate::expr::WindowFunction,
89 &dyn crate::simplify::SimplifyInfo,
90 ) -> Result<Expr>,
91>;