4

Is the use of BIND valid?

# Query 1
SELECT * {
  BIND (<http://ex/p2> as ?p)
  {
    BIND (<http://ex/p1> as ?p)
    ?s ?p ?o
  }
}

When run on turtle data:

@prefix h: <http://ex/> .
h:s1 h:p1 h:o1 .
h:s2 h:p2 h:o2 .

arq 3.11.0 gives as result:

-------------
| p | s | o |
=============
-------------

without throwing an error (not even when using --strict). But removing the group or changing the order does give an error:

# Query 2
SELECT * {
  BIND (<http://ex/p2> as ?p)
  BIND (<http://ex/p1> as ?p)
  ?s ?p ?o
}

BIND: Variable used when already in-scope: ?p in BIND(<http://ex/p1> AS ?p)

and:

# Query 3
SELECT * {
  {
    BIND (<http://ex/p1> as ?p)
    ?s ?p ?o
  }
  BIND (<http://ex/p2> as ?p)
}

BIND: Variable used when already in-scope: ?p in BIND(<http://ex/p2> AS ?p)

Questions:

  1. Does Query 1 violate 10.1 BIND which states:

    The variable introduced by the BIND clause must not have been used in the group graph pattern up to the point of use in BIND.

  2. My reading of 18.2.1 Variable Scope is that in Query 1, the variable ?p is in-scope inside the inner group and therefore also in-scope outside the group. Would the arq error message for Query 2 also be applicable to the Query 1?

I would be grateful for a clarification.

zut
  • 796
  • 4
  • 12

1 Answers1

2
  1. No, Query 1 does not violate the rules for BIND, and the query is valid. That is because query evaluation of groups in SPARQL is defined inside-out. Inner groups are evaluated separately, and before outer groups. If the inner group is taken on its own, the use of BIND in it is obviously fine, because ?p has not yet been bound in the group. In the spec text you quote, the two key parts are: “must not have been used … in the group graph patternup to the point of use in BIND.”
  2. In-scope variables need to be determined for each clause in a group separately, or in other words, at different places in a group, different variables are in scope. For the outer group in Query 1, before the BIND, no variables are in scope, and hence the BIND is valid. After the BIND, ?p is in scope, which is why a second BIND gives an error in Query 2. One way to think about this: A group starts with an empty scope, and each clause may contribute variables to the group. If a variable has already been contributed by a preceding clause, BIND may not assign that variable.
cygri
  • 9,102
  • 1
  • 22
  • 47
  • 1
    I didn't know that the order of `BIND` matters but it looks like it's indeed the case given that's the only difference between query 1 and query 3. Funny to see this, should keep it in mind - thanks for clarification – UninformedUser May 14 '19 at 17:18
  • Yes, with BIND, OPTIONAL and MINUS, the order matters. – cygri May 14 '19 at 20:03
  • yeah, with `OPTIONAL` and `MINUS` I knew already, with `BIND` I had no problems so far or better said, I used it never with same variable names etc. - but honestly didn't think about the order. Nice to know. But, not sure whether this is intuitive for all people. Sure, the same holds for `OPTIONAL` I guess, when not thinking of a left-join. So one more time that I learned it's worth reading specs of a formal language carefully – UninformedUser May 15 '19 at 03:25