Skip to content

perf(planner): start named and variable-length paths at their anchor - #225

Merged
prrao87 merged 1 commit into
mainfrom
perf/path-phrasing-equivalence
Nov 27, 2026
Merged

prrao87 merged 1 commit into
mainfrom
perf/path-phrasing-equivalence

Conversation

@prrao87

@prrao87 prrao87 commented Nov 27, 2026

Copy link
Copy Markdown
Member

Part of #223.

Why

The same anchored path query ran 20 to over 400 times slower than its fastest phrasing, depending on how it was written (release build, table below). The planner only chose a start for fixed-length, unnamed patterns, so:

  • a variable-length path anchored at its far end was expanded from every node at the near end;
  • a named path (p = ...) with its anchor in WHERE kept the filter above PathMatch, because DataFusion cannot push start-node columns through that node, so the walk started from every node;
  • a MATCH after WITH was planned without its stage's WHERE, so it could not use the anchor at all.

The goal is that equivalent phrasings get the same plan, starting at the anchor.

What changed

  • Paths are walked from their more selective end. A single named or variable-length path is read backwards when its written end is bound, or when that end's conditions (inline or in WHERE) score higher than the start's. This reuses the existing scoring and the end check already used for trails across segments.
  • Named paths keep their written order. A path walked backwards is marked reversed on PathMatch, which reverses each path's nodes and relationships before building p. nodes(p) and relationships(p) are unchanged.
  • A start-node WHERE filters the start scan. The planner filters a path's start scan with the WHERE conjuncts that read only the start node; the full WHERE still runs above. Conjuncts with pattern predicates or comprehensions are left out, and so are variables that label inference plans with different labels per branch, because a property can be missing from some of those labels.
  • A MATCH after WITH uses its stage's WHERE for these choices, as the first stage does. A WHERE after OPTIONAL MATCH still belongs to that clause.

A variable-length segment with a relationship variable ([r:T*1..3]) is still walked as written, because r is a list in that order. Choosing the start from table statistics, rather than the kind of condition, is a later item on #223.

Validation

  • cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings, make test (1,883 passed), ruff, the strict docs build and typos all pass locally. CI has not run yet.
  • New unit tests check that all eight phrasings below start at the anchor, that only start-node conjuncts reach the start scan, and that a MATCH after WITH picks its start from the stage's WHERE. A new Python test checks that a named path walked from its end returns the same rows, nodes and relationships, in written order, as the unanchored query filtered afterwards, for both the CSR walk and the edge inventory.

The rerouted shapes. 2,000 nodes and 40,000 edges, MATCH ... (c)-[:AUTH*1..2]->(d) ... RETURN count(*), release wheels, in memory, best of five. Answers match on both wheels.

Phrasing main Branch
Unnamed, anchor at start, inline or in WHERE 0.43 to 0.48 ms 0.38 to 0.45 ms
Unnamed, anchor at end, inline 9.1 ms 0.38 ms
Unnamed, anchor at end, in WHERE 11.2 ms 0.37 ms
Named, anchor at start, inline 0.54 ms 0.59 ms
Named, anchor at start, in WHERE 197 ms 0.61 ms
Named, anchor at end, inline 11.8 ms 0.55 ms
Named, anchor at end, in WHERE 196 ms 0.56 ms
Named, anchor at end in WHERE, count(nodes(p)) 265 ms 0.76 ms
After WITH, unnamed, anchor at end in WHERE 11.8 ms 0.53 ms
After WITH, named, anchor at end in WHERE 207 ms 0.72 ms

Benchmark suites. Every query in all three suites gets the same DataFusion logical and physical plan on both wheels; the only difference in the printed plans is the new reversed: false field in IC14's graph plan. Timings follow the CONTRIBUTING.md harness, alternating main and branch twice each, with no runner changes. Every answer check passed, and no query moved by more than 15%.

Suite Mode Geomean branch / main main run 2 / run 1
graph-benchmark-ldbc (30 queries) memory 0.980 0.945
graph-benchmark-ldbc lazy 0.996 1.002
graph-benchmark-ldbc-complex (IC1 to IC14) memory 1.005 1.003
graph-benchmark-ldbc-complex lazy 0.997 0.990
graph-benchmark (9 queries) memory 1.003 0.990

The first graph-benchmark-ldbc memory run on main started at a load average of 6, which explains its 0.945 gap. The other runs ran at loads of 3 to 5.

Setup: lance-csr main at 42bdb59, branch at 8345967; graph-benchmark-ldbc cbd6d62, graph-benchmark-ldbc-complex a3eb756, graph-benchmark 45e4dd1. Apple M5 MacBook Pro, 24 GB, macOS 26.6.2.

Every query, mean of two runs each

graph-benchmark, memory

Query main (ms) branch (ms) Change
query1 7.61 7.48 -2%
query2 12.25 12.26 +0%
query3 2.72 2.77 +2%
query4 1.62 1.61 -0%
query5 1.61 1.64 +2%
query6 1.89 1.93 +2%
query7 3.70 3.65 -1%
query8 13.12 13.18 +0%
query9 12.46 12.45 -0%

Geomean branch/main: 1.003; main run 2 / run 1: 0.990 (largest single-query main gap 4%)

graph-benchmark-ldbc, memory

Query main (ms) branch (ms) Change
query1 0.72 0.68 -5%
query2 0.59 0.57 -3%
query3 0.69 0.68 -1%
query4 0.60 0.55 -8%
query5 0.88 0.82 -7%
query6 0.50 0.47 -7%
query7 1.15 1.18 +2%
query8 0.80 0.69 -13%
query9 0.90 0.90 +0%
query10 0.77 0.80 +4%
query11 1.63 1.58 -3%
query12 0.80 0.79 -1%
query13 0.83 0.84 +1%
query14 0.57 0.56 -1%
query15 0.59 0.59 +0%
query16 0.71 0.71 +1%
query17 0.94 0.96 +2%
query18 0.73 0.74 +1%
query19 0.98 0.99 +1%
query20 2.14 2.08 -3%
query21 0.79 0.80 +2%
query22 0.78 0.75 -4%
query23 0.81 0.77 -5%
query24 0.73 0.73 +1%
query25 0.85 0.84 -1%
query26 0.78 0.79 +1%
query27 0.90 0.91 +0%
query28 1.43 1.41 -1%
query29 0.75 0.73 -2%
query30 20.40 18.92 -7%

Geomean branch/main: 0.980; main run 2 / run 1: 0.945 (largest single-query main gap 38%)

graph-benchmark-ldbc, lazy

Query main (ms) branch (ms) Change
query1 1.16 1.17 +0%
query2 1.42 1.41 -1%
query3 0.82 0.82 +0%
query4 0.81 0.81 -0%
query5 2.19 2.18 -1%
query6 0.76 0.76 -1%
query7 1.14 1.13 -1%
query8 1.75 1.73 -1%
query9 1.38 1.37 -1%
query10 0.96 0.96 -0%
query11 2.01 2.04 +1%
query12 6.76 6.58 -3%
query13 1.08 1.10 +2%
query14 0.71 0.72 +1%
query15 0.79 0.79 +1%
query16 1.14 1.13 -1%
query17 1.42 1.42 -0%
query18 1.18 1.16 -2%
query19 1.37 1.37 -0%
query20 16.79 16.84 +0%
query21 0.86 0.85 -1%
query22 0.96 0.96 -0%
query23 1.27 1.27 -0%
query24 1.10 1.09 -1%
query25 1.23 1.22 -0%
query26 1.10 1.10 +0%
query27 1.35 1.33 -1%
query28 2.09 2.05 -2%
query29 1.06 1.06 -0%
query30 19.54 19.54 +0%

Geomean branch/main: 0.996; main run 2 / run 1: 1.002 (largest single-query main gap 3%)

graph-benchmark-ldbc-complex, memory

Query main (ms) branch (ms) Change
query1 24.92 24.57 -1%
query2 2.54 2.60 +2%
query3 25.56 26.13 +2%
query4 5.87 5.89 +0%
query5 70.51 72.59 +3%
query6 20.85 20.96 +1%
query7 9.92 9.97 +0%
query8 2.38 2.39 +0%
query9 36.84 36.55 -1%
query10 20.84 20.83 -0%
query11 3.03 3.03 +0%
query12 16.21 16.29 +0%
query13 1.16 1.14 -1%
query14 247.89 248.73 +0%

Geomean branch/main: 1.005; main run 2 / run 1: 1.003 (largest single-query main gap 1%)

graph-benchmark-ldbc-complex, lazy

Query main (ms) branch (ms) Change
query1 35.01 34.59 -1%
query2 25.04 25.47 +2%
query3 168.48 166.14 -1%
query4 13.10 12.91 -1%
query5 94.76 96.51 +2%
query6 22.48 22.55 +0%
query7 13.90 13.80 -1%
query8 3.42 3.39 -1%
query9 224.19 219.06 -2%
query10 29.25 29.37 +0%
query11 5.44 5.45 +0%
query12 19.13 19.11 -0%
query13 1.44 1.44 +0%
query14 259.73 257.10 -1%

Geomean branch/main: 0.997; main run 2 / run 1: 0.990 (largest single-query main gap 7%)

🤖 Generated with Claude Code

The same anchored path ran 33x to 480x slower depending on phrasing:
whether the anchor was written inline or in WHERE, at the start or the
end, and whether the path was named.

- A single named or variable-length path is walked from its written
  end when that end is bound or its conditions score higher. PathMatch
  carries a `reversed` flag and reverses each path's nodes and
  relationships before building `p`, so path values keep written order.
- PathMatch blocks DataFusion's pushdown of start-node columns, so the
  planner filters the start scan with the WHERE conjuncts that read only
  the start node. Variables that label branches give different labels
  are skipped, since a property may be missing from some of them.
- MATCH clauses after WITH get their stage's WHERE for these choices,
  as the first stage does.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying lance-csr with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8345967
Status: ✅  Deploy successful!
Preview URL: https://027a0f43.lance-csr.pages.dev
Branch Preview URL: https://perf-path-phrasing-equivalen.lance-csr.pages.dev

View logs

@prrao87
prrao87 marked this pull request as ready for review November 27, 2026 23:15
@prrao87
prrao87 merged commit cd386ef into main Nov 27, 2026
10 checks passed
@prrao87
prrao87 deleted the perf/path-phrasing-equivalence branch November 27, 2026 23:16
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