Repository navigation
Description
Problem
The two-hop count rewrite counts MATCH (a)-[:R]->(b)-[:S]->(c) RETURN count(*) as Σ_b n1(b) × n2(b), the product of each middle node's neighbours on either side, without building any path. A longer pattern, such as (a)-[:R]->(b)-[:S]->(c)-[:T]->(d) RETURN count(*), keeps the join plan and builds every path before counting it.
The first-hop count from #113 collapses only the first hop of a longer pattern; the hops after it still emit one row per match. Variable-length hops (-[:R*2..3]->) are never counted from degrees.
Idea
Count each end hop per node in place and sum their products over the middle hop's edges: Σ_(b,c) in(b) × out(c) for three hops, which follows the edges rather than the paths. Longer fixed-length patterns chain the same per-node sums.
Two things limit it:
- Same-type hops. Cypher's rule that a path uses each edge once can exclude pairs of hops that aren't adjacent, such as
r1 = r3on a two-cycle, so the correction goes beyond the self-loop and shared-end cases the two-hop count handles. A first version could apply only when no two hops share a type. - Variable-length hops. The rule applies across the whole path, so degree products overcount any path that would revisit an edge. Short bounded lengths may be countable with corrections; otherwise they keep the current plan.
What to test on
Query 8 of graph-benchmark is the canonical example of this pattern. It is also the place to compare lance-csr with an engine that factorizes its intermediate results. Use LadybugDB for that comparison:
MATCH (a:Person)-[r1:Follows]->(b:Person)-[r2:Follows]->(c:Person)
RETURN count(*) AS numPathsThe benchmark generates a social network of 100,000 persons and 2,417,738 Follows edges, and has runners for Neo4j, Kuzu, LadybugDB, lance-csr, lance-graph and DuckPGQ over the same data. Its README reports q8 at 11.5 ms on Ladybug 0.21.2, which answers it without enumerating paths, against 14.5 ms on lance-csr 0.9.1, which already counts two hops from degrees, and 3.4 s on Neo4j, which builds every path (Apple M5, 24 GB).
q8 has two hops, so lance-csr already covers it. For this issue, add a hop at a time on the same data and runners:
MATCH (a:Person)-[:Follows]->(b:Person)-[:Follows]->(c:Person)-[:Follows]->(d:Person)
RETURN count(*) AS numPaths- What to report. The time on lance-csr and LadybugDB for two, three and four hops. A factorized engine's work follows the edges, so its time should grow slowly with each hop. lance-csr on
mainbuilds every path from the third hop on, and the dataset has 500 super nodes with hundreds to thousands of followers each, so the number of paths grows quickly. That gap is what this issue closes. - It is the same-type case. Every hop is
Follows, so this query needs the corrections described under "Same-type hops". A first version limited to hops of different types won't apply to it. To test that version on this dataset, use a chain over different types, such as(i:Interest)<-[:HasInterest]-(a:Person)-[:Follows]->(b:Person)-[:LivesIn]->(c:City), and compare it with LadybugDB too. - Check the counts before the times. On two hops, Cypher's rule that a path uses each edge once only excludes pairs that reuse a self-loop, and the generator removes self-loops from its base edges. On three hops it also excludes paths whose first and third edge are the same, which happens wherever two persons follow each other. An engine that doesn't apply the rule returns a larger count, so confirm that lance-csr and LadybugDB agree, or say which rule each applies.
Experiments to run next
These upcoming experiments are ordered so that the first two, which need no engine change, decide whether the others are worth doing.
-
Baseline against LadybugDB. Run q8 at two, three and four hops on lance-csr
mainand LadybugDB, and record counts and times. This gives the size of the gap, shows whether lance-csr finishes at four hops, and shows from the counts which edge-uniqueness rule LadybugDB applies. Every later experiment is measured against it. -
Write the pushdown by hand in Cypher. The idea can be tried without changing the planner:
MATCH (a)-[:R]->(b) WITH b, count(*) AS n MATCH (b)-[:S]->(c) WITH c, sum(n) AS w MATCH (c)-[:T]->(d) RETURN sum(w)
Time it against the plain three-hop query on
main. If it lands near LadybugDB, the planner rewrite only automates it. If the grouped sum over the middle hop is slow, the fix is an operator that carries per-node weights along the CSR in place, as the degree count does for the end hops, and that is a larger change. Whether this Cypher plans as intended on lance-csr is itself unchecked. -
Derive the same-type correction for three hops, then check it by brute force. This addresses the first limitation. With three hops of one type, the excluded paths are those that reuse a self-loop and those whose first and third edge coincide, which needs two nodes joined in both directions. That should be a closed form over degrees, self-loop counts and mutual-edge counts. Validate it against the join path on small random graphs with self-loops, parallel edges and mutual edges, then measure how large the correction is on the benchmark data. The expected limit is four hops: the first and fourth edge coinciding closes a triangle, and triangle counts can't be read from degrees.
-
Treat a bounded variable-length hop as a sum of fixed lengths. This addresses the second limitation.
count(*)over-[:R*1..3]->is the one-hop, two-hop and three-hop counts added together, each with its own correction, so experiment 3 answers it up to three hops. Longer bounds meet the same limit.
Separately, q8 itself takes 14.5 ms on lance-csr against 11.5 ms on LadybugDB, although neither enumerates paths. A profile would show where lance-csr's time goes. One candidate is the hash join of the two per-node counts: each direction's CSR numbers its nodes separately, so the counts can't simply be multiplied position by position. If the join dominates, q8 can get faster without any of the experiments above.
Split from #78.
Activity
[-]Count paths of three or more hops from node degrees[/-][+]Push count(*) below the joins for paths of three or more hops[/+]prrao87 commentedon Nov 14, 2026
Experiment results (2026-11-14)
Experiments 1 to 3 were run on
mainat1cdd1b5with the graph-benchmark data (100,000 persons, 2,417,738Followsedges), release builds, Apple M5, 24 GB. A planner rewrite was built to test the idea. It is not being merged; the reasons are at the end.Baseline (experiments 1 and 2)
count(*)overmainFollowshopsFollowshopsFollowshopsInterest <- Person -> Person -> Citymain, so a planner rewrite mostly automates it.Same-type correction for three hops (experiment 3)
For
(a)-->(b)-->(c)-->(d)of one type, the excluded walks are those witha = b = corb = c = d(two self-loops at one node) and those witha = candb = d(the first and third edge join the same nodes). Withs(x)the self-loops atx,inandoutits degrees, andm(x, y)the edges fromxtoy, they numberΣ s(x)² × (in(x) + out(x)) - 2 × s(x)³ + Σ m(x, y)² × m(y, x)This matched path enumeration on random graphs with self-loops, parallel edges and pairs joined both ways. On the benchmark data it is 594, the number of pairs who follow each other. Four hops of one type were not attempted, as expected: they need triangle counts.
The rewrite that was tried
It handled an unfiltered
count(*)over a directed chain of three or more hops, either all of different types, or three of one type pointing the same way. The end hops were counted per node in place and summed over the edges of the hops between them.count(*)overmainFollowshopsmainon every query, including a synthetic graph with skewed degrees, self-loops and parallel edges.Why it is not being merged
The findings suggest this is better served by the general mechanism in #134 than by another special case in the count planner. Experiment 4 (variable-length hops) and the q8 profile were not done.
prrao87 commentedon Nov 14, 2026
Closing this issue now until we see a wider variety of patterns and have a real use case to test this on. We should focus on improving the use cases that exist!