Closed prohde closed 1 year ago
I couldnt generate that error(with python 3.11). My script:
#import sys
#sys.path.insert(0,"path/to/rdflib-6.2")
import rdflib
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
"""
ex = rdflib.Namespace("http://example.com/")
g = rdflib.Graph()
g.bind("ex", ex)
g.parse(format="ttl", data="""@prefix ex: <http://example.com/>.
<1> a ex:a;
ex:d ex:b.
<2> a ex:a;
ex:d ex:c;
ex:d ex:b.
<3> a ex:a;
ex:d ex:c.
""")
print(list(g.query(query)))
Hi @WhiteGobo,
this is because all of your instances (<1>, <2>, and <3>) have a connection to at least ex:b or ex:c via ex:d. If you remove the last triple, expecting the count for <3> to be 0 (zero), then the same error appears.
import rdflib
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
"""
ex = rdflib.Namespace("http://example.com/")
g = rdflib.Graph()
g.bind("ex", ex)
g.parse(format="ttl", data="""@prefix ex: <http://example.com/>.
<1> a ex:a;
ex:d ex:b.
<2> a ex:a;
ex:d ex:c;
ex:d ex:b.
<3> a ex:a.
""")
print(list(g.query(query)))
Ok i have made a fix in an extra branch. That should resolve that Error. Link to the branch
I need some time to make a PR out of this, because i would create a test and ive got to look at plugins/sparql/aggregates.py
because they tried to catch the NotBoundError and im not sure if that line of code ever gets used.
I ran into an issue when serializing the results of SPARQL queries with aggregates from optional graph patterns, i.e., they might potentially be unbound. I am using
rdflib==6.2.0
.The query in question is:
For each graduate student, I want to know how many undergraduate degrees he/she has from the list of universities provided using the VALUES clause. I am using OPTIONAL here since I am also interested in getting
0
if the student doesn't have a degree from one of the specified universities.The query runs fine in my SPARQL endpoint but when I try to use rdflib as an in-memory RDF graph, I get the following exception:
I tried the following rewriting of my original query to bypass that issue but with no success.
To my understanding, the error should occur in the count which shouldn't be executed due to the IF statement.
Many thanks in advance!