Repository navigation
Description
filter_fold skips its node-only soundness guard entirely when the filter function is sameTerm, on a premise stated in the module doc (fluree-db-query/src/filter_fold.rs:18-19):
sameTermis term equality, so it folds with no node guard.
That premise does not hold in this engine, because the join the fold rewrites into is not term equality for literals: the join key for a string literal ignores datatype and language, so "abc", "abc"^^xsd:string, "abc"@en, "abc"@EN and "abc"^^ex:custom all collapse into one group.
Unlike #1721 — which needs a stats-drift precondition to reach an unsound fold — this one needs nothing. No spurious retraction, no drift, no unusual ledger state. It fires on any ledger whenever stats are present.
Measured
A 13-object matrix under one predicate, comparing the unfolded semantics against what actually ships. (Projecting both sides — SELECT ?a ?b ?x ?y — makes find_foldable bail at filter_fold.rs:203 via the (true, true) => continue arm, which is how the unfolded answer was obtained.)
| rows | non-identity pairs | |
|---|---|---|
sameTerm unfolded |
19 | 6 — dt_z/dt_off, i_int/i_long, s_plain/s_str. All defensible: normalized dateTime instants, and "abc" ≡ "abc"^^xsd:string, which genuinely is the same term in RDF 1.1. |
sameTerm folded (shipping behavior) |
47 | 34 — including "abc"^^xsd:string ≡ "abc"@en, "abc"@en ≡ "abc"^^ex:custom, and 1 ≡ 1.0 |
A 19-row answer becomes a 47-row answer. Every FILTER(sameTerm(?x, ?y)) over a literal-valued predicate is affected.
Note the direction: the fold is looser than the semantics it replaces, so it adds rows. sameTerm is the strictest equality SPARQL offers — it's specifically the function you reach for when you do not want value equality — so answering it with something looser than = inverts its whole purpose.
Fix, either of two shapes
- Gate the
sameTermfold on the same node-valued guard as=(filter_fold.rs:194). Cheapest, and immediately correct. Costs the optimization on literal-valued predicates. - Make the join key carry datatype and language for string literals, so the join really is term equality and the premise becomes true. Larger, reaches beyond this fold, but fixes the root rather than the symptom — and would also shrink the looseness that filter_fold's node-only soundness guard reads an estimate that a spurious retraction can drive to zero #1721 exploits.
Whichever lands, the module doc at :18-19 needs to state the actual invariant rather than assume it.
Relationship to #1721
Same root cause, stated plainly: the fold's correctness rests on "the join implements RDF term equality," and it does not. #1721 is the case where a bad statistic lets a query into that unsound rewrite; this is the case where the code walks in deliberately. Fixing the join key (option 2) would address both; gating the branch (option 1) addresses only this one.
Found while investigating #1721; full write-up including the matrix harness is in that investigation.
Activity
aaj3f commentedon Aug 27, 2026
Two corrections to the numbers above, and a note on why the fix took the shape it did.
The row counts moved. They were measured at
47e96b7b6; #1676 (aaaa7d429) landed on 2026-08-21, after that pin, making the join key term-exact for plain and lang-tagged strings. Re-measured on the same 13-object matrix atfd78564ca:sameTermfolds 35 where the unfolded answer is 21 — not 47 vs 19. The defect is unchanged in kind and the fold is still looser than the semantics it replaces; only the magnitude moved. Plain=answers 31 before and after, and is pinned as a companion guard.Practical consequence worth recording: the repro fixture in the issue above (
"abc"^^xsd:stringvs"abc"@en) no longer reproduces — #1676 fixed that pair. Ported verbatim, those tests pass vacuously. The fixture that still exercises the divergence is a custom-datatype pair.Option (b) is closed, and for a stronger reason than blast radius. The issue offers "make the join key carry datatype and language for string literals" as the root fix. Half of it is already done (#1676), and completing the other half still wouldn't make the premise true: the join is deliberately lenient across numeric subtypes —
1≡"1"^^xsd:long≡1.0— pinned as a product decision byit_literal_identity.rs:189 bare_numeric_literals_stay_lenient_across_subtypesand rooted in the index's normalized numeric key. MakingsameTermfold-safe that way would mean reverting a deliberate behavior and touching persisted key encoding.So the premise at
filter_fold.rs:18-19isn't merely unverified — it asserts something the engine doesn't want to hold. #1728 takes option (a), gating thesameTermfold on the same node-valued guard as=, and rewrites the module doc to describe the encoded-term semantics as they actually are: looser than term equality for numerics, looser again where the encoding flattens datatypes, and not value equality either. Both functions now fold only where all three notions coincide.The remaining half of #1676's migration — non-
xsd:stringstring-dictionary datatypes still flattening on the indexed read path, which is a wrong-results path with no fold involved — is filed separately as #1729.