Repository navigation
Description
Problem
Five readers each implement "walk a leaf's leaflets at a historical t" on their own:
BinaryCursor::next_batch(fluree-db-binary-index/src/read/binary_cursor.rs)- four batched lanes in
fluree-db-query/src/join.rs:scan_matches,for_each_object_probe_match,batched_subject_probe_binary_uncharged,batched_subject_star_spot_uncharged
Each one opens the leaf, decides whether the leaflet's history sidecar applies, skips by directory metadata, loads columns, and replays. The rules for what the directory entry still means under replay live in every copy, and they have been added one at a time:
| Directory fact | Wrong under replay because | Guarded |
|---|---|---|
row_count == 0 |
a fully retracted leaflet keeps its facts in the sidecar | earlier fix, copied into each loop by hand |
first_key / last_key |
current keys can omit subjects that existed at an earlier t |
earlier fix, copied into each loop by hand |
o_type_const |
it is taken over live rows; retracted facts keep their own type | #2373, which found it missing from every copy, and in two planner-side proofs outside the loops (predicate_otype_uniform, predicate_unsafe_for_cross_predicate_o_key_join) |
HAS_O_I (flags) |
set from live rows; a list whose entries were all retracted later no longer marks its leaflet | not guarded. The list census caches (list_predicate_ids, leaf_list_subjects) and ListView::for_db trust it, so below the index a predicate whose lists were retracted later is not a list predicate: a CONSTRUCT that restates list entries at that t emits them as plain values. Store-level caches with no notion of t; a different layer from the loops |
p_const |
taken over live rows; an index built before v4.1.6 (#1624) filed a fully retracted predicate's history under the next predicate's leaflet, so a replay restores rows of a predicate p_const does not name |
only in psot_p_run (#2404). Not guarded: the cursor's skip_leaflet(entry.p_const, …) and the entry.p_const != Some(p_id) pre-filter in scan_matches, for_each_object_probe_match and batched_subject_probe_binary_uncharged. On such an index, a plain scan of the retracted predicate at a t before its retraction returns nothing (reproduced on a v4.1.5-written ledger in the #2404 review) |
#2373 fixed a wrong-answer bug of this kind (a bound-object query at an earlier t returned nothing, and the join lanes returned a retracted ref as a string). It moved two of the predicates onto LeafletDirEntryV3 (replays_history, o_type_const_for), but the loops are still separate. The next rule of this kind has to be found and applied in five places again.
Proposal
Put the per-leaflet decision in one place and have all five readers call it.
The four join lanes already share a loader, LeafScan::load_leaflet. The proposal is one function that takes a directory entry, the read's replay_to, and what the caller is looking for, and returns either "skip" or the loaded, replayed batch together with the constants that are still valid for that batch. The cursor and LeafScan::load_leaflet both go through it.
What stays where it is: each lane's row loop (subject binary search, probe windows, overlay merge). Only the decide/load/replay step moves, so the hot loops should be untouched.
Scope notes:
- The cursor also folds overlay state into its skip decisions (
has_ov). The shared function needs to take that as an input, or the cursor keeps that part. - The history-range scan (
fluree-db-query/src/binary_history.rs) has its own leaflet skip. It is not wrong today because its filter never binds an object type, so itso_type_constcheck is dead. It should either use the shared decision or lose the dead check. - Needs a perf A/B on warm time-travel and head runs for the join lanes before merging.
Simpler alternative
Make the raw o_type_const field unreachable and route every read through an accessor that states whether history is in play. About 45 sites across 15 files read the field directly today, mostly head-only fast paths.
This is mechanical and turns one convention into a compile error. It is weaker than the proposal in two ways: it leaves the five loops separate, and it covers only one of the directory facts in the table. The head-only fast paths are protected by their to_t == max_t gates, which this would not strengthen. If the proposal lands, this alternative adds little.
Done when
- One function decides skip/load/replay for a leaflet at
t, and the cursor and the four join lanes use it. retracted_type_shapes_*andfully_retracted_shapes_*init_query_time_travel_bgp.rsstill pass, with their lane stamps.- The decision covers every row of the table,
p_constincluded, with a test that reads a replayed leaflet whosep_constno longer describes its rows (an index built before v4.1.6, or a hand-built leaflet). - The perf A/B shows no regression on the batched join lanes.
Activity
bplatz commentedon Nov 18, 2026
Added
p_constto the table: it is taken over live rows and read raw under replay. The #2404 review found it on an index built before v4.1.6. #2404 guardspsot_p_runonly; the cursor and the batched lanes' pre-filters still trust it.