Trait datafusion_expr::ScalarUDFImpl
source · pub trait ScalarUDFImpl: Debug + Send + Sync {
// Required methods
fn as_any(&self) -> &dyn Any;
fn name(&self) -> &str;
fn signature(&self) -> &Signature;
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType>;
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue>;
// Provided methods
fn aliases(&self) -> &[String] { ... }
fn monotonicity(&self) -> Result<Option<FuncMonotonicity>> { ... }
}
Expand description
Trait for implementing ScalarUDF
.
This trait exposes the full API for implementing user defined functions and can be used to implement any function.
See advanced_udf.rs
for a full example with complete implementation and
ScalarUDF
for other available options.
§Basic Example
#[derive(Debug)]
struct AddOne {
signature: Signature
};
impl AddOne {
fn new() -> Self {
Self {
signature: Signature::uniform(1, vec![DataType::Int32], Volatility::Immutable)
}
}
}
/// Implement the ScalarUDFImpl trait for AddOne
impl ScalarUDFImpl for AddOne {
fn as_any(&self) -> &dyn Any { self }
fn name(&self) -> &str { "add_one" }
fn signature(&self) -> &Signature { &self.signature }
fn return_type(&self, args: &[DataType]) -> Result<DataType> {
if !matches!(args.get(0), Some(&DataType::Int32)) {
return plan_err!("add_one only accepts Int32 arguments");
}
Ok(DataType::Int32)
}
// The actual implementation would add one to the argument
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { unimplemented!() }
}
// Create a new ScalarUDF from the implementation
let add_one = ScalarUDF::from(AddOne::new());
// Call the function `add_one(col)`
let expr = add_one.call(vec![col("a")]);
Required Methods§
sourcefn signature(&self) -> &Signature
fn signature(&self) -> &Signature
Returns the function’s Signature
for information about what input
types are accepted and the function’s Volatility.
sourcefn return_type(&self, arg_types: &[DataType]) -> Result<DataType>
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType>
What [DataType
] will be returned by this function, given the types of
the arguments
sourcefn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue>
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue>
Invoke the function on args
, returning the appropriate result
The function will be invoked passed with the slice of ColumnarValue
(either scalar or array).
§Zero Argument Functions
If the function has zero parameters (e.g. now()
) it will be passed a
single element slice which is a a null array to indicate the batch’s row
count (so the function can know the resulting array size).
§Performance
For the best performance, the implementations of invoke
should handle
the common case when one or more of their arguments are constant values
(aka ColumnarValue::Scalar
). Calling ColumnarValue::into_array
and treating all arguments as arrays will work, but will be slower.
Provided Methods§
sourcefn aliases(&self) -> &[String]
fn aliases(&self) -> &[String]
Returns any aliases (alternate names) for this function.
Aliases can be used to invoke the same function using different names.
For example in some databases now()
and current_timestamp()
are
aliases for the same function. This behavior can be obtained by
returning current_timestamp
as an alias for the now
function.
Note: aliases
should only include names other than Self::name
.
Defaults to []
(no aliases)
sourcefn monotonicity(&self) -> Result<Option<FuncMonotonicity>>
fn monotonicity(&self) -> Result<Option<FuncMonotonicity>>
This function specifies monotonicity behaviors for User defined scalar functions.