What is the scope of "values" in SPARQL query graph
Asked Answered
S

1

5

I have a query that includes many subqueries, which also include some subqueries. I want to know the scope of the values clause in a SPARQL query. Does it go down to the subqueries as well?

If I defined a values clause in the subquery, and used the same variable in a father query, will that variable have just the values specified in the son query? (what about vice versa?)

I looked at the SPARQL 1.1 spec, but all I found is the following, which doesn't appear to answer my question:

A VALUES block of data can appear in a query pattern or at the end of a SELECT query, including a subquery.

Schargel answered 25/2, 2016 at 12:33 Comment(0)
M
8

SPARQL subqueries are evaluated from the innermost to the outermost. A values block in an outer query isn't available to the inner queries. As an example:

select ?x ?y ?z {
  values ?x { "x" }

  { select (?x as ?y) ("z" as ?z) {} }
}
-----------------
| x   | y | z   |
=================
| "x" |   | "z" |
-----------------

If the value of ?x had been available in the inner query, then ?y would have the value "x", but it doesn't.

On the other hand, bindings from a values block in a subquery can be passed outward, if they are selected. For instance, in this query, the subquery binds ?x and ?y, but only projects ?x:

select ?x ?y {
  {
    select ?x {
      values ?x { "x" }
      values ?y { "y" }
    }
  }
}
-----------
| x   | y |
===========
| "x" |   |
-----------

The official answer is in the spec, but not in the part about values. It's in 18.2.1 Variable Scope. It's not the easiest table to follow, but the thing to note is rules like:

Group { P1 P2 … } : v is in-scope if it is in-scope in one or more of P1, P2, …

which means that the set of in-scope variables for a pattern { ... } is defined as the union of the in-scope variables of the things that appear within in. Variables get "passed" outward to the their enclosing forms, not the other way around.

There's one exception to that, I think, which is a values block that's outside the outermost query, which, I think, is pretty much the same as a values block inside the outermost query:

select ?x ?y ?yy {
  { select (?y as ?yy) {} }
}
values (?x ?y) { (1 2) }
--------------
| x | y | yy |
==============
| 1 | 2 |    |
--------------
Mada answered 25/2, 2016 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.