Skip to content

perf(cypher): prune named paths by all() equalities and accept bare booleans - #205

Merged
prrao87 merged 2 commits into
mainfrom
perf/prune-paths-by-all-predicate
Nov 25, 2026
Merged

prrao87 merged 2 commits into
mainfrom
perf/prune-paths-by-all-predicate

Conversation

@prrao87

@prrao87 prrao87 commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Why

Fixes #179. A common investigation query expands a few hops with the same condition on every relationship:

MATCH p = (a:N {id: 0})-[:R*1..3]->(:N)
WHERE all(r IN relationships(p) WHERE r.day = 5)
RETURN count(*)

lance-csr built every path up to the maximum length and applied the all(...) filter afterwards, so the cost followed the unfiltered fan-out, not the few paths that pass. Users had to rewrite the pattern as explicit hops, one query per length.

While testing, a second gap showed up: a boolean value on its own was a syntax error in any condition, so WHERE n.active and all(r IN relationships(p) WHERE r.day = 5 AND r.ok) both failed to parse.

What changed

  • Pruning named paths. For a top-level AND term of the form all(r IN relationships(p) WHERE ...) on a named path, the planner copies each r.key = <literal or $param> into the property map of every relationship in p. The path operator already checks those maps edge by edge, so a failing edge now ends its branch during the walk. The WHERE itself still runs, so anything not copied is still checked after the walk; it just isn't faster.
  • What is left alone. any, single, OR, NOT, non-equality comparisons such as r.day >= 5, keys the inline map already sets, and shortestPath, where pruning edges would change which path is shortest.
  • Bare booleans. A value counts as a condition on its own (WHERE n.active, CASE WHEN n.active), next to AND or OR, or after NOT. It reads as value = true, so false and null both fail, as in openCypher. A value alone in a projection, such as RETURN n.name, still parses as a value.
  • Docs. The Cypher guide notes the all() behaviour next to the existing property-map note and adds bare booleans to the operator table; the internals page describes the rewrite.

Validation

  • cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings and cargo fmt pass.

  • make test (1800 passed, 2 skipped) and make lint pass.

  • zensical build --strict and typos pass.

  • New tests: planner tests for which equalities are copied and which forms are left alone; a parser test for each bare boolean position; Python tests comparing pruned results with filtering after the walk (with and without CSR, including a $param) and bare booleans with false and null rows.

  • Timing on the issue's repro (20,000 nodes, 3M random edges, one start node, use_csr=True), debug builds of this branch and of main, best of 3, same counts on both:

    Query main this branch
    Three explicit hops 0.48 s 0.49 s
    all() on *1..3 21.42 s 2.60 s
    all() with $day 21.40 s 2.59 s

    These are debug builds, not the release benchmark suites, and I didn't run the LANL graph. The remaining gap to explicit hops is likely the edge inventory build over all 3M edges; that isn't profiled here.

  • Not yet run: a check of the new queries against a second Cypher engine, and CI.

🤖 Generated with Claude Code

WHERE, WHEN and list comprehension filters rejected a boolean value
on its own, so `WHERE n.active` and `r.day = 5 AND r.ok` were syntax
errors. A bare value now counts as a condition on its own, next to
AND or OR, or after NOT, and reads as `value = true`, so false and
null both fail. A value alone in a projection stays a value.
`WHERE all(r IN relationships(p) WHERE r.day = 5)` on a named
variable-length path built every path up to the maximum length and
filtered afterwards, so its cost followed the unfiltered fan-out.
Copy the equalities on `r` into each relationship's property map,
which the path operator already checks edge by edge, so a failing
edge ends that branch. The WHERE still runs. Shortest paths are left
alone, since pruning would change which path is shortest.

Fixes #179
@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: 24b084b
Status: ✅  Deploy successful!
Preview URL: https://ecc0a9b8.lance-csr.pages.dev
Branch Preview URL: https://perf-prune-paths-by-all-pred.lance-csr.pages.dev

View logs

@prrao87
prrao87 marked this pull request as ready for review November 25, 2026 02:59
@prrao87
prrao87 merged commit 32f55dc into main Nov 25, 2026
10 checks passed
@prrao87
prrao87 deleted the perf/prune-paths-by-all-predicate branch November 25, 2026 03:04
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.

Apply all(r IN relationships(p) WHERE ...) during variable-length expansion

1 participant