Skip to content

FILTER over an OPTIONAL variable drops rows an RDF-star annotation binds #1999

Open

Description

@bplatz

A FILTER on a variable that an earlier OPTIONAL leaves unbound, and that an RDF-star annotation later binds, drops the rows where only the annotation binds it.

Repro

{
  "@context": {"ex": "http://example.org/ns/"},
  "@graph": [
    {"@id": "ex:a1", "ex:name": "A1",
     "ex:knows": {"@id": "ex:b1", "@annotation": {"ex:since": 2021}}},
    {"@id": "ex:a2", "ex:name": "A2", "ex:joined": 2021,
     "ex:knows": {"@id": "ex:b2", "@annotation": {"ex:since": 2021}}},
    {"@id": "ex:a3", "ex:name": "A3", "ex:joined": 1990,
     "ex:knows": {"@id": "ex:b3", "@annotation": {"ex:since": 1990}}}
  ]
}
PREFIX ex: <http://example.org/ns/>
SELECT ?a ?b ?since WHERE {
  ?a ex:name ?n .
  OPTIONAL { ?a ex:joined ?since }
  ?a ex:knows ?b {| ex:since ?since |} .
  FILTER(?since > 2000)
}
Rows
Expected a1 b1 2021, a2 b2 2021
Actual a2 b2 2021

a1 has no ex:joined, so its ?since comes only from the annotation, and the FILTER drops it. Without the FILTER all three rows come back correctly, a1 with 2021.

The result is the same in novelty, after a full rebuild, and with a sealed annotation arena. It was found in review of #1995, and the query crate at that PR's merge-base returns the same rows, so it predates it.

Probable cause (not confirmed)

DefaultGraphSourceOperator plans the annotation chain on top of its child operator (build_where_operators_seeded(Some(child), …) in fluree-db-query/src/default_graph_source.rs). That child doesn't implement Operator::bound_in_every_row, so the chain treats every variable in its schema as bound on every row, including the ?since the OPTIONAL leaves unbound. sink_filters_into_annotation_chains copies the FILTER into the chain, where it can then run before the annotation binds ?since. #1995 fixed this class of bug for VALUES UNDEF cells, OPTIONALs and per-row seeds in the main join chain; this lane still makes the old assumption.

A suggestion from the review: the where-plan loop knows which variables are bound on every row where it builds this wrapper, and could pass them to the wrapper's inner build as the seed's bound_in_every_row.

The review also reported a1 losing ?since with no FILTER. That didn't reproduce with the data above.

Activity

  1. added
    bug
    P2
    area:query
    on Sep 30, 2026
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

    P2area:querybug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions