Skip to content

fix(transact): net an update's facts as sets, not by row count - #2144

Merged
bplatz merged 6 commits into
mainfrom
fix/update-set-netting
Oct 23, 2026
Merged

bplatz merged 6 commits into
mainfrom
fix/update-set-netting

Conversation

@bplatz

@bplatz bplatz commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

An update's templates are instantiated once per WHERE solution, so one update can stage the same fact several times on either side. Staging netted each fact by counting those copies and kept whichever side had more:

  • A stored fact deleted on more solutions than it is re-inserted on was retracted. Under SPARQL 1.1 Update's (DS − D) ∪ I it stays, because it is in the insert set. This lost data:
    DELETE { ex:r1 ex:p "v" } INSERT { ?keep ex:p "v" }
    WHERE { VALUES (?x ?keep) { (1 ex:r1) (2 UNDEF) (3 UNDEF) } }
  • A stored fact deleted once and re-inserted on several solutions was asserted again. The commit carried a redundant assertion, an update that changed nothing still wrote a commit, and after an index build history showed the fact retracted and re-asserted.

The accumulator now nets each fact by presence:

  • asserted: the assertion survives;
  • retracted and not asserted: the retraction survives;
  • both: nothing survives.

Every retraction names a stored fact (current_facts), so "both" always means a stored fact the update keeps as it is. Sync and upsert already netted their assertions as a set (FlakeAccumulator::mixed_set_assertions). Every update now nets both sides that way, so that separate mode is removed.

Fixes #2129.

Behavior changes

  • An update that deletes a stored triple on more WHERE solutions than it inserts it on now keeps the triple; it used to retract it.
  • An update that deletes and re-inserts stored triples as they are no longer commits them, and commits nothing if nothing else changes. A stored triple that is only inserted still commits an assertion, as before.
  • Requests with several operations are unaffected. Each operation is staged as its own transaction, so a later operation still sees what an earlier one wrote.
  • A ledger that already ran the data-losing shape keeps that retraction as an ordinary commit. The fact stays gone and has to be re-inserted; nothing in the ledger flags it.

Tests

it_delete_stored_facts, each on a novelty-only ledger and an indexed one:

  • a_fact_deleted_on_more_rows_than_it_is_inserted_on_is_kept: SPARQL (with OPTIONAL, and the VALUES … UNDEF form above) and JSON-LD, in the default graph and a named graph. Nothing is committed and the fact remains.
  • a_fact_inserted_on_more_rows_than_it_is_deleted_on_is_not_asserted_again: the WHERE/DELETE/INSERT over several rows commits a redundant assertion of an unchanged fact #2129 reproduction in SPARQL and JSON-LD. The commit holds two flakes (retract b=2, assert b=3), and running it again commits nothing.

Both fail against main's accumulator: the first commits the retraction, the second commits three flakes. The accumulator's unit tests are updated, and one checks that a restated fact is reported to the modify-policy check whatever the counts.

An update's templates are instantiated once per WHERE solution, so one
update can push the same fact several times on either side. The
accumulator netted each fact by counting those pushes and kept the side
pushed more often. A stored fact deleted on more solutions than it was
re-inserted on was retracted, though SPARQL 1.1 Update's (DS − D) ∪ I
keeps it; one deleted once and re-inserted on several solutions was
asserted again, so an update that changed nothing still committed.

Each fact now nets by presence: an asserted fact survives as its
assertion, a fact only retracted as its retraction, and a fact both
retracted and asserted not at all. Every retraction names a stored fact,
so the last case is a stored fact the update keeps as it is. Sync and
upsert already counted their assertions once; with both sides sets for
every update, that separate mode is gone.
@bplatz bplatz added the bug Something isn't working as expected label Oct 20, 2026
@bplatz
bplatz requested a review from a team October 20, 2026 10:17

@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 the right fix, and at a design level no pushback from me. Netting by presence is exactly (DS − D) ∪ I, and it's only sound because the accumulator can only take a Retraction of a stored fact (current_facts.rs:181), so "retracted and asserted" really does mean "a stored fact the update keeps". I also like that the special set mode for sync/upsert goes away rather than growing a sibling. Two notes, neither of which I think should hold this:

(1) This is the staging-side instance of a class we keep meeting, where a redundant assert or retract loses (or wins) a cancelling-pair tie: #1818's netting, #2022, the datalog equal-t drop, and the update netting this fixes. Here the Retraction type is what makes a one-rule fix possible, so I don't think anything more structural belongs in this PR.

(2) #2153 is the read-side sibling (a retraction and an assertion of one fact at one t). A git merge-tree of the two heads is clean, and since this accumulator now emits at most one op per fact identity, #2153's build_commit same-t check never sees a pair from it, so I'd read the two as composing rather than overlapping.

Claude-assisted review below:


✅ Approve with nits.

This is a small diff for what it fixes, and I like that it deletes a mode rather than adding one: sync, upsert and every other update now net through the same four-arm match.

I checked the semantics against SPARQL 1.1 Update with a few shapes beyond the PR's tests (scratch test in it_delete_stored_facts.rs, each on a novelty-only ledger and an indexed one), and every one came out the way (DS − D) ∪ I says:

  • an absent triple deleted on three solutions and inserted on one is inserted (one flake);
  • an absent triple deleted and inserted on three solutions is inserted once;
  • a stored triple deleted on three solutions, with an INSERT that never binds, is retracted;
  • a WHERE-witnessed delete over three cross-joined rows plus a constant re-insert on all three commits nothing;
  • INSERT { _:b … } over three solutions still mints three fresh nodes, alone and next to a DELETE of a stored triple;
  • an OPTIONAL replace where only some subjects have a new value retracts the old value on the rest and keeps the subject whose new value equals its old one.

Two small things, neither blocking:

  • 🟡 one sentence in retractions.md promises a little more than the code does (inline at docs/transactions/retractions.md:337): an INSERT of a stored triple that no solution deletes still commits an assertion, before and after this PR.
  • fwiw, a ledger that already ran the data-loss shape holds that retraction as an ordinary commit, so the fact stays gone there and needs re-inserting. That might be worth a line wherever this fix gets announced, since nothing in the ledger will flag it.

Adherence to repo commitments:

  • Patterns/abstractions: ✔ one netting rule for every update, leaning on the existing Retraction invariant; the mixed_set_assertions special case is removed rather than extended.
  • Performance (speed first, memory second): ✔ staging only, not a query path: each bucket drops its two counters, a repeat push is dropped instead of replacing the held flake, and the first push still pays its one Flake::clone(). No performance-degradation risk.
  • Deployment targets: ✔ n/a — host-neutral staging logic in fluree-db-transact; it behaves the same in the server, the embedded library and wasm32, and touches no runtime fence.
  • Testing: ✔ two integration tests (SPARQL with OPTIONAL and VALUES … UNDEF, JSON-LD, default and named graph, novelty-only and indexed), wired through grp_transact; both ran in CI by name and go red against main's accumulator; the unit tests are updated, including the restated-fact report to the modify-policy check.
  • Conventions: ✔ multi-line commit body; retractions.md updated; fmt/clippy green.

Verified locally at branch HEAD (448570fe4): all 34 tests in it_delete_stored_facts pass, with a_fact_deleted_on_more_rows_than_it_is_inserted_on_is_kept and a_fact_inserted_on_more_rows_than_it_is_deleted_on_is_not_asserted_again by name, plus the 20 accumulator unit tests. With accumulator.rs and stage.rs put back to main, both new tests fail the way the PR body says: the OPTIONAL form commits its retraction, and the #2129 reproduction commits 3 flakes instead of 2. CI's test job ran both new tests by name, and git merge-tree of this head with #2142, #2154 and #2153 is clean.

Approving so you can merge when ready — the docs sentence is the only thing I'd fold in first.

Comment thread docs/transactions/retractions.md Outdated

The same holds when the `WHERE` has several solutions and the templates are
instantiated once for each: a triple that any solution inserts is kept,
however many solutions delete it, and a commit holds only the triples whose

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: "a commit holds only the triples whose stored state changes" is broader than what an update does.

The netting part of this paragraph matches what I ran, but an INSERT of a triple that's already stored and that no solution deletes is still committed as an assertion. At this head, with ex:a ex:v 1 . ex:b ex:v 2 stored, DELETE { ex:b ex:v 2 } INSERT { ex:a ex:v 1 . ex:b ex:v 3 } WHERE {} commits 3 flakes where ex:b's change is 2, and INSERT DATA { ex:a ex:v 1 } on its own commits 1 flake at a new t. main does the same, so it's the sentence rather than the code.

Maybe something like "…a triple that any solution inserts is kept, however many solutions delete it, and a stored triple that solutions both delete and insert is not committed at all." It's 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 13f4a2d

…s commit

A stored triple that solutions both delete and insert is not committed.
A stored triple that is only inserted still commits an assertion, so a
commit can hold triples whose stored state doesn't change.
@bplatz
bplatz merged commit 109ab32 into main Oct 23, 2026
19 checks passed
@bplatz
bplatz deleted the fix/update-set-netting branch October 23, 2026 21:33
bplatz added a commit that referenced this pull request Oct 24, 2026
main's accumulator holds a fact's assertion and retraction rather than
counting them (#2144), so FlakeAccumulator::retracts reads whether the
bucket holds a retraction.
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

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WHERE/DELETE/INSERT over several rows commits a redundant assertion of an unchanged fact

2 participants