common_function/
function_factory.rs1use std::sync::Arc;
16
17use datafusion_expr::ScalarUDF;
18
19use crate::function::{FunctionContext, FunctionRef};
20use crate::scalars::udf::create_udf;
21
22#[derive(Clone)]
24pub struct ScalarFunctionFactory {
25    pub name: String,
26    pub factory: Arc<dyn Fn(FunctionContext) -> ScalarUDF + Send + Sync>,
27}
28
29impl ScalarFunctionFactory {
30    pub fn name(&self) -> &str {
32        &self.name
33    }
34
35    pub fn provide(&self, ctx: FunctionContext) -> ScalarUDF {
37        (self.factory)(ctx)
38    }
39}
40
41impl From<ScalarUDF> for ScalarFunctionFactory {
42    fn from(df_udf: ScalarUDF) -> Self {
43        let name = df_udf.name().to_string();
44        let func = Arc::new(move |_ctx| df_udf.clone());
45        Self {
46            name,
47            factory: func,
48        }
49    }
50}
51
52impl From<FunctionRef> for ScalarFunctionFactory {
53    fn from(func: FunctionRef) -> Self {
54        let name = func.name().to_string();
55        let func = Arc::new(move |_| create_udf(func.clone()));
56        Self {
57            name,
58            factory: func,
59        }
60    }
61}