Skip to content

fix(export): write stored lists in their stored order - #2158

Merged
bplatz merged 3 commits into
mainfrom
fix/export-list-order
Oct 24, 2026
Merged

bplatz merged 3 commits into
mainfrom
fix/export-list-order

Conversation

@bplatz

@bplatz bplatz commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Export wrote a stored list as repeated values of its predicate, in index order rather than list order, so a JSON-LD @list or a Turtle ( … ) came back as a set: order, duplicates and list-ness lost. A JSON-LD query returned the same list in order.

A list is stored as values carrying a list index, and the SPOT scan reaches them in object order. Each writer now gathers a subject's list items while its rows stream past and writes each list, in index order, when the subject closes. Novelty rows that missed V3 translation join the same buffer, so a list split between them and the scan comes out whole.

Format Output
Turtle, TriG ex:s ex:items ( "c" "a" "b" ) .
JSON-LD "ex:items": {"@list": ["c", "a", "b"]}
N-Triples, N-Quads ex:s ex:items _:l0-0 ., then the rdf:first/rdf:rest spine, ending in rdf:nil

Turtle, TriG and JSON-LD insert back as the same list. N-Triples and N-Quads have no list syntax, so they write the spine a Turtle ( … ) denotes. Every format is then the same RDF graph, but Fluree stores an inserted spine as the triples it spells out, not as a list; the export docs say to copy lists through Turtle, TriG or JSON-LD. Spine cells are labelled l<graph id>-<n>, which no stored blank node is written as (they are fdb-…, or escaped x…), and the graph id keeps an N-Quads dataset's graphs from sharing cells.

Two smaller changes the list output needed:

  • List items take no annotation markers. The export looked bundles up with no list index, so a list item matched the bundles of a plain s p o edge with the same object, although the list index is part of the edge (EdgeKey::matches). The plain edge writes its own marker.
  • JSON-LD rdf:type values other than a bare IRI stay an rdf:type property. @type kept only the @id, which would have dropped a list there, and already dropped an annotated type's @annotation. Fluree's JSON-LD insert refused rdf:type as a key, so it now reads one whose values are node objects, or a @list of them, as it reads any other property. A literal value is still refused, since it would type the node with a string.

ExportStats::triples_written counts a list's items, not its spine triples, so the count is the same in every format.

Tests in it_export_lists.rs:

  • every format, from novelty, from the index and from novelty over an index, where one list mixes translated rows with an untranslated decimal. The fixture also holds an annotated type, a list of types and an annotated plain value. Each Turtle, TriG and JSON-LD export is re-inserted, and the copy's triples and lists match the source's;
  • a named-graph list round-trips through TriG, and N-Quads gives each graph its own spine cells;
  • a list and an annotated plain value on one predicate.

Partially addresses #1694: Turtle → Fluree → Turtle now keeps a collection. A stored list still cannot be queried through rdf:first/rdf:rest, which is the storage decision that issue tracks.

Follow-up: #2170 (an export round-trip test over every stored shape and format, from this PR's review).

Export wrote a list's items as repeated values of the predicate, in
object order, so a JSON-LD @list or a Turtle ( ... ) came back as a set.
Each writer now gathers a subject's list items, untranslated novelty
included, and writes each list in index order when the subject closes:
( ... ) in Turtle and TriG, {"@list": [...]} in JSON-LD, and the
rdf:first/rdf:rest spine in N-Triples and N-Quads, which have no list
syntax.

A list item no longer takes the annotation markers of the plain edge
with the same object, and a JSON-LD rdf:type value that is not a bare
IRI stays an rdf:type property instead of being dropped from @type.
@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 the per-subject buffer is the right altitude to me: SPOT order can't hand you list order without holding the list, and the JSON-LD writer already held a whole subject, so the Turtle and N-Triples writers now share a bound we'd already accepted. Writing the spine for N-Triples rather than refusing is a good call too.

One headline note, on the class the must below belongs to: this stack is about Fluree re-inserting its own export, and the rdf:type change is the third time export has written a shape Fluree's own insert refuses:

the pre-4.2 raw f:reifies* facts (e88b2e9); blank-node labels the Turtle reader rejects (#1956, fixed in #2156); and here, an annotated or list-valued rdf:type the JSON-LD reader refuses

Each was found after the fact and fixed with a point test. These two PRs' tests are already most of a harness that would catch the next one up front: every export format × one fixture ledger holding every stored shape (lists, annotations, types, legacy labels) → insert into a fresh ledger → compare. I wouldn't hold this PR for it, and the point fix is right here, but I wonder if shaping the existing round-trip tests that way is worth doing while they're fresh.

Claude-assisted review below:


✅ Approving to unblock you — with 1 thing that needs to be addressed before this merges.

Must address before merge:

  1. A JSON-LD export with an annotated (or list-valued) rdf:type is now refused by Fluree's own JSON-LD insert (fluree-db-api/src/export.rs:1402-1424, against fluree-db-transact/src/parse/jsonld.rs:1799-1802). It inserted at the base (losing the annotation); at this head the whole document fails to insert. Fix path: let the JSON-LD reader accept rdf:type with node-object or @list values (or keep writing every type into @type), plus a JSON-LD round-trip test for an annotated type. Repro and details inline.

The list work itself is solid. Each writer holds only the current subject's list items, folds in that subject's untranslated rows, and sorts by index when the subject closes, so order, duplicates and list-ness all survive, from novelty, from the index and from novelty over the index. I tried to break it in a few ways the tests don't cover and couldn't:

  • an indexed list replaced by upsert in novelty (["a","b","a"] → ["a","b"], and a list holding a decimal → ["q"]): every format writes only the current items, in order, matching the crawl, so the overlay's retracts land on the right list positions;
  • an empty @list (stored as rdf:nil) and a Turtle list of blank nodes ( [ … ] [ … ] ): both re-insert through Turtle and JSON-LD with the same triples and the same crawl, from novelty and from the index;
  • two @list inserts on one subject and predicate: export writes ( "a" "c" "b" ), the same order the JSON-LD crawl returns, so the two readers of the merged positions agree.

On memory, which was my main worry for a streaming writer: a subject's list items are held until the subject closes, so the bound is the largest single subject's lists, the same bound the JSON-LD writer already holds for a whole subject, and the SubjectLists doc says so.

Note that CI hasn't run on this head: as a stacked PR it only got the release plan job, with no test, clippy or fmt run. I ran cargo fmt -p fluree-db-api -- --check, cargo clippy -p fluree-db-api --lib --test grp_transact -- -D warnings and the grp_transact tests locally, all clean, but it's worth watching the full run once this retargets onto main.

Adherence to repo commitments:

  • Patterns/abstractions: ✔ one SubjectLists shared by the three writers, with TurtleBlock, JsonLdNode and NtLists as thin per-format owners; the annotation change follows EdgeKey::matches (the list index is part of the edge) rather than inventing a rule.
  • Performance (speed first, memory second): ✔ export isn't a query hot path, and list-free data pays one read of the o_i column per row (already decoded, ColumnProjection::all() at export.rs:612) plus, for N-Triples, a u64 compare. Memory is bounded by one subject's list items. There's no export bench to move. No performance-degradation risk.
  • Deployment targets: ✔ host-neutral writer code (no filesystem, threads or clock) reached by every host that exports; the per-subject buffer is the only new memory, bounded as above.
  • Testing: ⚠️ it_export_lists.rs is wired into grp_transact and its three tests pass locally. I dropped the index sort in SubjectLists::take and lists_export_in_order_and_reinsert_as_the_same_lists went red; I dropped the N-Triples fold of untranslated items and it went red again. The rdf:type branch in write_jsonld_node has no test, which is how the must above got through.
  • Conventions: ✔ thorough commit body; docs/cli/export.md updated.

Verified locally at aaab46af6: cargo test -p fluree-db-api --test grp_transact -- it_export_lists it_stable_blank_nodes (12 passed), the clippy and fmt runs above, and the probes and mutations described here.

Approving now so you can merge without waiting on another pass from me — just be sure the rdf:type round trip (and a test for it) is in before you do.

let rest: Vec<&serde_json::Value> =
values.iter().filter(|v| bare_id(v).is_none()).collect();
if !rest.is_empty() {
write_jsonld_property(writer, pred, rest.into_iter())?;

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.

🔴 Must address before merge: a JSON-LD export with an annotated rdf:type no longer inserts back into Fluree.

When a type value isn't a bare {"@id": …}, this now writes it as an rdf:type property, e.g. "@type": "ex:B", "rdf:type": {"@id":"ex:A","@annotation":{"@id":"ex:r"}}. That's valid JSON-LD, but Fluree's JSON-LD insert refuses rdf:type as a key outright (fluree-db-transact/src/parse/jsonld.rs:1799-1802, "is not a valid predicate IRI. Please use the JSON-LD "@type" keyword instead."), so the whole document fails, not just that value.

Concretely: I inserted ex:s a ex:A ~ ex:r {| ex:since 2020 |} ; a ex:B . as Turtle, exported JSON-LD and inserted the export into a fresh ledger. At the base (77e5bc6ec) the insert succeeds and drops the ~ ex:r link, which is the loss the PR body describes; at aaab46af6 it fails with that parse error. A list on rdf:type (Turtle ex:s a ( ex:A ex:B )) goes the same way, as "rdf:type": {"@list": […]}.

I think the fix that keeps both properties is on the reader: accept rdf:type as a key when its values are node objects ({"@id": …}, with or without @annotation) or a @list, route them through the regular-predicate path, and keep refusing plain string values, which is the footgun that error exists for. Then a JSON-LD round-trip test for an annotated type pins it. I haven't tried that change myself. If you'd rather not touch the insert surface in this PR, the fallback is to keep writing every type into @type as before and say in the export docs that JSON-LD drops annotations on types; that keeps the export insertable, though still lossy.

Commenting here because fluree-db-transact/src/parse/jsonld.rs is not in this diff.

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 2bc424c

Base automatically changed from fix/export-roundtrip to main October 23, 2026 22:18
JSON-LD's @type holds IRIs only, so export writes an annotated type, or
a list of types, as an rdf:type property. Fluree's JSON-LD insert
refused rdf:type as a key outright, so an export holding one didn't
insert back at all. It now reads rdf:type values that are node objects,
or a @list of them, the way it reads any other property. It still
refuses a literal, which would type the node with a string.

The export round-trip fixture now also holds an annotated type, a plain
type, a list of types and an annotated plain value. So every format
re-inserts them from novelty, from the index and from both.
@bplatz
bplatz merged commit 2aeabef into main Oct 24, 2026
19 checks passed
@bplatz
bplatz deleted the fix/export-list-order branch October 24, 2026 11:18
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.

2 participants