datafusion_python/expr/
create_function.rs1use std::{
19 fmt::{self, Display, Formatter},
20 sync::Arc,
21};
22
23use datafusion::logical_expr::{
24 CreateFunction, CreateFunctionBody, OperateFunctionArg, Volatility,
25};
26use pyo3::{prelude::*, IntoPyObjectExt};
27
28use super::logical_node::LogicalNode;
29use super::PyExpr;
30use crate::common::{data_type::PyDataType, df_schema::PyDFSchema};
31use crate::sql::logical::PyLogicalPlan;
32
33#[pyclass(frozen, name = "CreateFunction", module = "datafusion.expr", subclass)]
34#[derive(Clone)]
35pub struct PyCreateFunction {
36 create: CreateFunction,
37}
38
39impl From<PyCreateFunction> for CreateFunction {
40 fn from(create: PyCreateFunction) -> Self {
41 create.create
42 }
43}
44
45impl From<CreateFunction> for PyCreateFunction {
46 fn from(create: CreateFunction) -> PyCreateFunction {
47 PyCreateFunction { create }
48 }
49}
50
51impl Display for PyCreateFunction {
52 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
53 write!(f, "CreateFunction: name {:?}", self.create.name)
54 }
55}
56
57#[pyclass(
58 frozen,
59 name = "OperateFunctionArg",
60 module = "datafusion.expr",
61 subclass
62)]
63#[derive(Clone)]
64pub struct PyOperateFunctionArg {
65 arg: OperateFunctionArg,
66}
67
68#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
69#[pyclass(frozen, eq, eq_int, name = "Volatility", module = "datafusion.expr")]
70pub enum PyVolatility {
71 Immutable,
72 Stable,
73 Volatile,
74}
75
76#[pyclass(
77 frozen,
78 name = "CreateFunctionBody",
79 module = "datafusion.expr",
80 subclass
81)]
82#[derive(Clone)]
83pub struct PyCreateFunctionBody {
84 body: CreateFunctionBody,
85}
86
87#[pymethods]
88impl PyCreateFunctionBody {
89 pub fn language(&self) -> Option<String> {
90 self.body
91 .language
92 .as_ref()
93 .map(|language| language.to_string())
94 }
95
96 pub fn behavior(&self) -> Option<PyVolatility> {
97 self.body.behavior.as_ref().map(|behavior| match behavior {
98 Volatility::Immutable => PyVolatility::Immutable,
99 Volatility::Stable => PyVolatility::Stable,
100 Volatility::Volatile => PyVolatility::Volatile,
101 })
102 }
103
104 pub fn function_body(&self) -> Option<PyExpr> {
105 self.body
106 .function_body
107 .as_ref()
108 .map(|function_body| function_body.clone().into())
109 }
110}
111
112#[pymethods]
113impl PyCreateFunction {
114 #[new]
115 #[pyo3(signature = (or_replace, temporary, name, params, schema, return_type=None, args=None))]
116 pub fn new(
117 or_replace: bool,
118 temporary: bool,
119 name: String,
120 params: PyCreateFunctionBody,
121 schema: PyDFSchema,
122 return_type: Option<PyDataType>,
123 args: Option<Vec<PyOperateFunctionArg>>,
124 ) -> Self {
125 PyCreateFunction {
126 create: CreateFunction {
127 or_replace,
128 temporary,
129 name,
130 args: args.map(|args| args.into_iter().map(|arg| arg.arg).collect()),
131 return_type: return_type.map(|return_type| return_type.data_type),
132 params: params.body,
133 schema: Arc::new(schema.into()),
134 },
135 }
136 }
137
138 pub fn or_replace(&self) -> bool {
139 self.create.or_replace
140 }
141
142 pub fn temporary(&self) -> bool {
143 self.create.temporary
144 }
145
146 pub fn name(&self) -> String {
147 self.create.name.clone()
148 }
149
150 pub fn params(&self) -> PyCreateFunctionBody {
151 PyCreateFunctionBody {
152 body: self.create.params.clone(),
153 }
154 }
155
156 pub fn schema(&self) -> PyDFSchema {
157 (*self.create.schema).clone().into()
158 }
159
160 pub fn return_type(&self) -> Option<PyDataType> {
161 self.create
162 .return_type
163 .as_ref()
164 .map(|return_type| return_type.clone().into())
165 }
166
167 pub fn args(&self) -> Option<Vec<PyOperateFunctionArg>> {
168 self.create.args.as_ref().map(|args| {
169 args.iter()
170 .map(|arg| PyOperateFunctionArg { arg: arg.clone() })
171 .collect()
172 })
173 }
174
175 fn __repr__(&self) -> PyResult<String> {
176 Ok(format!("CreateFunction({self})"))
177 }
178
179 fn __name__(&self) -> PyResult<String> {
180 Ok("CreateFunction".to_string())
181 }
182}
183
184impl LogicalNode for PyCreateFunction {
185 fn inputs(&self) -> Vec<PyLogicalPlan> {
186 vec![]
187 }
188
189 fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
190 self.clone().into_bound_py_any(py)
191 }
192}