Skip to content

perf(cypher): skip path values the query never reads - #213

Merged
prrao87 merged 1 commit into
perf/named-path-csr-walkfrom
perf/skip-unused-path-values
Nov 25, 2026
Merged

prrao87 merged 1 commit into
perf/named-path-csr-walkfrom
perf/skip-unused-path-values

Conversation

@prrao87

@prrao87 prrao87 commented Nov 25, 2026

Copy link
Copy Markdown
Member

Why

A named path built its Arrow path value for every path it found, even when the query never read the path variable. On the LDBC SF1 graph, MATCH p = (a:Person {ID: 1131})-[:knows*1..3]->(b:Person) RETURN count(*) finds 86,096 paths, and PathMatchExec emitted about 100 MB of node and relationship records only for count(*) to discard them. Both routes did this work. On the CSR walk from #211 it was the one shape left slower than main in lazy mode, because the walk read each returned relationship's columns from Lance by row.

Closes #212. Stacked on #211 (base perf/named-path-csr-walk); retarget to main once that merges.

What changed

  • The optimizer rule that already drops unused CsrExpand and NodeLookup columns now also drops the path column from PathMatch when no expression in the plan references it. The node then shows as PathMatchExec: endpoints and emits only boundary columns, on either route.
  • On the CSR walk, relationship records are read only when something needs them: the path value or a relationship variable on a fixed-length step.
  • Any reference keeps the value: p, length(p), nodes(p), relationships(p), WITH p AS q. The check is by column name across the whole plan, so it errs toward keeping the value.
  • Docs: the path-values section of the Cypher guide and the PathMatch internals page describe when the value is skipped.

Validation

  • cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings (and the Node crate by manifest), make test (1807 passed), make lint, zensical build --strict and typos pass locally, after rebasing onto perf(cypher): walk the csr for named trail paths #211's de2a035.
  • New tests on both routes, in Rust and Python: queries that never read p plan PathMatchExec: endpoints and return the same counts as count(p); queries that read p or its functions keep PathMatchExec: p.
  • EXPLAIN ANALYZE of the count query on SF1 (lazy): PathMatchExec output 100.9 MB → 12.2 MB, compute 69 ms → 15 ms, same 86,096 rows.

Release wheels per CONTRIBUTING.md: main (e3278fd), #211 (de2a035), this branch, and a control (this branch with the pruning hook disabled, to separate build layout from the change). Each wheel ran twice per mode in alternating order, with load under 6.

SF1 named-path shapes from person 1131, best of 7, ms:

shape mode main #211 branch
CSR *1..3 count(*) memory 68.2 70.2 27.3
CSR *1..3 count(*) lazy 71.3 85.6 28.6
CSR *1..3 RETURN b.ID lazy 70.4 86.0 27.8
CSR *1..2 then hasInterest count(*) lazy 136.0 139.1 44.1
inventory *1..3 count(*) memory 68.6 67.4 42.9
inventory *1..3 count(*) lazy 71.6 71.2 46.1
CSR *1..3 sum(length(p)) (reads p) lazy 71.4 86.2 88.3
CSR *1..2 RETURN p (reads p) lazy 24.6 6.4 6.6

Queries that read p are unchanged from #211 (within 3%). The lazy sum(length(p)) shape stays slower than main because it still needs every relationship record; that is #211's trade-off, not this change.

Both suites, geomean of mean times, branch relative to each wheel:

suite / mode vs main vs #211 vs control
graph-benchmark-ldbc / memory 1.005 1.003 1.005
graph-benchmark-ldbc / lazy 1.003 0.997 1.002
graph-benchmark-ldbc-complex / memory 0.996 0.996 0.995
graph-benchmark-ldbc-complex / lazy 1.003 0.994 1.010

No suite query uses a named path, and their plans match #211's (IC3 differs only in map print order). Every query is within run-to-run noise; the largest single move is IC12 lazy, whose two main runs themselves differ by 8%.

🤖 Generated with Claude Code

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.
@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: 5a45a30
Status: ✅  Deploy successful!
Preview URL: https://31df58a0.lance-csr.pages.dev
Branch Preview URL: https://perf-skip-unused-path-values.lance-csr.pages.dev

View logs

@prrao87
prrao87 marked this pull request as ready for review November 25, 2026 22:42
@prrao87
prrao87 merged commit 3872f76 into perf/named-path-csr-walk Nov 25, 2026
10 checks passed
@prrao87
prrao87 deleted the perf/skip-unused-path-values branch November 25, 2026 22:44
prrao87 added a commit that referenced this pull request Nov 25, 2026
* perf(cypher): walk the CSR for named trail paths

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

* fix(cypher): give a relationship one identity on every path route

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.

* perf(cypher): share a filter state across an undirected step (#214)

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

* perf(cypher): skip path values the query never reads (#213)

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.
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