Wednesday, November 11, 2015

MINUS vs NOT EXISTS in SPARQL

In the lecture yesterday, we've had the question in how far MINUS differs from NOT EXISTS in SPARQL.  NOT EXISTS and MINUS represent two different ways of thinking about negation.

  • NOT EXISTS is based on testing whether a pattern exists in the data, given the bindings already determined by the query pattern.
  • MINUS is based on removing matches based on the evaluation of two patterns. 
While in most cases both versions deliver the same result, there are some cases where both can produce different answers. Let's have a look at an example [1]:

Given the following RDF knowledge base:

@prefix : <http://example/> .
:a :b :c .

Consider the following SPARQL query with NOT EXISTS:

SELECT *

  ?s ?p ?o
  FILTER NOT EXISTS { ?x ?y ?z }
}

What would be the result in this case?
The result set of the SPARQL query has no solutions because {?x ?y ?z} matches given any ?s ?p ?o, so NOT EXISTS {?x ?y ?z} eliminates any solutions.

Now consider the same query with MINUS:

SELECT *

   ?s ?p ?o 
   MINUS 
     { ?x ?y ?z }
}

With MINUS, there is no shared variable between the first part (?s ?p ?o) and the second (?x ?y ?z) so no bindings are eliminated.

You will find additional examples in [1]. You can also find information about this issue in the Wiki of the W3C SPARQL Working group [2].






1 comment:

  1. The explanation of MINUS and NOT EXISTS seem to be swapped. Am I right?

    ReplyDelete