Skip to content

fix: same-subject star joins drop novelty bindings on indexed ledgers - #1305

Merged
bplatz merged 7 commits into
mainfrom
fix/same-subject-star
Jun 10, 2026
Merged

bplatz merged 7 commits into
mainfrom
fix/same-subject-star

Conversation

@bplatz

@bplatz bplatz commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

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 ?c returned 60 instead of 67 because the novelty assertion ex:a ex:price 7 was lost by the join. Any aggregate over such a join can undercount (SUM/AVG/COUNT, and MIN/MAX when 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 Sid bindings, but the join's batched key extraction resolves those back to raw subject ids via find_subject_id_by_parts and 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.rs use_batched (covers the subject, object, and exists flush helpers)
  • property_join.rs can_batched_probe and can_spot_walk_remaining
  • optional.rs both build_batch implementations

This 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_facet in fluree-db-api/tests/it_query_sparql_indexed.rs seeds and indexes line items, adds a novelty-only price, and asserts SUM = 67 across: the single-predicate control, the faceted same-subject star in both pattern orders, a FROM-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.

bplatz added 2 commits June 10, 2026 03:47
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.
@bplatz
bplatz requested review from aaj3f and zonotope June 10, 2026 07:51
Base automatically changed from perf/novelty-write-cost-base to main June 10, 2026 15:51

@aaj3f aaj3f left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +703 to +720
/// 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,
},
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in ddedce8

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.
@bplatz
bplatz merged commit f60faa5 into main Jun 10, 2026
13 of 14 checks passed
@bplatz
bplatz deleted the fix/same-subject-star branch June 10, 2026 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants