Files
lancedb/python/src/graph.rs
T
Jack Ye 5f4d448a34 fix: refuse graph schemas a definition cannot carry, and take any Table.add input on insert
A node or edge type whose schema would read back as another type from its JSON form (timestamp units and time zones, durations, times, dictionaries) is refused before a request carries it. Inserting into a property graph registers DataFrame converters first, as Table.add does, and the async graph handles are in the API reference.
2026-09-29 09:13:42 -05:00

50 lines
1.8 KiB
Rust

// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
//! Property graph schemas, between pyarrow and the JSON form a graph's
//! definition carries.
use arrow::{datatypes::Schema, pyarrow::PyArrowType};
use lance_namespace::models::JsonArrowSchema;
use pyo3::{PyResult, exceptions::PyValueError, pyfunction};
/// A kind of graph, spelled as its routes and catalog objects are.
#[derive(Debug, Clone, Copy)]
pub enum GraphKind {
Property,
Virtual,
MaterializedVirtual,
}
impl GraphKind {
pub fn parse(kind: &str) -> PyResult<Self> {
match kind {
"property_graph" => Ok(Self::Property),
"virtual_property_graph" => Ok(Self::Virtual),
"materialized_virtual_property_graph" => Ok(Self::MaterializedVirtual),
other => Err(PyValueError::new_err(format!(
"'{other}' is not a kind of property graph"
))),
}
}
}
/// A pyarrow schema as the JSON Arrow schema of a node or edge type, refused
/// when that form would not read back as the same schema.
#[pyfunction]
pub fn graph_schema_to_json(schema: PyArrowType<Schema>) -> PyResult<String> {
let json = lancedb::graph::schema_to_json(&schema.0)
.map_err(|error| PyValueError::new_err(error.to_string()))?;
serde_json::to_string(&json).map_err(|error| PyValueError::new_err(error.to_string()))
}
/// A node or edge type's JSON Arrow schema as a pyarrow schema.
#[pyfunction]
pub fn graph_schema_from_json(json: String) -> PyResult<PyArrowType<Schema>> {
let json: JsonArrowSchema =
serde_json::from_str(&json).map_err(|error| PyValueError::new_err(error.to_string()))?;
let schema = lance_namespace::schema::convert_json_arrow_schema(&json)
.map_err(|error| PyValueError::new_err(error.to_string()))?;
Ok(PyArrowType(schema))
}