Repository navigation
fix(query): filters on the index lane answer as they do over novelty - #2226
Conversation
…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.
|
Novelty rows the index can't encode skip the filters the scan compiled This isn't introduced here; this part of 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. ReproLoad the test's @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") }
The other compiled filters do the same at the last stage:
All three behave the same with fast paths on and off, and Cause
Suggested fix
|
| /// 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() { |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Approving, but there's feedback to go through before this merges:
- Novelty rows the index can't encode skip the filters the scan compiled. Once a ledger is indexed, a new row with a language tag or datatype the index doesn't have yet passes
LANG(?v) = "en",ISBLANK(?v)and?s = ?v. The repro and a suggested fix are in the comment. It's not introduced here, but it's the promise in this PR's title, so it would be good to fix it in this PR. - The rebuilt-store test can pass against the bug, because it depends on the allocator reusing an address.
… 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.
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.
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.
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 theenrows, the exact complement of its answer, andlangMatches(LANG(?v), "en")andSTRSTARTS(LANG(?v), "en")droppeden-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 forLANG(?o) = "en", which it turns into a check on the object's language type. But it matched the shape, a two-argument call withLANG(?o)on one side and a string on the other, without checking which function it was. So!=,<,>,langMatches,STRSTARTSand anything else overLANG(?o)and a tag all became "the tag equalsen". 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 ofLANG(?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 forLANG(?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, thoughLANG(?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_CACHEineval/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:LANGandDATATYPEto the strict form (an IRI is a type error) and JSON-LD to the lenient one (an IRI'slangis"", decision D-12). On one ledger, whichever surface ran first answered for the other. After JSON-LD's(= (lang ?v) ""), SPARQL'sLANG(?v) = ""kept an IRI row, and after SPARQL'sLANG(?v) != "en", JSON-LD's(!= (lang ?v) "en")dropped one.Functionnow derivesHash, agreeing with itsEq, 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.BinaryIndexStore::store_id), which the overlay dirty-id cache infrontier.rsalready uses for the same reason. This showed up in the test itself: when the SPARQL and JSON-LD runs used separateFlureeinstances 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."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 previewREGEX(?v, "b")read the first one'sfalseand 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.
LANG(?o) = "tag"→ language-type checkbinary_scan.rslang_tag_equalityLANG(?o) = "tag"operator_tree.rs(detect_predicate_count_rows_lang_filter)""operator_tree.rs(detect_count_rows_with_encoded_filters)""fell back correctly, now declined up frontSTRSTARTS(?o / STR(?o), "p"), anchoredREGEX→ string-prefix rangesbinary_scan.rs(extract_object_string_prefix)ISBLANK(?o)→ blank-node checkbinary_scan.rs?s = ?o/?s != ?o→ subject/object ref checkbinary_scan.rsEq/Ne)ISBLANK/ISLITERALoperator_tree.rs?o op constrange boundsplanner.rs(extract_range_constraints)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 bystore_id.Tests
it_filter_pushdown_operators(ingrp_query) runs seventeen SPARQL filters and seven JSON-LD ones against hand-written answers: LANG, DATATYPE, isIRI/isLiteral and STR under=,!=,<,>,langMatchesandSTRSTARTS, 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 pairsLANG(?v) = ""/(= (lang ?v) "")andSTR(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_onebuilds and drops an indexed ledger 32 times, its only string alternating between"abc"and"xyz"at the same dictionary position, under an unanchoredREGEX(?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_stringsstages 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 thatFunction's hash agrees with itsEqfor the strict and lenientLangandDatatype, 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:
=LANG filters over the index.LANG(?v) = ""case fails, 0 where 4 are expected."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
u64id replaces a pointer. The filter hash is computed once per prepared filter, and again per row for filters nested under&&,||,!orIF, so I measured it directly, as retired instructions per call (best of 7 × 1M, release, macOSproc_pid_rusage):LANG(?v) = ""REGEX(STR(?v), "…")Hashing the mode as its own
boolwrite 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 underLANG(?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%).