Skip to content

fix(query): directory-only aggregates judge leaflets by their rows, not routing keys - #2491

Open
bplatz wants to merge 3 commits into
mainfrom
fix/leaflet-seam-live-leads
Open

bplatz wants to merge 3 commits into
mainfrom
fix/leaflet-seam-live-leads

Conversation

@bplatz

@bplatz bplatz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Summary

The directory-only aggregates now judge a leaflet by its rows, not by its routing keys. This is the main half of the fix #2402 made on next.

A leaflet's routing keys are widened over the history it holds, so they bound its rows without naming them. Several fast paths treated them as the rows' own values:

  • Distinct counts came out one short at every leaflet boundary where a retraction landed in the next leaflet: COUNT(DISTINCT ?s), COUNT(DISTINCT ?o), per-predicate distinct counts, and the subject total of whole-graph aggregates.
  • MIN and MAX could return a retracted extreme.
  • The range semijoin could skip a leaflet whose routing key a retraction had widened into another type.

Fixes #2394
Fixes #2398
Fixes #2399

Changes

  • One shared walk for distinct groups (fluree-db-query/src/fast_lead_walk.rs, as on next). The whole-graph, per-predicate and live-arena-handle walks each had their own copy of the boundary dedup. They now share one walk with a single rule: add the counts, and subtract one where a leaflet's first row shares its group with the previous leaflet's last row.
    • A leaflet that holds no history has routing keys that are its rows' keys, since no writer on main drops history. Those boundaries settle by comparing keys, as before.
    • Where a leaflet holds history and its routing group meets its neighbour's, the walk reads the leaflet's first and last groups from its lead column. Groups that differ settle the boundary without a read.
    • Each leaflet is read at most once per walk, serial or parallel. Boundaries between parallel chunks are settled after the reduce, so the combine step does no I/O.
    • The vector and big-number tallies of the whole-graph object count are kept, as two tallies of the shared walk.
  • MIN/MAX. The numeric path reads the first row of the predicate's first leaflet, or the last row of its last. The string path takes each type-constant leaflet's first or last row the same way; it fires only on bulk-imported indexes, which hold no history, so it reads nothing more in practice.
  • Range semijoin prunes with post_o_key_bounds, as the batched join already does.
  • Smaller fixes, from fix(index): directory aggregates read leaflets' row groups, not routing keys #2402. The OPST walk decides non-identifying objects, and vector-only leaflets, from o_type_const. The arena classifier skips a leaflet whose rows all have one other type.

Behaviour changes

  • A leaflet with rows but no lead_group_count now sends the whole-graph walks to the general pipeline. They used to skip it, and undercount.
  • count_distinct_lead_groups returns Option, and its callers decline on None instead of counting zero.

Performance

A boundary costs a read only where a leaflet holds history and its routing group meets its neighbour's. The read decodes the leaflet's lead column through the leaflet cache, once per walk; a remote leaf is range-read. Leaflets without retractions hold no history, so a bulk-imported index, or a predicate never retracted from, stays directory-only.

Measured on a file-backed ledger built from commits: 400k subjects, about 2.4M facts, default leaflet size, one property retracted from 10% of subjects. That retraction puts history in every SPOT leaflet, the worst case for distinct subjects. Each query ran on a freshly opened instance (OS page cache warm), dev-fast profile, best of three:

Query Directory-only (same data, bulk-imported) This PR (built from commits) General pipeline
COUNT(DISTINCT ?s) 0.2 ms 5.3 ms (75 of about 95 leaflets read) 122 ms
COUNT(DISTINCT ?o) 0.2 ms 1.5 ms (15 read) 168 ms
COUNT(DISTINCT ?o) of one predicate 0.1 ms 0.1 ms (none read) 27 ms
MAX of one predicate 0.1 ms 0.1 ms (none read) 76 ms

On remote storage each of those reads is a range request; that cold cost is not measured here. A repeated query finds the leaflets in the leaflet cache.

Tests

  • it_leaflet_live_edges (new, its own binary, from fix(index): directory aggregates read leaflets' row groups, not routing keys #2402). Every case puts a retraction at the leaflet boundaries, then compares the fast path and the general pipeline on the same index against an unindexed ledger, with the lane's routing stamp pinned. Each runs after a full build and after an incremental build, walking leaves serially and in parallel chunks.
    • Distinct subjects and distinct objects, global and per predicate, with JSON-LD twins.
    • The per-predicate subject count behind SUM(STRLEN(GROUP_CONCAT)).
    • MIN, MAX and the range semijoin.
    • A subject that spans a widened boundary, a subject whose rows fill every leaf, and an object held by every subject of a predicate.
    • Indexes v4.2.6 wrote (tests/fixtures/leaflet-edges-4.2.6, from fix(index): directory aggregates read leaflets' row groups, not routing keys #2402 without its erasure ledger), full-built and incremental.
  • Non-vacuity. Against main's query code, six of the nine fail: distinct subjects, distinct objects, SUM(STRLEN(GROUP_CONCAT)), MIN/MAX, the range semijoin, and the v4.2.6 fixture. The three spanning cases pass there; with a walk that never subtracts at a boundary, they fail, as do the distinct-count cases and the fixture.
  • Unit tests in fast_lead_walk: a group read from a leaflet's columns equals the group of a history-free leaflet's routing key, in every order; a leaflet's edges are unproven only where it holds history; with a loader that counts reads, the walk reads each leaflet at most once under every way of cutting the leaflets into chunks, and subtracts only where the rows share a group.
  • Docs. docs/design/index-format.md and docs/design/performance.md describe routing keys as bounds.

Merging into next

next has this fix in its own form (#2402): stored live groups, with the lead-column read as its fallback for v4 indexes. When main is next merged into next, keep next's side of fast_lead_walk.rs, fast_count.rs, fast_min_max_string.rs, range_semijoin.rs, the LiveGroup additions in leaflet.rs, holds_history in leaf.rs, and the two docs. Keep next's it_leaflet_live_edges.rs, which has the v5 and erasure cases. The fixture directory differs only by the erasure ledger next keeps.

…ot routing keys

A leaflet's routing keys are widened over the history it holds, so they
bound its rows without naming them. Distinct-group counts subtracted at a
boundary the keys met but the rows did not, MIN/MAX could return a
retracted extreme, and the range semijoin compared a key widened into
another type with its bounds.

The distinct walks now share one walk (fast_lead_walk). A leaflet with no
history has routing keys that are its rows' keys; where a leaflet holds
history and its routing group meets its neighbour's, the walk reads its
first and last groups from the lead column, at most once per walk. MIN and
MAX read the first or last row of the predicate's end leaflet, and the
range semijoin prunes with post_o_key_bounds.
Each case puts a retraction at the leaflet boundaries and compares the fast
path and the general pipeline on the same index against an unindexed
ledger, after a full and an incremental build, walking serially and in
parallel chunks. Also runs against indexes v4.2.6 wrote.
@bplatz bplatz added bug Something isn't working as expected P1 Next up: wrong results, live user pain, unblocks area:query Query execution, planning, fast paths, overlay, result formatting labels Nov 23, 2026
@bplatz
bplatz requested a review from a team November 23, 2026 18:19

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:query Query execution, planning, fast paths, overlay, result formatting bug Something isn't working as expected P1 Next up: wrong results, live user pain, unblocks

Projects

None yet

1 participant