rdflib: Problem with nested FILTER NOT EXISTS statements

The following query:

PREFIX ex: <http://www.example.de#>
SELECT ?a ?b WHERE {
 ?a ex:rel ?b
 FILTER NOT EXISTS {
  ?a ex:rel ?b.
  FILTER NOT EXISTS {?c ex:rel2 ?b.}
 }
}

should not deliver any results on this single triple (because it lacks a triple binding for ?c ex:rel2 ?b).:

@prefix ex: <http://www.example.de#>.
ex:a ex:rel ex:b.

In contrast, however, it should deliver results on the complete set of triples:

@prefix ex: <http://www.example.de#>.
ex:a ex:rel ex:b.
ex:c ex:rel2 ex:b.

The problem is that rdflib (also the new 4.2.2 version which fixed a similar issue #615 ) in my test delivers a result in both cases. It seems to me that the two nested FILTER statements are not evaluated correctly:

data loaded
Triples: 1
(rdflib.term.URIRef(u'http://www.example.de#a'), rdflib.term.URIRef(u'http://www.example.de#rel'), rdflib.term.URIRef(u'http://www.example.de#b'))
--------------------------------------------------------------------------------
PREFIX ex: <http://www.example.de#>

SELECT ?a ?b WHERE {
 ?a ex:rel ?b
 FILTER NOT EXISTS {
 ?a ex:rel ?b.
  FILTER NOT EXISTS {?c ex:rel2 ?b.}
 }
}
OUTPUT:
number of results:1
(rdflib.term.URIRef(u'http://www.example.de#a'), rdflib.term.URIRef(u'http://www.example.de#b'))

Nested filter statements are very important for checking completeness of a dataset. Is there a way to fix this?

simon

About this issue

  • Original URL
  • State: open
  • Created 10 years ago
  • Comments: 16 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks for the quick response. The fix looks very promising. (I was testing out ways to circumvent using the “filter not exists” statement to check whether this also occurs when using nested bound statements and it seemed to be having similar issues to the “filter not exists” statements. So the “elif graphPattern.translated:” should also solve these nested and filtered bound statements I was experimenting with.) Thanks!