On 9 Jul 2013, at 21:01, Joshua TAYLOR <[email protected]> wrote:

> prefix : <http://example.org/>
> select ?element ?index ?element2 ?index2 where {
>  ?element :atIndex ?index .
>  OPTIONAL {
>    BIND( ?index - 1 as ?index2 )
>    ?element2 :atIndex ?index2 .
>  }
> }
> order by ?index
> 
> 
> produces lots more results; 

SPARQL is evaluated inside out, so start with:

>  {
>    BIND( ?index - 1 as ?index2 )
>    ?element2 :atIndex ?index2 .
>  }

?index is unbound, so ?index2 isn't bound by that first bit. Effectively you 
just have:

{ ?element2 :atIndex ?index2 }

OPTIONAL does nothing, since there's no shared variable, so you end up with a 
cross join:

{ ?element :atIndex ?index  }
{ ?element2 :atIndex ?index2 }

i.e. every combination.

> Now, using a FILTER in the OPTIONAL:
> 
> 
> prefix : <http://example.org/>
> 
> select ?element ?index ?element2 ?index2 where {
>  ?element :atIndex ?index .
>  OPTIONAL {
>    FILTER( ?index - 1 = ?index2 )
>    ?element2 :atIndex ?index2 .
>  }
> }
> order by ?index
> 
> 
> produces results that actually show that ?index2 is constrained, and
> are almost the same the as the first query (except that the case where
> ?index2 is -1 doesn't occur):

Ok, _now_ I'm scratching my head.

Damian

Reply via email to