RDFLib/rdflibissueplayground — Issue #25: SPARQL FROM loads an IRI from the web instead of using the graphs context
Author: gjhiggins
Created: 2012-02-20 07:43:48+00:00
State: open

christop...@gmail.com, 2011-06-14T21:41:08.000Z

I have a graph with some triples stored in a certain context. When querying with SPARQL the context's URI is interpreted as an URL which is then queried:

>>> import rdflib
>>> from rdflib import *
>>> rdflib.plugin.register('sparql', rdflib.query.Processor,
... 'rdfextras.sparql.processor', 'Processor')
>>>
>>> rdflib.plugin.register('sparql', rdflib.query.Result,
... 'rdfextras.sparql.query', 'SPARQLQueryResult')
>>> graph = ConjunctiveGraph("IOMemory")
>>> graph.load("http://www.w3.org/People/Berners-Lee/card.rdf", "http://test")
>>> r = graph.query("select distinct ?s FROM { ?s ?p ?o }").serialize('json')
Traceback (most recent call last):
File "", line 1, in
File "/tmp/rdflib/graph.py", line 800, in query
return result(processor.query(query_object, initBindings, initNs, **kwargs))
File "/tmp/rdfextras/sparql/processor.py", line 43, in query
extensionFunctions=extensionFunctions)
File "/tmp/rdfextras/sparql/algebra.py", line 305, in TopEvaluate
LoadGraph(dtSet,dataSetBase,memGraph)
File "/tmp/rdfextras/sparql/algebra.py", line 256, in LoadGraph
source=Resolver().resolve(str(dtSet), dataSetBase)
File "/usr/lib/python2.6/dist-packages/Ft/Lib/Uri.py", line 988, in resolve
uri=uri, loc=uri, msg=str(e))
Ft.Lib.UriException: Error retrieving resource 'http://test':

From OpenLink Virtuoso and AllegroGraph I know that FROM & FROM NAMED queries access the graph's different contexts.

I found a short way to hack around this, however the overall documentation is low, and I can't understand what exactly happends in this place (rdfextras/sparql/algebra.py):

in line 302 ff. of TopEvaluate() a graph is created and LoadGraph() does the URL lookup:

memStore = plugin.get('IOMemory',Store)()
memGraph = Graph(memStore)
LoadGraph(dtSet,dataSetBase,memGraph)

If I change that to

memGraph = dataset.get_context(dtSet)

and thus just load the data from the context I yield the expected behaviour.

I would take a deeper look into the code, however
1) as said above, documentation is few and I don't want to break any expected behaviour here
2) It seems triplestores out there behave differently. So far I couldn't find any reasonable explanation on how things "should" work.

Comment 1 by gromgull
Hmm - it seems both behaviours could be useful.
There is a test - which assumes the current behaviour:

http://code.google.com/p/rdfextras/source/browse/test/test_sparql/test_advanced_sparql_constructs.py#64

We could perhaps introduce a flag, but default to NOT loading more triples