W3C home > Mailing lists > Public > public-rdf-dawg-comments@w3.org > May 2009

Re: question about OPTIONAL and UNION key words

From: Richard Newman <rnewman@twinql.com>
Date: Thu, 28 May 2009 00:43:20 -0700
Cc: public-rdf-dawg-comments@w3.org
Message-Id: <7C1EB86A-92FC-4EC7-A0E5-CAAC8628F954@twinql.com>
To: Mo Zhou <mozhou@umail.iu.edu>
> I recently read the specification. It is very helpful. However I got
> confused by the difference between OPTIONAL and UNION key words. In
> all queries using OPTIONAL, we can use UNION to write a query that
> generates the same results. and vice versa.
> ...

> So why the specification includes both OPTIONAL and UNION?

I'm afraid you've misread the spec.

You can rewrite some queries in the other form, but not in the way you  

OPTIONAL binds a dependent clause to another; UNION executes clauses  
in parallel. Your first query:

   ?x foaf:name ?name .
   optional {?x foaf:mbox ?mbox}
   optional {?x foaf:homepage ?hpage}

can be approximately phrased in English as "find all ?x with a name.  
for each of those ?x, try to find their mailbox...".

Your second query:

   {?x foaf:name ?name }
   {?x foaf:mbox ?mbox}
   {?x foaf:homepage ?hpage}

is instead "find all people with names. also find all people with  
mboxes. also find all people with homepages".

You might find it helpful to consider the algebraic forms of these.  
The UNION query (pulled out of AllegroGraph) is:

     (:bgp #(?x !<http://xmlns.com/foaf/0.1/name> ?name))
     (:bgp #(?x !<http://xmlns.com/foaf/0.1/mbox> ?mbox)))
   (:bgp #(?x !<http://xmlns.com/foaf/0.1/homepage> ?hpage)))

whilst the OPTIONAL query is:

     (:bgp #(?x !<http://xmlns.com/foaf/0.1/name> ?name))
     (:bgp #(?x !<http://xmlns.com/foaf/0.1/mbox> ?mbox))
   (:bgp #(?x !<http://xmlns.com/foaf/0.1/homepage> ?hpage))

Quite different!

Received on Thursday, 28 May 2009 07:43:54 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 20:52:10 UTC