Skip to content

feat(graph): export graphs as csr arrays and maintain lance datasets - #187

Merged
prrao87 merged 1 commit into
mainfrom
feat/graph-export
Nov 21, 2026
Merged

prrao87 merged 1 commit into
mainfrom
feat/graph-export

Conversation

@prrao87

@prrao87 prrao87 commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Why

Running a graph algorithm such as PageRank or connected components on lance-csr data meant pulling every edge into Python and handing it to a library like networkx. That's slow, and each user had to rebuild the same node numbering. Libraries such as icebug, igraph and scipy all take a graph as compressed sparse rows (CSR) over dense node ids, so lance-csr can build that form once, in Rust, from the same dataset versions its queries see.

Exporting the graph raises two more problems, and this PR solves both:

  • Getting results back into Lance. An algorithm returns one value per dense id, which then has to be matched to node keys, split by label and merged into each node table. write_node_column does that in one call and moves the engine onto the new versions, so the next Cypher query can sort or filter by the result.
  • Keeping datasets healthy after many writes. Each write adds a Lance version, and appends leave small fragments. Doing the maintenance by hand around a version-pinned engine is easy to get wrong: a cleanup can delete the version the engine reads, and a LanceDB handle opened before the engine's writes makes optimize() fail with a commit conflict. optimize_datasets compacts each dataset from its newest version, moves the engine onto the result, and only then deletes old versions, so neither failure can happen.

What changed

  • CypherEngine.export_graph(rel_types=None, *, directed=True) returns a GraphExport of pyarrow arrays: out_indptr/out_indices, in_indptr/in_indices for directed graphs, node_keys mapping dense ids back to keys, and label_ranges. It scans only the key and endpoint columns, gives each label its own id range, and deduplicates and sorts edges with a parallel packed-key sort. icebug's Graph.fromCSR takes the arrays without copying.
  • CypherEngine.write_node_column(export, column, values, *, overwrite=False) (Python only) splits the values by label, pairs them with the keys and runs Lance's merge into each node table, then pins the engine to the new versions. Existing columns need overwrite=True; key columns and in-memory Arrow tables are refused.
  • CypherEngine.optimize_datasets(*, cleanup_older_than=None) compacts every Lance dataset the engine reads, starting from the newest version, moves the engine onto the result, and only then deletes versions older than the window (7 days by default, as in LanceDB). It works the same for pylance datasets and LanceDB tables and returns per-dataset statistics. Lance indexes aren't updated.
  • Node gets exportGraph, documented for drawing the whole graph in a browser (dense ids and link pairs for GPU renderers), and optimizeDatasets. writeNodeColumn is deliberately left out of Node.
  • Docs: a new Graph algorithms page (CSR explained, icebug example, writing results back), a "Compaction and old versions" section on the Lance page, a "Drawing the whole graph" section in the TypeScript guide, a warning that LanceDB bundles its own, often newer, Lance, and a runnable icebug example.

Performance

End to end, from reading Lance to a results table, on a 100,000 node, 2.4 million edge graph (FOLLOWS from graph-benchmark), icebug through this PR against networkx 3.7:

Algorithm networkx lance-csr + icebug Faster Peak memory
PageRank 1.98 s 0.087 s 23× 1,370 → 221 MiB
Connected components 1.11 s 0.097 s 11× 917 → 238 MiB
Louvain 26.1 s 0.37 s 71× 1,872 → 245 MiB

On the lance-csr side, opening the engine takes about 0.035 s and export_graph about 0.05 s; the rest is icebug. PageRank matches networkx to within 4e-10 and components give the same partition. Louvain compares two different implementations (modularity 0.121 vs 0.122), so its communities aren't identical.

At 4 million nodes and 69 million edges (synthetic, hubs in the first rows), networkx wasn't run: at its ~590 bytes per edge above it would need about 40 GB on this 24 GB machine. Against the prototype that built the CSR with numpy:

Algorithm numpy prototype this PR Peak memory
PageRank 10.1 s 7.4 s (engine 3.4, export 2.1, algorithm 1.9) 7.0 → 2.0 GB
Connected components 11.0 s 6.3 s (engine 3.5, export 2.1, algorithm 0.7) 8.5 → 2.4 GB
Louvain 40.6 s 40.6 s (algorithm 34.9) 9.6 → 5.1 GB

The export alone drops from about 8 s to 2.1 s. Opening the engine is a one-time cost when the same engine serves queries.

Measured on an Apple M5 (10 cores, 24 GB, load about 1.2 for the 2.4M-edge runs), median of 3 runs after a warm-up, each algorithm and library in its own process, with a release build of this branch, icebug 13.6, pylance 14.0.3 and pyarrow 26.0.1. These measure the new export path, not Cypher queries.

icebug itself has packaging issues reported upstream (Ladybug-Memory/icebug#89, Ladybug-Memory/icebug#90). lance-csr doesn't depend on it.

Validation

  • cargo test --workspace, cargo fmt --check, workspace clippy and clippy on the Node crate: pass. New Rust tests cover hand-built CSRs, label collisions, Lance vs Arrow input, write_node_column and optimize_datasets with the CSR on.
  • Python make test (1738 passed, 1 skipped) and make lint: pass. The icebug test runs when icebug and networkx are installed (PageRank and components checked against networkx in the prototype).
  • Node npm test (86 passed), npm run lint, native.d.ts and the TypeDoc reference regenerated.
  • Docs: check_snippets.py, zensical build --strict and typos: pass. The example runs end to end with icebug.
  • Compaction behavior was checked against a real LanceDB 0.41 table before writing the docs.
  • Not run: the two benchmark suites from CONTRIBUTING.md. The change adds new methods and doesn't touch query planning or execution, and the PR makes no speed claim about queries. Object storage and LanceDB releases bundling a newer Lance major are untested.

🤖 Generated with Claude Code

Add CypherEngine.export_graph, which returns the graph as dense CSR
arrays for graph-algorithm libraries such as icebug, and
write_node_column (Python), which merges per-node results back into the
Lance node tables. Add optimize_datasets, which compacts the engine's
Lance datasets, moves the engine onto the result and then deletes old
versions. The Node package gets exportGraph, documented for drawing the
whole graph, and optimizeDatasets.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying lance-csr with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0779829
Status: ✅  Deploy successful!
Preview URL: https://566c9dd6.lance-csr.pages.dev
Branch Preview URL: https://feat-graph-export.lance-csr.pages.dev

View logs

@prrao87
prrao87 marked this pull request as ready for review November 21, 2026 19:14
@prrao87
prrao87 merged commit d7fdf48 into main Nov 21, 2026
10 checks passed
@prrao87
prrao87 deleted the feat/graph-export branch November 21, 2026 20:06
prrao87 added a commit that referenced this pull request Nov 21, 2026
Minor bump: #191 is a breaking change (marked feat(csr)!), and #187 adds
graph export and dataset maintenance APIs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant