uni-plugin-pyo3 2.0.2

PyO3 live-callable plugin loader for uni-db
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Scalar function adapter — turns a Python callable into an
//! [`uni_plugin::traits::scalar::ScalarPluginFn`].
//!
//! Two modes (proposal §5.4):
//!
//! - **Vectorized** (`vectorized: true`): one [`Python::attach`] per
//!   batch; the user fn receives one PyArrow Array per input column
//!   and returns one PyArrow Array. Marshalling is zero-copy via the
//!   Arrow PyCapsule Interface ([`crate::arrow_bridge`]).
//!
//! - **Row-by-row** (`vectorized: false`): one [`Python::attach`] per
//!   batch (we hold the GIL across all rows in the batch — design
//!   decision in `plans/magical-rolling-pinwheel.md` §design #6);
//!   inside the closure the host iterates rows and calls the Python
//!   fn once per row with native Python scalar args.
//!
//! Both modes serialize on the global GIL — see proposal §5.4.1 for
//! the operational discussion.

#![cfg(feature = "pyo3")]

use std::sync::Arc;

use arrow_array::{Array, ArrayRef};
use arrow_schema::DataType;
use datafusion::logical_expr::ColumnarValue;
use pyo3::prelude::*;
use pyo3::types::{PyAnyMethods, PyTuple};
use smol_str::SmolStr;

use uni_plugin::errors::FnError;
use uni_plugin::traits::scalar::{ArgType, FnSignature, NullHandling, ScalarPluginFn};

use crate::adapter_scalar_helpers::{PrimitiveColumnBuilder, classify_pyerr, scalar_to_py};
use crate::arrow_bridge::{arrow_array_to_pyarrow, assert_array_datatype, pyarrow_to_arrow_array};
use crate::runtime::PyPluginRuntime;

/// Scalar plugin adapter dispatching to a Python callable held in
/// [`PyPluginRuntime`].
#[derive(Debug)]
pub struct PyScalarFn {
    runtime: Arc<PyPluginRuntime>,
    local_name: SmolStr,
    signature: FnSignature,
    vectorized: bool,
}

impl PyScalarFn {
    /// Construct a row-mode scalar adapter.
    #[must_use]
    pub fn new(
        runtime: Arc<PyPluginRuntime>,
        local_name: impl Into<SmolStr>,
        signature: FnSignature,
    ) -> Self {
        Self {
            runtime,
            local_name: local_name.into(),
            signature,
            vectorized: false,
        }
    }

    /// Construct a vectorized scalar adapter — Python sees PyArrow
    /// Arrays per column and returns a PyArrow Array.
    #[must_use]
    pub fn new_vectorized(
        runtime: Arc<PyPluginRuntime>,
        local_name: impl Into<SmolStr>,
        signature: FnSignature,
    ) -> Self {
        Self {
            runtime,
            local_name: local_name.into(),
            signature,
            vectorized: true,
        }
    }

    fn return_datatype(&self) -> Result<DataType, FnError> {
        match &self.signature.returns {
            ArgType::Primitive(dt) => Ok(dt.clone()),
            other => Err(FnError::new(
                0x80,
                format!("PyO3 scalar adapter only supports primitive returns, got {other:?}"),
            )),
        }
    }

    fn arg_datatype(&self, i: usize) -> Result<DataType, FnError> {
        match self.signature.args.get(i) {
            Some(ArgType::Primitive(dt)) => Ok(dt.clone()),
            Some(other) => Err(FnError::new(
                0x80,
                format!("PyO3 scalar arg {i}: only primitives supported, got {other:?}"),
            )),
            None => Err(FnError::new(0x80, format!("missing arg type at index {i}"))),
        }
    }

    fn lookup_callable(&self) -> Result<Py<PyAny>, FnError> {
        self.runtime.get(self.local_name.as_str()).ok_or_else(|| {
            FnError::new(
                0x82,
                format!(
                    "python callable `{}` not in runtime `{}`",
                    self.local_name,
                    self.runtime.plugin_id.as_str()
                ),
            )
        })
    }
}

impl ScalarPluginFn for PyScalarFn {
    fn signature(&self) -> &FnSignature {
        &self.signature
    }

    fn invoke(&self, args: &[ColumnarValue], rows: usize) -> Result<ColumnarValue, FnError> {
        if self.vectorized {
            self.invoke_vectorized(args, rows)
        } else {
            self.invoke_row(args, rows)
        }
    }
}

impl PyScalarFn {
    fn invoke_vectorized(
        &self,
        args: &[ColumnarValue],
        rows: usize,
    ) -> Result<ColumnarValue, FnError> {
        let callable = self.lookup_callable()?;
        let ret_ty = self.return_datatype()?;
        let arr_args = materialize_args(args, rows)?;

        let local_name = self.local_name.clone();
        let result_arr = Python::attach(|py| -> Result<ArrayRef, FnError> {
            let mut py_args: Vec<Bound<'_, PyAny>> = Vec::with_capacity(arr_args.len());
            for arr in &arr_args {
                let py_arr = arrow_array_to_pyarrow(py, arr.as_ref())
                    .map_err(|e| FnError::new(0x83, e.to_string()))?;
                py_args.push(py_arr);
            }
            let bound = callable.bind(py);
            let tuple = PyTuple::new(py, py_args)
                .map_err(|e| classify_pyerr(0x820, "", local_name.as_str(), e))?;
            let result = bound
                .call1(tuple)
                .map_err(|e| classify_pyerr(0x820, "", local_name.as_str(), e))?;
            let array = pyarrow_to_arrow_array(py, &result)
                .map_err(|e| FnError::new(0x84, e.to_string()))?;
            assert_array_datatype(array.as_ref(), &ret_ty)
                .map_err(|e| FnError::new(0x85, e.to_string()))?;
            if array.len() != rows {
                return Err(FnError::new(
                    0x86,
                    format!(
                        "PyO3 vectorized `{}` returned {} rows, expected {}",
                        local_name,
                        array.len(),
                        rows
                    ),
                ));
            }
            Ok(array)
        })?;
        Ok(ColumnarValue::Array(result_arr))
    }

    fn invoke_row(&self, args: &[ColumnarValue], rows: usize) -> Result<ColumnarValue, FnError> {
        let callable = self.lookup_callable()?;
        let ret_ty = self.return_datatype()?;
        let arr_args = materialize_args(args, rows)?;
        let arg_dts: Vec<DataType> = (0..args.len())
            .map(|i| self.arg_datatype(i))
            .collect::<Result<_, FnError>>()?;
        let null_handling = self.signature.null_handling;
        let local_name = self.local_name.clone();

        Python::attach(|py| -> Result<ColumnarValue, FnError> {
            let bound = callable.bind(py);
            let mut out =
                PrimitiveColumnBuilder::new(&ret_ty, rows, 0x83, "PyO3 row-mode: return type")?;
            for row in 0..rows {
                // Build per-row Python args. Propagate NULLs by short-
                // circuiting to null output when any arg is null and
                // `PropagateNulls` is set.
                let mut py_args: Vec<Bound<'_, PyAny>> = Vec::with_capacity(args.len());
                let mut short_circuit = false;
                for (i, arr) in arr_args.iter().enumerate() {
                    if arr.is_null(row) {
                        match null_handling {
                            NullHandling::PropagateNulls => {
                                short_circuit = true;
                                break;
                            }
                            NullHandling::UserHandled => {
                                py_args.push(py.None().into_bound(py));
                            }
                        }
                    } else {
                        let v = scalar_to_py(py, arr.as_ref(), row, &arg_dts[i])?;
                        py_args.push(v);
                    }
                }
                if short_circuit {
                    out.push_null();
                    continue;
                }
                let tuple = PyTuple::new(py, py_args)
                    .map_err(|e| classify_pyerr(0x820, "", local_name.as_str(), e))?;
                let result = bound
                    .call1(tuple)
                    .map_err(|e| classify_pyerr(0x820, "", local_name.as_str(), e))?;
                out.push_py(&result, 0x820, "", &local_name)?;
            }
            Ok(ColumnarValue::Array(out.finish()))
        })
    }
}

/// Materialize each ColumnarValue as an `ArrayRef` of length `rows`.
fn materialize_args(args: &[ColumnarValue], rows: usize) -> Result<Vec<ArrayRef>, FnError> {
    args.iter()
        .map(|cv| match cv {
            ColumnarValue::Array(a) => Ok(a.clone()),
            ColumnarValue::Scalar(s) => s
                .to_array_of_size(rows)
                .map_err(|e| FnError::new(0x83, format!("scalar→array: {e}"))),
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use arrow_array::Float64Array;
    use datafusion::logical_expr::Volatility;
    use std::ffi::CString;

    use uni_plugin::PluginId;

    fn ensure_python() -> bool {
        Python::initialize();
        true
    }

    fn runtime_with_python_fn(name: &str, body: &str) -> Arc<PyPluginRuntime> {
        let rt = PyPluginRuntime::new(PluginId::new("ai.test.pyo3"));
        Python::attach(|py| {
            let code = CString::new(format!("def {name}{body}\n")).unwrap();
            let module = pyo3::types::PyModule::from_code(
                py,
                code.as_c_str(),
                std::ffi::CString::new("test_module.py").unwrap().as_c_str(),
                std::ffi::CString::new("test_module").unwrap().as_c_str(),
            )
            .expect("module compiles");
            let fn_obj = module.getattr(name).expect("function defined").unbind();
            rt.insert(name, fn_obj);
        });
        rt
    }

    fn float_sig(args: usize) -> FnSignature {
        FnSignature::new(
            (0..args)
                .map(|_| ArgType::Primitive(DataType::Float64))
                .collect(),
            ArgType::Primitive(DataType::Float64),
            Volatility::Immutable,
        )
    }

    #[test]
    fn scalar_vec_two_floats_add() {
        if !ensure_python() {
            return;
        }
        let rt = runtime_with_python_fn(
            "add",
            "(x, y):\n    import pyarrow.compute as pc\n    return pc.add(x, y)",
        );
        let adapter = PyScalarFn::new_vectorized(rt, "add", float_sig(2));
        let a: ArrayRef = Arc::new(Float64Array::from(vec![1.0_f64, 2.0, 3.0]));
        let b: ArrayRef = Arc::new(Float64Array::from(vec![10.0_f64, 20.0, 30.0]));
        let out = adapter
            .invoke(&[ColumnarValue::Array(a), ColumnarValue::Array(b)], 3)
            .expect("invoke");
        let arr = match out {
            ColumnarValue::Array(a) => a,
            _ => panic!("expected array"),
        };
        let f = arr.as_any().downcast_ref::<Float64Array>().unwrap();
        assert!((f.value(0) - 11.0).abs() < 1e-12);
        assert!((f.value(2) - 33.0).abs() < 1e-12);
    }

    #[test]
    fn scalar_row_two_floats_add() {
        if !ensure_python() {
            return;
        }
        let rt = runtime_with_python_fn("add_row", "(x, y):\n    return x + y");
        let adapter = PyScalarFn::new(rt, "add_row", float_sig(2));
        let a: ArrayRef = Arc::new(Float64Array::from(vec![1.0_f64, 2.0, 3.0]));
        let b: ArrayRef = Arc::new(Float64Array::from(vec![0.5_f64, 0.25, 0.125]));
        let out = adapter
            .invoke(&[ColumnarValue::Array(a), ColumnarValue::Array(b)], 3)
            .expect("invoke");
        let arr = match out {
            ColumnarValue::Array(a) => a,
            _ => panic!("expected array"),
        };
        let f = arr.as_any().downcast_ref::<Float64Array>().unwrap();
        assert!((f.value(0) - 1.5).abs() < 1e-12);
        assert!((f.value(1) - 2.25).abs() < 1e-12);
        assert!((f.value(2) - 3.125).abs() < 1e-12);
    }

    #[test]
    fn scalar_row_propagates_nulls() {
        if !ensure_python() {
            return;
        }
        let rt = runtime_with_python_fn("noop", "(x):\n    return x * 2.0");
        let adapter = PyScalarFn::new(
            rt,
            "noop",
            FnSignature::new(
                vec![ArgType::Primitive(DataType::Float64)],
                ArgType::Primitive(DataType::Float64),
                Volatility::Immutable,
            ),
        );
        let a: ArrayRef = Arc::new(Float64Array::from(vec![Some(1.0), None, Some(3.0)]));
        let out = adapter
            .invoke(&[ColumnarValue::Array(a)], 3)
            .expect("invoke");
        let arr = match out {
            ColumnarValue::Array(a) => a,
            _ => panic!("expected array"),
        };
        let f = arr.as_any().downcast_ref::<Float64Array>().unwrap();
        assert!(!f.is_null(0));
        assert!(f.is_null(1));
        assert!(!f.is_null(2));
        assert!((f.value(2) - 6.0).abs() < 1e-12);
    }

    #[test]
    fn scalar_python_exception_maps_to_fnerror() {
        if !ensure_python() {
            return;
        }
        let rt = runtime_with_python_fn("boom", "(x):\n    raise ValueError('nope: ' + str(x))");
        let adapter = PyScalarFn::new(
            rt,
            "boom",
            FnSignature::new(
                vec![ArgType::Primitive(DataType::Float64)],
                ArgType::Primitive(DataType::Float64),
                Volatility::Immutable,
            ),
        );
        let a: ArrayRef = Arc::new(Float64Array::from(vec![42.0_f64]));
        let err = adapter.invoke(&[ColumnarValue::Array(a)], 1).unwrap_err();
        let msg = err.message;
        assert!(msg.contains("ValueError"), "unexpected msg: {msg}");
        assert!(msg.contains("nope"));
    }

    #[test]
    fn scalar_vec_returns_wrong_length_errors() {
        if !ensure_python() {
            return;
        }
        // Python returns a single-element array — adapter must surface as FnError.
        let rt = runtime_with_python_fn(
            "shrink",
            "(x):\n    import pyarrow as pa\n    return pa.array([99.0])",
        );
        let adapter = PyScalarFn::new_vectorized(rt, "shrink", float_sig(1));
        let a: ArrayRef = Arc::new(Float64Array::from(vec![1.0_f64, 2.0, 3.0]));
        let err = adapter.invoke(&[ColumnarValue::Array(a)], 3).unwrap_err();
        assert!(err.message.contains("returned 1 rows, expected 3"));
    }

    #[test]
    fn missing_callable_errors() {
        if !ensure_python() {
            return;
        }
        let rt = PyPluginRuntime::new(PluginId::new("ai.test.empty"));
        let adapter = PyScalarFn::new(rt, "nope", float_sig(1));
        let a: ArrayRef = Arc::new(Float64Array::from(vec![1.0_f64]));
        let err = adapter.invoke(&[ColumnarValue::Array(a)], 1).unwrap_err();
        assert!(err.message.contains("`nope` not in runtime"));
    }
}