On Wed, Jul 10, 2013 at 11:25 AM, Damian Steer <[email protected]> wrote: > > On 10 Jul 2013, at 12:36, Joshua TAYLOR <[email protected]> wrote: > >> I know that when there are *sub*-queries in a query, they're evaluted >> from the inside out (i.e., innermost subqueries are evaluated first), >> but this doesn't make sense for OPTIONAL patterns, does it? The whole >> point of an optional pattern is to *extend* the solutions of the >> enclosing pattern if possible. > > They are extended in the join. Evaluate one block, evaluate other block, > join. What order this happens in may vary, as you say, but the answers are > supposed to be equivalent. > > I'm still fuzzy looking at the spec on the FILTER case, but on BIND here are > a couple of fun facts: > > * 'Use of BIND ends the preceding basic graph pattern' > > OPTIONAL { > BIND( ?index - 1 as ?index2 ) > ?element2 :atIndex ?index2 . > } > > BIND here is doing nothing at all, since it closes the block and there's > nothing preceding it. If you look at the output of --explain you'll find > > (extend ((?index2 (- ?index 1))) > (table unit)) > > i.e. it's acting on nothing. > > If you add it after the ?element2 triple you'll get an error, since ?index2 > is already bound. > > * You can rearrange your second query to get what you want: > > OPTIONAL { > ?element2 :atIndex ?index2 . > BIND( ?index2 + 1 as ?index) > }
This doesn't actually work though, because BIND isn't allowed to bind a variable that's already in scope. So this query : prefix : <http://example.org/> select ?element ?index ?element2 ?index2 where { ?element :atIndex ?index . OPTIONAL { ?element2 :atIndex ?index2 . BIND( ?index - 1 as ?index2 ) } } order by ?index produces: $ arq --data data.n3 --query query.sparql BIND: Variable used when already in-scope: ?index2 in BIND(( ?index - 1 ) AS ?index2) -- Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
