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.11k stars 547 forks source link

Fix explicit dataset (`FROM` and `FROM NAMED` clauses) #2794

Open apicouSP opened 1 month ago

apicouSP commented 1 month ago

Summary of changes

WHAT

FROM or FROM NAMED clause: redefine entirely the query's RDF dataset

When a query (SELECT, CONSTRUCT, ASK, DESCRIBE) is using a FROM or FROM NAMED clause, we redefine entirely the query's RDF dataset Include only the graphs in FROM clauses in the query's default graph Include only the graphs in the FROM NAMED clauses in the query's named graphs

As a consequence, when a user defines a FROM clause in his query but does not define FROM NAMED, then the named graphs are considered empty set. And vice versa: if a user defines a FROM NAMED clause but does not define a FROM clause, then the default graph is considered empty. I added tests specifically for that That's my interpretation of this

Also since the RDF Dataset is entirely redefined when using FROM or FROM NAMED clauses then for those queries the parameter SPARQL_DEFAULT_GRAPH_UNION will be ignored

Load external graphs only if they don't already exist

Try to load external graphs only if they don't already exist in the given ConjunctiveGraph

As a consequence, you don't have to set SPARQL_DEFAULT_GRAPH_UNION to False if all the graphs mentioned in FROM and FROM NAMED clauses already exist in the given ConjunctiveGraph

WHAT it is not

WHY:

So queries behave closer to W3C spec

Issues:

This PR fixes this issue (confirmed) And also that issue as far as I understand This also solves partially this discussion

Checklist

apicouSP commented 2 weeks ago

I now use Dataset instead of ConjunctiveGraph as this one is now deprecated. I also fixed style and type errors in new tests. Now black, ruff and mypy should pass. Let me know if you need something to merge this! Sorry I made too much commits.

coveralls commented 1 week ago

Coverage Status

coverage: 91.058% (+0.03%) from 91.03% when pulling 8ed095c90a0c6eedf614857a9417fe9145fef65e on apicouSP:fix-explicit-dataset into 0ecc40009ae397c2798c0c08a2d751a1a9d2f8a7 on RDFLib:main.