rdflib icon indicating copy to clipboard operation
rdflib copied to clipboard

SPARQL queries don't work for Datasets

Open sdasda7777 opened this issue 2 years ago • 3 comments

Hi, I tried to create a Dataset and query it with SPARQL, however I had no success. I retraced my steps to the SPARQL query example, which worked, however when I changed the container from Graph to Dataset, it stopped working. Curiously, it seems to work just fine with ConjunctiveGraph. My code looks like this:

from rdflib.graph import ConjunctiveGraph, Dataset, Graph

foaf="""[content of examples/foaf.n3]"""

#ds = Graph()
#ds = ConjunctiveGraph()
ds = Dataset()
ds.parse(data=foaf, format="n3")

for row in ds.query("SELECT ?s WHERE { [] foaf:knows ?s .}"):
    print(row)

I looked through open issues, and this one seems the most similar, but I don't think it is related, because when I parse it with Dataset, I can enumerate it and see that the data was in fact loaded (not sure if correctly, though).

sdasda7777 avatar Apr 16 '23 12:04 sdasda7777

Ah, interesting: When using Datasets, using GRAPH clause seems to be mandatory, for some reason 🤔 When I use the following query, it prints everything from both ConjunctiveGraph and Dataset:

query_string = """
SELECT ?s ?p ?o ?g
WHERE {
    GRAPH ?g { ?s ?p ?o }
}
"""

sdasda7777 avatar Apr 16 '23 13:04 sdasda7777

Isn't this because of #2375? Default graph pattern doesn't match anything because a label has been added to it?

namedgraph avatar Jun 08 '23 15:06 namedgraph

Isn't this because of #2375? Default graph pattern doesn't match anything because a label has been added to it?

I think you are right, but I'm not 100% sure.

aucampia avatar Jun 08 '23 16:06 aucampia