Skip to content

fix(query): equality surfaces match terms first committed after the last index - #2047

Closed
bplatz wants to merge 2 commits into
mainfrom
fix/novelty-encoded-equality
Closed

bplatz wants to merge 2 commits into
mainfrom
fix/novelty-encoded-equality

Conversation

@bplatz

@bplatz bplatz commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Problem

Terms committed after the last index build compared unequal to themselves when one side of a comparison came from a batched index lane and the other from VALUES, a UNION branch, or a scan over novelty. Wrong answers, no error:

  • VALUES ?o { "new" "old" } ?d a :C ; :p ?o dropped the rows whose string first appeared after the last index build.
  • ?r :type ?t . VALUES ?t { :A :B } FILTER NOT EXISTS { ?r :template ?a } returned a new subject that has a template; OPTIONAL + !BOUND did the same, and EXISTS dropped it.
  • DISTINCT returned such a term twice, MINUS did not remove it, GROUP BY split it into two groups.
  • FILTER(sameTerm(?r, :new)) was false for a new subject.
  • COUNT(DISTINCT) and MINUS over a skolemized blank node split it the same way.

Each answer corrects itself after a reindex, and the single-value VALUES and FILTER IN forms were unaffected, which is why it read as intermittent.

Cause

The batched probe lanes emit EncodedSid/EncodedLit under novelty, carrying DictNovelty ids for subjects and strings the index has not seen. Equality surfaces normalize decoded bindings to that encoded form through encoded_equivalent, which looked only in the persisted dictionaries, on the premise that encoded bindings never carry novelty ids. It also skipped blank-node subjects, though skolemized blank nodes are subject-dictionary entries that index lanes emit encoded. The SAMETERM fast path had its own persisted-only lookup.

Change

One value → id lookup and one normalizer for every site:

  • DictLookup (fluree-db-binary-index): persisted dictionaries, then DictNovelty, in the order populate_dict_novelty_safe allocates. Exposed as BinaryGraphView::dict_lookup() and ExecutionContext::dict_lookup(). Replaces the hand-rolled copies in the binary scan and range providers, batched join / OPTIONAL / property-join key resolution, the annotation probe (Dicts), fulltext and DictOverlay.
  • EqualityNorm::for_ctx carries the store, the novelty layer and the graph view. normalize_for_key and binding_to_group_key_normalized take it in place of loose store/view pairs; equality_norm_store, normalize_for_key_cow and EqualityNorm::parts are removed. DISTINCT, both GROUP BY operators, COUNT(DISTINCT), MINUS, semijoin, VALUES, subquery, range semijoin, membership join, the annotation probe, R2RML and the hash join all key through it. The hash join's private JoinKey/Sid lookup is gone, and VALUES and the SQL lane build their norm once per open instead of per comparison.

Behavior notes beyond the fix: the streaming GROUP BY now normalizes on a dataset spanning several graphs of one ledger, as DISTINCT already did; the hash join and R2RML no longer normalize on multi-ledger datasets, where no lane emits encoded bindings.

Paths checked and left persisted-only

These look up subjects in the persisted dictionaries alone by design: they read index leaflets, which hold no novelty ids.

  • frontier.rs — a node without a persisted id is PathNode::Novel.
  • sid_iri::sid_to_store_s_id — leaflet scan filters.
  • filter.rs EXISTS cache and the fast_path_common lookups — behind fast_path_store, which declines whenever novelty is present.

Language tags need no novelty lookup: overlay translation refuses a tag the index has not seen and keeps those values decoded.

Tests

it_novelty_encoded_equality.rs indexes a base, adds a novelty tail, and asserts explicit rows, then reindexes and requires identical answers. Each case also asserts the lane that pairs the encoded term with a decoded one actually ran (property-join completion event, subject/object batched flush span, or HashJoinOperator in the physical plan), so a planner change cannot leave a case passing vacuously. SPARQL cases have JSON-LD twins for VALUES, NOT EXISTS, sameTerm and the hash join.

Mutation checks:

  • Dropping the novelty layer from EqualityNorm::normalize fails the 10 VALUES / EXISTS / NOT EXISTS / OPTIONAL / DISTINCT / MINUS / GROUP BY cases.
  • Dropping it from the SAMETERM fast path fails the 2 sameTerm cases.
  • Restoring the blank-node skip fails the 4 blank-node cases (hash join ×2, COUNT(DISTINCT), MINUS).

…ast index

The batched probe lanes emit `EncodedSid`/`EncodedLit` under novelty, with
`DictNovelty` ids for subjects and strings the index has not seen. Equality
surfaces normalize decoded bindings to that encoded form, but the normalizer
looked only in the persisted dictionaries, on the premise that encoded
bindings never carry novelty ids. A novelty-only term in two representations
was therefore two terms:

- VALUES joined to a typed star dropped rows whose string value was new
  since the last index build.
- FILTER NOT EXISTS / OPTIONAL + !BOUND kept, and EXISTS dropped, rows whose
  subject was new since the last index build.
- DISTINCT returned such a term twice, MINUS did not remove it, GROUP BY
  split it into two groups.
- The SAMETERM fast path compared an encoded subject against a constant
  through the persisted dictionary only, so `sameTerm(?s, ex:new)` was false.

The normalizer also skipped blank-node subjects, though skolemized blank
nodes are subject-dictionary entries that index lanes emit encoded, so the
same split hit COUNT(DISTINCT) and MINUS over a blank node under novelty.

One value -> id lookup and one normalizer now serve every site:

- `DictLookup` (binary-index) reads the persisted dictionaries, then
  `DictNovelty`, in the order `populate_dict_novelty_safe` allocates. It
  replaces the hand-rolled copies in the binary scan and range providers,
  the batched join/OPTIONAL/property-join key resolution, the annotation
  probe, fulltext and `DictOverlay`.
- `EqualityNorm::for_ctx` carries the store, the novelty layer and the graph
  view; `normalize_for_key` / `binding_to_group_key_normalized` take it in
  place of loose store/view pairs. DISTINCT, GROUP BY (both operators),
  COUNT(DISTINCT), MINUS, semijoin, VALUES, subquery, range semijoin,
  membership join, the annotation probe, R2RML and the hash join all key
  through it. The hash join's own `JoinKey` and Sid lookup are gone, and
  VALUES builds its norm once per open rather than per comparison.

Lookups left persisted-only are the ones that read index leaflets alone:
frontier `PathNode::Id`, `sid_to_store_s_id` scan filters, and the EXISTS
cache and fast-path-common lookups behind the overlay-free gate.
@bplatz bplatz added bug Something isn't working as expected area:query Query execution, planning, fast paths, overlay, result formatting labels Oct 10, 2026
@bplatz
bplatz requested a review from a team October 10, 2026 14:37
bplatz added a commit that referenced this pull request Oct 11, 2026
Carried over from #2047, whose fixes this branch subsumes. Covers shapes
the OPTIONAL suite does not: a novelty-only string through the
property-join lane (VALUES, DISTINCT, MINUS, GROUP BY), EXISTS / NOT
EXISTS / OPTIONAL+!BOUND and sameTerm keyed on a novelty-only subject,
and a skolemized blank node across a hash join, COUNT(DISTINCT) and
MINUS. Each case asserts the lane that pairs the encoded term with a
decoded one ran, then compares against the reindexed ledger.

On main, 14 of the 16 cases fail.
@bplatz

bplatz commented Oct 11, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #2010. Its TermDicts resolves decoded terms through the persisted dictionaries and then novelty, for subjects and strings, which is this PR's fix. All 16 cases of it_novelty_encoded_equality.rs pass on #2010 (14 fail on main), so the test file moved there in ceaa8f5. The DictLookup consolidation isn't carried over; the remaining hand-rolled lookups (binary_range, binary_scan, dict_overlay, fulltext) can move onto TermDicts in a follow-up if wanted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:query Query execution, planning, fast paths, overlay, result formatting bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant