Skip to content

fix(query): fulltext() scores values read under a policy - #2369

Merged
bplatz merged 4 commits into
mainfrom
fix/fulltext-defaults-under-policy
Nov 13, 2026
Merged

bplatz merged 4 commits into
mainfrom
fix/fulltext-defaults-under-policy

Conversation

@bplatz

@bplatz bplatz commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2329

Problem

fulltext(?v, "…") over a property declared in f:fullTextDefaults returned no rows as soon as the request carried a policy, including a policy that allows every read. A value typed @fulltext still scored, but with a different score than the same query gets without a policy.

Cause

Under a policy the scan reads decoded flakes so each one can be checked. The literal bindings built on that path carried no predicate id. fulltext() finds a value's BM25 arena by that id, so under a policy it never found one:

  • a configured property has no other way to score, so the value came back unbound and the filter dropped the row;
  • an @fulltext value fell back to the per-document term-frequency score.

Fix

The decoded-flake path attaches the predicate id to a literal binding, as the cursor path already does. The cost is one memoized hash lookup per literal flake. The same path also serves history scans, the overlay-only probe and unindexed novelty rows, which run no policy check; the lookup is small next to the value clone and row allocation each of those already does.

Behaviour to be aware of

Scores under a policy now come from the arena's corpus statistics (document count, document frequencies, average length), which cover every document of the property, including ones the policy hides. The scores of visible documents therefore depend on hidden ones. For configured properties this is inherent to scoring them at all. For @fulltext values it is new: they previously used the per-document fallback under a policy, which reads no corpus statistics. In exchange, a policy-scoped request now gets the same scores as an unrestricted one for the documents it can see. A reader can therefore tell from their scores when hidden documents containing a term come and go; docs/security/policy-in-queries.md and the "Scoring under a policy" section of docs/indexing-and-search/fulltext.md say so.

Tests

JSON-LD and SPARQL twins in it_query_fulltext.rs:

  • under an allow-all policy the reader gets exactly the unrestricted hits and scores, on a configured property and an @fulltext property, as a single scan and through a join, on indexed data and with an unindexed document added;
  • under a policy that hides one document, that document is not a hit, and the visible one scores exactly as it does unrestricted (the hidden document's statistics count toward it).

All four fail with the fix reverted.

A scan under a policy reads decoded flakes, and the literal bindings it
built from them carried no predicate id. fulltext() finds a value's BM25
arena by that id, so a property declared in f:fullTextDefaults scored
nothing and an @fulltext value fell back to the per-document score.
@bplatz bplatz added the bug Something isn't working as expected label Nov 12, 2026
@bplatz
bplatz requested a review from a team November 12, 2026 19:45

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

@bplatz nice find, and the fix sits in the right place: decoded_object_binding is the single spot where a decoded flake becomes an object binding, so the predicate now rides along on the policy lane (and on history, the overlay-only probe and untranslated novelty rows) for one memoized lookup per literal row. Claude reverted the hunk on a box: all four tests fail, and every allow-all combination they loop over (16 of them) stops matching the unrestricted result, so they guard it well. Two headline notes:

(1) On your "behaviour to be aware of" note: I'm on board with a reader's scores coming from the whole property's statistics. It's what makes a policy-scoped reader get the same scores as an unrestricted request, and it's the posture the BM25 graph source already has (score against the whole index, then drop hidden hits).

One ask, the same one we landed on for #2264: let's say it where someone configuring policy will look. Claude checked what a reader can learn from it: with three ledgers whose reader-visible data is identical, the reader's score for ex:doc1 on "graph" is 0.4421 when a hidden document mentions "graph", 0.9227 when it doesn't, and 0.6931 when there is no hidden document, and it moves as soon as a hidden document is committed. So a sentence in the "Full-text and vector search" bullet of docs/security/policy-in-queries.md and in "How Scoring Works" in docs/indexing-and-search/fulltext.md: scores come from statistics over every document of the property, including ones the identity can't see, so a reader can tell from their scores when hidden documents containing a term come and go. And a score assertion in assert_hidden_document_is_not_a_hit (the doc3-hidden reader gets the unrestricted score for ex:doc1), so a later move to policy-scoped statistics is a deliberate change rather than a silent one.

(2) This is another instance of the class I described on #2022 as "two paths do the same thing and sometimes one of those paths re-derives (and drops) something the first path already did". Here the decoded lane built a literal without the predicate the cursor lane attaches, and fulltext() is the consumer that noticed:

513fed4f9 (this same p_id, materialized vs encoded rows); #1470 (float tag and lang EBV, encoded vs Lit); #1443 (GRAPH ?g bound as an encoded binding); 45f7d45c2 (OPTIONAL and equality over encoded bindings in three index states)

Your allow-all-reader-equals-unrestricted check is basically the structural fix in miniature, since an allow-all policy forces the decoded lane. Lifted into tests/support so the other function suites (vector, T()/OP(), lang/datatype, GRAPH ?g) run each query both ways, it would turn the next one into a red test. Not for this PR though, the point fix is right here.

Full review below:


✅ Approving to unblock: please get the docs sentence and the score assertion in before merging.

This is a clean fix for a nasty silent failure, @bplatz: a configured property returning no rows the moment a request carries any policy, even one that allows everything. Attaching the predicate in decoded_object_binding through predicate_id_for_sid reuses the per-store memo object_binding.rs and list_view.rs already use per row, and the JSON-LD/SPARQL twins pin exact score parity for both a configured property and an @fulltext value, as a single scan and through a join, indexed and with novelty. fwiw the function also serves history scans, the overlay-only probe and untranslated novelty rows, which carry no policy check, so the body's "on a path that already runs a policy check per flake" undersells the reach a little; all good, and the lookup is noise on those lanes too.

Adherence to repo commitments:

  • Patterns/abstractions: ✔ fixes the one decoded-flake → binding chokepoint and reuses BinaryIndexStore::predicate_id_for_sid; no new mechanism.
  • Performance (speed first, memory second): ✔ the map is built once per store (OnceLock); per literal row on the decoded lanes it adds a length-bit test and one FxHashMap probe, no allocation, on lanes that already clone each value and allocate a row (and, under a policy, run a per-flake policy check). The cursor lane is untouched. No performance-degradation risk.
  • Deployment targets: ✔ n/a: an in-memory lookup on an existing per-store memo, the same on the embedded, server and wasm32 hosts; wasm32 and wasm-smoke are green on the head SHA.
  • Testing: ✔ four integration tests wired through grp_query, run by CI on the head SHA; with the hunk reverted all four fail, and all 16 allow-all combinations (2 surfaces × 2 properties × joined or not × indexed or novelty) stop matching the unrestricted result.
  • Conventions: ✔ self-describing subject with a body that explains the mechanism; fmt and clippy green. The Binding::Lit::p_id doc comment is the one stale line (inline).

Verified at branch HEAD 161f8f920: CI test green with the four new tests passing by name (15860 tests run); on an EC2 box Claude ran the new tests and the it_query_fulltext module green, reverted the binary_scan.rs hunk and saw all four new tests fail, then restored; git merge-tree shows no conflict with #2214 or #2202 beyond what #2214 already has with main.

p_id: self
.store
.as_ref()
.and_then(|store| store.predicate_id_for_sid(&flake.p)),

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.

Optional. This is the right spot for it: decoded_object_binding is the one place a decoded flake becomes an object binding, so the policy lane, history scans, the overlay-only probe and untranslated novelty rows all pick up the predicate at once, through the store's existing memo.

One small thing nearby: the field doc on Binding::Lit::p_id (fluree-db-query/src/binding.rs:114-116) still says "Set when early-materializing novelty values in BinaryScanOperator". After this PR that's one of two lanes that set it, and I suspect that comment is how the decoded lane ended up writing None in the first place.

/// Optional predicate ID from binary index (metadata, excluded from eq/hash).
/// Set by every BinaryScanOperator lane that reads a stored predicate:
/// materialized cursor rows and decoded flakes alike.
/// Used by `fulltext()` to identify the correct per-predicate arena/stats.
p_id: Option<u32>,

Minor and non-blocking, but if you agree it's right, I'd rather see it folded in now than lost in the backlog.

Commenting here because fluree-db-query/src/binding.rs is not in this diff.

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 bda1746

BM25 statistics cover every document of a property, so a policy-scoped
reader scores a visible document as an unrestricted request does, and can
tell from its scores when hidden documents containing a term come and go.
The policy and full-text docs say so, and the hidden-document test pins the
reader's score to the unrestricted one, so scoping the statistics to what a
policy shows would be a deliberate change.
@bplatz

bplatz commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Docs sentence and score assertion addressed in 5253825

@bplatz
bplatz merged commit 5ceb7a7 into main Nov 13, 2026
19 checks passed
@bplatz
bplatz deleted the fix/fulltext-defaults-under-policy branch November 13, 2026 20:00
bplatz added a commit that referenced this pull request Nov 13, 2026
Brings #2296, #2309, #2313, #2315, #2316, #2317, #2367, #2368, #2369,
#2373, #2374, #2378 and #2379.

Ports onto next:

- #2374 (bound-object probe reads POST): main's POST walk and per-type
  flush replace next's OPST walk. The lane keys (o_type, o_key), so next's
  triple-term handles are one more covered type (object_probe_covers takes
  TripleTermDict) rather than a flush per type; a term's matches are read
  whole, as a ref's are. The wildcard cursor still keys refs only. Pooled
  graphs decline through dedicated_branch(Post); o_i is 64-bit. Next's
  pair join keys its subject through the shared LaneKey.
- #2373 (leaflet type constant under replay): replays_history and
  o_type_const_for beside next's may_hold_keys; the row-labelling lanes
  take main's const-unless-replaying form over next's always-read-column.
- #2368: the cyclic join decodes for eager callers with next's list index
  sentinel; both reasoning tests kept (cyclic operator and pair lane).
- #2317 (GRAPH streams): main's work queue and InnerStream with next's
  pooled-graph pruning before each enumerated graph.
- #2315: list predicates are an ordered set in next's multi-ledger view;
  the annotation edge probe stays deleted (no bundle lane on next).
- #2296 (rebase reads its new line): ported onto next's fenced resets,
  erasure checks and peer sync (follow_record keeps next's unchanged-head
  arm); a non-fast-forward rebase moves head and index in one fenced
  reset.
- Main's new tests: async genesis_ledger, graph_source_store for mapping
  writes, leaf_rows for leaflets-per-leaf, storage lock off for two file
  instances. New pattern/function matches cover TermComponents and the
  triple-term functions; AnnotationTarget arms dropped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fulltext() over a property declared in f:fullTextDefaults scores nothing under a policy

2 participants