RDFLib / rdflib

RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information.
https://rdflib.readthedocs.org
BSD 3-Clause "New" or "Revised" License
2.15k stars 555 forks source link

SPARQL queries don't work for Datasets #2356

Open sdasda7777 opened 1 year ago

sdasda7777 commented 1 year ago

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 commented 1 year ago

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 }
}
"""
namedgraph commented 1 year ago

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

aucampia commented 1 year ago

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.