Skip to content

Two OPTIONALs on the same variable: the second loses its results and fan-out when the first matched nothing (Poisoned blocks the merge) #1734

Closed

Description

@aaj3f

Two OPTIONALs on the same variable lose the second one's results — including its fan-out — when the first matched nothing. This is the commonest instance of the "already-present-but-unbound column" class described in #1713, and it is not closed by that issue's fix.

Repro

SELECT ?s ?f WHERE {
  ?s schema:name ?name .
  OPTIONAL { ?s ex:greeting ?f }
  OPTIONAL { ?s ex:friend ?f }
}

On the seed_values_dataset fixture: 6 rows — nikola's two greetings, and alice/brian/cam/liam all null. Per §18.2.4 the answer is 9: nikola ×2, alice 1, cam 2, liam 3, brian genuinely unbound.

Note what's lost. It isn't only the binding — it's the fan-out: liam has three friends and contributes one null row instead of three. The same two OPTIONALs over distinct variables return the correct 9, so this is specifically the shared-variable interaction.

Mechanism, and why it's a design question rather than a bug fix

An OPTIONAL that matches nothing doesn't leave the variable Unbound — it records Binding::Poisoned (create_poisoned_row, fluree-db-query/src/optional.rs:2278-2292). unify_check then refuses any pairing involving a poisoned value (:2318-2320), so the second OPTIONAL can neither match nor extend.

That blocking behavior is deliberate and documented: fluree-db-query/src/binding.rs:48-53 says in as many words that "unlike Unbound, a Poisoned binding BLOCKS future pattern matching." So this isn't an oversight to patch — closing this door means deciding whether that blocking semantics is right at all, and what should replace it for the left-join case. That is a design call, which is why it isn't being folded into the P1 fix in #1724.

The distinction the fix in #1724 rests on is matches!(binding, Binding::Unbound): it patches a shared column the required row left unbound, and deliberately skips a column that is anything else. Poisoned is that "anything else."

Scope: measured, not inferred

Output is byte-identical at #1724's merge-base and at its head — this shape is unchanged by that PR in either direction. #1724 genuinely closes the Unbound doors (UNION-left-unbound, and VALUES … UNDEF, on both the SPARQL and JSON-LD surfaces and through both builder lanes); this is the Poisoned door, which it does not touch and shouldn't.

Why file it now

#1713 is being closed by #1724 on the strength of the general claim — "a shared variable the required row left unbound, however it got that way." That claim is true for the Unbound cases and it's the right way to describe what shipped. But the commonest real-world instance of a variable already occupying a row is a preceding OPTIONAL that didn't match, and that case stays live. Without this issue, #1713 closes and the gap becomes invisible.

Notes for whoever takes it

  • The decision to make first: should a non-matching OPTIONAL leave Poisoned (blocking) or Unbound (joinable) for a variable a later OPTIONAL may bind? SPARQL's algebra has no notion of poisoning — it's an engine optimization — so the question is which shapes rely on the blocking behavior and whether they can be served another way.
  • Check the W3C skip register: this may overlap nested-opt-1/nested-opt-2, though those are separately diagnosed in OPTIONAL's right operand is evaluated correlated with the left, where SPARQL §18.2 evaluates it independently (nested-opt-1) #1725 as a right-operand scoping problem rather than a merge one. Worth confirming they're distinct before assuming either fix closes the other.
  • Per the surface-parity rule, the JSON-LD ["optional", …] form needs the same treatment and the same test.

Activity

  1. added
    bug
    P1
    triage:needs-decision
    area:query
    on Aug 28, 2026
  2. aaj3f commented on Aug 28, 2026

    @aaj3f
    ContributorAuthor

    One addition worth having before this is picked up, from #1724's review: that PR's merge makes the Unbound/Poisoned split observable rather than theoretical.

    Before #1724, neither door worked — a shared variable the required row already carried was uniformly wrong regardless of how it got there. After it, behavior is selectively correct depending on how the null arose: a column that is Unbound (UNION branch, VALUES … UNDEF) gets patched by the merge; a column that is Poisoned (a preceding OPTIONAL that matched nothing) blocks it. And both render as null in output. Two rows that look identical in a result set behave differently in the next OPTIONAL.

    That's the argument for this issue's priority: the inconsistency is now user-visible with no way to distinguish the two cases from the outside.

    Credit to @bplatz for the observation.

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

    P1area:querybugtriage:needs-decision

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions