Skip to content

fix(query): resolve overflow subject IRIs from novelty - #1829

Merged
bplatz merged 5 commits into
mainfrom
fix/novelty-overflow-subject-iri
Sep 15, 2026
Merged

bplatz merged 5 commits into
mainfrom
fix/novelty-overflow-subject-iri

Conversation

@bplatz

@bplatz bplatz commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

An encoded subject whose name already contains its full IRI can fail result formatting while it remains in dictionary novelty. The 100M BSBM Update replay failed on Explore Q5 with no namespace prefix for code=65534, then recovered after indexing caught up. Every novelty subject resolver that reaches the namespace table directly incorrectly required a prefix for OVERFLOW; persisted decoding already handles it correctly. EMPTY has the same full-IRI representation and the same defect.

Where it fails

SPARQL JSON materializes bindings through DictOverlay, which already handled both codes. The failing paths resolve an encoded subject id directly:

  • XML (format/sparql_xml.rs) and CSV/TSV (format/delimited.rs) call BinaryGraphView::resolve_subject_iri for Binding::EncodedSid. Most query shapes materialize Binding::Sid before formatting and never reach it. A join shaped like Explore Q5 (shared reference object, numeric range filter, DISTINCT, ORDER BY … LIMIT) keeps the subject encoded.
  • Export (export.rs ExportResolver) guarded on 0xFFFF instead of 0xFFFE, so the guard never fired, and it had no EMPTY case.

Changes

  • Add namespaces::is_full_iri and route every subject decoder that special-cases EMPTY/OVERFLOW through it: both BinaryGraphView novelty paths, DictOverlay's three copies (via BinaryGraphView::subject_iri_from_parts), export, persisted decoding, LedgerSnapshot/NamespaceCodes decoding, the formatter's IriCompactor, and SHACL. Ordinary namespaces keep their prefix lookup, including snapshot namespace fallback and missing-prefix errors.
  • Return the stored name from STR() / xsd:string() for EMPTY and OVERFLOW subjects. The prefix lookup found no entry for OVERFLOW and rendered the internal "65534:<iri>" form, so a filter comparing against the IRI matched nothing. This affected both novelty and persisted overflow subjects.
  • Fix the vocab doc comment that stated OVERFLOW is 0xFFFF.
  • Document that core's private NS_OVERFLOW = 0xFFFF special case in DictNovelty/NsVecBiDict never fires. Real overflow subjects take the ordinary per-namespace path, as they do in the indexer, so this is not a correctness issue. The code is unchanged; see DictNovelty overflow special case keyed on 0xFFFF never fires #1843.

Validation

  • End to end: exhaust the namespace table, index, commit a novelty overflow subject, run the Q5-shaped join, and export. The test asserts the subject reaches the formatters as EncodedSid, then checks XML, CSV, TSV, and N-Triples export. Reverting the graph-view fix fails the query formatting with no namespace prefix for code=65534; reverting the export fix fails the export with the same error. The same test checks STR(), xsd:string(), and a JSON-LD (str ?s) filter over a novelty and a persisted overflow subject; each returns no rows without the fix.
  • Unit coverage exercises both novelty resolution paths, persisted decoding parity, ordinary namespaces, blank nodes, snapshot namespace fallback, and missing namespace/subject errors. Formatter coverage checks XML escaping, SPARQL JSON DOM and streaming output, CSV, and TSV for both full-IRI namespace codes.
  • Library tests pass for vocab, core, binary-index, query, SHACL, and API; the API SPARQL group and export tests pass. Clippy with warnings denied and formatting pass for the touched crates.

The original 400-mix Update replay has not been rerun with this patch. The end-to-end test reproduces the reported error on the Q5 shape, but a pristine full replay remains necessary before restoring that workload to published performance results.

Partially addresses #1842: the STR() part is resolved here. Still open: a constant-IRI lookup (<iri> <p> ?o, VALUES) misses an overflow subject in novelty, because query-side encoding produces Sid(EMPTY, iri). Pre-existing.
Follow-up: #1843 (core NS_OVERFLOW special case keyed on 0xFFFF)

@bplatz bplatz added bug Something isn't working as expected area:query Query execution, planning, fast paths, overlay, result formatting labels Sep 13, 2026
@bplatz
bplatz requested review from aaj3f and zonotope September 13, 2026 00:27

@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 so the problem and the fix make sense on their face-value, but I did have some trouble getting Claude evaluation attempts to actually hit the problem. The review below goes into more detail, but it may be worthwhile to actually try to trace the execution from the Explore Q5 you mention down into the code paths this PR improves. There are some other items below re: consolidation abstraction and additional paths that could benefit from the same treatment (e.g. export):


The diagnosis here is spot on: EMPTY and OVERFLOW store the full IRI as the SID name, the persisted decoder has always known that, and the two novelty paths didn't — so consolidating them onto one helper that mirrors resolve_subject_iri's persisted branch is exactly the right move. I especially like the negative test; it's what keeps this from being a blanket bypass, and when I mutated the OVERFLOW case out, your matrix test caught it immediately. What's holding this up is that the family is bigger than the body says. There's a third novelty subject-IRI resolver in fluree-db-api/src/export.rs:159-180, and its guard is dead code — it tests 0xFFFF when the overflow code is 0xFFFE, so it never fires and the export path still emits no namespace prefix for code=65534; it also has no EMPTY case. The likely source is the doc comment at fluree-vocab/src/lib.rs:1715, which says 0xFFFF while the constant below it says 0xFFFE, and the same wrong value has been copied into ns_vec_bi_dict.rs:22 and dict_novelty.rs:48. Those are a few lines and they belong with this fix rather than behind a PR that reads as if the family is now handled.

One thing worth flagging separately: I couldn't reproduce the failure end to end. With a real 65,600-namespace overflow subject in novelty, queried across four result formats in subject and object position, output was identical on this head, on the parent, and on a build with your fix removed — those queries go through DictOverlay, which already handled both codes. So the fix is correct but I can't independently confirm the route from Explore Q5 to it; naming that caller chain would help the next reviewer a lot. For what it's worth on our side: solo's main pins this crate at b61a9a6fc, the pre-fix code is present there verbatim, but I don't see a realistic path to it on a solo stack — and if there were one, materializer.rs:626 swallows the error into a fabricated _:unknown_<id>, so it would be a silent wrong IRI rather than a 500.

Adherence to repo commitments:

  • Patterns/abstractions: ⚠️ the helper is the right shape and mirrors the persisted decoder, but it leaves five copies of the same rule and one wrong one; namespaces::is_full_iri would fix that properly.
  • Performance (speed first, memory second): ✔ neutral — two u16 comparisons, and for the two special codes it skips a HashMap lookup and a format! that used to run.
  • Testing: ✔ a [EMPTY, OVERFLOW] × [initialized, uninitialized] matrix with a persisted-parity assertion, a negative test preserving prefix lookups and error messages, and formatter coverage wired at grp_query_sparql.rs:54-55. A mutation on the OVERFLOW case goes red.
  • Conventions: ✔ multi-line conventional commit, no AI attribution, cargo fmt --all -- --check and cargo clippy -p fluree-db-binary-index --all-targets -- -D warnings both clean.

Verified locally at branch HEAD 64be83e86: 2 binary-index novelty tests pass; dropping || ns_code == namespaces::OVERFLOW at :2916 fails novelty_full_iri_namespaces_resolve_before_and_after_indexing at :4346; the end-to-end overflow probe is identical on head, parent, and mutated head; git merge-tree --write-tree origin/main 64be83e86 is conflict-free and the production hunk applies cleanly to origin/main on its own.

Just be sure to get the export.rs copy and the 0xFFFF doc comment in before this merges — they're a handful of lines, and it'd be a shame to land the fix for this family while leaving the one resolver whose guard is silently dead.


Notes with no line in this diff to anchor to:

fluree-vocab/src/lib.rs:1715 — blocking (one line), not in this diff; anchor at binary_index_store.rs:2915. The doc comment says "Code OVERFLOW (0xFFFF)" and the constant fifty-five lines later is 0xFFFE. Claude caught this: that stale comment is almost certainly where the export.rs bug came from, and the same 0xFFFF appears as a private NS_OVERFLOW in fluree-db-core/src/ns_vec_bi_dict.rs:22 and fluree-db-core/src/dict_novelty.rs:48. Fixing the comment costs nothing and stops the next copy.


/// Match persisted subject decoding: EMPTY and OVERFLOW names already
/// contain the full IRI and need no namespace-table entry.
fn subject_iri_from_parts(&self, ns_code: u16, suffix: &str) -> io::Result<String> {

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.

fluree-db-binary-index/src/read/binary_index_store.rs:2915 — blocking, and the real location is fluree-db-api/src/export.rs:159-180, which isn't in this diff. There's a third novelty subject-IRI resolver with the same defect, and its guard is dead code. export.rs:167 reads if ns_code == 0xFFFF, but the overflow namespace code is 0xFFFE (fluree-vocab/src/lib.rs:1770), so that branch never fires and the export path still falls through to namespace_prefix(0xFFFE) and produces the exact no namespace prefix for code=65534 this PR is fixing. It has no EMPTY case at all. The PR body says "Both novelty resolution paths" — there are three, and the third is still broken. I'd fold the fix in here rather than leave a known-bad copy behind a PR that claims the family is handled.

// fluree-db-api/src/export.rs
if let Some((ns_code, suffix)) = dn.subjects.resolve_subject(s_id) {
    // EMPTY and OVERFLOW store the full IRI as the name; no prefix lookup.
    if ns_code == namespaces::EMPTY || ns_code == namespaces::OVERFLOW {
        return Ok(suffix.to_string());
    }
    let prefix = self.store.namespace_prefix(ns_code)?;
    return Ok(format!("{prefix}{suffix}"));
}

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 c7ee609

/// Match persisted subject decoding: EMPTY and OVERFLOW names already
/// contain the full IRI and need no namespace-table entry.
fn subject_iri_from_parts(&self, ns_code: u16, suffix: &str) -> io::Result<String> {
if ns_code == namespaces::EMPTY || ns_code == namespaces::OVERFLOW {

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.

fluree-db-binary-index/src/read/binary_index_store.rs:2916 — optional, but I'd like it in this PR. After this change the EMPTY || OVERFLOW test exists in five places — :1628, this helper, and three separate copies in fluree-db-query/src/dict_overlay.rs at :225, :245 and :256 — plus the sixth site that got the constant wrong. That's a rule that wants to live on the constant. A namespaces::is_full_iri(ns_code) -> bool in fluree-vocab, with every site calling it, would make the next resolver impossible to get wrong and is a strictly smaller diff than the one you already wrote. Minor and non-blocking — but if you agree it's right, I'd rather see it in this PR than lost in the backlog.

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 c7ee609. namespaces::is_full_iri now backs every decoder that special-cased the pair, which was more than five: format/iri.rs (3), LedgerSnapshot::decode_sid, NamespaceCodes::decode_sid_strict, and SHACL's decode_sid_with_ns_map also had it. DictOverlay's three copies now call BinaryGraphView::subject_iri_from_parts.

On the core NS_OVERFLOW = 0xFFFF copies: they're live code, not just comments. Real overflow subjects (0xFFFE) take the ordinary per-namespace path in both novelty and the indexer, so the two agree. Flipping the constant would change allocation and retirement for overflow subjects, so I corrected the comments here and filed #1843 for the code.

Some((ns_code, suffix)) => self
.namespace_prefix(ns_code)
.map(|prefix| format!("{prefix}{suffix}")),
Some((ns_code, suffix)) => self.subject_iri_from_parts(ns_code, suffix),

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.

fluree-db-binary-index/src/read/binary_index_store.rs:2810 — question, not a suggestion. I couldn't reproduce the reported failure end to end, and I'd like to understand why before I trust my own read of the blast radius. I built a fixture that actually exhausts the u16 namespace table — 65,600 distinct namespaces, so a real Sid::new(OVERFLOW, iri) lands in dictionary novelty — plus a bare @id for EMPTY, and queried both pre-indexing across SPARQL JSON, XML, CSV and TSV in subject and object position. Byte-identical output on this head and on parent 3ce528455. Then I deleted the OVERFLOW half of your fix and reran: still correct. Those queries route through DictOverlay::resolve_subject_iri, which already handled both codes at dict_overlay.rs:225/245/256, so they never reach this function. Could you name the caller chain from Explore Q5 down to here — I'm guessing materializer.rs:626 or one of the join.rs sites — either in the body or as a comment? It matters for judging whether other callers of BinaryGraphView need the same audit.

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 c7ee609. Your probe went through DictOverlay because most shapes materialize Binding::Sid before formatting. The failing chain is Binding::EncodedSid → XML write_encoded_ref (format/sparql_xml.rs) or format/delimited.rs → BinaryGraphView::resolve_subject_iri → the novelty fast path. SPARQL JSON never takes it.

A Q5-shaped join (shared reference object, numeric range filter, DISTINCT, ORDER BY … LIMIT) keeps the subject encoded. The new overflow_subject_in_novelty_formats_and_exports test asserts the binding is EncodedSid. With the OVERFLOW case removed it fails with no namespace prefix for code=65534 in XML and CSV; with the export fix reverted, export fails the same way. The PR body now names the chain.

Some((ns_code, suffix)) => self
.namespace_prefix(ns_code)
.map(|prefix| format!("{prefix}{suffix}")),
Some((ns_code, suffix)) => self.subject_iri_from_parts(ns_code, suffix),

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.

fluree-db-binary-index/src/read/binary_index_store.rs:2810 — optional, pre-existing, separate issue. In the same probe, FILTER(STR(?s) = "http://ns65599.example/s") over a novelty overflow subject returns zero rows while the unfiltered query renders exactly that IRI. The EMPTY equivalent works fine. Identical on head and parent, so this is nothing you introduced and nothing this PR should try to carry — but it's the same overflow family and it's a silent-wrong-answer, so it's worth an issue while the context is fresh.

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.

Filed #1842. It's broader than novelty: in the same fixture, the STR filter also misses persisted overflow subjects, and a bound-IRI lookup (<iri> <p> ?o) misses the novelty one.

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 the STR() part in cb5d784. The "65534:<iri>" fallback also broke xsd:string(?s) and JSON-LD (str ?s); all three are now tested. #1842 now tracks only the constant-IRI lookup, which has a different cause.


#[test]
fn novelty_subject_iri_preserves_prefix_lookup_and_missing_namespace_errors() {
use fluree_db_core::dict_novelty::DictNovelty;

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.

fluree-db-binary-index/src/read/binary_index_store.rs:4375 — praise. The negative test is the half that makes the fix safe rather than a blanket bypass: ordinary namespaces still take the prefix path, blank nodes still resolve, the snapshot-namespace fallback still works, and a missing code still errors with the original message. Easy to skip when you're fixing a "just return the name" bug, and you didn't.

let _ = std::fs::remove_dir_all(cache_dir);
}

#[test]

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.

fluree-db-binary-index/src/read/binary_index_store.rs:4323 — praise. Looping [EMPTY, OVERFLOW] × [initialized, uninitialized] covers both resolution paths and both codes in one matrix, and then persisting the same entry and asserting the persisted decode matches is the parity assertion that actually pins the invariant. I mutated the OVERFLOW case out and this test caught it.

Base automatically changed from perf/subquery-bounded-streaming to main September 15, 2026 02:15
Export's novelty fallback compared against 0xFFFF, but the overflow
namespace is 0xFFFE, so exporting a novelty overflow subject failed with
"no namespace prefix for code=65534". It also had no EMPTY case.

Add namespaces::is_full_iri and route every subject decoder that
special-cases EMPTY/OVERFLOW through it, including DictOverlay's three
copies via BinaryGraphView::subject_iri_from_parts. Correct the vocab doc
comment that stated 0xFFFF, and document that core's private NS_OVERFLOW
special case never fires (#1843).

Add an end-to-end test that exhausts the namespace table, indexes, and
commits a novelty overflow subject: a Q5-shaped join keeps it encoded to
the XML/CSV/TSV formatters, and export emits it. Reverting either the
graph-view or the export fix fails the test with the reported error.
STR() and xsd:string() expand a SID by looking up its namespace prefix.
OVERFLOW has no prefix entry, so the fallback rendered the internal
"65534:<iri>" form and filters comparing against the IRI silently matched
nothing, for novelty and persisted overflow subjects alike. Return the
stored name for EMPTY and OVERFLOW via namespaces::is_full_iri.

Extend the end-to-end overflow test with STR(), xsd:string() and a
JSON-LD (str ?s) twin over a novelty and a persisted overflow subject;
each returns no rows without the fix.
@bplatz
bplatz merged commit 94bdf8f into main Sep 15, 2026
23 of 24 checks passed
@bplatz
bplatz deleted the fix/novelty-overflow-subject-iri branch September 15, 2026 03:01
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