Skip to content

fix(query): filters on the index lane answer as they do over novelty - #2226

Merged
bplatz merged 7 commits into
mainfrom
fix/lang-pushdown-operator
Nov 12, 2026
Merged

bplatz merged 7 commits into
mainfrom
fix/lang-pushdown-operator

Conversation

@aaj3f

@aaj3f aaj3f commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Filters on the index lane answer as they do over novelty

Ways a filter over an indexed ledger, or over a staged view of one, gave a different answer from the same filter over novelty. They share the code that turns a filter into a check on the encoded object, so they're one PR.

LANG(?o) under any operator but =

Over an indexed ledger, FILTER(LANG(?v) != "en") returned only the en rows, the exact complement of its answer, and langMatches(LANG(?v), "en") and STRSTARTS(LANG(?v), "en") dropped en-gb. The same filters over novelty were right, so the answer depended on whether the data had been indexed yet.

The binary scan compiles some filters into checks on the encoded object, in compile_encoded_pre_filters_and_prune_inline_ops (binary_scan.rs). Its LANG arm is meant for LANG(?o) = "en", which it turns into a check on the object's language type. But it matched the shape, a two-argument call with LANG(?o) on one side and a string on the other, without checking which function it was. So !=, <, >, langMatches, STRSTARTS and anything else over LANG(?o) and a tag all became "the tag equals en". COUNT over those filters counted the same wrong rows. Novelty never reaches the rewrite, and the W3C suite loads its data by transaction, so nothing exercised it until the suite read an index.

The arm now runs only for =; every other function of LANG(?o) stays a row filter.

COUNT with LANG(?o) = ""

SELECT (COUNT(*) AS ?n) WHERE { ?s :v ?v FILTER(LANG(?v) = "") } returned 0 over an index with fast paths on. The SELECT was right. The COUNT fast path for LANG(?o) = "tag" (count_rows_lang_filter_operator) looks the tag up in the index's language dictionary and counts the objects of that language type. "" isn't a tag, so the lookup fails and the count is 0, though LANG(?v) = "" holds for every literal without a tag. The scan's pre-filter already left "" as a row filter, only by the accident that the lookup failed there too.

The scan and both COUNT fast paths each had their own copy of the LANG(?o) = <string> match. They now share one, binary_scan::lang_tag_equality, which yields only a non-empty tag, so none of them can read "" as a tag again.

Remembered filter verdicts

On the index lane, a filter over one variable remembers its verdict per encoded value (ENCODED_BOOL_PREDICATE_CACHE in eval/helpers.rs, a thread-local LRU), keyed by the store, the graph, a hash of the filter, and the value. Three parts of that key were short:

  • The filter hash took only which function each call was, not its fields. SPARQL lowers LANG and DATATYPE to the strict form (an IRI is a type error) and JSON-LD to the lenient one (an IRI's lang is "", decision D-12). On one ledger, whichever surface ran first answered for the other. After JSON-LD's (= (lang ?v) ""), SPARQL's LANG(?v) = "" kept an IRI row, and after SPARQL's LANG(?v) != "en", JSON-LD's (!= (lang ?v) "en") dropped one. Function now derives Hash, agreeing with its Eq, and the filter hash takes the strict/lenient mode. The mode rides in the arity word the hash already writes rather than a write of its own; that's the perf note below. A debug assertion fails if a function with fields the hash doesn't take ever reaches it, so a new variant with a field can't quietly reopen this.
  • The store part was the store's address. A store built after another is dropped can land at the same address and read the dropped one's verdicts against a different dictionary. It's now the store's instance id (BinaryIndexStore::store_id), which the overlay dirty-id cache in frontier.rs already uses for the same reason. This showed up in the test itself: when the SPARQL and JSON-LD runs used separate Fluree instances to stay clear of the first problem, the second instance's store sometimes sat where the first one's had been, and the strict verdicts leaked across anyway.
  • The value part didn't say whose dictionary its ids came from. A string or subject id above the store's persisted watermarks resolves from the view's dictionary novelty, and a staged view (a preview, a SHACL or post-state policy check) gives its own new terms ids from the same frontier. Two previews over one store that stage "xyz" and "abc" give both strings the same id. The scan decodes every row once there's novelty, but the join's probe keeps a string encoded, so in the second preview REGEX(?v, "b") read the first one's false and dropped its row. Predicate ids past the store's count are allocated per scan, the same way. A verdict is now remembered only when every id in the key is one the decoder reads from the store: string and subject ids up to the watermarks, predicate ids below the store's count. Anything else is evaluated each time, which is what a view's own terms cost before the cache existed.

The rest of the class

I went through every rewrite that turns a filter into an encoded check or a scan bound, looking for the same mistake: matching a shape without checking the operator, or the operands.

Rewrite Where
LANG(?o) = "tag" → language-type check binary_scan.rs fixed (operator), then shared via lang_tag_equality
COUNT with LANG(?o) = "tag" operator_tree.rs (detect_predicate_count_rows_lang_filter) checked the operator; fixed for ""
COUNT with compiled pre-filters operator_tree.rs (detect_count_rows_with_encoded_filters) checked the operator; "" fell back correctly, now declined up front
STRSTARTS(?o / STR(?o), "p"), anchored REGEX → string-prefix ranges binary_scan.rs (extract_object_string_prefix) checks the operator, rejects an empty prefix
ISBLANK(?o) → blank-node check binary_scan.rs checks the operator
?s = ?o / ?s != ?o → subject/object ref check binary_scan.rs checks the operator (Eq / Ne)
COUNT with ISBLANK / ISLITERAL operator_tree.rs checks the operator
?o op const range bounds planner.rs (extract_range_constraints) checks the operator, and applies only to a bare variable

There's no DATATYPE, isIRI or STR rewrite, so those always run as row filters. The test covers them anyway, so a future rewrite has to keep them right.

For the cache key, the other store-address key in the query crate is OverlayOpsBinding (fast_path_common.rs). That one lives inside a single execution, which holds its stores alive, so an address there can't be reused while it's in use. Everything that outlives a query keys by store_id.

Tests

it_filter_pushdown_operators (in grp_query) runs seventeen SPARQL filters and seven JSON-LD ones against hand-written answers: LANG, DATATYPE, isIRI/isLiteral and STR under =, !=, <, >, langMatches and STRSTARTS, each SPARQL one as a SELECT and as a COUNT. Each runs over novelty and over the index, with fast paths on and off. Both surfaces now share one ledger, and the whole matrix runs twice, SPARQL first and JSON-LD first, so a verdict one surface leaves behind is in reach of the other. The pairs LANG(?v) = "" / (= (lang ?v) "") and STR(DATATYPE(?v)) != "x" / (!= (str (datatype ?v)) "x") differ only on the IRI row, which is what the strict/lenient split decides. The answers are written by hand, not compared lane to lane, because with fast paths off both lanes run the same scan and would have agreed on the wrong answer.

a_rebuilt_store_does_not_answer_for_a_dropped_one builds and drops an indexed ledger 32 times, its only string alternating between "abc" and "xyz" at the same dictionary position, under an unanchored REGEX(?v, "b"). The encoded value the filter sees is the same each round while its answer flips. (An anchored regex becomes a prefix range and never reaches the cache, which is why it's unanchored.)

previews_over_one_store_do_not_share_verdicts_for_their_own_strings stages two previews over one indexed ledger, alternating "xyz" and "abc" as a new subject's value, and queries each through a join, ?s :kind :thing . ?s :v ?v FILTER(REGEX(?v, "b")). only_ids_the_store_persisted_are_remembered (unit) checks each id kind at and one past its bound.

strict_and_lenient_functions_hash_apart (unit, eval/helpers.rs) checks that Function's hash agrees with its Eq for the strict and lenient Lang and Datatype, that the filter hash tells them apart, and that the debug check recognizes a function with fields.

Each fix, reverted on its own with the tests in place:

  • The operator check: 64 cases fail, 32 in each order, all non-= LANG filters over the index.
  • The empty-tag check: the COUNT LANG(?v) = "" case fails, 0 where 4 are expected.
  • The filter hash: 18 cases fail, the IRI row in both orders, for both the LANG and DATATYPE pairs, with fast paths on and off; the unit test fails too.
  • The store id: the rebuilt-store test fails on each of three runs (14, 14 and 1 stale rounds of 32; whether an address gets reused is up to the allocator).
  • The persisted-id check: the preview test fails, each "abc" preview dropping its row. Taking out the string, subject or predicate bound alone fails the unit test.

Performance

The verdict lookup itself is unchanged; a u64 id replaces a pointer. The filter hash is computed once per prepared filter, and again per row for filters nested under &&, ||, ! or IF, so I measured it directly, as retired instructions per call (best of 7 × 1M, release, macOS proc_pid_rusage):

Filter before after
LANG(?v) = "" 1246 1256 (+0.8%)
REGEX(STR(?v), "…") 1287 1291 (+0.3%)

Hashing the mode as its own bool write cost +8.5% on the LANG filter, because a one-byte write leaves every later word write straddling the hasher's buffer; writing it as a word still cost +4.9%. Folding it into the arity word is what keeps it flat. At the query level, a SELECT over 20,000 indexed rows under LANG(?v) != "en" (every row through the remembered verdict) is 164.6M retired instructions before and 164.9M after (+0.2%), median of 15. The persisted-id check costs about 28 instructions a row on its own, and on that query it moves 164.64M to 164.70M (+0.04%).

aaj3f added 5 commits October 29, 2026 14:49
…e index

The binary scan compiles some filters into checks on the encoded object
(`compile_encoded_pre_filters_and_prune_inline_ops`). Its LANG arm matched
any two-argument call of LANG(?o) and a string constant and compiled it to
an exact language-tag check, without checking the function. Over an
indexed ledger, `FILTER(LANG(?v) != "en")` kept only the `en` rows, the
complement of its answer, and `langMatches(LANG(?v), "en")` and
`STRSTARTS(LANG(?v), "en")` dropped `en-gb`; COUNT over those filters
counted the same wrong rows. Novelty never reaches the rewrite, so the
two lanes disagreed, and the W3C suite, which loads by transaction,
never read the index.

The arm now runs only for `=`. The other rewrites in the function
(STRSTARTS/REGEX prefix, ISBLANK, ?s = ?o / ?s != ?o) and the count fast
paths in operator_tree.rs already check their function.

The test runs LANG, DATATYPE, isIRI/isLiteral and STR under =, !=, <, >,
langMatches and STRSTARTS, and COUNT over each, against hand-written
answers, over novelty and the index, with fast paths on and off, in
SPARQL and (LANG) JSON-LD.
The COUNT fast path for `FILTER(LANG(?o) = "tag")` looked the tag up in
the index's language dictionary and counted the objects of that language
type. `""` is no tag, so the lookup failed and the count was 0, though
`LANG(?v) = ""` holds for every literal without one. The plain SELECT was
right: the scan's encoded pre-filter already left `""` as a row filter.

The scan and both COUNT fast paths each matched `LANG(?o) = <string>` on
their own. They now share one matcher, which yields only a non-empty tag,
so none of them can read `""` as a tag.
…ion and the store's id

On the index lane a filter over one variable remembers its verdict per
encoded value, keyed by the store, the graph, a hash of the filter and
the value. Two parts of that key were short:

- The hash took only which function each call was, not its fields. SPARQL
  lowers LANG and DATATYPE to the strict form (an IRI is a type error)
  and JSON-LD to the lenient one (an IRI's LANG is ""), so on one ledger
  whichever surface ran first answered for the other: after JSON-LD's
  `(= (lang ?v) "")`, SPARQL's `LANG(?v) = ""` kept an IRI row, and after
  SPARQL's `LANG(?v) != "en"`, JSON-LD's `(!= (lang ?v) "en")` dropped one.
  `Function` now derives `Hash`, agreeing with its `Eq`, and the filter
  hash takes the whole function.
- The store part was the store's address. A store built after another
  was dropped can sit at the same address and read the dropped one's
  verdicts for a different dictionary. It is now the store's instance id,
  as the overlay dirty-id cache already uses.

The operator matrix now runs both surfaces on one ledger in both orders,
with a LANG = "" pair and a DATATYPE pair, and a new test rebuilds a
store over and over with the meaning of one encoded value flipping.
A verdict was keyed by the encoded value's dictionary ids under the
store's id. A string or subject id above the persisted watermarks is
resolved from the view's dictionary novelty instead, and a layered view
(a staged preview, a SHACL or post-state policy check) gives its new
terms ids of its own from the same frontier. Two previews over one store
staging "xyz" and "abc" give both strings the same id; the join's probe
keeps them encoded, so the second preview's REGEX(?v, "b") read the
first one's false and dropped its row. Predicate ids past the store's
count are likewise allocated per scan.

The cache now takes a verdict only when every id in the key is one the
decoder would read from the store: string and subject ids up to the
watermarks, predicate ids below the store's count. Anything else is
evaluated each time.
@aaj3f aaj3f added bug Something isn't working as expected area:query Query execution, planning, fast paths, overlay, result formatting labels Oct 30, 2026
@aaj3f
aaj3f requested a review from bplatz October 30, 2026 17:36
@bplatz

bplatz commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Novelty rows the index can't encode skip the filters the scan compiled

This isn't introduced here; this part of binary_scan.rs is the same on main. But it breaks the promise in this PR's title, that a filter over an indexed ledger answers as it does over novelty, and the "rest of the class" table doesn't cover it.

Once a ledger is indexed, the scan can't encode a novelty row whose language tag or custom datatype isn't in the index's dictionaries yet. It emits that row separately, as a decoded flake. Those rows never meet the encoded pre-filters, and the original filters have already been removed from the row filters, so nothing checks them.

Repro

Load the test's DATA fixture, reindex, then insert:

@prefix : <http://example.org/#> .
:z :v "neu"@de .
:y :v "q"^^:newdt .

Query:

PREFIX : <http://example.org/#>
SELECT ?s WHERE { ?s :v ?v FILTER(LANG(?v) = "en") }
Stage ?s Expected
Everything in novelty, before indexing :a :a
Indexed :a :a
Indexed, plus the two rows above :a, :y, :z :a

The other compiled filters do the same at the last stage:

Filter ?s Expected
ISBLANK(?v) :y, :z none
?s = ?v :y, :z none

All three behave the same with fast paths on and off, and COUNT(*) with the same filter counts the same rows (3 for the LANG filter).

Cause

  • open compiles the filters into encoded pre-filters and removes them from inline_ops.
  • Novelty flakes that can't be translated are kept as raw flakes in range_iter. A tag or datatype that only novelty has is Unsupported there by design (L3766-L3781).
  • flakes_to_bindings, which emits those rows, never runs encoded_pre_filters. Only the encoded lane runs them (L1453).

Suggested fix

  • Keep the compiled filters' original InlineOperators and run them as row filters in flakes_to_bindings, so the raw lane checks what the encoded lane checks.
  • Add a phase to it_filter_pushdown_operators that puts novelty over the index, including a new tag and a new datatype, so the matrix covers this lane.

/// The regex is unanchored: an anchored one becomes a prefix range on the
/// index and never reaches the remembered verdicts.
#[tokio::test(flavor = "current_thread")]
async fn a_rebuilt_store_does_not_answer_for_a_dropped_one() {

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.

This test fails against the bug only when the allocator happens to put the new store at the dropped one's address. In the revert runs in the PR body, one run had just 1 stale round of 32, so on another allocator (CI's Linux runners, say) it could pass with the bug in place. It's worth keeping, but it shouldn't be the only test behind the store_id change. A deterministic one would check that the cache key carries store.store_id(), for instance by factoring the key into a function a unit test can call.

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

Approving, but there's feedback to go through before this merges:

… scan compiled

Once a ledger is indexed, a novelty row whose language tag or datatype
the index's dictionaries do not hold leaves the scan decoded. The scan
compiles `LANG(?o) = "tag"`, `ISBLANK(?o)` and `?s = ?o` into checks on
the encoded object and drops them from its row filters, so those rows
were checked by neither and passed all three, in SELECT and COUNT alike.
The scan now keeps the filters it compiled, when such rows exist, and
evaluates them on each decoded row.

The verdict cache's key is built by one function, with a unit test that
it carries the store's instance id: the rebuilt-store test only fails
when the allocator reuses the dropped store's address.
@bplatz
bplatz merged commit be90425 into main Nov 12, 2026
19 checks passed
@bplatz
bplatz deleted the fix/lang-pushdown-operator branch November 12, 2026 03:21
bplatz added a commit that referenced this pull request Nov 12, 2026
Brings in #2157, #2226, #2297, #2314 and #2331.

Conflicts:

- eval/helpers.rs (#2226): the filter verdict cache key takes main's store
  instance id and next's u32 graph id, and main's test builds its stores
  with next's `BinaryIndexStore::empty()`.
- grp_transact.rs lists both sides' test modules.
- #2157 changed `retract` and `drop_branch` on every nameservice backend,
  and the nested-ledger prefixes a sweep excludes. next replaced those
  operations with the fenced record store, so the nameservice, sweep and
  drop-test hunks take next's side here. The next commit carries the fix
  to next's operations.
aaj3f added a commit that referenced this pull request Nov 13, 2026
Brings in main at 467f0da, with #2271's LaneOutput admission, #2251's
graph drop and #2226's language-tag pushdown.

- cyclic_bgp.rs: the per-member subject probe plan takes
  LaneOutput::Encoded, since the cascade emits encoded bindings.
- join.rs: the batched gate takes main's deferred overlay plan and eager
  rule, and keeps this branch's one-graph scope test. The union probes'
  per-member plans take LaneOutput::Encoded: the union variant emits
  encoded rows and declines eager materialization. The one-graph object
  probe takes main's branch_or_none, Arc'd ops and eager subject decoding;
  the subject probe takes main's subject_lane_object_binding.
- join.rs: main's flush now hands a declined plan's rows to the per-row
  path. The one-graph plan declines every union scope, so that check skips
  the union variant, which carries its members' own plans.
- join.rs: open_per_row_scan takes the scope's TermView, which this
  branch threads through the per-row path.
- registers: main greened the optional-complex cluster, this branch
  open-eq-05/06; both leave the register.
- tests: graph_id_for_iri is assigned_graph_id on main.
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.

2 participants