A fast graph query engine on Lance¶
lance-csr is an open source openCypher query engine for graphs stored as Lance datasets or Arrow tables. Written in Rust on Apache DataFusion, it ships as a Python package, a Rust crate and an npm package, and runs embedded on one machine with no server to operate. Its distinguishing feature is a compressed sparse row (CSR) adjacency index that turns each relationship hop from a hash join into an array lookup, making multi-hop traversals fast.
import pyarrow as pa
from lance_csr import CypherEngine, GraphConfig
config = (
GraphConfig.builder()
.node_table("Person", "id")
.rel_table("KNOWS", "Person", "Person", "src", "dst")
.build()
)
engine = CypherEngine(
config,
{
"Person": pa.table({"id": [1, 2, 3], "name": ["Alice", "Bob", "Carol"]}),
"KNOWS": pa.table({"src": [1, 2], "dst": [2, 3]}),
},
use_csr=True,
)
result = engine.execute(
"MATCH (a:Person {name: 'Alice'})-[:KNOWS*1..2]->(b) "
"RETURN b.name AS friend ORDER BY friend"
)
print(result.to_pylist()) # [{'friend': 'Bob'}, {'friend': 'Carol'}]
let config = GraphConfig::builder()
.node_table("Person", "id")
.rel_table("KNOWS", "Person", "Person", "src", "dst")
.build()?;
let person = RecordBatch::try_from_iter([
("id", Arc::new(Int64Array::from(vec![1, 2, 3])) as ArrayRef),
(
"name",
Arc::new(StringArray::from(vec!["Alice", "Bob", "Carol"])) as _,
),
])?;
let knows = RecordBatch::try_from_iter([
("src", Arc::new(Int64Array::from(vec![1, 2])) as ArrayRef),
("dst", Arc::new(Int64Array::from(vec![2, 3])) as _),
])?;
let sources = HashMap::from([
("Person".to_string(), EngineSource::Arrow(person)),
("KNOWS".to_string(), EngineSource::Arrow(knows)),
]);
let engine = CypherEngine::new(config, sources, true).await?;
let result = engine
.execute(
"MATCH (a:Person {name: 'Alice'})-[:KNOWS*1..2]->(b) \
RETURN b.name AS friend ORDER BY friend",
HashMap::new(),
)
.await?;
print_batches(&[result])?;
// +--------+
// | friend |
// +--------+
// | Bob |
// | Carol |
// +--------+
import { tableFromArrays } from "apache-arrow";
import { CypherEngine, GraphConfig } from "lance-csr";
const config = GraphConfig.builder()
.nodeTable("Person", "id")
.relTable("KNOWS", "Person", "Person", "src", "dst")
.build();
const engine = await CypherEngine.create(
config,
{
Person: tableFromArrays({
id: BigInt64Array.from([1n, 2n, 3n]),
name: ["Alice", "Bob", "Carol"],
}),
KNOWS: tableFromArrays({
src: BigInt64Array.from([1n, 2n]),
dst: BigInt64Array.from([2n, 3n]),
}),
},
{ useCsr: true },
);
const result = await engine.execute(
"MATCH (a:Person {name: 'Alice'})-[:KNOWS*1..2]->(b) RETURN b.name AS friend ORDER BY friend",
);
console.log(result.toArray().map((row) => row.toJSON()));
// [ { friend: 'Bob' }, { friend: 'Carol' } ]
-
Get started
Install with
uv, define a graph over a few tables, and run your first query in minutes. -
Model your graph
Map existing node and relationship tables onto a property graph with
GraphConfig. -
Traverse fast with the CSR
How
use_csr=Trueaccelerates multi-hop queries, stays current on writes, and persists to disk. -
Query Lance on disk
Lazy, version-pinned reads,
checkout_latest(), streaming batches, compaction and LanceDB tables. -
Hybrid graph and vector RAG
Combine Cypher traversals with vector similarity filtering and reranking over the same Lance tables.
-
Graph algorithms and SQL
Export zero-copy CSR arrays to icebug or SciPy, write scores back to Lance, or mix SQL with
GRAPH_TABLE.
Why lance-csr?¶
Fast and portable¶
- An embedded openCypher engine that runs inside your Python, Rust or Node.js process on a single machine. Install the package and query with no database server, cluster or JVM to operate.
One copy of your multimodal and graph data¶
- Lance stores text, images, audio, video and embeddings alongside structured columns. lance-csr runs Cypher
traversals, hybrid graph and vector queries, SQL
GRAPH_TABLEjoins and dense CSR exports directly over those datasets, without syncing a second copy into a standalone graph database.
Pushing DataFusion for graph traversal plans¶
- The project explores how far DataFusion's vectorized engine can go on multi-hop graph workloads. The CSR runs as custom DataFusion extension operators while DataFusion continues to plan and parallelize everything around them.
Broad openCypher coverage¶
- Supports enough read-only openCypher to run the GDC Interactive complex queries
(formerly LDBC SNB):
OPTIONAL MATCH,EXISTS,CALL { ... }, chainedWITH,UNWIND, list and pattern comprehensions,reduce, relationship alternation, variable-length trails, named paths,shortestPathand$parameters.
Understanding the query flow¶
flowchart LR
Q["Cypher text"] --> P["Parse, analyze,<br/>plan the graph"]
P --> X{"Hop has<br/>a CSR?"}
X -- yes --> C["CsrExpand<br/>array lookups"]
X -- no --> J["Hash join"]
C --> R["Arrow results"]
J --> R
With use_csr=True, every hop that the CSR can express becomes a neighbour lookup; anything it can't express
falls back to an ordinary join, so enabling the CSR never changes query results. Selection depends on what the
CSR supports rather than estimated cost: see when the CSR helps, or read
How it works for the full pipeline.
What it is not¶
The primary purpose of lance-csr is to serve as a fast, lightweight query engine over Lance tables on one machine. It is not meant for distributed execution.
Out of scope: distributed execution
lance-csr is deliberately single-machine. Graph queries across several machines are out of scope for this repository, and issues or pull requests that target distributed execution are closed. If you need distributed graph processing, HDC Labs builds that separately.
- Read-only query engine. Queries never mutate your tables (
persist_csr=Truewrites only CSR cache files beside relationship tables, andwrite_node_column()/optimize_datasets()are explicit maintenance calls). Create and update your tables withpylance, LanceDB or PyArrow. - Not a server. There is no network protocol, authentication or multi-tenant process. It is an embedded library.
- Not all of openCypher. See the Cypher reference for what is and isn't implemented.
Project status¶
lance-csr is a fork of lance-graph, actively developed in open source by HDC Labs ๐ as a research test bed for fast multi-hop graph queries over Lance using Cypher and Apache DataFusion.
If your use case requires distributed graph query processing or distributed graph algorithms, reach out to HDC Labs to learn more about our distributed engine.
lance-csr is in alpha. Prebuilt wheels cover Linux x86-64 and aarch64, Apple Silicon and Windows x86-64 for CPython 3.12 and newer; npm binaries cover the same platforms except Windows for Node.js 22 and newer.