SPARQL is weird…


It really is. The following query is the only way I found to exclude folders when looking for files:

select ?r where {
   ?r a nfo:FileDataObject .
   OPTIONAL { ?r2 a nfo:Folder . FILTER(?r = ?r2) . } .
   FILTER( !BOUND(?r2) ) .
}

Now to me this looks really weird. And maybe I am simply not seeing the wood for all the trees…

10 thoughts on “SPARQL is weird…

  1. No, you are seeing it right. The lack of any convenient way to do negation is one of the most commonly asked for features in SPARQL. To that end, the 2nd SPARQL Working Group is currently working on standardizing a more straightforward way of asking queries like this. The exact syntax is still very much up in the air, but your query above will probably look something like:

    select ?r where {
    ?r a nfo:FileDataObject .
    NOT { ?r a nfo:Folder }
    }

    Lee

  2. Yup. That’s look like what I ended up with for some media-related queries I’m doing for Bangarang. I don’t feel like a complete idiot now. :-)

    • I see. Does bif:exists have a better performance than the “classical” SPARQL 1.0 approach? Because otherwise there is no real advantage in changing the query as using bif:exists is rather complex, too.

  3. bif:exists() may be faster than OPTIONAL… FILTER() or may be slower. When the query is simple enough the optimizer will find full set of possible execution plans for any of these notations and these sets will be identical, so the best plan found will be the same for both variants. When the query is complicated enough (and number of execution plans grow exponentially), the compiler will evaluate some subset of whole set of plans starting from different points (left outer join in case of OPTIONAL…FILTER() and “straightforward” subselect for bif:exists()). Substs will usually intersect and at least some execution plains in the intersection are usually very good, but some plans outside the intersection might be just perfect, so the notation works as a “fine tuning” for the optimizer.

    With usual settings, “query is complicated enough” means “one page long” for SPARQL on RDF data or “half page long” for sophisticated SPARQL-BI on RDF Views.

  4. My late 2c. The lack of negation in SPARQL is part of the “Open World Assumption” endorsed by Semantic Web designers. It is a feature, not a bug.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s