mirror of
https://github.com/lancedb/lancedb.git
synced 2026-08-18 20:18:37 +00:00
feat: expose Python function job results
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
use arrow::pyarrow::ToPyArrow;
|
||||
use pyo3::{Bound, Py, PyAny, PyResult, Python, pyclass, pymethods, types::PyTuple};
|
||||
|
||||
/// Immutable first-class Function handle backed by the exact Rust value.
|
||||
#[pyclass(frozen, skip_from_py_object)]
|
||||
#[derive(Clone)]
|
||||
pub struct Function {
|
||||
inner: lancedb::function::Function,
|
||||
}
|
||||
|
||||
impl Function {
|
||||
pub(crate) fn new(inner: lancedb::function::Function) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
|
||||
/// Crate-private accessor for later call-authoring slices.
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn inner(&self) -> &lancedb::function::Function {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl Function {
|
||||
#[getter]
|
||||
fn id(&self) -> &str {
|
||||
self.inner.id().as_str()
|
||||
}
|
||||
|
||||
#[getter]
|
||||
fn parameters<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyTuple>> {
|
||||
let parameters = self.inner.signature().parameters();
|
||||
let mut pairs = Vec::with_capacity(parameters.len());
|
||||
for parameter in parameters {
|
||||
let data_type = parameter.data_type().to_pyarrow(py)?;
|
||||
pairs.push((parameter.name(), data_type));
|
||||
}
|
||||
PyTuple::new(py, pairs)
|
||||
}
|
||||
|
||||
#[getter]
|
||||
fn output_type(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
|
||||
self.inner
|
||||
.signature()
|
||||
.output()
|
||||
.data_type()
|
||||
.to_pyarrow(py)
|
||||
.map(|obj| obj.unbind())
|
||||
}
|
||||
|
||||
#[getter]
|
||||
fn output_nullable(&self) -> bool {
|
||||
self.inner.signature().output().nullable()
|
||||
}
|
||||
|
||||
fn __repr__(&self) -> String {
|
||||
format!("Function(id={:?})", self.inner.id().as_str())
|
||||
}
|
||||
}
|
||||
+22
-2
@@ -3,6 +3,7 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::function::Function;
|
||||
use crate::runtime::future_into_py;
|
||||
use pyo3::{Bound, PyAny, PyRef, PyResult, pyclass, pymethods};
|
||||
|
||||
@@ -21,6 +22,23 @@ impl Job {
|
||||
}
|
||||
}
|
||||
|
||||
/// Project a Rust [`lancedb::JobResult`] onto the Python success surface.
|
||||
///
|
||||
/// Delegates variant interpretation to [`lancedb::JobResult::into_function`]:
|
||||
/// no nested Function collapses to Python `None`; an exact Function becomes
|
||||
/// the corresponding [`Function`] handle.
|
||||
fn project_wait_result(result: lancedb::JobResult) -> Option<Function> {
|
||||
result.into_function().map(Function::new)
|
||||
}
|
||||
|
||||
/// Project a describe `result` onto Python `Optional[Function]`.
|
||||
///
|
||||
/// Rust `None`, `Some(JobResult::None)`, and JSON null all become Python
|
||||
/// `None`. Only `Some(JobResult::Function)` becomes a [`Function`] handle.
|
||||
fn project_description_result(result: Option<lancedb::JobResult>) -> Option<Function> {
|
||||
result.and_then(project_wait_result)
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl Job {
|
||||
#[getter]
|
||||
@@ -39,8 +57,8 @@ impl Job {
|
||||
pub fn wait(self_: PyRef<'_, Self>) -> PyResult<Bound<'_, PyAny>> {
|
||||
let inner = self_.inner.clone();
|
||||
future_into_py(self_.py(), async move {
|
||||
inner.wait().await.infer_error()?;
|
||||
Ok(())
|
||||
let result = inner.wait().await.infer_error()?;
|
||||
Ok(project_wait_result(result))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -115,6 +133,7 @@ pub struct JobDescription {
|
||||
creation_ms: i64,
|
||||
spec_json: Option<String>,
|
||||
failure: Option<JobFailureInfo>,
|
||||
result: Option<Function>,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
@@ -140,6 +159,7 @@ impl From<lancedb::database::JobDescription> for JobDescription {
|
||||
message: failure.message,
|
||||
retryable: failure.retryable,
|
||||
}),
|
||||
result: project_description_result(description.result),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ pub mod arrow;
|
||||
pub mod connection;
|
||||
pub mod error;
|
||||
pub mod expr;
|
||||
pub mod function;
|
||||
pub mod header;
|
||||
pub mod index;
|
||||
pub mod job;
|
||||
@@ -45,6 +46,7 @@ pub fn _lancedb(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||
m.add_class::<Connection>()?;
|
||||
m.add_class::<Session>()?;
|
||||
m.add_class::<Table>()?;
|
||||
m.add_class::<crate::function::Function>()?;
|
||||
m.add_class::<crate::job::Job>()?;
|
||||
m.add_class::<crate::job::JobInfo>()?;
|
||||
m.add_class::<crate::job::JobDescription>()?;
|
||||
|
||||
Reference in New Issue
Block a user