GitHub user afs added a comment to the discussion: Discrepancies in SPARQL 
implementations - Aggregate expression in SELECT and its alias in GROUP BY

> SPARQL 1.1 specification does not clarify this use case.

It may be counter-intuitive but isn't an error per se.

It's an outcome of the scoping rules: 
https://www.w3.org/TR/sparql11-query/#variableScope .
Scopes are usually more obvious in the query e.g. nested SELECTs.

A variable is in-scope if is potentially defined. If it is not mentioned, it 
isn't potentially defined, so it is not "in-scope".

The `WHERE` clause does not mention `?bookCount` so `?bookCount` is not 
in-scope coming out of the BGP.
If it had a `OPTIONAL { ?a ?b ?bookCount . FILTER(false)}` it would be 
potentially defined / in-scope.

The `GROUP BY` group keys are expressions, they don't assign and don't 
contribute to variables being potentially defined.
You can use `AS` in grouping, and that is an assignment. `GROUP BY (expr AS 
?var)` would add `?var`to the scope.

The output of the group stage can not contain a setting for `?bookCount` 
because it is never used in a place where it can be set.
That makes it legal in `AS ?bookCount`.

```
    (project (?author ?bookCount)
      (extend ((?bookCount ?.0))
        (group (?author ?bookCount) ((?.0 (count ?book)))
          (bgp
            (triple ?book <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
ex:Book)
            (triple ?book ex:writtenBy ?author)
          ))))
```
If you want the intuition to happen, the variable scope table would need a new 
row saying any variable mentioned in `GROUP BY expr` is in-scope. It would be 
the only place where expressions contribute to scope.

There is a case to made that it should be like `SELECT ?v`, although the `?v` 
can not be a general expression.



GitHub link: 
https://github.com/apache/jena/discussions/3644#discussioncomment-15261613

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to