Repository navigation
fix: SPARQL UPDATE commits referencing fresh namespaces aren't queryable post-publish - #1217
Merged
Merged
Conversation
aaj3f
approved these changes
May 6, 2026
aaj3f
left a comment
Contributor
There was a problem hiding this comment.
Looks good to me. One minor note but not really something needing to be fixed. I also thought for a moment about if the retry loop on NamespaceConflict could be perf-tuned by hoisting out any expensive work that isn't retry-relevant, but it's such a rare path it doesn't seem worth it.
Comment on lines
+251
to
+258
| // Hand off the lowering registry's allocations so `stage_transaction_from_txn` | ||
| // can merge them into its own snapshot-derived registry. Without this, the | ||
| // first SPARQL `INSERT DATA` on a fresh ledger commits flakes referencing | ||
| // namespace codes the staging registry never learned about — the commit's | ||
| // persisted namespace map omits them, and post-commit SELECT can't resolve | ||
| // the predicate IRI back to the same Sid. | ||
| txn.namespace_delta = ns.delta().clone(); | ||
| Ok(txn) |
Contributor
There was a problem hiding this comment.
Not really a recommendation, just thinking it could be easy to regress the work these final two lines do if a future code didn't realize how load-bearing this is and did something like directly return Option<Txn> from one of the lower_* fns above, where namespace_delta would still be empty.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SPARQL
INSERT DATA(or anylower_sparql_update-derivedTxn) committed against a fresh ledger leaves committed flakes whose namespace_codes the post-commit snapshot can't resolve back to IRIs. A subsequentSELECTreturns no rows — even though the flakes are physically in storage.In production this is normally masked: JSON-LD inserts pre-allocate the namespaces before any SPARQL touches them, so the staging registry happens to land on the same codes. The bug surfaces when SPARQL is the first commit allocating a given namespace.
Root cause
lower_sparql_updateallocates IRIs against a caller-ownedNamespaceRegistryto build the templates'Sids (e.g.ex:doc1→Sid{13, "doc1"}).stage_transaction_from_txnthen builds an independent registry from the ledger snapshot, never sees those allocations, and commits flakes carrying namespace_code 13. The staging registry's persistence delta is empty forhttp://example.org/, so the commit envelope omits the(13, "http://example.org/")mapping. On reload, the snapshot's namespace map doesn't contain code 13, andsid_for_iri("http://example.org/...")re-allocates fresh — predicate Sid mismatch, no rows.Verified with diagnostic prints comparing SPARQL vs. JSON-LD commit state on a fresh ledger:
Fix
A two-step handoff so allocations made during lowering survive into the commit's persisted namespace map.
fluree-db-transact/src/ir.rs—Txngains anamespace_delta: HashMap<u16, String>field, documented as the lowering→staging contract. JSON-LD producers (parse_transaction) share the staging registry directly and leave it empty.fluree-db-transact/src/lower_sparql_update.rs—lower_sparql_updatecapturesns.delta().clone()into the resultingTxn.namespace_deltaafter the inner lowering call. All four inner Txn literals (InsertData,DeleteData,DeleteWhere,Modify) initialize the field.fluree-db-core/src/ns_encoding.rs— newNamespaceCodes::adopt_delta_for_persistenceadds entries to both the lookup tables AND the persistence delta. Distinct from the existingmerge_delta, which only updates lookup tables (it's used for post-publish sync from the shared allocator and intentionally skips delta to avoid re-persisting). Same conflict-checking semantics asmerge_delta.fluree-db-transact/src/namespace.rs—NamespaceRegistry::adopt_delta_for_persistencethin wrapper.fluree-db-api/src/tx.rs—stage_transaction_from_txncallsadopt_delta_for_persistence(&txn.namespace_delta)immediately after building the staging registry. On conflict, surfaces asApiError::Internal(would indicate the caller staged against a stale snapshot).Tests
fluree-db-api/tests/it_vector_corruption_repro.rs— un-ignoressparql_insert_data_embedding_vector_literal_round_trips_after_indexing; tightens it to verify all 4 vector element values, not just length. Updates the module comment to reflect that only one deferred concern remains (vector retraction). The test exercises the fix on a vector typed literal, but the same fix applies to all SPARQL-lowered Txns regardless of value type.Pre-fix:
INSERT DATA { ex:doc1 ex:embedding "[0.1, 0.2, 0.3, 0.4]"^^f:embeddingVector }on a fresh ledger → published commit has the flake → SELECT returns[].Post-fix: same flow → SELECT returns
[[[0.1, 0.2, 0.3, 0.4]]].The diagnostic baseline I built during investigation (showing the same bug for a plain string
INSERT DATA { ex:doc1 ex:name "Alice" }on a fresh ledger) is not committed — the un-ignored vector test exercises the same code path and is sufficient regression coverage.Notes for reviewers
merge_deltavs.adopt_delta_for_persistencesplit is load-bearing.merge_deltais called post-publish to sync the serial registry with what the shared allocator already persisted — adding to delta there would double-persist on the next commit. The new method is for the inverse case: adopting allocations that haven't been persisted yet.sparql_update_supports_subquery_aggregate_and_bindinfluree-db-server/tests/integration.rs) didn't catch this because they always seed via JSON-LD INSERT first, pre-allocating the relevant namespaces.