Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rdflib/plugins/sparql/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def evalLeftJoin(
for b in evalPart(c, join.p2):
if _ebv(join.expr, b.forget(ctx)):
ok = True
yield b
yield b.merge(a)
if not ok:
# we've cheated, the ctx above may contain
# vars bound outside our scope
Expand Down
37 changes: 37 additions & 0 deletions test/test_sparql/test_optional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from rdflib import Graph, Literal, URIRef, Variable


def test_binding_with_optional_clause() -> None:
"""
Optional clauses should bind variables if feasible.

See https://github.com/RDFLib/rdflib/issues/2957
"""
g = Graph().parse(
data="""
prefix ex: <https://www.example.org/>
ex:document ex:subject "Nice cars" .
ex:someCar ex:type "Car" .
"""
)
result = g.query(
"""prefix ex: <https://www.example.org/>
select ?subject ?car
where {
$this ex:subject ?subject.
optional
{
# an offending subselect clause
select ?car
where {
?car ex:type "Car".
}
}
}"""
)
assert len(result.bindings) == 1
(first,) = result.bindings
assert first.get(Variable("car")) == URIRef("https://www.example.org/someCar")
assert first.get(Variable("subject")) == Literal(
"Nice cars"
), "optional clause didnt bind"
Loading