Michael-J-Ward commented on code in PR #906:
URL: https://github.com/apache/datafusion-python/pull/906#discussion_r1797750380
##########
src/udf.rs:
##########
@@ -24,39 +24,51 @@ use datafusion::arrow::datatypes::DataType;
use datafusion::arrow::pyarrow::FromPyArrow;
use datafusion::arrow::pyarrow::{PyArrowType, ToPyArrow};
use datafusion::error::DataFusionError;
-use datafusion::logical_expr::create_udf;
use datafusion::logical_expr::function::ScalarFunctionImplementation;
use datafusion::logical_expr::ScalarUDF;
+use datafusion::logical_expr::{create_udf, ColumnarValue};
use crate::expr::PyExpr;
use crate::utils::parse_volatility;
+/// Create a Rust callable function fr a python function that expects pyarrow
arrays
+fn pyarrow_function_to_rust(
+ func: PyObject,
+) -> impl Fn(&[ArrayRef]) -> Result<ArrayRef, DataFusionError> {
+ move |args: &[ArrayRef]| -> Result<ArrayRef, DataFusionError> {
+ Python::with_gil(|py| {
+ // 1. cast args to Pyarrow arrays
+ let py_args = args
+ .iter()
+ .map(|arg| arg.into_data().to_pyarrow(py).unwrap())
+ .collect::<Vec<_>>();
+ let py_args = PyTuple::new_bound(py, py_args);
+
+ // 2. call function
+ let value = func
+ .call_bound(py, py_args, None)
+ .map_err(|e| DataFusionError::Execution(format!("{e:?}")))?;
+
+ // 3. cast to arrow::array::Array
+ let array_data =
ArrayData::from_pyarrow_bound(value.bind(py)).unwrap();
Review Comment:
I simply moved the code for clarity from the old `to_rust_function` for
clarity without editing it, but that's a good idea.
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]