|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright The Lance Authors |
| 3 | + |
| 4 | +use std::collections::HashMap; |
| 5 | + |
| 6 | +use async_trait::async_trait; |
| 7 | +use lance_namespace::models::{DescribeTableRequest, DescribeTableResponse}; |
| 8 | +use lance_namespace::{Error as NamespaceError, LanceNamespace, Result as NamespaceResult}; |
| 9 | +use pyo3::prelude::*; |
| 10 | +use pyo3::prelude::PyAnyMethods; |
| 11 | +use pyo3::types::{PyAny, PyDict, PyList}; |
| 12 | +use snafu::Location; |
| 13 | + |
| 14 | +#[derive(Clone, Debug)] |
| 15 | +pub struct PyNamespaceAdapter { |
| 16 | + namespace: Py<PyAny>, |
| 17 | +} |
| 18 | + |
| 19 | +impl PyNamespaceAdapter { |
| 20 | + pub fn new(namespace: Py<PyAny>) -> Self { |
| 21 | + Self { namespace } |
| 22 | + } |
| 23 | + |
| 24 | + fn convert_response( |
| 25 | + py: Python, |
| 26 | + response: &Bound<'_, PyAny>, |
| 27 | + ) -> NamespaceResult<DescribeTableResponse> { |
| 28 | + let location = extract_field::<String>(py, response, "location")?; |
| 29 | + let storage_options = extract_field::<HashMap<String, String>>( |
| 30 | + py, |
| 31 | + response, |
| 32 | + "storage_options", |
| 33 | + )?; |
| 34 | + |
| 35 | + Ok(DescribeTableResponse { |
| 36 | + location, |
| 37 | + storage_options, |
| 38 | + ..DescribeTableResponse::new() |
| 39 | + }) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +fn extract_field<T>( |
| 44 | + py: Python, |
| 45 | + obj: &Bound<'_, PyAny>, |
| 46 | + key: &str, |
| 47 | +) -> NamespaceResult<Option<T>> |
| 48 | +where |
| 49 | + T: for<'a> FromPyObject<'a>, |
| 50 | +{ |
| 51 | + if let Ok(attr) = obj.getattr(key) { |
| 52 | + if attr.is_none() { |
| 53 | + return Ok(None); |
| 54 | + } |
| 55 | + return attr |
| 56 | + .extract::<T>() |
| 57 | + .map(Some) |
| 58 | + .map_err(py_err_to_namespace_error); |
| 59 | + } |
| 60 | + |
| 61 | + if obj |
| 62 | + .hasattr("get") |
| 63 | + .map_err(py_err_to_namespace_error)? |
| 64 | + { |
| 65 | + let value = obj |
| 66 | + .call_method1("get", (key, py.None())) |
| 67 | + .map_err(py_err_to_namespace_error)?; |
| 68 | + if value.is_none() { |
| 69 | + return Ok(None); |
| 70 | + } |
| 71 | + return value |
| 72 | + .extract::<T>() |
| 73 | + .map(Some) |
| 74 | + .map_err(py_err_to_namespace_error); |
| 75 | + } |
| 76 | + |
| 77 | + Ok(None) |
| 78 | +} |
| 79 | + |
| 80 | +unsafe impl Send for PyNamespaceAdapter {} |
| 81 | +unsafe impl Sync for PyNamespaceAdapter {} |
| 82 | + |
| 83 | +#[derive(Debug)] |
| 84 | +struct PyNamespaceError(String); |
| 85 | + |
| 86 | +impl std::fmt::Display for PyNamespaceError { |
| 87 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 88 | + write!(f, "{}", self.0) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl std::error::Error for PyNamespaceError {} |
| 93 | + |
| 94 | +fn py_err_to_namespace_error(err: PyErr) -> NamespaceError { |
| 95 | + NamespaceError::Namespace { |
| 96 | + source: Box::new(PyNamespaceError(err.to_string())), |
| 97 | + location: Location::new(file!(), line!(), column!()), |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#[async_trait] |
| 102 | +impl LanceNamespace for PyNamespaceAdapter { |
| 103 | + fn namespace_id(&self) -> String { |
| 104 | + Python::with_gil(|py| { |
| 105 | + let obj = self.namespace.bind(py); |
| 106 | + if let Ok(id_obj) = obj.call_method0("namespace_id") { |
| 107 | + if let Ok(id) = id_obj.extract::<String>() { |
| 108 | + return id; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + obj.repr() |
| 113 | + .map(|repr| repr.to_string()) |
| 114 | + .unwrap_or_else(|_| "PyNamespaceAdapter".to_string()) |
| 115 | + }) |
| 116 | + } |
| 117 | + |
| 118 | + async fn describe_table( |
| 119 | + &self, |
| 120 | + request: DescribeTableRequest, |
| 121 | + ) -> NamespaceResult<DescribeTableResponse> { |
| 122 | + let namespace = self.namespace.clone(); |
| 123 | + let id = request.id.clone(); |
| 124 | + let version = request.version; |
| 125 | + |
| 126 | + tokio::task::spawn_blocking(move || { |
| 127 | + Python::with_gil(|py| -> NamespaceResult<DescribeTableResponse> { |
| 128 | + let obj = namespace.bind(py); |
| 129 | + let kwargs = PyDict::new(py); |
| 130 | + |
| 131 | + if let Some(id_components) = id { |
| 132 | + let py_list = |
| 133 | + PyList::new(py, &id_components).map_err(py_err_to_namespace_error)?; |
| 134 | + kwargs |
| 135 | + .set_item("id", py_list) |
| 136 | + .map_err(py_err_to_namespace_error)?; |
| 137 | + } |
| 138 | + |
| 139 | + if let Some(version) = version { |
| 140 | + kwargs |
| 141 | + .set_item("version", version) |
| 142 | + .map_err(py_err_to_namespace_error)?; |
| 143 | + } |
| 144 | + |
| 145 | + let result = obj |
| 146 | + .call_method("describe_table", (), Some(&kwargs)) |
| 147 | + .map_err(py_err_to_namespace_error)?; |
| 148 | + |
| 149 | + Self::convert_response(py, &result) |
| 150 | + }) |
| 151 | + }) |
| 152 | + .await |
| 153 | + .map_err(|err| NamespaceError::Namespace { |
| 154 | + source: Box::new(PyNamespaceError(format!( |
| 155 | + "Python describe_table task failed: {}", |
| 156 | + err |
| 157 | + ))), |
| 158 | + location: Location::new(file!(), line!(), column!()), |
| 159 | + })? |
| 160 | + } |
| 161 | +} |
0 commit comments