Brackets, OPTIONALs, BINDs, semantics, oh my.

Hi all,

I’ve a question on the model query writers should have when reasoning about their work. I think the naive view would be that adding grouping would _not_ alter the meaning of the query but this is not the case. 

A first example comes from OPTIONAL. A query like

    select * {
      ?a :ppp1 ?c .
      {
        optional { ?a :ppp2 ?d }
      }
   }

Is technically equivalent to

    select * {
      ?a :ppp1 ?c .
      ?a :ppp2 ?d .
   }

because the first form looks like

     (join (bgp ?a :ppp1 ?c)
             (left-join identity (bgp ?a :ppp2 ?d))

and the left-join will discard no solutions at run-time leaving the outer-most join to merge the two patterns and _discard_ any ?a’s that don’t have a :ppp2 triple.

This follows pretty clearly from the SPARQL definition but also seems surprising from a query writers perspective.

A second example occurs with BIND. 

     select * {
       ?a :ppp1 ?c .
        bind( 2 * ?c as ?twiceC )
     }

is _not_ the same as

     select * {
       ?a :ppp1 ?c .
        {
          bind( 2 * ?c as ?twiceC )
        }
     }

The second form will leave ?twiceC unbound as ?c has no binding inside of the BIND. Note that this also means that you cannot distribute common patterns out a union because

    select * {
      { 
        ?x a ?p .
        ?p ex:bar ?foo .
      }
      union
      { 
        ?x a ?p .
        bind(?p as ?foo)
      }
    }

is _not_ the same as

    select * {
      ?x a ?p .
      { 
        ?p ex:bar ?foo .
      }
      union
      { 
        bind(?p as ?foo)
      }
    }


If my examples are correct, then SPARQL seems more difficult than it should be to reason about than it should be. I’d welcome comments and thoughts.

thanks,
--
Gary Warren King, metabang.com 
Cell: (413) 559 8738
Fax: (206) 338-4052
gwkkwg on Skype * garethsan on AIM * gwking on twitter

Received on Wednesday, 16 December 2015 13:57:55 UTC