W3C home > Mailing lists > Public > public-rdf-dawg@w3.org > January to March 2010

Examples of MINUS and NOT EXISTS

From: Andy Seaborne <andy.seaborne@talis.com>
Date: Tue, 30 Mar 2010 13:46:50 +0100
Message-ID: <4BB1F2BA.8060102@talis.com>
To: SPARQL Working Group <public-rdf-dawg@w3.org>
kasei gave an example of a query that was different under MINUS and NOT 
EXISTS.  I've used it to generate a number of cases to show here they 
are the same wand where different.  It reinforces Lee's characterization 
of the graph-centric and table-centric (resource-centic?) styles.


http://www.w3.org/2009/sparql/docs/features/#Negation_description
mentions testing whether a triples do or not do occur in the graph.

Query 2/3 is interesting: testing for the (non)existence of a specific 
triple:

The data is a single triple graph in all cases:

# Data -------
@prefix : <http://example/> .

:a :b :c .
# Data -------


Query 2:
{ ?s ?p ?o NOT EXISTS { :a :b :c } } ==> no rows
{ ?s ?p ?o MINUS { :a :b :c } } ==> one row

Query 3:
{ ?s ?p ?o NOT EXISTS { :x :y :z } } ==> one row
{ ?s ?p ?o MINUS { :x :y :z } } ==> one row

This one is also a place where they differ:

Query 5:
{ ?s ?p ?o NOT EXISTS { ?x ?y ?z } } ==> no rows
{ ?s ?p ?o MINUS { ?x ?y ?z } } ==> one rows

These examples are mechanically produced with a simple implementation of 
each operation directly from the spec.  So, bugs not withstanding, we 
can analyse other examples systematically.

	Andy



# -------- Query 1 : NOT EXIST
# Query ------------------------------------
   1 PREFIX  :     <http://example/>
   2
   3 SELECT  *
   4 WHERE
   5   { ?s ?p ?o
   6     NOT EXISTS
   7       { ?s ?p ?o }
   8   }
# Results ----------------------------------
-------------
| s | p | o |
=============
-------------

# -------- Query 1 : MINUS
# Query ------------------------------------
   1 PREFIX  :     <http://example/>
   2
   3 SELECT  *
   4 WHERE
   5   { ?s ?p ?o
   6     MINUS
   7       { ?s ?p ?o }
   8   }
# Results ----------------------------------
-------------
| s | p | o |
=============
-------------

# -------- Query 2 : NOT EXIST
# Query ------------------------------------
   1 PREFIX  :     <http://example/>
   2
   3 SELECT  *
   4 WHERE
   5   { ?s ?p ?o
   6     NOT EXISTS
   7       { :a :b :c }
   8   }
# Results ----------------------------------
-------------
| s | p | o |
=============
-------------

# -------- Query 2 : MINUS
# Query ------------------------------------
   1 PREFIX  :     <http://example/>
   2
   3 SELECT  *
   4 WHERE
   5   { ?s ?p ?o
   6     MINUS
   7       { :a :b :c }
   8   }
# Results ----------------------------------
----------------
| s  | p  | o  |
================
| :a | :b | :c |
----------------

# -------- Query 3 : NOT EXIST
# Query ------------------------------------
   1 PREFIX  :     <http://example/>
   2
   3 SELECT  *
   4 WHERE
   5   { ?s ?p ?o
   6     NOT EXISTS
   7       { :x :y :z }
   8   }
# Results ----------------------------------
----------------
| s  | p  | o  |
================
| :a | :b | :c |
----------------

# -------- Query 3 : MINUS
# Query ------------------------------------
   1 PREFIX  :     <http://example/>
   2
   3 SELECT  *
   4 WHERE
   5   { ?s ?p ?o
   6     MINUS
   7       { :x :y :z }
   8   }
# Results ----------------------------------
----------------
| s  | p  | o  |
================
| :a | :b | :c |
----------------

# -------- Query 4 : NOT EXIST
# Query ------------------------------------
   1 PREFIX  :     <http://example/>
   2
   3 SELECT  *
   4 WHERE
   5   { ?s ?p ?o
   6     NOT EXISTS
   7       { ?s :b :c }
   8   }
# Results ----------------------------------
-------------
| s | p | o |
=============
-------------

# -------- Query 4 : MINUS
# Query ------------------------------------
   1 PREFIX  :     <http://example/>
   2
   3 SELECT  *
   4 WHERE
   5   { ?s ?p ?o
   6     MINUS
   7       { ?s :b :c }
   8   }
# Results ----------------------------------
-------------
| s | p | o |
=============
-------------

# -------- Query 5 : NOT EXIST
# Query ------------------------------------
   1 PREFIX  :     <http://example/>
   2
   3 SELECT  *
   4 WHERE
   5   { ?s ?p ?o
   6     NOT EXISTS
   7       { ?x ?y ?z }
   8   }
# Results ----------------------------------
-------------
| s | p | o |
=============
-------------

# -------- Query 5 : MINUS
# Query ------------------------------------
   1 PREFIX  :     <http://example/>
   2
   3 SELECT  *
   4 WHERE
   5   { ?s ?p ?o
   6     MINUS
   7       { ?x ?y ?z }
   8   }
# Results ----------------------------------
----------------
| s  | p  | o  |
================
| :a | :b | :c |
----------------

# -------- Query 6 : NOT EXIST
# Query ------------------------------------
   1 PREFIX  :     <http://example/>
   2
   3 SELECT  *
   4 WHERE
   5   { ?s ?p ?o
   6     NOT EXISTS
   7       { ?s :b :c
   8         FILTER ( ?s = :a )
   9       }
  10   }
# Results ----------------------------------
-------------
| s | p | o |
=============
-------------

# -------- Query 6 : MINUS
# Query ------------------------------------
   1 PREFIX  :     <http://example/>
   2
   3 SELECT  *
   4 WHERE
   5   { ?s ?p ?o
   6     MINUS
   7       { ?s :b :c
   8         FILTER ( ?s = :a )
   9       }
  10   }
# Results ----------------------------------
-------------
| s | p | o |
=============
-------------
Received on Tuesday, 30 March 2010 12:47:22 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 16:15:42 GMT