W3C home > Mailing lists > Public > public-rdf-dawg-comments@w3.org > October 2011

Re: Clarification of variable scoping

From: Andy Seaborne <andy.seaborne@epimorphics.com>
Date: Fri, 07 Oct 2011 09:02:01 +0100
Message-ID: <4E8EB1F9.9050105@epimorphics.com>
To: public-rdf-dawg-comments@w3.org
David,

Section 10.1 "BIND: Assigning to Variables" says it ends the current 
basic graph pattern. Section 18.2.2.5 "Translate Graph Patterns" defines 
how it maps the "extend" operator in the SPARQL algebra. It is then 
within a group graph pattern. The purpose of bind is to set a variable 
based on the current basic graph pattern, not the whole query.

Evaluation of a group graph pattern in SPARQL happens by evaluating each 
of the separate elements of the group and then combining them with JOIN. 
While it can be tempting to top-to-bottom, that is not how query 
evaluation is defined (from SPARQL 1.0).

The query document isn't a complete tutorial for SPARQL (that would be a 
much larger task). The formal sections give a precise definition and 
section 18.2.2.5 covers how the BIND syntax maps to the extend operator 
and how it interacts with the syntax group, becoming a "join" to be 
evaluated.

We would be grateful if you would acknowledge that your comment has been 
answered by sending a reply to this mailing list.

Andy (on behalf of the SPARQL WG)

(more inline)

On 13/09/11 18:20, David Booth wrote:
> First, thanks again for the great work on SPARQL!
>
> I think a bit more clarification (and perhaps examples) of variable
> scoping would be helpful in the SPARQL Query spec.
>
> I tried the following query (in Parliament 2.7.1):
>
>    # Example 1: no results
>    SELECT *
>    WHERE {
>      BIND( 10 as ?ten )
>      GRAPH<http://example/bookStore>  {
>        FILTER( ?ten>= ?ten )
>      }
>    }
>
> and it produced 0 results.  However, when I moved the BIND inside the
> GRAPH clause, the following query worked, producing one result with 10
> bound to ?ten:
>
>    # Example 2: one result
>    SELECT *
>    WHERE {
>      GRAPH<http://example/bookStore>  {
>        BIND( 10 as ?ten )
>        FILTER( ?ten>= ?ten )
>      }
>    }
>
> Based on investigation, it looks like ?ten is unbound at the point of
> the FILTER clause in example 1, whereas it *is* bound at the point of
> the FILTER clause in example 2.   This behavior was non-intuitive (to
> me), but it is not evident to me from the spec whether this is
> considered correct behavior or not.  In looking at the variable scoping
> rules in section 18.2.1
> http://www.w3.org/TR/sparql11-query/#variableScope
> the table says that "v is in-scope".  But "in-scope" *where*?  It is not
> clear where the scopes are and which scope is meant.  The spec *does*
> clearly say that "use of a variable in FILTER, or in MINUS does not
> cause a variable to be in-scope outside of those forms".  But I would
> have expected the BIND statement to *cause* the variable to be bound
> within the FILTER clause.
>
> This behavior seems counterintuitive to one who is accustomed to
> variable scoping rules in programming languages, since AFAIK the purpose
> of BIND is to set a variable that can be used in throughout the query.
> But if the behavior above is correct, I do not yet see how to do that.
> For example, I'd like to use the same variable ?limit bound to the
> constant 10 in multiple GRAPH clauses like this:
>
>    SELECT *
>    WHERE {
>      GRAPH ... {
>        FILTER( ?n<= ?limit )
>      }
>      GRAPH ... {
>        FILTER( ?n<= ?limit )
>      }
>      GRAPH ... {
>        FILTER( ?n<= ?limit )
>      }
>
>    }
>
> But where should I put the BIND statement to achieve this?

You may be able to achieve the effect by placing the FILTER further out:

SELECT *
WHERE {
   { SELECT * # including ?n
     {
       GRAPH ... { }
       GRAPH ... { }
       GRAPH ... { }
     }
   }
   FILTER( ?n <= ?limit )
}


>
>
> Thanks
>
Received on Friday, 7 October 2011 08:02:40 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 7 October 2011 08:02:40 GMT