|
| 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 | +use std::sync::Arc; |
| 19 | + |
| 20 | +use pyo3::prelude::*; |
| 21 | + |
| 22 | +use crate::dataframe::PyTableProvider; |
| 23 | +use crate::errors::py_datafusion_err; |
| 24 | +use crate::expr::PyExpr; |
| 25 | +use crate::utils::validate_pycapsule; |
| 26 | +use datafusion::catalog::{TableFunctionImpl, TableProvider}; |
| 27 | +use datafusion::common::exec_err; |
| 28 | +use datafusion::logical_expr::Expr; |
| 29 | +use datafusion_ffi::udtf::{FFI_TableFunction, ForeignTableFunction}; |
| 30 | +use pyo3::types::PyCapsule; |
| 31 | + |
| 32 | +/// Represents a user defined table function |
| 33 | +#[pyclass(name = "TableFunction", module = "datafusion")] |
| 34 | +#[derive(Debug, Clone)] |
| 35 | +pub struct PyTableFunction { |
| 36 | + pub(crate) name: String, |
| 37 | + pub(crate) inner: PyTableFunctionInner, |
| 38 | +} |
| 39 | + |
| 40 | +// TODO: Implement pure python based user defined table functions |
| 41 | +#[derive(Debug, Clone)] |
| 42 | +pub(crate) enum PyTableFunctionInner { |
| 43 | + // PythonFunction(Arc<PyObject>), |
| 44 | + FFIFunction(Arc<dyn TableFunctionImpl>), |
| 45 | +} |
| 46 | + |
| 47 | +#[pymethods] |
| 48 | +impl PyTableFunction { |
| 49 | + #[new] |
| 50 | + #[pyo3(signature=(name, func))] |
| 51 | + pub fn new(name: &str, func: Bound<'_, PyAny>) -> PyResult<Self> { |
| 52 | + if func.hasattr("__datafusion_table_function__")? { |
| 53 | + let capsule = func.getattr("__datafusion_table_function__")?.call0()?; |
| 54 | + let capsule = capsule.downcast::<PyCapsule>().map_err(py_datafusion_err)?; |
| 55 | + validate_pycapsule(capsule, "datafusion_table_function")?; |
| 56 | + |
| 57 | + let ffi_func = unsafe { capsule.reference::<FFI_TableFunction>() }; |
| 58 | + let foreign_func: ForeignTableFunction = ffi_func.to_owned().into(); |
| 59 | + |
| 60 | + Ok(Self { |
| 61 | + name: name.to_string(), |
| 62 | + inner: PyTableFunctionInner::FFIFunction(Arc::new(foreign_func)), |
| 63 | + }) |
| 64 | + } else { |
| 65 | + exec_err!("Python based Table Functions are not yet implemented") |
| 66 | + .map_err(py_datafusion_err) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + #[pyo3(signature = (*args))] |
| 71 | + pub fn __call__(&self, args: Vec<PyExpr>) -> PyResult<PyTableProvider> { |
| 72 | + let args: Vec<Expr> = args.iter().map(|e| e.expr.clone()).collect(); |
| 73 | + let table_provider = self.call(&args).map_err(py_datafusion_err)?; |
| 74 | + |
| 75 | + Ok(PyTableProvider::new(table_provider)) |
| 76 | + } |
| 77 | + |
| 78 | + fn __repr__(&self) -> PyResult<String> { |
| 79 | + Ok(format!("TableUDF({})", self.name)) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl TableFunctionImpl for PyTableFunction { |
| 84 | + fn call(&self, args: &[Expr]) -> datafusion::common::Result<Arc<dyn TableProvider>> { |
| 85 | + match &self.inner { |
| 86 | + PyTableFunctionInner::FFIFunction(func) => func.call(args), |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments