<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>15728</bug_id>
          
          <creation_ts>2012-01-26 08:59:22 +0000</creation_ts>
          <short_desc>[XQ30] Scope of variables defined in group-by</short_desc>
          <delta_ts>2013-06-19 09:13:05 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>XPath / XQuery / XSLT</product>
          <component>XQuery 3.0</component>
          <version>Member-only Editors Drafts</version>
          <rep_platform>PC</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>CLOSED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Michael Kay">mike</reporter>
          <assigned_to name="Jonathan Robie">jonathan.robie</assigned_to>
          <cc>jim.melton</cc>
    
    <cc>jmdyck</cc>
          
          <qa_contact name="Mailing list for public feedback on specs from XSL and XML Query WGs">public-qt-comments</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>63149</commentid>
    <comment_count>0</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2012-01-26 08:59:22 +0000</bug_when>
    <thetext>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&apos;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&apos;t use the value of $s from the post-grouping tuple because that&apos;s only available after doing the grouping, and we can&apos;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&apos;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&apos;s not a big deal to also introduce custom rules for the group by clause.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>63169</commentid>
    <comment_count>1</comment_count>
    <who name="Michael Dyck">jmdyck</who>
    <bug_when>2012-01-27 00:01:01 +0000</bug_when>
    <thetext>(For some previous discussion of this problem, see Bug 15044 comment #3 et seq.)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>64726</commentid>
    <comment_count>2</comment_count>
    <who name="Jonathan Robie">jonathan.robie</who>
    <bug_when>2012-02-29 14:54:13 +0000</bug_when>
    <thetext>In yesterday&apos;s telcon, we agreed that:

1. This sentence does not apply to Group By:

&lt;quote&gt;The scope of a bound variable includes all subexpressions of the containing FLWOR that appear after the variable binding.&lt;/quote&gt;

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.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>