Skip to content

fix: Fluree re-inserts its own export of SPARQL UPDATE blank nodes - #2156

Merged
bplatz merged 4 commits into
mainfrom
fix/export-roundtrip
Oct 23, 2026
Merged

bplatz merged 4 commits into
mainfrom
fix/export-roundtrip

Conversation

@bplatz

@bplatz bplatz commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

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:

Written Stored before Stored now
[ 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
a template [] _:fdb-…-0-[]0 _:fdb-…-0--anon0
`{ … }without~`

The minted labels now start with -, as the Turtle write path's anonymous nodes already do (-b{N}). A written BLANK_NODE_LABEL can'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: _:x followed by the label's hex. The shared rule moves into fluree_graph_ir::syntax::escape_blank_label, which rdf_text and export both call; its output is unchanged.

This answers #1956's round-trip question:

  • A valid label (every label minted now) is written as stored, and re-inserting the export addresses the same node.
  • An escaped label re-inserts as a new blank node with the same properties and links. Its old id isn't kept.
  • JSON-LD export writes every label as stored, so it keeps every id.
  • fluree create --from gives every blank node a new id whatever its label, as before.

Tests

  • Round trip: a SPARQL UPDATE mints every kind of node, from novelty and from the index. Its Turtle and N-Triples exports are re-inserted into fresh ledgers, and each re-exports to the same N-Triples. The test fails with the old minting.
  • Escaped labels: a stored #bnpl label exports escaped, parses, and re-inserts with its link intact. The test fails with the verbatim writer.
  • Unit tests: the escape, both export writers, and the parser test that pins the minted labels out of the user's label space.

Export writing list values in list order is separate and will come in its own PR.

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.
@bplatz bplatz added bug Something isn't working as expected area:sparql SPARQL/Turtle/TriG/JSON-LD parsing, lowering, UPDATE semantics, W3C conformance labels Oct 21, 2026
@bplatz
bplatz requested a review from a team October 21, 2026 21:33

@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 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/#reif in 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-op Transaction pins 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 _:-bnpl0 variables (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 in fluree-db-core/src/skolem.rs:47-61), and an escaped one starts with x.
  • 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_label instead 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_label check per blank-node term, and the minting is the same format! 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_transact round-trip tests, all six present by name in this head's nextest run, with testsuite-sparql green. I reverted the export writer to verbatim and a_stored_label_turtle_rejects_exports_escaped went red; I put #bnpl back in the parser and sparql_update_blank_nodes_export_and_reinsert_as_the_same_nodes went red.
  • Conventions: ✔ multi-line commit bodies; docs/cli/export.md and docs/query/output-formats.md updated.

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"] {

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.

🟡 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.

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 3be286c

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 `-`.
@bplatz
bplatz merged commit 66397e9 into main Oct 23, 2026
19 checks passed
@bplatz
bplatz deleted the fix/export-roundtrip branch October 23, 2026 22:18
@bplatz

bplatz commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

@aaj3f the single reserved-label helper from your headline note is in #2169. It covers the minters that already use the - spelling. JSON-LD's _:b{N} and _:fluree_ann_ minters are left out, since their spelling is part of upsert and sync identity.

bplatz added a commit that referenced this pull request Oct 24, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:sparql SPARQL/Turtle/TriG/JSON-LD parsing, lowering, UPDATE semantics, W3C conformance bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export writes blank-node labels that its own Turtle parser rejects

2 participants