Take on SPARQL OPTIONAL algebra

16 views
Skip to first unread message

Christoph Burgmer

unread,
Jan 23, 2011, 2:09:47 PM1/23/11
to rdflib-dev
Hi list, Chimezie,

Tickets http://code.google.com/p/rdfextras/issues/detail?id=7 and
http://code.google.com/p/rdfextras/issues/detail?id=8 both have issues
arising from the usage of the OPTIONAL keyword. I believe both stem
from a missunderstanding of how the OPTIONAL keywords works for an
empty left side, e.g. "SELECT ?s WHERE { OPTIONAL { ?s ?p ?o } }"

The two lines in rdfextras/sparql/algebra.py that take care of this
case return an EmptyGraphPatternExpression pointing to a discussion on
rdf-dawg [1]. However I don't see how the implemented behaviour is
justified by both the SPARQL recommendation [2] nor the linked
discussion.

Also, Franz AllegroGraph and OpenLink Virtuoso both behave differently
when being confronted with the respective queries.

I thus want to propose the patch attached to [3] to change the current
behaviour to return results for the following test cases:

>>> import rdflib
>>> rdflib.plugin.register('sparql', rdflib.query.Processor,
... 'rdfextras.sparql.processor', 'Processor')
>>> rdflib.plugin.register('sparql', rdflib.query.Result,
... 'rdfextras.sparql.query',
'SPARQLQueryResult')
>>>
>>> from rdflib.graph import ConjunctiveGraph
>>> from rdflib.term import URIRef
>>> x = ConjunctiveGraph()
>>> x.add((URIRef("http://a"), URIRef("http://b"), URIRef("http://c")))
>>>
>>> print(list(x.query("SELECT ?s WHERE { ?s ?p ?o } ", processor="sparql")))
[rdflib.term.URIRef('http://a')]
>>> print(list(x.query("SELECT ?s WHERE { OPTIONAL { ?s ?p ?o } } ", processor="sparql")))
[rdflib.term.URIRef('http://a')]
>>>
>>> list(x.query("select ?x ?a ?b { optional { ?a ?b ?c } ?x ?y ?z optional { ?d ?e ?f } }"))
[[rdflib.term.URIRef('http://a'), rdflib.term.URIRef('http://a'),
rdflib.term.URIRef('http://b')]]
>>>

I can't say I fully understand the workings of the SPARQL algebra, so
I might be wrong on this one. Any help to clearing that up
appreciated.
-Chris

[1] http://lists.w3.org/Archives/Public/public-rdf-dawg/2007AprJun/0046.html
[2] http://www.w3.org/TR/rdf-sparql-query/#optionals
[3] http://code.google.com/p/rdfextras/issues/detail?id=7
Reply all
Reply to author
Forward
0 new messages