Skip to content

Planner hoists a triple above a preceding OPTIONAL that binds a shared variable, returning wrong rows #1924

Closed

Description

@bplatz

A required triple that follows an OPTIONAL in the same group is planned before it when the two share a variable the OPTIONAL binds. That turns Join(LeftJoin(A, B), C) into LeftJoin(Join(A, C), B), and the two are not equivalent when C binds a variable of B. The result is spurious rows on SPARQL, JSON-LD, and Cypher alike.

Repro (SPARQL)

@prefix ex: <http://example.com/> .
ex:alice ex:knows ex:bob .
ex:bob   ex:worksFor ex:acme .
ex:carol ex:knows ex:bob ; ex:worksFor ex:globex .
ex:dave  ex:knows ex:erin .
PREFIX ex: <http://example.com/>
SELECT ?p ?org ?y WHERE {
  ?p ex:knows ?f .
  OPTIONAL { ?p ex:worksFor ?org }
  ?y ex:worksFor ?org
}

Expected: 5 rows. Alice and dave (?org unbound) join both worksFor triples. Carol (?org = globex) joins only her own.

alice acme   bob      alice globex carol
carol globex carol
dave  acme   bob      dave  globex carol

Actual: 6 rows. It adds carol acme bob. ?y ex:worksFor ?org ran first and bound ?org = acme for carol. The OPTIONAL then failed on carol worksFor acme and kept the row. Output is identical on main (09f15cb) and on #1923.

Repro (Cypher)

CREATE (:Person {name: "Alice"})-[:WORKS_FOR]->(:Company {name: "Acme"})-[:LOCATED_IN]->(:City {name: "NYC"}),
       (:Person {name: "Bob"}),
       (:Person {name: "Carol"})-[:WORKS_FOR]->(:Company {name: "Globex"})-[:LOCATED_IN]->(:City {name: "SF"})
MATCH (p:Person)
OPTIONAL MATCH (p)-[:WORKS_FOR]->(o)
MATCH (o)-[:LOCATED_IN]->(c)
RETURN p.name AS person, c.name AS city ORDER BY person

Expected: Alice NYC, Carol SF. Bob's o is null, and a null matches nothing.

Actual: all six person×city pairs, on main and on #1923 alike. The MATCH is hoisted above the OPTIONAL MATCH, so o is bound to every located company first. The OPTIONAL MATCH then keeps each row whether or not the person works there.

OPTIONAL MATCH … MATCH over the same variable is a common Cypher idiom. Inserting WITH p, o between the clauses is a workaround, because it creates a subquery boundary; #1923's cypher_null_from_optional_match_matches_nothing_in_later_match uses it for that reason.

Where

reorder_patterns (fluree-db-query/src/planner.rs:1670 on main) ranks the triple as a source and places it ahead of the OPTIONAL, which is an expander. Moving an inner join across a left join is only sound when the moved pattern shares no variable that the OPTIONAL may bind. Of the triple's variables, ?org is bound only by the OPTIONAL.

Related, and likely fixed alongside: #1925 (sibling OPTIONALs evaluated out of order).

Notes for whoever takes it

  • The fix constrains placement, so check plan-shape and performance on shapes that currently benefit from hoisting. Most real queries are "well-designed": every OPTIONAL variable that appears outside it also appears in the required part. Those are unaffected and must keep their current plans.
  • Pin both surfaces, SPARQL and Cypher, and confirm the Cypher test passes without its WITH.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1area:querybug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions