mirror of
https://github.com/lancedb/lancedb.git
synced 2026-08-18 12:08:35 +00:00
feat: expose function lookup in Python
This commit is contained in:
@@ -23,7 +23,7 @@ use lancedb::{
|
||||
connection::NamespaceClientPushdownOperation,
|
||||
database::namespace::LanceNamespaceDatabase,
|
||||
database::{CreateTableMode, Database, ReadConsistency},
|
||||
function::RegisterFunctionJobSpec,
|
||||
function::{FunctionId, RegisterFunctionJobSpec},
|
||||
};
|
||||
use pyo3::{
|
||||
Bound, FromPyObject, Py, PyAny, PyRef, PyResult, Python,
|
||||
@@ -609,6 +609,37 @@ impl Connection {
|
||||
Ok(crate::job::Job::new(job))
|
||||
})
|
||||
}
|
||||
|
||||
/// Look up the Function currently bound to a database-scoped name.
|
||||
///
|
||||
/// Wraps the exact Rust [`lancedb::function::Function`] once. Empty names
|
||||
/// fail as [`PyValueError`] before transport via the Rust connection.
|
||||
pub fn _lookup_function_by_name<'py>(
|
||||
self_: PyRef<'py, Self>,
|
||||
name: String,
|
||||
) -> PyResult<Bound<'py, PyAny>> {
|
||||
let inner = self_.get_inner()?.clone();
|
||||
future_into_py(self_.py(), async move {
|
||||
let function = inner.lookup_function_by_name(&name).await.infer_error()?;
|
||||
Ok(crate::function::Function::new(function))
|
||||
})
|
||||
}
|
||||
|
||||
/// Look up an immutable Function by exact opaque Function ID string.
|
||||
///
|
||||
/// Constructs [`FunctionId`] with [`FunctionId::try_new`] before dispatch so
|
||||
/// empty IDs fail as [`PyValueError`] before transport.
|
||||
pub fn _lookup_function_by_id<'py>(
|
||||
self_: PyRef<'py, Self>,
|
||||
function_id: String,
|
||||
) -> PyResult<Bound<'py, PyAny>> {
|
||||
let inner = self_.get_inner()?.clone();
|
||||
future_into_py(self_.py(), async move {
|
||||
let id = FunctionId::try_new(function_id).infer_error()?;
|
||||
let function = inner.lookup_function_by_id(&id).await.infer_error()?;
|
||||
Ok(crate::function::Function::new(function))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
|
||||
@@ -114,6 +114,16 @@ impl<T> PythonErrorExt<T> for std::result::Result<T, LanceError> {
|
||||
.getattr(intern!(py, "JobCancelledError"))?;
|
||||
Err(PyErr::from_value(cls.call1((err.to_string(),))?))
|
||||
}),
|
||||
LanceError::Function { code, message } => Python::attach(|py| {
|
||||
let cls = py
|
||||
.import(intern!(py, "lancedb.exceptions"))?
|
||||
.getattr(intern!(py, "FunctionError"))?;
|
||||
// Structural projection only: code.as_str() + sanitized message.
|
||||
// Never infer a code from HTTP status or diagnostic text.
|
||||
Err(PyErr::from_value(
|
||||
cls.call1((message.as_str(), code.as_str()))?,
|
||||
))
|
||||
}),
|
||||
_ => self.runtime_error(),
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user