Repository navigation
fix: Fluree re-inserts its own export of SPARQL UPDATE blank nodes - #2156
Conversation
The labels SPARQL UPDATE minted for `[ … ]`, `( … )`, an unnamed reifier, a template `[]` and an annotation without `~` used `#` or `[]` to stay out of the labels a user can write. Turtle doesn't allow either, so the `_:fdb-…` id each node is stored under couldn't be exported as Turtle or N-Triples, and Fluree couldn't re-insert its own export. They now start with `-`, as the Turtle write path's anonymous nodes already do: a written label can't start with `-`, and a stored id holds it mid-label, where Turtle allows it. An export of these nodes re-inserts as the same nodes.
Export wrote blank-node labels as stored. Labels earlier versions minted for a SPARQL UPDATE's `[ … ]` and annotations, and labels from imports by Fluree 4.1.4 or earlier, hold characters Turtle and N-Triples don't allow, so an export of such data didn't parse. Export now escapes such a label as every other RDF text writer does (`_:x` and its hex), through one `syntax::escape_blank_label`. The file parses, and the node re-inserts as a new blank node with the same properties. Valid labels, every label minted now included, are written as stored, and JSON-LD export is unchanged.
aaj3f
left a comment
There was a problem hiding this comment.
@bplatz this is nice, and at a design level no pushback from me. Minting with a leading - (the one spot a written label can't use, but a stored fdb-… id can hold mid-label) gets both halves at once, and moving export onto #1940's escape retires the last writer with its own blank-label policy. On #1956's open question, the PR's answer is: valid labels keep their id, legacy labels re-insert as new nodes, JSON-LD keeps every id, and --format ledger stays the lossless path, all written down in docs/cli/export.md. Since that settles what a Turtle or N-Triples backup of an older ledger can promise, is it the contract we want for text exports, or should legacy ids survive a text round trip too (which would need a reversible escape on insert)? Technically it's coherent as written; this is a question of what we commit to, not a blocker.
One headline note, not for this PR: keeping minted labels out of the user's label space has been decided minter by minter, and with different mechanisms each time:
unlexable and serializable
-b{N}in the Turtle sinks (e1a0a35); unlexable but unserializable_:[]{N}for DELETE WHERE variables (6762ed7, fine there since they're never stored) and for INSERT-template nodes (dd86e74, which this PR fixes);#bnpl/#coll/#reifin the SPARQL parser (also fixed here); a firewall on the writable_:fluree_ann_prefix for JSON-LD annotations; and a re-parse for JSON-LD's_:b{N}(acfbd4a), whose spelling upsert identity depends on
The - spelling this PR converges on is the one that satisfies both requirements (no one can write it, and fdb-{scope}-{label} still serializes), so perhaps one mint_reserved_label(kind, n) beside is_blank_node_label in fluree-graph-ir::syntax, with one test asserting both halves, would let the next minter inherit them instead of rediscovering them. The point fixes are right for this PR.
Claude-assisted review below:
✅ Approve with nits.
This is a tidy fix, @bplatz: two commits that each do one thing, and round-trip tests that exercise exactly the property #1956 asked for. The minting change keeps every synthetic label out of the user's space (a BLANK_NODE_LABEL can't start with - in SPARQL, Turtle or N-Triples) while the stored fdb-{txn}-{solution}--bnpl0 still serializes, and the export change escapes only what the grammar rejects, through the same escape_blank_label the CONSTRUCT writer uses, so the two can't drift again.
A few things I checked rather than assumed, since a label scheme change touches identity:
- Identity across versions holds. A SPARQL UPDATE's skolem scope is a fresh timestamp (
fluree-db-transact/src/stage.rs:727-731; the multi-opTransactionpins none for SPARQL,fluree-db-api/src/transaction.rs:92-99), a caller-pinned id must be unique per commit (fluree-db-transact/src/ir.rs:863-871), and the one content-derived scope (RDF-text upsert,fluree-db-transact/src/parse/rdf_text/mod.rs:126-132) uses the Turtle sink's-b{N}, which this PR doesn't touch. So nothing re-derives a SPARQL-minted label, and a ledger holding both#-era and-labels has no path where one node gets named both ways. - No other surface can put a
-…label into an update. Parameters only carry_:fdb-…ids (fluree-db-sparql/src/params.rs:276-290), and the query side still hides the_:-bnpl0variables (is_internal_var_name,fluree-db-query/src/var_registry.rs:40). - The escape can't collide with a label written as stored. Every stored label starts with
fdb-(the minters influree-db-core/src/skolem.rs:47-61), and an escaped one starts withx. - A nice side effect: a node SPARQL UPDATE minted is now addressable from SPARQL. I ran
INSERT DATA { _:fdb-…-0--bnpl0 ex:extra 1 }against one and it landed on the stored node, where the#spelling couldn't even be lexed. A Turtle export re-inserted into its own source ledger is also a no-op now.
The one inline note is optional: the SPARQL parser's new test pins the grammar, not what the parser mints.
Adherence to repo commitments:
- Patterns/abstractions: ✔ export moves onto the shared
syntax::escape_blank_labelinstead of keeping its own rule, and SPARQL moves onto the-reservation the Turtle sinks already use. - Performance (speed first, memory second): ✔ no hot path. Export adds an O(label)
is_blank_node_labelcheck per blank-node term, and the minting is the sameformat!with a different character. No performance-degradation risk. - Deployment targets: ✔ n/a — a label spelling and a text writer, nothing that reaches for the filesystem, threads or a clock; older versions read the new labels fine, since
-mid-label is legal to every lexer. - Testing: ✔ unit tests for the escape, both export writers and the lowering counter, plus two
grp_transactround-trip tests, all six present by name in this head's nextest run, withtestsuite-sparqlgreen. I reverted the export writer to verbatim anda_stored_label_turtle_rejects_exports_escapedwent red; I put#bnplback in the parser andsparql_update_blank_nodes_export_and_reinsert_as_the_same_nodeswent red. - Conventions: ✔ multi-line commit bodies;
docs/cli/export.mdanddocs/query/output-formats.mdupdated.
Verified locally at 77e5bc6ec: cargo test -p fluree-db-api --test grp_transact -- it_stable_blank_nodes (9 passed), plus the probes and mutations above.
Approving so you can merge when ready, but maybe worth folding in the one small test note first.
| if let QueryBody::Select(q) = &ast.body { | ||
| assert_eq!(bgp_triple_count(&q.where_clause.pattern), 3); | ||
| } | ||
| for synthetic in ["-bnpl0", "-coll0", "-reif0"] { |
There was a problem hiding this comment.
🟡 Optional: this pins the grammar half (no one can write _:-bnpl0), but not the half the parser owns, which is that what it mints starts with -.
I changed parse_blank_node_property_list_inner to mint bnpl{N} (no dash, so a user's _:bnpl0 in the same request would fuse with it) and cargo test -p fluree-db-sparql --lib stayed green, as did it_stable_blank_nodes, since the round trip is happy with any valid label.
The Turtle parser already pins its own mints this way (fluree-graph-turtle/src/parser.rs:3160-3166 asserts every minted label starts_with('-')). Something similar here, say parsing an update with a [ … ], a ( … ) and an anonymous << … >> and asserting every synthesized label starts with -, would hold all three spellings in place. Minor and non-blocking, but if you agree it's right I'd rather see it folded in now than lost in the backlog.
The existing test pins that no one can write `_:-bnpl0`. It passed with the parser minting `bnpl0`, which a user's `_:bnpl0` would fuse with. This one parses an update with a `[ … ]`, a `( … )` and a standalone `<< … >>`, and asserts every label minted for them starts with `-`.
Brings #2142, #2144, #2154, #2155, #2156, #2158, #2159, #2165, #2166 and #2167 into next. Where main's RDF lists (#2159) meet next's RDF 1.2 links: - The list view walks an EdgeAnnotation's body; next has no AnnotationTarget. - Export marks annotated edges skipping list rows, and writes an rdf:reifies row as next's @reifies block through main's JsonLdNode, untranslated rows included. - The JSON-LD and import readers keep next's graph argument and triple-term spool records beside main's gap-free list positions and list chunking. - One collection writer: main's shared module, which both JSON-LD and Turtle use, replaces next's Turtle-only one, and now also keeps the labels of blank nodes inside triple terms, of reifiers, and of blank graph names. A reified cell keeps its triples while the well-formed tail after it folds, as JSON-LD's list conversion does; next's writer test now says so. - Hiding list members also turns off next's wildcard lane, which reads leaflets directly, and next's independent-scan count builds its right scan with the same hiding. Elsewhere: - List-predicate discovery opens a leaf ranged unless the store cannot read ranges, which next fetches whole. - GC reports main's walk stop reason without next's removed artifact cache directory. - Tests from main await next's async genesis_ledger, reload with without_storage_lock, and build stores without a cache directory.
Fluree couldn't re-insert its own Turtle or N-Triples export when the data held blank nodes from a SPARQL UPDATE. Those nodes were stored under labels the Turtle grammar rejects, and export wrote labels as stored.
Fixes #1956
Minting (first commit)
SPARQL UPDATE minted labels with
#or[]so they could never meet a label a user writes. The stored_:fdb-…id kept those characters:[ ex:name "q" ]_:fdb-…-0-#bnpl0_:fdb-…-0--bnpl0( "a" "b" )cells_:fdb-…-0-#coll1_:fdb-…-0--coll1<< s p o >>with no reifier_:fdb-…-0-#reif0_:fdb-…-0--reif0[]_:fdb-…-0-[]0_:fdb-…-0--anon0without~`The minted labels now start with
-, as the Turtle write path's anonymous nodes already do (-b{N}). A writtenBLANK_NODE_LABELcan't start with-, so the labels stay out of the user's space, and the stored id holds it mid-label, where Turtle allows it. Such nodes now export, and re-insert as the same nodes.Labels already stored (second commit)
Data written by earlier versions still holds these labels, and imports by Fluree 4.1.4 or earlier hold labels with
/and:. Turtle, TriG, N-Triples and N-Quads export now escape a label the grammar rejects the way every other RDF text writer does since #1940:_:xfollowed by the label's hex. The shared rule moves intofluree_graph_ir::syntax::escape_blank_label, whichrdf_textand export both call; its output is unchanged.This answers #1956's round-trip question:
fluree create --fromgives every blank node a new id whatever its label, as before.Tests
#bnpllabel exports escaped, parses, and re-inserts with its link intact. The test fails with the verbatim writer.Export writing list values in list order is separate and will come in its own PR.