Using FROM and FROM NAMED on ConjunctiveGraph behaves not standard conform · Issue #811 · RDFLib/rdflib (original) (raw)
I want to use FROM
and FROM NAMED
in a SPARQL query to select the default graph resp. named graphs to execute the query on. But the RDFlib implementation does not act as it is described in the SPARQL 1.1 specification especially the section "13.2 Specifying RDF Datasets" (https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#specifyingDataset)
To demonstrate this behavior, I've created an MWE:
#!/usr/bin/env python3
import rdflib.plugins.sparql
from rdflib import ConjunctiveGraph
data = """
<urn:a> <urn:a> <urn:a> <urn:a> .
<urn:b> <urn:b> <urn:b> <urn:b> .
<urn:c> <urn:c> <urn:c> <urn:c> .
"""
if __name__ == "__main__":
rdflib.plugins.sparql.SPARQL_DEFAULT_GRAPH_UNION = False # Line A
rdflib.plugins.sparql.SPARQL_LOAD_GRAPHS = False
graph = ConjunctiveGraph()
graph.parse(data=data, format='nquads')
result = graph.query("SELECT * {?s ?p ?o}") # Line B
for resrow in result:
print(resrow)
Running this code behaves as expected, while I will show derived examples in the following, by altering Line A and Line B:
1. Replacing the Default Graph
rdflib.plugins.sparql.SPARQL_DEFAULT_GRAPH_UNION = True # Line A
result = graph.query("SELECT * FROM <urn:b> {?s ?p ?o}") # Line B
- What I see: returns all three statements.
- What I expect: Only the statement
<urn:b> <urn:b> <urn:b>
as result - Why is the actual result not correct?:
A SPARQL query may specify the dataset to be used for matching by using the FROM clause and the FROM NAMED clause to describe the RDF dataset. If a query provides such a dataset description, then it is used in place of any dataset that the query service would use if no dataset description is provided in a query.
(https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#specifyingDataset)
2. Specifying a Named Graph but Querying the Default Gaph
rdflib.plugins.sparql.SPARQL_DEFAULT_GRAPH_UNION = True # Line A
result = graph.query("SELECT * FROM NAMED <urn:b> { ?s ?p ?o}") # Line B
- What I see: returns all three statements.
- What I expect: no result
- Why is the actual result not correct?:
If there is no FROM clause, but there is one or more FROM NAMED clauses, then the dataset includes an empty graph for the default graph.
(https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#specifyingDataset)
3. Specifying a Named Graph
rdflib.plugins.sparql.SPARQL_DEFAULT_GRAPH_UNION = False # Line A
result = graph.query("SELECT * FROM NAMED <urn:b> { GRAPH ?g {?s ?p ?o}}") # Line B
- What I see: returns all three statements.
- What I expect: Only the statement
<urn:b> <urn:b> <urn:b> <urn:b>
as result - Why is the actual result not correct?: Because this is the idea of
NAMED GRAPH
to specify a named graph to query.
A query can supply IRIs for the named graphs in the RDF Dataset using the FROM NAMED clause. Each IRI is used to provide one named graph in the RDF Dataset.
(https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#specifyingDataset)
Because I think all of these three cases are related to each other I've put them into one issue, but sure they could also be split into three issues.