This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 15728 - [XQ30] Scope of variables defined in group-by
Summary: [XQ30] Scope of variables defined in group-by
Status: CLOSED FIXED
Alias: None
Product: XPath / XQuery / XSLT
Classification: Unclassified
Component: XQuery 3.0 (show other bugs)
Version: Member-only Editors Drafts
Hardware: PC All
: P2 normal
Target Milestone: ---
Assignee: Jonathan Robie
QA Contact: Mailing list for public feedback on specs from XSL and XML Query WGs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-26 08:59 UTC by Michael Kay
Modified: 2013-06-19 09:13 UTC (History)
2 users (show)

See Also:


Attachments

Description Michael Kay 2012-01-26 08:59:22 UTC
The scope of variables declared in a group-by clause is governed by the general rules in 3.10.1:

The scope of a bound variable includes all subexpressions of the containing FLWOR that appear after the variable binding.

(This differs from the window clause, which has rules of its own).

This means that it is legal to write:

for $e in //employee
group by $s := $e/salary, $t := string($s)

But we don't say what this is supposed to mean. We say in 3.10.7:

For each pre-grouping tuple, the grouping keys are computed by evaluating the expression in the GroupingSpec and atomizing the result.

But $s is not present in the pre-grouping tuple, and therefore $t cannot be computed for a pre-grouping tuple. And we can't use the value of $s from the post-grouping tuple because that's only available after doing the grouping, and we can't do the grouping until we know $t. We could try to define some kind of model where the grouping is done in a sequence of steps each of which generates intermediate tuples, but it would be complex and probably lead to more surprises. Or we could try to do a syntactic transformation of the above to

for $e in //employee
let $s := $e/salary
let $t := string($s)
group by $s := $s, $t := $t

(which is the reverse of the current transformation)

As I can't see any practical uses that require this kind of construct, I think the simplest solution is to modify the scope rules so that in a group by clause, the variables declared within the clause are not in scope within that clause. We already have custom scope rules for the window clause, it's not a big deal to also introduce custom rules for the group by clause.
Comment 1 Michael Dyck 2012-01-27 00:01:01 UTC
(For some previous discussion of this problem, see Bug 15044 comment #3 et seq.)
Comment 2 Jonathan Robie 2012-02-29 14:54:13 UTC
In yesterday's telcon, we agreed that:

1. This sentence does not apply to Group By:

<quote>The scope of a bound variable includes all subexpressions of the containing FLWOR that appear after the variable binding.</quote>

Grouping variables, like Windowing variables, have their own rules.

2. All bindings in group by expressions are computed before the group by clause is evaluated and are based on pre-grouping tuples.

3. Variables bound in one grouping spec in a group by clause can be used in other grouping specs, but these variables are also based on pre-grouping values.

4. Pre-grouping values are not implicitly atomized, post-grouping values are implicitly atomized.