Skip to content

perf(cypher): walk the csr for named trail paths - #211

Merged
prrao87 merged 4 commits into
mainfrom
perf/named-path-csr-walk
Nov 25, 2026
Merged

prrao87 merged 4 commits into
mainfrom
perf/named-path-csr-walk

Conversation

@prrao87

@prrao87 prrao87 commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Why

A named path (MATCH p = ...) built an inventory of every edge of every relationship table it followed before it started walking. After #205, a path filtered by all(r IN relationships(p) WHERE r.day = 5) dropped failing edges during the walk, but it still paid for the whole table first. On the repro in #208 (20K nodes, 3M edges) that took 2.6 s in a debug build, about 5x slower than the same query written as explicit hops, and the time grew with the table's size rather than with the number of paths found. Profiling showed the walk itself took under 1 ms; building the inventory took the rest.

Closes #208. Closes #212.

Two follow-ups were reviewed and merged into this branch, so they land with it: #213 skips building path values the query never reads (#212), and #214 makes the two halves of an undirected step share one filter state (#210, already closed).

What changed

When the engine has a CSR for every relationship type and direction a named trail follows, the path now walks that CSR from its start node, as explicit hops do, instead of building the edge inventory.

  • Each step reads the neighbours of only the nodes the walk reaches. A node's candidate edges are built once per input batch, so trails that meet at the same node share them.
  • A step's relationship equalities (inline {day: 5} maps, or the all() equalities from perf(cypher): prune named paths by all() equalities and accept bare booleans #205) are checked only on the edges reached. The rule matches CsrExpandExec: fetch the filter columns of just those edges and, on a Lance table, switch to one read of the whole column once 1/256 of the table has been checked. In memory it never switches.
  • An edge's identity is its row in the relationship table. Relationship identities were already documented as query-local, so parallel edges stay distinct, including through collect() and UNWIND.
  • For a relationship type the engine has a CSR for, the edge inventory (still used by shortest paths) now reads the rows the CSR stores, with that same row identity, instead of scanning the table. A trail and a shortest path in one query therefore agree on which relationships are equal. Before this, on a Lance table with several fragments or deleted rows, relationships(p)[0] = relationships(q)[0] was false for the same edge when p walked the CSR and q was a shortest path. An independent review found this; the second commit fixes it, and both routes now build the identity through one helper.
  • Relationship records (properties, identity, start and end nodes) are built only for the edges of returned paths, once per distinct edge in an input batch.
  • An undirected step reads the outgoing and incoming CSR separately, so each edge keeps its stored direction.
  • The plan scans only the node tables. EXPLAIN shows PathMatchExec: p csr walk.
  • When no expression in the plan reads the path variable (count(*), RETURN b.id), the optimizer drops the path column and the node shows as PathMatchExec: endpoints, on either route. Any reference (p, length(p), nodes(p), relationships(p), WITH p AS q) keeps it. On the CSR walk, relationship records are then read only for a path value or a relationship variable on a fixed-length step (perf(cypher): skip path values the query never reads #213).
  • Both halves of an undirected step share one filter state, so on Lance they count their checks together and read the filter column into one mask instead of two (perf(cypher): share a filter state across an undirected step #214).
  • Shortest paths, and paths over a relationship type without a CSR (use_csr=False), keep the edge inventory. Both routes still read the participating node tables in full.

The design follows Kùzu, which evaluates relationship predicates inside its recursive joins over CSR adjacency lists, so the work follows the frontier rather than the size of the relationship table. The CSR guide and the operator internals now describe the walk and credit Kùzu, and the path-value notes no longer say path values always read whole relationship tables.

The Python and TypeScript APIs don't change.

Validation

Correctness:

  • cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings and cargo fmt --all --check pass.
  • In python/: make test (1807 passed, 2 skipped) and make lint pass.
  • New tests run 11 path shapes with use_csr=True and False and require identical results, in memory and on multi-fragment Lance tables with deletions: both directions and undirected, several types, mixed labels with colliding keys, self-loops, parallel edges, inline and all() filters, and a filter on a property one type lacks. Another test makes a walk check edges one by one and then switch to the mask on a 30,000-edge Lance table. A Rust test checks that both filter modes keep the same rows.
  • A regression test runs a trail path and a shortest path over the same edge in one query, in memory and on a multi-fragment Lance table with a deleted row, with use_csr on and off, and requires the relationships and paths to compare equal.
  • On SF1, the 86,096 path values of (:Person {ID: 1131})-[:knows*1..3]->() (11 output batches) match main row for row, apart from the query-local relationship ids, in both modes.
  • zensical build --strict, check_snippets.py and typos pass.

Performance, release wheels built from main and this branch, with the CONTRIBUTING.md harness, runs alternated, on an idle Apple silicon laptop (load average 2.5 to 5.6).

Query shapes the walk takes over (best of 5, ms; SF1 is the LDBC graph, "random" is 20K nodes with 3M R and 300K S edges):

Shape memory: main → branch lazy: main → branch
#208: *1..3 with all(r.day = 5), random 171 → 1.1 178 → 6.5
same, undirected 171 → 1.1 181 → 32
inline {day: 5} *1..3, returning p, random 170 → 0.9 181 → 7.2
R|S*1..2 with all(), random 184 → 1.0 195 → 4.4
unfiltered *1..2 count, random 187 → 9.8 198 → 26
unnamed undirected R|S*1..2, random 224 → 40 238 → 104
SF1 knows*1..2 returning p (5,430 paths) 24 → 4.8 25.5 → 6.5
SF1 single hop with a relationship variable 20.6 → 1.0 22 → 1.8
SF1 knows*1..2 then hasInterest 137 → 120 140 → 139
SF1 unfiltered knows*1..3 count (86K paths) 71 → 73 74 → 86

These timings predate #213 and #214. The last row was the one shape that got slower: count(*) built all 86K path values, and in lazy mode the walk read those relationships from Lance by row while main scans the 180K-edge table once. With #213 the path values are skipped, and the shape takes 27.3 ms in memory and 28.6 ms lazy (main: 68.2 and 71.3). A query that does read every path, such as sum(length(p)) over the same paths, still pays the by-row reads: 71.4 ms on main, 88.3 ms lazy here. The undirected lazy row read the filter column twice; #214 removes the second read (confirmed with a read counter in a debug build), but the row was not re-timed.

Both suites: no query changed plan (IC3's filter prints its two AND terms in a different order per process, on main too), and every result matched its expected answer. Geometric means of branch/main per-query ratios, two runs each:

Suite memory lazy
graph-benchmark-ldbc (30 queries) 1.001 0.997
graph-benchmark-ldbc-complex (IC1–IC14) 1.004 0.979

The identity fix changes how shortest-path inventories read relationship tables when a CSR exists. Rerun of the IC suite against main with that commit (two runs each, load 4.6 to 6.0): geometric mean 1.004 in memory and 1.005 lazy, with IC13 and IC14, the shortest-path queries, within ±3%.

An earlier version shared the mask-switch code between CsrExpandExec and the walk. A control wheel showed that it slowed IC12 by 10%, IC5 by 7% and custom query 30 by 6%, with identical plans, so CsrExpandExec keeps its inline code and a comment records why. The table above is for the final code. graph-benchmark (custom, non-LDBC, memory) was only measured on that earlier version: geometric mean 1.008, with no plan changes and no named paths in its queries.

#213 measured both suites again with its own control wheel: every geometric mean against main was between 0.996 and 1.005, and no suite query uses a named path. #214 was checked with tests only, without benchmarks, since it removes a read and adds no work.

Not done: release timings for the walk on graphs much larger than 3M edges, and a release re-timing of the undirected lazy shape after #214.

🤖 Generated with Claude Code

Named paths (`MATCH p = ...`) built an inventory of every edge of the
relationship table before walking, so a selective path over a large
table paid for the whole table. When the engine has a CSR for every
type and direction a trail follows, the path now walks that CSR from
its start node instead, as explicit hops do. Each step checks its
relationship equalities only on the edges it reaches, using the same
per-edge-then-mask rule as CsrExpandExec, and relationship records are
built only for returned paths, once per distinct edge in a batch.

The design follows Kùzu, which evaluates relationship predicates inside
recursive joins over CSR adjacency lists. Shortest paths and types
without a CSR keep the edge inventory.

Closes #208
@prrao87
prrao87 marked this pull request as ready for review November 25, 2026 17:55
The CSR walk named an edge by its row in the engine's edge store (the
row address on Lance), while the edge inventory, which shortest paths
still use, named it by its position in a scan. On a Lance table with
several fragments or deleted rows, a trail and a shortest path in one
query then disagreed: `relationships(p)[0] = relationships(q)[0]` was
false for the same edge.

For a relationship type the engine has a CSR for, the inventory now
reads the rows the CSR stores, with their row identity, instead of
scanning the table, so both routes name each edge the same way. Both
routes build the identity through one helper, and the CSR route reads
its rows from the same place.
@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: 3872f76
Status: ✅  Deploy successful!
Preview URL: https://323f4bab.lance-csr.pages.dev
Branch Preview URL: https://perf-named-path-csr-walk.lance-csr.pages.dev

View logs

Both halves of an undirected named-path step read the same relationship
table with the same filter, but each kept its own EdgeFilterState, so on
Lance each could read the whole filter column into its own mask. Hops of
one step over the same type now share one state.

Closes #210
A named path whose variable nothing references still built an Arrow path
value per path, and on the CSR walk read every relationship record for it.
The column-pruning optimizer rule now drops the path column from PathMatch
when no expression uses it, and the CSR walk skips relationship records
when no output needs them.
@prrao87
prrao87 merged commit bbec0b7 into main Nov 25, 2026
10 checks passed
@prrao87
prrao87 deleted the perf/named-path-csr-walk branch November 25, 2026 22:50
prrao87 added a commit to MuddyBootsCode/lance-csr that referenced this pull request Nov 26, 2026
Resolve conflicts with named-path CSR walks (hyperdimensionalcomputingai#211) and all()/none()
pruning (hyperdimensionalcomputingai#215):

- path_ops.rs: keep both the trail builders and main's csr_route.
- logical_plan.rs: pass no relationship condition at the two shared-trail
  calls of plan_inventory_path, since only named paths have one.
- operators.md: describe main's two PathMatch routes, then trails across
  segments of an unnamed path in their own subsection.
- test_path_csr_walk.py: an unnamed multi-type segment now takes iterative
  CsrExpand. Rows still match the edge inventory, and on LDBC SF1 it runs
  in 0.5 ms instead of 2.8 ms (knows|studyAt*1..2) and 6 ms instead of
  140 ms (knows|workAt*1..3).
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.

Named paths build path values that the query never reads Named paths filtered by all() are still 5x slower than explicit hops

1 participant