Open white-gecko opened 6 years ago
For a while, I thought this might be related to the identifier-as-context changes but now I realise that it's unrelated. fwiw, test included:
import rdflib.plugins.sparql
from rdflib import ConjunctiveGraph
def test_issue811():
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> .
"""
rdflib.plugins.sparql.SPARQL_DEFAULT_GRAPH_UNION = False
rdflib.plugins.sparql.SPARQL_LOAD_GRAPHS = False
graph = ConjunctiveGraph()
graph.parse(data=data, format="nquads")
assert len(graph) == 3
assert len(graph.query("SELECT * {?s ?p ?o .}")) == 0
# Set default graph as UNION, CORRECT result
rdflib.plugins.sparql.SPARQL_DEFAULT_GRAPH_UNION = True
assert len(graph.query("SELECT * {?s ?p ?o .}")) == 3
# Use FROM to specify <urn:b> as the default graph
# Set default graph as UNION, INCORRECT result
rdflib.plugins.sparql.SPARQL_DEFAULT_GRAPH_UNION = True
assert (
len(graph.query("SELECT * FROM <urn:b> {?s ?p ?o}")) == 3
), "should be 1 triple"
# Set default graph as NON-UNION, CORRECT result
rdflib.plugins.sparql.SPARQL_DEFAULT_GRAPH_UNION = False
assert (
len(graph.query("SELECT * FROM <urn:b> {?s ?p ?o}")) == 1
), "should be 1 triple"
# Use FROM NAMED to specify <urn:b> as target graph
# Set default graph as UNION, INCORRECT result
rdflib.plugins.sparql.SPARQL_DEFAULT_GRAPH_UNION = True
assert (
len(graph.query("SELECT * FROM NAMED <urn:b> {?s ?p ?o}")) == 3
), "should be 1 triple"
# Set default graph as NON-UNION, CORRECT result
rdflib.plugins.sparql.SPARQL_DEFAULT_GRAPH_UNION = False
assert (
len(graph.query("SELECT * FROM NAMED <urn:b> {?s ?p ?o}")) == 1
), "should be 1 triple"
will add with xfail if I have time
Since the issue still doesn't have a fix, I made one:
For the moment, just for the queries, it would still be necessary to manage update USING
/USING NAMED
clauses.
What I implemented:
Include only the graphs in FROM
clause in the query's default graph
Include only the graphs in the FROM NAMED
clause in the query's named graphs
And also:
Try to load external graphs only if they don't already exist in the given ConjunctiveGraph
In my understanding of the w3c spec, if we define a FROM
clause, the query's RDF dataset is considered explicit, and if there is no FROM NAMED
clause, then named graph is considered empty set. And vice versa. That is what I implemented.
I want to use
FROM
andFROM 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:
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
<urn:b> <urn:b> <urn:b>
as result2. Specifying a Named Graph but Querying the Default Gaph
3. Specifying a Named Graph
<urn:b> <urn:b> <urn:b> <urn:b>
as resultNAMED GRAPH
to specify a named graph to query.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.