Skip to content
Merged
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
6 changes: 5 additions & 1 deletion rdflib/plugins/sparql/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ def eval_full_row(self, row: FrozenBindings) -> FrozenBindings:
return row

def use_row(self, row: FrozenBindings) -> bool:
return self.eval_row(row) not in self.seen
try:
return self.eval_row(row) not in self.seen
except NotBoundError:
# happens when counting zero optional nodes. See issue #2229
return False


@overload
Expand Down
39 changes: 38 additions & 1 deletion test/test_sparql/test_agg_distinct.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rdflib import Graph
from rdflib import Graph, URIRef
from rdflib.term import Literal

query_tpl = """
SELECT ?x (MIN(?y_) as ?y) (%s(DISTINCT ?z_) as ?z) {
Expand Down Expand Up @@ -116,3 +117,39 @@ def test_count_distinct():
"""
)
assert list(results)[0][0].toPython() == 2


def test_count_optional_values():
"""Problematic query because ?inst may be not bound.
So when counting over not bound variables it throws a NotBoundError.
"""
g = Graph()
g.bind("ex", "http://example.com/")
g.parse(
format="ttl",
data="""@prefix ex: <http://example.com/>.
ex:1 a ex:a;
ex:d ex:b.
ex:2 a ex:a;
ex:d ex:c;
ex:d ex:b.
ex:3 a ex:a.
""",
)

query = """
SELECT DISTINCT ?x (COUNT(DISTINCT ?inst) as ?cnt)
WHERE {
?x a ex:a
OPTIONAL {
VALUES ?inst {ex:b ex:c}.
?x ex:d ?inst.
}
} GROUP BY ?x
"""
results = dict(g.query(query))
assert results == {
URIRef("http://example.com/1"): Literal(1),
URIRef("http://example.com/2"): Literal(2),
URIRef("http://example.com/3"): Literal(0),
}