Repository navigation
fix(query): history scans match a bound object on indexed events - #2133
Conversation
The history scan narrows its walk of persisted leaves and sidecars by subject and predicate only, and matched the bound object, its datatype and its bounds on novelty alone. On an indexed ledger a history query bound to an object - a literal, a reference, or an equality or range filter the planner folds into the scan - returned every event of the subject and predicate, through JSON-LD and SPARQL alike. Persisted events are now matched the way novelty events are.
aaj3f
left a comment
There was a problem hiding this comment.
@bplatz nice catch, and I think the fix sits at the right altitude: the persisted events now go through the same flake_matches_range_eq + ObjectBounds::matches the novelty walk already uses, rather than a third copy of the equality rules. One headline note: this is the third time the history operator's persisted lane and its novelty lane have disagreed about a part of the pattern.
dee0a35 (a literal subject/predicate that didn't resolve walked every leaflet; its commit body even notes "object filtering is not applied there"); ffbe919 / #1329 (
xsd:decimaldatatype on the index vs novelty); and this PR (the bound object, its datatype and its bounds)
The class fix would be one "does this decoded event match the pattern" check, built once in collect_history_flakes and applied to both lanes, so a component added to one lane can't be forgotten in the other. To be clear, I don't think this PR should do more than the small nit below in that direction; the point fix is right here, and this is good to merge.
Full review below:
✅ Approve with nits.
This is a clean fix, @bplatz. It's small, it reuses the novelty lane's own predicate instead of re-deriving it, and the test runs every shape on both lanes (nothing indexed, then reindexed), which is exactly the kind of test this operator needs.
What I checked beyond the test: I ran history_range_bound_object_matches_only_that_object (green, by name), then replaced the retain with a no-op and it went red on the indexed lane exactly as the body says (["alice", "alice", "alice", "bob", "bob", "bob"] vs ["alice", "bob", "bob"]). I also probed the cases I was worried about with a throwaway test, on both lanes and against the current-state query: "B"@en vs "B"@fr (the language check in flakes_to_bindings still does its job on both lanes, so only the @en events come back), a bare 30 / 30.0 against xsd:integer, xsd:int, xsd:double and xsd:decimal values (cross-type numeric equality, the same on both lanes), a typed "30"^^xsd:int (exact datatype only), and an xsd:dateTime written with Z vs +01:00 for the same instant (matches either way). Novelty, indexed and current-state agree on every one of them, and with the retain removed the indexed lane leaked the "C"@en event for a bound "B"@en and the 31 for a bare 30.
On completeness, I went through the other places that build a BinaryFilter to see whether another persisted walk drops the object the same way. The current-state BinaryScanOperator::open puts it back through object slices plus the decoded-value filter, binary_range_eq_v3 resolves it into the filter itself, the time-travel replay rebuilds a leaflet and then goes through the same row filter and decoded-value check as any current-state scan, and the count fast path is gated off whenever an object is bound. So as far as I can tell history was the only one.
On performance, this is neutral vs main. The persisted walk decodes exactly the rows it decoded before (main decoded them too and returned them as wrong rows); the retain is an in-place pass with no per-row allocation, and fewer rows now reach the policy filter and flakes_to_bindings. Pushing the object into the BinaryFilter (with the same object-slice encoding the current-state scan uses) would skip decoding the non-matching events on a broad ?s ex:p "o" history range. That's an optimization, though, not something this fix needs, and I wouldn't hold the PR for it.
Adherence to repo commitments:
- Patterns/abstractions: ✔ reuses
flake_matches_range_eqandObjectBounds::matches, the novelty lane's own check; the one nit is about keeping that a single definition. - Performance (speed first, memory second): ✔ touches the
fluree-db-queryhistory operator, but the added work is an in-place filter over rowsmainalready decoded, with no per-row allocation. No performance-degradation risk. - Deployment targets: ✔ n/a: host-neutral query-engine logic (no I/O, threads, clocks or caches), same behavior embedded in solo, in the server and in wasm.
- Testing: ✔
it_query_history_range.rsis wired throughgrp_query_history(a[[test]]target); the new test ran by name in CI'stestjob and locally, and goes red with the fix reverted. JSON-LD and SPARQL are both covered. - Conventions: ✔ self-describing subject with a full body; fmt and clippy green in CI.
Verified locally at 7cc410168: cargo test -p fluree-db-api --test grp_query_history -- history_range_bound_object_matches_only_that_object passes; with the retain removed it fails at it_query_history_range.rs:1042; a throwaway probe (language tags, numeric promotion, xsd:dateTime offsets, JSON-LD and SPARQL) agreed across novelty, indexed and current-state. Worktree restored after.
Approving, with one small note inline that I think is worth folding in before merge.
| // The persisted walk narrows by subject and predicate only; a bound | ||
| // object, its datatype and its bounds are matched on the decoded | ||
| // events, as the novelty walk below does. | ||
| let object_match = RangeMatch { |
There was a problem hiding this comment.
🟡 Optional / nit: build the object part of the pattern once, and have both lanes use it.
object_match here and the novelty match_val a few lines down (binary_history.rs:381-387) are two RangeMatch literals that have to agree on o and dt, and the object-bounds check is written twice too (in this retain, and inline in the novelty callback). That's the same shape as the bug this PR fixes, the two lanes each deciding the pattern on their own, just one level smaller.
The concrete risk is the next component. If someone teaches the novelty walk to match something new (a language tag, which today only flakes_to_bindings checks, or another bound), this retain keeps the old rule without anything going red.
Something like this (illustrative, not a drop-in):
let object_match = RangeMatch { s: None, p: None, o: o_val_opt.clone(), dt: /* as now */, t: None };
let object_matches = |f: &Flake| {
flake_matches_range_eq(f, &object_match)
&& self.object_bounds.as_ref().is_none_or(|b| b.matches(&f.o))
};
flakes.retain(|f| object_matches(f));
// novelty walk: s/p here, the object through the same closure
let match_val = RangeMatch { s: s_sid.clone(), p: p_sid.clone(), ..RangeMatch::default() };
// if !flake_matches_range_eq(f, &match_val) || !object_matches(f) { return; }This is minor and non-blocking, but if you agree it's right, I'd rather see it folded in now than lost in the backlog.
The persisted and novelty walks each built a RangeMatch for the bound object and its datatype, and each checked the object bounds, so a rule added to one lane could be left out of the other. Both now go through one check; the novelty walk's own match keeps only the subject and predicate, which the persisted walk matches by id in its BinaryFilter.
The parent now carries main 4a0d14a (#2109, #2118, #2133). Four files conflicted: - temporal.rs: main's `cmp_with_lexical` body is the one this branch already had (#2109's); the two docs are merged, so it says both that a dateTime or time lexical is held to the microsecond and that the evaluator and the bounds pass it only a plain xsd:string. `lexical_as_kind_of` stays, over the same parse functions. - query_bounds.rs: this branch's string-vs-temporal arm (the plain-string guard and the per-kind parse cache) replaces main's simplified one, which called `cmp_with_lexical` with no guard. `string_bound_otypes` stays. - eval/compare.rs: the plain-string guard stays on the evaluator's coercion. - binary_history.rs: main's shared object check (#2133) is kept, with the term-aware `matches(&f.o, &f.dt)`; this branch's separate bound test in the novelty walk is dropped, as the shared check covers both lanes.
Problem
On an indexed ledger, a history query bound to an object returned events for other objects too. The history scan narrows its walk of persisted leaves and history sidecars by subject and predicate only —
BinaryFiltercarries no object — and it applied the bound object, its datatype and its bounds to novelty events alone. Once a ledger was indexed:{"@id": "?s", "ex:name": "B"}over afrom/torange returned everyex:nameevent, not only those with"B"(six rows instead of three in the new test).{"@id": "ex:bob"}) behaved the same.(= ?v "B"),(> ?v 30)). The planner doesn't apply them again after the scan, so the extra events reached the results.FROM <l@t:1> TO <l@t:latest>with?s ex:name "B"did the same.Before indexing, these queries were correct; they went wrong as soon as the ledger was indexed.
Fix
After the persisted walk, the decoded events are matched against the pattern's bound object and datatype and against its object bounds. The novelty walk goes through the same check, so the two lanes can't drift apart on the object again. The novelty walk's own match keeps only the subject and predicate, which the persisted walk matches by id.
Also in this PR, a fix for
main:error::tests::every_variant_answers_the_same_typed_or_flattenedfailed there once #2131 merged. #2009 addedApiError::ConfigUnavailableand #2131 addedStorageInUse, and each moved the test's variant count from 47 to 48. The merge took the identical edit once, so 49 variants were counted as 48. The count is now 49.Tests
history_range_bound_object_matches_only_that_objectruns each shape twice: on a ledger with nothing indexed, then again after a reindex. It covers JSON-LD (bound literal, bound reference, equality filter, range filter) and SPARQL (bound literal). With the fix reverted, the indexed lane fails: the bound literal returns all sixex:nameevents.Follow-up: #2143, pushing the bound object into the persisted walk's
BinaryFilterso non-matching events aren't decoded at all.