Repository navigation
fix(transact): net an update's facts as sets, not by row count - #2144
Conversation
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.
# Conflicts: # fluree-db-transact/src/generate/accumulator.rs
aaj3f
left a comment
There was a problem hiding this comment.
@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
INSERTthat 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 aDELETEof a stored triple;- an
OPTIONALreplace 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.mdpromises a little more than the code does (inline atdocs/transactions/retractions.md:337): anINSERTof 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
Retractioninvariant; themixed_set_assertionsspecial 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
OPTIONALandVALUES … UNDEF, JSON-LD, default and named graph, novelty-only and indexed), wired throughgrp_transact; both ran in CI by name and go red againstmain's accumulator; the unit tests are updated, including the restated-fact report to the modify-policy check. - Conventions: ✔ multi-line commit body;
retractions.mdupdated; 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.
|
|
||
| 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 |
There was a problem hiding this comment.
🟡 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.
…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.
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.
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.
Summary
An update's templates are instantiated once per
WHEREsolution, 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:(DS − D) ∪ Iit stays, because it is in the insert set. This lost data:The accumulator now nets each fact by presence:
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
WHEREsolutions than it inserts it on now keeps the triple; it used to retract 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 (withOPTIONAL, and theVALUES … UNDEFform 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 (retractb=2, assertb=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.