Repository navigation
Description
Found while fixing #1690 and verified on pristine main (a824bbfd1). This is pre-existing and unrelated to that PR — it reproduces with the planner changes fully reverted, and by construction can't be theirs (no VALUES, no subquery, so neither the barrier nor must_bind_vars is reached).
What happens
An OPTIONAL placed after a UNION is a total no-op over exactly the rows the UNION left the variable unbound on:
SELECT ?s ?f WHERE {
?s schema:name ?name .
{ { ?s ex:friend ?f } UNION { ?s schema:age ?age } }
OPTIONAL { ?s ex:friend ?f }
}On the seed_values_dataset fixture (fluree-db-api/tests/it_query_values.rs:16-47) this returns 10 rows — byte-identical to the same query with the OPTIONAL clause deleted entirely. Per §18.2.4 the answer is 13: the UNION produces 6 solutions binding ?f and 4 leaving it unbound, and the left join should extend those 4 unbound rows with their matching ex:friend values rather than passing them through untouched.
Deleting a clause and getting the same answer is the cleanest possible statement of the bug: the left join contributes nothing.
Relationship to the existing skip register
testsuite-sparql/tests/registers/mod.rs:143-144 already registers algebra/manifest#nested-opt-1 and #nested-opt-2 as failing, annotated "correlated-OPTIONAL independence (PR-W1-OPT)". So we know we fail this family — what's new here is a minimal in-repo repro, a concrete row count, and the mechanism stated as behavior rather than as a test name. join-scope-1 and filter-nested-2 in that same comment are recorded as already fixed by PR-W1 Families A/B, so this is the remainder of that cluster rather than a fresh regression.
Why it's worth its own issue
The registers say "these W3C tests don't pass." They don't say "an OPTIONAL after a UNION silently does nothing," which is the user-facing shape and is reachable in ordinary JSON-LD/SPARQL queries that never touch the W3C suite. Anyone hitting it sees a left join quietly dropping the extension it was written to add, with no error.
Per the surface-parity rule the JSON-LD ["union", …] followed by ["optional", …] form should be checked for the same behavior, and the fix wants a twin test.
Note for whoever takes it
#1701 hit this while pinning its own fix and deliberately did not widen into it. As a consequence, its test a_union_binding_the_var_does_not_suppress_the_barrier asserts the plan shape rather than rows, and says so inline — pinning rows there would have pinned this defect's output as expected. Once this lands, that test can be upgraded to a row assertion, which is the natural regression check for both issues at once.
Activity
bplatz commentedon Aug 28, 2026
This is wider than a UNION, and there's a door into it that shows the mechanism more sharply. Found while reviewing #1701; confirmed pre-existing on pristine
main(fe3c198c8).A
VALUESwith anUNDEFcolumn reaches it too — no UNION anywhere:On
seed_values_datasetthis returns 8 rows with?fNull on every one:Compare the same query without the leading VALUES:
Same eight rows, same multiplicity — every binding dropped.
ex:camappears twice andex:liamthree times in both, which is theex:friendfan-out, so the left join did match the triples and produce the right cardinality. It then failed to write the binding, because?fwas already present in the row asUnbound.That's a sharper statement than "the left join is a no-op": it isn't inert, it does the matching work and discards the result. The common precondition across both doors looks like the variable already occupying the row as Unbound rather than anything specific to UNION, which is probably the thing to fix and the thing to test.
Worth adding a third repro to whatever pins this — the UNION form, this VALUES/UNDEF form, and the JSON-LD twin per the surface-parity rule.
One consequence for #1701:
must_bind_varsthere classifiesPattern::Valuesas must-bind, so the barrier doesn't fire for the query above with a trailingVALUES ?f { ex:alice }— it returns the fabricated["ex:alice","ex:alice"]. I raised that on the PR, but the answer for that shape stays wrong until this issue lands regardless of the plan, exactly as it does for the UNION form.fix(query): an UNDEF VALUES column is not a must-bind
aaj3f commentedon Aug 28, 2026
This is wider and sharper than filed. Confirmed independently on pristine
main, and the UNION spelling above turns out to be one door of three.The essential ingredient is an already-present-but-unbound column, not the UNION. A
VALUEStable withUNDEFreaches it directly:Returns 8 rows with
?fnullon every one.The row count is what makes this sharper than "the left join is a no-op." Delete the
VALUESline and the same query returns those same 8 rows with?fbound on 6 of them —ex:alice→ex:brian;ex:cam→ex:alice,ex:brian;ex:liam→ex:alice,ex:brian,ex:cam; withex:brianandex:nikolagenuinely unbound. So the multiplicity is right in both cases: the OPTIONAL did match the triples, and then discarded the binding because?fwas already present in the row asUnbound. The join happened; the result was thrown away.That's a stronger and more precise statement than the original framing, and it changes what a fix has to do: it isn't "make the left join fire," it's "stop dropping a binding onto a column that is present-but-unbound."
The 10-vs-13 UNION spelling in the issue body above is the same defect through a second door, and #1701's review surfaced a third: a leading
VALUES … UNDEFalso suppresses the barrier that PR adds, which is why that fix is plan-shape-only for the UNDEF shape — this defect bites the same query afterward.Relevant to #1724, which claims to fix this issue: it should be checked against the
VALUES … UNDEFshape above, not only the UNION one. If it covers both, that's worth stating; if it only covers the UNION spelling, theFixesline is overclaiming and this issue should stay open for the remainder.Credit to @bplatz for finding this while reviewing #1701.
fix(query): merge the unbound variable back on the grouped OPTIONAL l…
bplatz commentedon Aug 28, 2026
Closing the loop on the
VALUES … UNDEFdoor I raised above: #1724 fixes it, and it fixes it as the class rather than as a second special case.Verified on that branch against the same fixture:
The bindings are actually present now and Null appears only where there genuinely is no
ex:friend— so this is the merge doing its job, not the row count landing right by coincidence. The UNION form goes 10 → 13 on the same branch and is no longer identical to the OPTIONAL-deleted answer.That also confirms the guess in my earlier comment. The precondition really is the variable arriving as
Binding::Unboundrather than anything UNION-specific — one change incombine_rowscloses both doors, andVALUES … UNDEFlowers to exactly that binding.One correction to what I wrote above, worth recording since it bounds what this issue closes: I described the defect as "the left join does the matching work and discards the result." That's right for the observable behaviour, but there were four distinct lanes involved, not one mechanism — see #1724's write-up. On the novelty lane the result cache keyed on only a third of the correlation, which is what made the counts land exactly on the no-OPTIONAL answer; on an indexed ledger the batched subject probe read the object off the plan-time template and returned 21 rows instead. Same symptom class, different causes per lane.
Remaining door, deliberately not closed by #1724 and now tracked as #1734: two
OPTIONALs on the same variable. A precedingOPTIONALthat matched nothing recordsBinding::Poisoned, notUnbound, and the merge correctly skips a column that isn'tUnbound. Confirmed still open on the #1724 branch — 6 rows where §18.2.4 wants 9.Worth noting for whoever takes #1734: the fix here makes that split observable. Previously neither door worked, so the behaviour was uniformly wrong; now it is selectively correct depending on how the null arose, and both spellings render as
nullin a result set. Two rows that look identical now behave differently in the nextOPTIONAL.