Skip to content

feat(export): export the subgraph a cypher query selects - #203

Merged
prrao87 merged 2 commits into
mainfrom
feat/export-edges
Nov 25, 2026
Merged

prrao87 merged 2 commits into
mainfrom
feat/export-edges

Conversation

@prrao87

@prrao87 prrao87 commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Why

Closes #202. export_graph exports whole relationship types, but many graph-algorithm pipelines run on part of the graph: select edges with a query, run PageRank on just those, then filter by score. Until now the query result had to be turned into CSR by hand (key-to-id mapping, symmetrising, deduplication, weights), repeating the rules export_graph already implements, and more slowly.

What changed

CypherEngine.export_subgraph (exportSubgraph in TypeScript) runs a Cypher query that returns one row per edge, with the source and target node keys as its first two columns, and returns the same GraphExport as export_graph:

# The follows between people in one city, as an undirected graph
g = engine.export_subgraph(
    "MATCH (a:Person)-[:FOLLOWS]->(b:Person) WHERE a.city = $city AND b.city = $city "
    "RETURN a.id, b.id",
    {"city": "London"},
    directed=False,
)
  • Same edge rules as export_graph: repeated edges combined by combine_edges, undirected a -> b and b -> a merged, self-loops once, null endpoints skipped, null weights dropped and counted. The options are export_graph's without rel_types; weight names a column of the result, and default_weight on its own weights every row.
  • Labels when the query allows it. The nodes are the keys present in the result. When both key columns are a labeled node's key (a.id with (a:Person) and id its key), ids are grouped per label as in export_graph, so equal keys in two labels stay apart, label_ranges is filled, and write_node_column can write results back (nodes outside the subgraph get null). Otherwise (a computed column, an unlabeled node, a WITH or UNION in between) all keys share one id space, label_ranges is empty, and write_node_column refuses the export with an error.
  • One edge builder. export_graph and export_subgraph share the code that maps endpoints to dense ids, collects packed keys or (key, weight) pairs, sorts and transposes. The Node binding shares its result conversion too.
  • Docs: a "Subgraphs" section on the graph algorithms page with its own parameter table and checked examples, docstrings and TSDoc, the Python-to-TypeScript table, and regenerated TypeDoc.

Validation

  • cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --check: pass. New Rust tests cover filters, weights from a result column, row counting, the unlabeled fallback, colliding keys across two labels, and the error cases.

  • make test export tests (including write-back from a labeled subgraph to a Lance node table) and make lint: pass. npm test (90) and npm run lint: pass. docs/check_snippets.py, zensical build --strict, typos: pass.

  • Tested on the SNAP Twitter graph (ego-Twitter, download: https://snap.stanford.edu/data/twitter_combined.txt.gz, 81,306 nodes and 2,420,766 edge rows), in a filter → PageRank → filter pipeline with undirected PageRank weighted by row count. The benchmark script isn't part of this PR.

    The seed step selects every edge touching a node at or above a degree cutoff. Replacing "run the query, then build the CSR in numpy" with one export_subgraph call gave identical arrays, weights, PageRank scores and final node sets (asserted before timing), and these warm medians (5 runs after 2 warm-ups, variants alternated run by run, release build, Apple Silicon, load 5 to 6 with other work running):

    Seed cutoff (degree percentile) Subgraph edge rows Query + numpy CSR export_subgraph Whole pipeline before → after
    p90 (8,134 seeds) 1,879,975 0.147 s 0.074 s 0.186 → 0.104 s
    p95 (4,076 seeds) 1,548,606 0.123 s 0.064 s 0.154 → 0.089 s
    p99 (816 seeds) 874,774 0.074 s 0.040 s 0.091 → 0.053 s

    The final filter also got cheaper (0.010 → 0.002 s at p90), because it reads the undirected CSR's rows instead of rescanning the edge rows. PageRank time is unchanged. Peak memory isn't compared: both variants ran in one process.

🤖 Generated with Claude Code

`CypherEngine.export_subgraph(query, params, ...)` (`exportSubgraph` in
TypeScript) runs a Cypher query whose first two columns are the source
and target node keys, one row per edge, and builds the same dense CSR
arrays as `export_graph`, by the same edge rules: repeated edges
combined by `combine_edges`, undirected pairs merged, self-loops once,
null endpoints skipped and null weights dropped and counted. Options are
those of `export_graph` without `rel_types`; `weight` names a column of
the result, and `default_weight` alone weights every row, so 1.0 counts
the rows behind each edge.

The nodes are the keys present in the result. When both key columns are
a labeled node's key (`RETURN a.id, b.id` with `(a:Person)` and `id` its
key), ids are grouped per label as in `export_graph`, so equal keys in
two labels stay apart and `write_node_column` can write results back.
Otherwise all keys share one id space, `label_ranges` is empty, and
`write_node_column` refuses the export with an error.

Both exports now share one edge builder: mapping endpoints to dense
ids, the packed-key or (key, weight) vectors, the sort and the in-edge
transpose. The Node binding shares its conversion of the result too.

Closes #202
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Deploying lance-csr with  Cloudflare Pages  Cloudflare Pages

Latest commit: de5e103
Status: ✅  Deploy successful!
Preview URL: https://05342a6e.lance-csr.pages.dev
Branch Preview URL: https://feat-export-edges.lance-csr.pages.dev

View logs

@prrao87
prrao87 marked this pull request as ready for review November 25, 2026 01:29
The Node crate has its own manifest, outside the workspace, so
`cargo fmt --all` and workspace clippy don't cover it; CI's rust-lint
job checks it separately.
@prrao87
prrao87 merged commit 413daa0 into main Nov 25, 2026
10 checks passed
@prrao87
prrao87 deleted the feat/export-edges branch November 25, 2026 01:45
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.

feat(export): export an edge list or Cypher result as dense CSR

1 participant