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.18k stars 558 forks source link

Using FROM and FROM NAMED on ConjunctiveGraph behaves not standard conform #811

Open white-gecko opened 6 years ago

white-gecko commented 6 years ago

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

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

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

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.

ghost commented 2 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"
aucampia commented 2 years ago

will add with xfail if I have time

aucampia commented 2 years ago

See also:

apicouSP commented 5 months ago

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.