Skip to content

upsert doesn't retract language-tagged (or @list) values: the retraction flakes are built with m: None #1976

Closed

Description

@aaj3f

Found this while validating a customer load of a bilingual (FR/EN) taxonomy, where the refresh path is "re-upsert the file". Every refresh leaves the previous labels in place next to the new ones, so each concept picks up another @en and @fr label per refresh instead of having them replaced. Plain strings and numbers are replaced correctly. It seems to be specifically values that carry FlakeMeta (a language tag or a list index).

Repro (db main @ ac1e8a61b, fluree 4.2.1 built from it, local/direct mode):

fluree create langtest
fluree insert langtest '@prefix ex: <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
ex:s rdfs:label "old"@en, "ancien"@fr ; ex:note "old" .'
fluree upsert langtest '@prefix ex: <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
ex:s rdfs:label "new"@en, "nouveau"@fr ; ex:note "new" .'
fluree query langtest 'SELECT ?p ?o (LANG(?o) AS ?lang) WHERE { <http://example.org/s> ?p ?o } ORDER BY ?p ?o'

Expected 3 rows: "new"@en, "nouveau"@fr, ex:note "new".

Actual, 5 rows. ex:note was replaced, but both old labels are still there:

| fr | ancien  | rdfs:label |
| en | new     | rdfs:label |
| fr | nouveau | rdfs:label |
| en | old     | rdfs:label |
|    | new     | ex:note    |

(The upsert commits 6 flakes: 3 asserts and 3 retractions, two of which retract facts that don't exist. More on that below.)

The same thing happens:

  • via JSON-LD upsert with {"@value": "…", "@language": "…"}
  • after fluree index, i.e. on the binary-index/materializer path, not only novelty
  • inside a TriG GRAPH <g> { … } block
  • with @list, which is the worse case. Upserting {"@list": ["x","y"]} over {"@list": ["a","b"]} reads back as ["a","x","b","y"]: the old items keep their list indices, so the two lists get interleaved

For contrast, fluree sync over the same lang-tagged data does the right thing (+2 asserted, -2 retracted), and so does the WHERE/DELETE path. It looks isolated to the upsert deletion generator.

Where. Claude traced the mechanism and checked each anchor at ac1e8a61b, fwiw:

  • fluree-db-transact/src/stage.rs:3223: binding_to_flake_object returns (val.clone(), dtc.datatype().clone()) for Binding::Lit, so dtc.lang_tag() is dropped.
  • fluree-db-transact/src/stage.rs:3546-3566: generate_upsert_deletions builds every retraction with Flake::new(…, false, None) / Flake::new_in_graph(…, false, None), so m is always None.
  • fluree-db-core/src/flake.rs:576-595: Flake's PartialEq/Hash include m. That makes (s p "old" rdf:langString m=None) a different fact from the asserted (… m={lang:"en"}), so the retraction never cancels it. It just gets committed as a retraction of something that isn't there. (For "a"@en, "a"@fr the commit's flake count suggests the two retractions even collapse into one.)
  • For named graphs on an unindexed ledger the meta is lost one step earlier: query_novelty_for_graph keeps only (flake.o, flake.dt) (stage.rs:3610) and rebuilds the binding with Binding::from_object (stage.rs:3624), dropping flake.m.

The WHERE/DELETE template path handles this correctly. generate/flakes.rs:204-241 threads the bound dtc.lang_tag() and template.list_index into FlakeMeta::from_parts, which is why that path works.

Fix direction. For lang tags, the minimal fix seems to be having binding_to_flake_object hand back the lang alongside (o, dt) and building the retraction with FlakeMeta::from_parts(lang, None). query_novelty_for_graph would also switch to Binding::from_object_with_meta (fluree-db-query/src/binding.rs:482). That minimal fix doesn't cover @list, though. Binding::Lit has no list index at all (binding.rs:104-118), so there's nothing on the ?o binding to rebuild m.i from. I think the more robust shape is for the deletion step to scan whole flakes for (s, p, g) rather than ?o bindings, and retract each flake as-is (same o, dt, m). That fixes lang and list together, and anything we add to FlakeMeta later. If you disagree on the approach, happy to talk it through.

There's also no upsert test that exercises either shape. test_upsert_replaces_existing_values (stage.rs:4065) uses plain strings, and none of fluree-db-api/tests/it_transact_upsert*.rs has @language or @list. We probably want one each for lang in novelty, lang after indexing, @list, and lang inside a named graph.

Why it matters. It's silent. Nothing errors, and upsert is documented as "replace existing values for entities that already exist", so anyone refreshing data by re-upserting (the obvious way to keep an externally-maintained vocabulary current) slowly accumulates every historical label. For SKOS-style vocabularies, where skos:prefLabel is supposed to be one value per language, that turns into a data-integrity problem after the first refresh. The @list interleaving corrupts ordered data outright.

This code is identical at ffeeec554, which is the rev fluree/solo pins, so it isn't a regression from the recent merge work. I haven't bisected how far back it goes. #1602 (closed) was a different upsert non-retraction (the novelty-window one), not this mechanism.

Activity

  1. added
    bug
    P2
    area:transact
    on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2area:transactbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions