Skip to content

fix: batched join probes ignored time-travel snapshot - #1223

Merged
bplatz merged 6 commits into
mainfrom
fix/time-travel-bgp
May 6, 2026
Merged

bplatz merged 6 commits into
mainfrom
fix/time-travel-bgp

Conversation

@bplatz

@bplatz bplatz commented May 6, 2026

Copy link
Copy Markdown
Contributor

SPARQL/JSON-LD time-travel queries silently returned latest-state results
when the WHERE clause combined ?s a <Class> with a same-subject triple
whose object was a literal (?s :status "paid") or a variable joined to
the type pattern. The @t:N snapshot qualifier on the dataset was ignored
in those shapes, so historical UIs showed the same data at every t.

This change gates the affected batched-probe paths on
to_t == store.max_t() and falls back to the per-row scan path
(BinaryCursor::set_to_t + history-sidecar replay) for historical
snapshots.

Reproduction (from the bug report)

Ledger with two commits where one entity's ns:status changes from
"approved" (t=1) to "paid" (t=2). 18 invoices were paid at t=1, 20 at
t=2.

# Pattern t=1 (expected 18) t=2 (expected 20) Verdict
A ?inv a ns:Invoice ; ns:status ?status GROUP BY ?status 20 20 ❌ latest
B ... ; ns:status ?s . BIND(?s AS ?aliased) GROUP BY ?aliased 18 20 ✓
C No ?s a <Class> triple 28 30 ✓
D ... ; ns:status ?s . FILTER(?s = "paid") 18 20 ✓
E ?inv a ns:Invoice ; ns:status "paid" 20 20 ❌ latest

Patterns A and E were broken; B, C, D worked.

Root cause

The query engine has three batched probe helpers in fluree-db-query that
walk base leaflet rows directly via load_leaflet_columns_cached and
decode_leaf_dir_v3_with_base:

  • batched_subject_probe_binary — used by
    flush_batched_exists_accumulator_binary (pattern E:
    ?s p <literal>).
  • scan_leaves_into_scatter — used by
    flush_batched_accumulator_binary (pattern A:
    ?s p ?o joined to a type triple).
  • batched_subject_star_spot — used by PropertyJoinOperator's
    same-subject star walk.

None of them apply ctx.to_t filtering or replay the FIR6 history
sidecar. The base index only stores the live state at max_t; retracts
go in the sidecar. So a historical query routed through any of these
helpers gets latest-state rows back.

Pattern D worked only because FILTER pushdown turned the literal match
into an inline op evaluated by per-row BinaryScanOperator scans, which
do honor ctx.to_t via BinaryCursor::set_to_t.

The fast_path_* operators are already gated on to_t == max_t via
fast_path_store(ctx). The bug is specifically in the join-side batched
probes that bypassed that gate.

Fix

Disable the batched probe paths when to_t < store.max_t() and let the
per-row scan path handle correctness. Two crates touched:

fluree-db-query/src/join.rs

NestedLoopJoinOperator::next_batch — extend the runtime use_batched
gate with at_latest_t so all three batched modes
(batched_eligible, batched_object_eligible,
batched_exists_eligible) fall through to per-row scans for historical
snapshots.

fluree-db-query/src/property_join.rs

  • Add PropertyJoinOperator::at_latest_t(ctx) helper.
  • Gate can_batched_probe (per-predicate batched probe) on it.
  • Gate can_spot_walk_remaining (driver-side SPOT star walk) on it.

The fallback per-row path was already correct; this just routes
historical queries to it.

Performance impact

Historical queries (to_t < max_t) now fall back to per-row scans for
the affected join shapes. They were silently incorrect before, so this
is a correctness regression on the latency dimension only when applied
to historical snapshots — the expected and now-tested behavior. Latest
queries (to_t == max_t, the common case) take the same fast path as
before.

@bplatz
bplatz requested review from aaj3f and zonotope May 6, 2026 11:50

@zonotope zonotope 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.

I see a number of code duplication issues that are not blockers but should be addressed later. Otherwise 👍🏾

@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.

Not a big deal, but the PR description seems to describe what this PR did before the last few commits. The performance section mentions that "Historical queries… now fall back to per-row scans" bit, but that's no longer true after your last few commits. Similarly, the PR mentions abandoning the batch probes with the replay_leaflet_at_t() sidecar. I only suggest maybe fixing this so that if tools like claude ever try to understand the code vis-a-vis historical PRs landed into main, this might be a point of confusion.

In the "Bug Report Table" of the PR description, there's 2 patterns that seem maybe untested and possibly worth adding:

  1. A test for BIND(?s AS ?aliased) (pattern B from the bug report) to lock in that the existing-correct path still works post-PR.
  2. A test that exercises batched_subject_star_spot directly via a 3+ predicate same-subject star (the SPOT walk path) at a historical t. Pattern A's group-by goes through scatter, not star, I think. Just to make sure the star path doesn't have any subtle gaps.

(I would have put the following in as line comments/suggested-edits but GitHub seems to have service failures on those API calls so I'm going to shove them in here)

fluree-db-query/src/join.rs
Several places where let to_t_u32 = u32::try_from(ctx.to_t).unwrap_or(u32::MAX); gets recomputed when it's invariant across all the various leaflet loop work. Not a huge perf win, but given all the perf tuning for batched-probe hot paths.

fluree-db-query/src/join.rs
Several places within the per-leaf loops call store.fetch_sidecar_bytes_sync(…). Those are all leaf-scoped, so it makes sense, but in environments like solo where these are network calls, I wonder if we could amortize it with additional caching on the leaflet cache? This wouldn't be a small change, so maybe worth a follow-up issue just to track it.

fluree-db-query/src/join.rs:2149-2154
This is more of a question than a recommendation. In this PR, scan_leaves_into_scatter switches from ColumnSet::CORE to ColumnProjection::all() when needs_replay == true. Why isn't this switch also needed for batched_subject_probe_binary and batched_subject_star_spot? Is it just that scan_leaves_into_scatter is the only one that narrowed projection so now it needs to widen it back, or...?

fluree-db-binary-index/src/read/replay.rs:496-512
Not a recommendation for now, but just wondering if it will be possible to have some bottlenecking w/ certain history queries as find_base_assert_at_target() paths to replay_leaflet() and does linear scan for first t match on each missing fact. We may just want to make sure we have telemetry/profiling for this if there's ever a future-facing scenario where particular queries with lots of asserts over time on the same keys end up being slower than we'd want them. I suppose the only solution for this would be a kind of key --> most_recent_assert_t index cache and it would only degrade, again, where we see repeat FactKeyV3

@bplatz

bplatz commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

@aaj3f @zonotope addressed feedback in 1f34b96 and 85407a5

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.

3 participants