Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions crates/lance-csr-node/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::sync::Arc;

use datafusion::physical_plan::SendableRecordBatchStream;
use futures::StreamExt;
use lance_csr::engine::{CypherEngine as RustCypherEngine, EngineOptions, MAX_TARGET_PARTITIONS};
use lance_csr::engine::{
CypherEngine as RustCypherEngine, EngineOptions, PersistCsr, MAX_TARGET_PARTITIONS,
};
use lance_csr::SqlEngine as RustSqlEngine;
use napi::bindgen_prelude::{BigInt64Array, BigUint64Array, Buffer};
use napi_derive::napi;
Expand Down Expand Up @@ -47,6 +49,7 @@ pub struct CsrStats {
pub version: Option<i64>,
pub base_version: Option<i64>,
pub build_ms: f64,
pub store_error: Option<String>,
}

#[napi(object)]
Expand Down Expand Up @@ -93,9 +96,16 @@ pub async fn open_cypher_engine(
datasets_in: HashMap<String, DatasetInput>,
use_csr: bool,
target_partitions: Option<f64>,
persist_csr: bool,
// "off", "load" or "store"; the TypeScript facade maps `persistCsr` to these.
persist_csr: String,
) -> napi::Result<CypherEngine> {
if persist_csr && !use_csr {
let persist_csr = match persist_csr.as_str() {
"off" => PersistCsr::Off,
"load" => PersistCsr::Load,
"store" => PersistCsr::LoadAndStore,
_ => return Err(invalid("persistCsr must be true, false or \"load\"")),
};
if persist_csr != PersistCsr::Off && !use_csr {
return Err(invalid("persistCsr requires useCsr: true"));
}
let target_partitions = target_partitions
Expand Down Expand Up @@ -242,6 +252,7 @@ impl CypherEngine {
version: entry.version.map(|v| v as i64),
base_version: entry.base_version.map(|v| v as i64),
build_ms: entry.build_ms,
store_error: entry.store_error,
};
(rel_type, stats)
})
Expand Down
32 changes: 28 additions & 4 deletions crates/lance-csr-python/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use arrow_schema::Schema;
use futures::StreamExt;
use lance_csr::{
ast::{DistanceMetric as RustDistanceMetric, GraphPattern, ReadingClause},
engine::{CypherEngine as RustCypherEngine, EngineOptions, EngineSource, MAX_TARGET_PARTITIONS},
engine::{
CypherEngine as RustCypherEngine, EngineOptions, EngineSource, PersistCsr,
MAX_TARGET_PARTITIONS,
},
CypherQuery as RustCypherQuery, ExecutionStrategy as RustExecutionStrategy,
GraphConfig as RustGraphConfig, GraphError as RustGraphError, SqlDialect as RustSqlDialect,
SqlEngine as RustSqlEngine, SqlQuery as RustSqlQuery, VectorSearch as RustVectorSearch,
Expand Down Expand Up @@ -1098,16 +1101,36 @@ pub struct CypherEngine {
#[pymethods]
impl CypherEngine {
#[new]
#[pyo3(signature = (config, datasets, use_csr = false, *, target_partitions = None, persist_csr = false))]
// `persist_csr` is a bool or "load"; None (left out) is False.
#[pyo3(
signature = (config, datasets, use_csr = false, *, target_partitions = None, persist_csr = None),
text_signature = "(config, datasets, use_csr=False, *, target_partitions=None, persist_csr=False)"
)]
fn new(
py: Python,
config: &GraphConfig,
datasets: &Bound<'_, PyDict>,
use_csr: bool,
target_partitions: Option<&Bound<'_, PyAny>>,
persist_csr: bool,
persist_csr: Option<&Bound<'_, PyAny>>,
) -> PyResult<Self> {
if persist_csr && !use_csr {
let persist_csr = match persist_csr {
None => PersistCsr::Off,
Some(value) if value.is_instance_of::<PyBool>() => {
if value.extract::<bool>()? {
PersistCsr::LoadAndStore
} else {
PersistCsr::Off
}
}
Some(value) if value.extract::<&str>().is_ok_and(|v| v == "load") => PersistCsr::Load,
Some(_) => {
return Err(PyValueError::new_err(
"persist_csr must be True, False or \"load\"",
))
}
};
if persist_csr != PersistCsr::Off && !use_csr {
return Err(PyValueError::new_err("persist_csr requires use_csr=True"));
}
let target_partitions = target_partitions
Expand Down Expand Up @@ -1248,6 +1271,7 @@ impl CypherEngine {
item.set_item("version", entry.version)?;
item.set_item("base_version", entry.base_version)?;
item.set_item("build_ms", entry.build_ms)?;
item.set_item("store_error", entry.store_error)?;
stats.set_item(rel_type, item)?;
}
Ok(stats.into_any().unbind())
Expand Down
12 changes: 10 additions & 2 deletions crates/lance-csr/src/csr_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,20 @@ pub(crate) async fn save(
.into_iter()
.map(|f| f.metadata().clone())
.collect();
let bytes = encode(version, src, dst, &fields, &fragments, outgoing, incoming)?;

let store = ds
.object_store(None)
.await
.map_err(|e| store_error(format!("Lance: {e}")))?;
// Encoding takes seconds on large graphs: find out first that the folder takes writes
// (read-only storage or credentials fail here). A probe left behind is never listed.
let probe = dir(ds).join(".write-probe");
store
.put(&probe, &[])
.await
.map_err(|e| store_error(format!("Failed to write {probe}: {e}")))?;
let _ = store.delete(&probe).await;
let bytes = encode(version, src, dst, &fields, &fragments, outgoing, incoming)?;

let path = file_path(ds, version);
// shortcut: the compressed file is held whole in memory before the upload (150 MiB for SF1); write it through a multipart upload if files get much larger.
let payload = object_store::PutPayload::from(bytes);
Expand Down
Loading