possible bug in SPARQL subqueries · Issue #607 · RDFLib/rdflib (original) (raw)

I think that SPARQL subqueries are not being correctly handled. It appears that the wrong variables are being projected from the subqueries and as well that the wrong variables end up being bound at the top level.

Here is a test file that shows the behaviour

query = """SELECT ?here ?there WHERE { ?here http://www.w3.org/ns/sh#p ?there }"""
print "\nDUMP property"
for row in testgraph.query(query): 
  print row

print "\nTwo chained triples"
query = """SELECT ?here ?there ?elsewhere
WHERE { ?here  http://www.w3.org/ns/sh#p ?there .
        ?there http://www.w3.org/ns/sh#p ?elsewhere . }"""
for row in testgraph.query(query): print row

print "\nSubquery"
query = """SELECT *
WHERE { ?here  http://www.w3.org/ns/sh#p ?there .
        { SELECT ?there
          WHERE { ?there http://www.w3.org/ns/sh#p ?elsewhere . } } } """
for row in testgraph.query(query): print row

print "\nSubquery"
query = """SELECT ?here ?there
WHERE { ?here  http://www.w3.org/ns/sh#p ?there .
        { SELECT ?there
          WHERE { ?there http://www.w3.org/ns/sh#p ?elsewhere . } } } """
for row in testgraph.query(query): print row

print "\nSubquery with variable renaming and disconnected variables"
query = """SELECT ?here ?there
WHERE { ?here  http://www.w3.org/ns/sh#p ?there .
        { SELECT (?here AS ?there)
          WHERE { ?here http://www.w3.org/ns/sh#p ?there . } } } """
for row in testgraph.query(query): print row

and the output on a very small graph

DUMP property
(rdflib.term.URIRef(u'http://www.w3.org/ns/sh#a'), rdflib.term.URIRef(u'http://www.w3.org/ns/sh#b'))
(rdflib.term.URIRef(u'http://www.w3.org/ns/sh#x'), rdflib.term.URIRef(u'http://www.w3.org/ns/sh#y'))
(rdflib.term.URIRef(u'http://www.w3.org/ns/sh#b'), rdflib.term.URIRef(u'http://www.w3.org/ns/sh#c'))

Two chained triples
(rdflib.term.URIRef(u'http://www.w3.org/ns/sh#a'), rdflib.term.URIRef(u'http://www.w3.org/ns/sh#b'), rdflib.term.URIRef(u'http://www.w3.org/ns/sh#c'))

Subquery
(rdflib.term.URIRef(u'http://www.w3.org/ns/sh#b'), None)

Subquery
(None, rdflib.term.URIRef(u'http://www.w3.org/ns/sh#b'))

Subquery with variable renaming and disconnected variables
(None, rdflib.term.URIRef(u'http://www.w3.org/ns/sh#b'))
(None, rdflib.term.URIRef(u'http://www.w3.org/ns/sh#y'))
(None, rdflib.term.URIRef(u'http://www.w3.org/ns/sh#c'))

I believe that the each of these should have a single solution, namely a projection of <a,b,c>.