Repository navigation
fix: same-subject star joins drop novelty bindings on indexed ledgers - #1305
Conversation
Batched join/probe helpers (nested-loop join flush paths, property-join subject probe / SPOT star walk, batched OPTIONAL builders) read base index leaflets directly and never merge the novelty overlay, silently dropping novelty asserts and resurrecting novelty-retracted rows. Aggregates over such joins undercounted (e.g. SUM missing a novelty-only value). Add ExecutionContext::overlay_free_single_graph() (single active graph with overlay epoch 0) and gate every batched leaflet-probe entry point on it, falling back to the per-row scans whose binary cursor merges overlay ops: - join.rs use_batched (covers subject/object/exists flush helpers) - property_join.rs can_batched_probe + can_spot_walk_remaining - optional.rs both build_batch impls Regression test covers the faceted same-subject star (both pattern orders), FROM-clause dataset mode, the ref-hop join shape that triggered the nested-loop path, an OPTIONAL probe, and the typed star.
aaj3f
left a comment
There was a problem hiding this comment.
Good fix for this issue. If my comment below is correct w/ its assumptions, we may just have a scenario where your batching improvements COULD get applied but don't because of a mischaracterization of epoch > 0 empty novelty as being non-empty novelty
| /// True when the active scope resolves to exactly one graph and that | ||
| /// graph carries no novelty overlay (epoch 0). | ||
| /// | ||
| /// The batched join/probe helpers (`flush_batched_*_binary`, | ||
| /// `batched_subject_probe_binary`, `batched_subject_star_spot`) read base | ||
| /// leaflets directly and never merge overlay flakes, so they silently drop | ||
| /// novelty asserts and resurrect novelty-retracted rows. Every batched | ||
| /// leaflet-probe path must check this and fall back to the per-row scan | ||
| /// (whose binary cursor merges the overlay) when it returns `false`. | ||
| pub fn overlay_free_single_graph(&self) -> bool { | ||
| match self.active_graphs() { | ||
| ActiveGraphs::Single => self.overlay().epoch() == 0, | ||
| ActiveGraphs::Many(graphs) => match graphs.as_slice() { | ||
| [g] => g.overlay.epoch() == 0, | ||
| _ => false, | ||
| }, | ||
| } | ||
| } |
There was a problem hiding this comment.
I may be wrong here, but it seems epoch() == 0 is not necessarily sufficient, as it only describes the empty novelty state obtained at point of LedgerState load. I think, for example, when background indexing catches up (i.e. apply_index() --> clear_up_to()), you would end up with "empty novelty" but epoch > 0, and then the paths made possible by this overlay_free_single_graph() -> bool check would be lost even as novelty is empty.
Perhaps we could leverage Novelty::is_empty() (which should be available on overlay() if downcast to Novelty.
overlay_free_single_graph gated batched leaflet probes on epoch() == 0, which only covers a fresh load. After background indexing catches up (apply_index/apply_loaded_db -> clear_up_to), novelty is empty but the epoch is bumped, so fully-indexed ledgers permanently lost the fast path. Add OverlayProvider::is_effectively_empty() (conservative default false) and implement it on Novelty via size == 0 — not store.is_empty(), since clear_up_to retains dead flakes in the arena. NoOverlay and CompositeOverlay implement it as well. Also align apply_index with apply_loaded_db's existing size > 0 check for re-populating dict novelty after an index swap.
Summary
On an indexed ledger with pending novelty, joins that use the batched base-leaflet probes could silently drop novelty facts that a single-predicate scan sees. In the reported repro,
SUM(?v)over?s ex:price ?v . ?s ex:currency ?creturned 60 instead of 67 because the novelty assertionex:a ex:price 7was lost by the join. Any aggregate over such a join can undercount (SUM/AVG/COUNT, andMIN/MAXwhen the extremum lives in novelty), and retracted-in-novelty rows could incorrectly reappear.Root cause
Several operators share batched helpers that read base index leaflets directly for amortization (one leaflet load → many subject probes): the nested-loop join's batched flush paths, the property join's subject probe and SPOT star walk, and the batched OPTIONAL builders. These helpers handle historical time travel via sidecar replay, but never merge the novelty overlay — base leaflets were treated as authoritative even when novelty was present.
The live trigger is the nested-loop join: under overlay, scans materialize subjects eagerly as
Sidbindings, but the join's batched key extraction resolves those back to raw subject ids viafind_subject_id_by_partsand proceeds with the base-only probe. A ref-hop shape (?o ex:item ?s . ?s ex:price ?v) reproduces the exact symptom. The property-join and OPTIONAL probe paths carry the same latent hazard and are gated as well.Fix
Adds
ExecutionContext::overlay_free_single_graph()— true when the active scope resolves to exactly one graph whose overlay epoch is 0, the same convention used by late materialization and the fast paths. Every batched leaflet-probe entry point now checks it and falls back to the per-row scan path, whose binary cursor merges overlay ops correctly:join.rsuse_batched(covers the subject, object, and exists flush helpers)property_join.rscan_batched_probeandcan_spot_walk_remainingoptional.rsbothbuild_batchimplementationsThis is the bail strategy: overlay-carrying queries lose the batched amortization but stay correct. A follow-up could merge overlay ops inside the probes (mirroring
BinaryCursor::set_overlay_ops) to recover the batched path under novelty.Testing
New regression test
indexed_inline_type_star_aggregate_with_overlay_multivalue_and_facetinfluree-db-api/tests/it_query_sparql_indexed.rsseeds and indexes line items, adds a novelty-only price, and assertsSUM = 67across: the single-predicate control, the faceted same-subject star in both pattern orders, aFROM-clause dataset-mode variant, the ref-hop join shape (verified failing before the fix), an OPTIONAL probe variant, and the typed star. Existing query, time-travel BGP, dataset, aggregate, and OPTIONAL suites all pass.