Repository navigation
fix: batched join probes ignored time-travel snapshot - #1223
Conversation
queries stay on the batched fast path
instead of pre-skipping them
zonotope
left a comment
There was a problem hiding this comment.
I see a number of code duplication issues that are not blockers but should be addressed later. Otherwise 👍🏾
aaj3f
left a comment
There was a problem hiding this comment.
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:
- 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. - A test that exercises
batched_subject_star_spotdirectly via a 3+ predicate same-subject star (the SPOT walk path) at a historicalt. 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
SPOT-walk regression tests at historical t
SPARQL/JSON-LD time-travel queries silently returned latest-state results
when the WHERE clause combined
?s a <Class>with a same-subject triplewhose object was a literal (
?s :status "paid") or a variable joined tothe type pattern. The
@t:Nsnapshot qualifier on the dataset was ignoredin 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 historicalsnapshots.
Reproduction (from the bug report)
Ledger with two commits where one entity's
ns:statuschanges from"approved"(t=1) to"paid"(t=2). 18 invoices were paid at t=1, 20 att=2.
?inv a ns:Invoice ; ns:status ?statusGROUP BY?status... ; ns:status ?s . BIND(?s AS ?aliased)GROUP BY?aliased?s a <Class>triple... ; ns:status ?s . FILTER(?s = "paid")?inv a ns:Invoice ; ns:status "paid"Patterns A and E were broken; B, C, D worked.
Root cause
The query engine has three batched probe helpers in
fluree-db-querythatwalk base leaflet rows directly via
load_leaflet_columns_cachedanddecode_leaf_dir_v3_with_base:batched_subject_probe_binary— used byflush_batched_exists_accumulator_binary(pattern E:?s p <literal>).scan_leaves_into_scatter— used byflush_batched_accumulator_binary(pattern A:?s p ?ojoined to a type triple).batched_subject_star_spot— used byPropertyJoinOperator'ssame-subject star walk.
None of them apply
ctx.to_tfiltering or replay the FIR6 historysidecar. The base index only stores the live state at
max_t; retractsgo 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
BinaryScanOperatorscans, whichdo honor
ctx.to_tviaBinaryCursor::set_to_t.The
fast_path_*operators are already gated onto_t == max_tviafast_path_store(ctx). The bug is specifically in the join-side batchedprobes that bypassed that gate.
Fix
Disable the batched probe paths when
to_t < store.max_t()and let theper-row scan path handle correctness. Two crates touched:
fluree-db-query/src/join.rsNestedLoopJoinOperator::next_batch— extend the runtimeuse_batchedgate with
at_latest_tso all three batched modes(
batched_eligible,batched_object_eligible,batched_exists_eligible) fall through to per-row scans for historicalsnapshots.
fluree-db-query/src/property_join.rsPropertyJoinOperator::at_latest_t(ctx)helper.can_batched_probe(per-predicate batched probe) on it.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 forthe 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 asbefore.