Custom operators¶
Every graph operator in lance-csr is a DataFusion extension: a logical node that implements
UserDefinedLogicalNodeCore, lowered by CsrExtensionPlanner to its own ExecutionPlan. The exception is
HopDegreesNode, which the optimizer replaces (see two-hop counts). This page covers each one,
with the plans engine.explain(query) prints for it. Hops that the CSR cannot express fall back to DataFusion
hash joins (the use_csr=False plan below).
Each operator also reports metrics for explain(..., analyze=True). A shared wrapper (exec_metrics.rs) times the
operator's input stream and its output stream, and records output_rows and elapsed_compute: the time spent in the
operator's own polls minus the time spent polling its input, which is how DataFusion's operators count it. Work an
operator hands to another task isn't counted, and the child plans PathMatch runs itself count as its own time.
The plans below come from a four-person graph: a Person table (id, name, age) and a KNOWS table (src,
dst, since) with four edges, in memory, with use_csr=True. Plans of real graphs differ in their numbers and
partitioning, not in their operators.
CsrExpand: a hop as an array lookup¶
Without it, (a:Person)-[:KNOWS]->(b:Person) is a hash join of a against the whole KNOWS table and a second hash
join against the whole Person table to fetch b. With it, each input row looks up a's neighbours in the CSR and
fetches b's rows by position.
Here is the same two-hop query planned both ways:
ProjectionExec: expr=[a__name@0 as a.name]
FilterExec: b__id@1 != a__id@2 OR c__id@0 != b__id@1, projection=[a__name@3]
ProjectionExec: expr=[c__id@0 as c__id, b__id@3 as b__id, a__id@6 as a__id, a__name@7 as a__name]
CsrExpandExec: key=b__id, edges=4, take=a[a__id*, a__name]
CsrExpandExec: key=c__id, edges=4, take=b[b__id*]
RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1
ProjectionExec: expr=[id@0 as c__id]
FilterExec: name@1 = Dan, projection=[id@0]
KeyStatisticsExec: distinct=[id=4]
DataSourceExec: partitions=1, partition_sizes=[1]
ProjectionExec: expr=[a__name@0 as a.name]
HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(knows_2__src@2, a__id@0)], ...
HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(b__id@1, knows_2__dst@1)], ...
HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(knows_1__src@1, b__id@0)], ...
HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(c__id@0, knows_1__dst@1)], ...
ProjectionExec: expr=[id@0 as c__id]
FilterExec: name@1 = Dan, projection=[id@0]
KeyStatisticsExec: distinct=[id=4]
DataSourceExec: partitions=1, partition_sizes=[1]
ProjectionExec: expr=[src@0 as knows_1__src, dst@1 as knows_1__dst]
KeyStatisticsExec: distinct=[dst=3, src=3]
DataSourceExec: partitions=1, partition_sizes=[1]
ProjectionExec: expr=[id@0 as b__id]
KeyStatisticsExec: distinct=[id=4]
DataSourceExec: partitions=1, partition_sizes=[1]
ProjectionExec: expr=[src@0 as knows_2__src, dst@1 as knows_2__dst]
KeyStatisticsExec: distinct=[dst=3, src=3]
DataSourceExec: partitions=1, partition_sizes=[1]
ProjectionExec: expr=[id@0 as a__id, name@1 as a__name]
KeyStatisticsExec: distinct=[id=4]
DataSourceExec: partitions=1, partition_sizes=[1]
Four hash joins over five scans become two CsrExpandExecs over one filtered scan. The FilterExec above them
keeps the two hops from reusing one edge. Both plans start from c (Dan), not a: that is
re-rooting.
What one expansion can do¶
| Capability | In explain() |
What it replaces |
|---|---|---|
Neighbour lookup in one direction, or both for -[:R]- |
key=a__id, edges=N, undirected |
Join against the relationship table |
Fused target lookup: neighbour id to row position, then take of only the columns the query reads |
take=b[b__name] |
Join against the target node table |
| Relationship properties fetched by edge row, since each CSR entry records the row its edge came from | edge=k[k__since] |
Keeping the relationship table in the join |
| Target filter: reads the filter's columns first, and fetches the rest only for neighbours that pass | filter=age > 35 |
FilterExec after a full fetch |
| Relationship filter: drops failing edges before anything else is fetched | edge_filter=since > 2020 |
FilterExec after the join |
| Closing edge: the target is already bound (a cycle), so only neighbours equal to it are kept | match=b__id |
Expanding everything, then filtering |
| Iterative variable-length traversal: one cursor per hop over typed (label, key) frontiers, with bounded output batches | iterative hops=1..3 |
One join branch per path length, combined with UNION ALL |
Outer expansion for a one-hop OPTIONAL MATCH with no WHERE of its own: an input row with no neighbour keeps one row, with nulls in the columns the hop adds. No filter moves into an outer hop, because that would turn rows dropped by the filter into rows of nulls |
outer |
Planning the input a second time to start the optional side, then a left join back |
| Degrees: one row per input node with the number of its neighbours that pass the target filter, and how many of them are the node itself, for the two-hop count | degrees=[__n1, __s1] |
A hash aggregate over one row per edge |
A hop that uses several of these at once:
MATCH (a:Person {id: 1})-[k:KNOWS]->(b:Person) WHERE k.since > 2020 AND b.age > 35 RETURN b.name
ProjectionExec: expr=[b__name@3 as b.name]
CsrExpandExec: key=a__id, edges=4, take=b[b__name], edge=k[], filter=age@0 > 35, edge_filter=since@0 > 2020
RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1
ProjectionExec: expr=[id@0 as a__id]
FilterExec: id@0 = 1
KeyStatisticsExec: distinct=[id=4]
DataSourceExec: partitions=1, partition_sizes=[1]
Both WHERE conditions moved into the expansion, and the columns only they read (age, since) are gone from its
output. Optimizer rules explains how.
A variable-length path is one operator instead of a branch per length:
MATCH (a:Person {id: 1})-[:KNOWS*1..3]->(b:Person) RETURN b.name
ProjectionExec: expr=[b__name@3 as b.name]
CsrExpandExec: key=a__id, edges=4, take=b[b__name], iterative hops=1..3
...
Performance details¶
- Adaptive reads of lazy Lance tables. A batch of scattered rows from a large Lance table touches most of a column's pages. Once a relationship filter has seen more than 1/256 of the relationship's edges, the operator reads the filter's columns once, in order, builds a bitset per fragment, and checks edges against it from then on. Target-column fetches switch to one sequential read once the fetched target rows pass ⅛ of the node table and more than one batch. A named relationship's output columns switch once a hop fetches more than two chunks of them: one take of a chunk of scattered rows costs 0.3 to 0.6 of reading the column (measured on tables of 180K to 20M edges). Edges are counted where they are fetched, after the filters. When the operator has no filter, closing column or path walk, each input batch's keys give an upper bound on its edges (their CSR degrees), so the operator adds that bound to the count when the batch arrives and a large hop reads the columns before its first take. The read keeps one position per row, indexed by fragment and row offset like the filter's bitset, and covers only fixed-width columns up to 256 MiB in total; variable-width columns stay on takes. In-memory Arrow tables always gather directly.
- Bounded memory per partition. Each partition holds one input batch and at most one output chunk of neighbour positions, sized by DataFusion's batch size, before it fetches properties. It yields partway through a high-degree node's neighbour list. The exception is the sequential reads described above: once a hop switches to a sequential read, the loaded columns stay in memory for the rest of the query and are shared across partitions.
- Fast hashing on the hot path. Integer-keyed maps on the per-edge lookup path use
ahash, as Arrow and DataFusion do; with the standard library's SipHash, hashing dominated expansion time in profiles. - A predicate pushdown boundary. The node lists its own output columns in
prevent_predicate_push_down_columns, so DataFusion pushes filters on input columns below it and keeps filters on the columns it creates above it.
A hop is eligible when its endpoint columns are integers, it has exactly one relationship type (alternation is split into branches first) and it has no inline relationship property map. Which hops use it has the full list. Any other hop stays a join.
NodeLookup: group by id, then look up¶
WITH b, count(*) AS n groups by the node b, which relationally means grouping by every one of b's columns, and
RETURN b.id, b.name, count(*) groups by two of them. Rows before a grouping can outnumber nodes many times over,
because they come out of an expansion, so carrying b's properties through the aggregate for each of them is
expensive. When b's label has a node index (unique ids) and the grouping includes b's id, whole or as b.id, the
planner groups by the id alone and adds a NodeLookup above the aggregate that fetches b's other grouped properties
once per group:
MATCH (a:Person)-[:KNOWS]->(b:Person) WITH b, count(*) AS n RETURN b.name, n
ProjectionExec: expr=[b__name@2 as b.name, __agg_0@1 as n]
NodeLookupExec: key=b__id, take=b[b__name]
AggregateExec: mode=FinalPartitioned, gby=[b__id@0 as b__id], aggr=[count(1) as __agg_0]
RepartitionExec: partitioning=Hash([b__id@0], 10), input_partitions=10
AggregateExec: mode=Partial, gby=[b__id@0 as b__id], aggr=[count(1) as __agg_0]
RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1
ProjectionExec: expr=[b__id@3 as b__id]
CsrExpandExec: key=a__id, edges=4, take=b[b__id*]
...
The expansion below takes only b's id: the lookup's b__name is a column of its own, so
PruneCsrTargetColumns doesn't count it as a use of the expansion's. Grouping by
b.name without b.id keeps the plain grouping, since two nodes may share a name. Unlike a join back to the node
table, the lookup's cost follows the number of groups, not the size of the table. A lazily read Lance node table
gets the lookup only when a CsrExpand in the aggregate's input fetches the grouped columns and no other node in that
input reads them (a projection passing them through doesn't count; a Filter or Distinct does). Pruning then drops
them from the expansion, so the lookup replaces that per-edge fetch instead of reading the columns a second time; see
grouping by a node. From Lance, once the looked-up rows pass ⅛ of the table, the
lookup reads its columns in one sequential pass and gathers from memory, with the same threshold and shared state as
CsrExpand's target columns. A list comprehension over a list of nodes uses
it too, to bind each element to its node.
PathExists: pattern predicates walked in place¶
A pattern predicate such as WHERE NOT (b)-[:KNOWS]->(a) asks, for each row, whether a path exists. Relationally,
the planner answers it by planning everything before the predicate a second time, matching the pattern from that
copy, and left-joining a mark back onto the rows. That doubles the plan beneath every predicate, and DataFusion plans
and runs both copies. When the pattern qualifies, PathExistsExec instead walks the CSR from each row's start node,
stops at the first path it finds, and appends the same mark (true, or null for no path), so the filter or CASE
above it is unchanged:
ProjectionExec: expr=[a__name@0 as a.name, b__name@1 as b.name]
FilterExec: __exists_1@2 IS NULL, projection=[a__name@0, b__name@1]
ProjectionExec: expr=[a__name@1 as a__name, b__name@5 as b__name, __exists_1@6 as __exists_1]
PathExistsExec: start=b__id, hops=1, match=a__id
RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1
CsrExpandExec: key=a__id, edges=4, take=b[b__id*, b__name]
ProjectionExec: expr=[id@0 as a__id, name@1 as a__name]
KeyStatisticsExec: distinct=[id=4]
DataSourceExec: partitions=1, partition_sizes=[1]
ProjectionExec: expr=[a__name@0 as a.name, b__name@1 as b.name]
FilterExec: __exists_1@2 IS NULL, projection=[a__name@0, b__name@1]
HashJoinExec: mode=CollectLeft, join_type=Left, on=[(b__id@2, __optional__b__id@0), (a__id@0, __optional__a__id@1)], ...
HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(knows_1__dst@2, b__id@0)], ...
... (the MATCH, planned once for the rows)
ProjectionExec: expr=[b__id@0 as __optional__b__id, a__id@3 as __optional__a__id, __exists_1@6 as __exists_1]
AggregateExec: mode=FinalPartitioned, gby=[b__id@0 as b__id, ..., __exists_1@6 as __exists_1], aggr=[]
...
HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(b__id@0, knows_3__src@0), (a__id@3, knows_3__dst@1)], ...
...
HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(knows_2__dst@3, b__id@0)], ...
... (the MATCH again, as the seed of the predicate)
A predicate qualifies when it is one path that:
- starts at a node bound earlier in the query;
- passes through anonymous nodes, with no properties and only the label the relationship implies;
- uses anonymous relationships of one type each, all different, so no two hops can bind the same relationship, and none variable-length;
- ends at an anonymous node or at another bound node (
match=in the plan); - has no
WHEREof its own.
Every node reached by the walk must exist in its label's node index (matching the relational plan's join to the node table), so an edge pointing to a missing node matches nothing. A predicate that doesn't qualify, a hop or label without a CSR, or an endpoint that isn't an integer id keeps the relational plan, which the logical operator carries as its fallback. Both give the same rows. The walk keeps no set of visited nodes, so a long chain of high-degree hops ending at a bound node costs the product of their degrees per row.
ShortestPath: breadth-first search over the adjacency¶
shortestPath and allShortestPaths run as a breadth-first search over the CSR adjacency. Without a CSR, the node
gets a second input, a scan of the relationship's two endpoint columns, and builds an adjacency from it once per
query, shared by all partitions.
- A source with one target searches from both ends using the outgoing and incoming adjacency. It expands the smaller frontier and finishes each layer before joining paths, so it keeps every shortest alternative.
- Several targets for the same source share one forward search. So does a single target when there is no incoming adjacency to search from.
- The forward searches of an input batch run together, up to 64 sources at a time. Each source owns one bit of a
64-bit mask. A node reached by any of them keeps two masks (the searches that reached it earlier, and those
reaching it at the current level) and one
(predecessor, mask)record per edge that reached it. A node's neighbours are read once per level for all the searches whose frontier holds it, and the per-node state doesn't grow with the number of sources. A search stops expanding when its last target is reached; the rest continue. - After the last level, each source's predecessors are collected by walking back from the targets it reached, so only nodes on a shortest path to a requested target get per-source state.
- Predecessors retain every shortest path, including parallel edges.
The batched search follows multi-source breadth-first search, from
The More the Merrier: Efficient Multi-Source Graph Traversal (Then
et al., VLDB 2015), which DuckPGQ uses for shortest paths
between sets of bound endpoints. Both index dense arrays of bitsets by vertex number. lance-csr's CSR stores raw
neighbour keys, so it keeps the masks in a hash map, and it shares predecessor records across searches because it
returns every shortest path, not one.
- The operator appends <path>__length and <path>__nodes to each input row, once per path. Rows with no path are
dropped for MATCH, and kept with nulls through a left join for OPTIONAL MATCH.
MATCH (a:Person {id: 1}), (b:Person {id: 4}) MATCH p = shortestPath((a)-[:KNOWS*]-(b)) RETURN length(p)
ProjectionExec: expr=[p__length@6 as length(p)]
ShortestPathExec: a__id -> b__id, Undirected, hops 1..4294967295, one
CrossJoinExec
...
PathMatch: named paths with real edge identity¶
CSR neighbours don't carry edge identity or properties, but MATCH p = ... with relationships(p) needs both,
and parallel edges must stay distinct even through collect() and UNWIND. PathMatchExec chooses between two
execution routes at planning time:
- CSR walk (
PathMatchExec: p csr walk): used for trail patterns when a CSR exists for every relationship type and direction traversed. Only the participating node tables are scanned into a node inventory. The walk enumerates trails depth first from each start node (resuming between output batches), reading each node's neighbours and edge rows on demand. Undirected steps read outgoing and incoming CSRs separately so each edge keeps its stored direction. Step predicates run throughEdgeFilterState(the same adaptive Lance read rule used byCsrExpandExec, shared across both halves of an undirected step). Edge identity is the CSR row address, and full relationship records (properties, identity, start and end nodes) are materialized only for paths in each output batch. Following Kùzu's design, work scales with the traversed frontier rather than the full edge table. - Edge inventory (
PathMatchExec: p edge inventory): used for rich shortest paths or relationship types without a CSR. Builds one immutable, query-local edge inventory where each edge's identity is its occurrence (using CSR row addresses where a CSR exists so trails and shortest paths in the same query assign identical edge IDs, or scan row position otherwise). Reuses bidirectional BFS over a bipartite node and edge graph for shortest paths.
Additional planning and schema details:
- Property metadata and untyped branches: path values store user properties in a marked child struct so they cannot collide with identity metadata. When untyped matches combine branches, the planner merges those marked property fields by name before casting each branch to the common schema (including nested endpoint records; see path values).
- Unnamed undirected segments with several types: bounded undirected segments with multiple types use
iterative
CsrExpandExecwhen CSR and node indexes exist; otherwisePathMatchExecemits boundary columns without building Arrow path values. - Skipping unused path values (
PathMatchExec: endpoints): building Arrow path structs is expensive when a query only counts paths or returns endpoints (on LDBC SF1, 86K paths from[:knows*1..3]produced ~100 MB of unused structs).PruneCsrTargetColumnsdrops the path column whenever nothing referencesp, letting the CSR walk skip relationship records unless a relationship variable needs them. - Predicate pushdown and caching: path property values are logical expressions so cached plan templates bind
fresh parameters per execution. Conjuncts of
all(r IN relationships(p) WHERE ...)andnone(r IN relationships(p) WHERE ...)that read onlyr's properties, parameters and literals are combined withANDinto each step's relationship predicate to prune failing edges during the walk (while keeping the outerWHERE). A property missing from a table evaluates to null, and a condition that references none of the table's columns is evaluated once for all of its edges. Only the condition's parameters are logical expressions of the node, since DataFusion cannot type its columns against the node's inputs. Each execution starts with fresh table scans that can share source batches within that execution.
Trails across segments of an unnamed path¶
For an unnamed path such as (p)-[:KNOWS*1..2]-(f)-[:KNOWS]-(g), the final hop must exclude edge occurrences
already used by the first segment:
- With CSR and node indexes (
TrailWalkExec): the planner can start at the most selective endpoint (even if written last), carries(relationship_type, edge_row)through every segment, and emits boundary node IDs so properties can be fetched on demand. - Without indexes (join chain): the planner builds one branch per combination of hop lengths and compares each hop against earlier occurrences of its type using Lance row addresses or shared Arrow row ordinals (custom catalogs without occurrence IDs number parallel rows per endpoint pair ordered by tested properties).
- Eligibility: decided purely from the pattern and available indexes, with no data scan. Patterns with a relationship variable, unresolved boundary labels, or more than 64 join branches without a suitable CSR walk fall back to ordinary expansions (which track edge uniqueness within each variable-length segment only).
KeyStatistics: statistics on the scan¶
KeyStatisticsExec is a transparent wrapper over a table scan that reports distinct counts for join keys (see
join statistics). Filters, projections, limits and sort pushdown
pass through to the inner scan unchanged, as with DataFusion's own CooperativeExec. It shows in plans as
KeyStatisticsExec: distinct=[id=4], with , rows/N appended when it divides the row count for equality filters
pushed into a Lance scan (see join statistics).