<?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>10651</bug_id>
          
          <creation_ts>2010-09-18 21:00:39 +0000</creation_ts>
          <short_desc>Optimisation avoiding sorts (K2-OrderbyExprWithout-10 &amp; K2-OrderbyExprWithout-40)</short_desc>
          <delta_ts>2010-10-19 15:10:37 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>XML Query Test Suite</product>
          <component>XML Query Test Suite</component>
          <version>unspecified</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="Martin Probst">Probst_Martin</reporter>
          <assigned_to name="Benjamin Nguyen">benjie.nguyen</assigned_to>
          
          
          <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>39099</commentid>
    <comment_count>0</comment_count>
    <who name="Martin Probst">Probst_Martin</who>
    <bug_when>2010-09-18 21:00:39 +0000</bug_when>
    <thetext>K2-OrderbyExprWithout-10 looks like this:

  for $a in (1, 4, 2)
  let $i := (1, 3, 2)
  order by $i
  return $i

The test tries to cause an order by with an order spec that is a sequence of items, and thus expects XPTY0004.

However, an XQuery engine might find out that the order spec expression $i is constant over the whole tuple sequence. Thus, the engine can evaluate the whole query without doing any sort, thus avoiding the error at all and yielding (1, 3, 2, 1, 3, 2, 1, 3, 2) as the result. Or actually, those values in any order because the ordering of the sequence is undefined as the order spec will always give identical values.

I think that is a legal optimisation according to the dreaded &quot;Errors and Optimizations&quot; section. While the implementation has to evaluate $i to yield the return value, in the context of the order by expression it does not need to evaluate the sequence at all, so it is free to swallow the error and give a result. Also, apart from legalese, I think this is the kind of optimisation that section wants to allow.

I suggest changing the query so that the $i part depends on $a, which means the sort factor for the tuple sequence is not constant over the whole query:

  for $a in (1, 4, 2)
  let $i := (1, $a, 2)
  order by $i
  return $i

The same applies to K2-OrderbyExprWithout-40, which is a more complicated expression but has the exact same problem and solution.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>39611</commentid>
    <comment_count>1</comment_count>
    <who name="Andrew Eisenberg">andrew.eisenberg</who>
    <bug_when>2010-09-27 20:42:58 +0000</bug_when>
    <thetext>You say:

&quot;Thus, the engine can evaluate the whole
query without doing any sort, thus avoiding the error at all and yielding (1,
3, 2, 1, 3, 2, 1, 3, 2) as the result. Or actually, those values in any order
because the ordering of the sequence is undefined as the order spec will always
give identical values.&quot;


I understand your argument that the order by does not need to be evaluated, as its argument is unchanging. I don&apos;t see the justification for any result other than (1, 3, 2, 1, 3, 2, 1, 3, 2) if an error is not returned.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>39669</commentid>
    <comment_count>2</comment_count>
    <who name="Martin Probst">Probst_Martin</who>
    <bug_when>2010-09-28 06:51:49 +0000</bug_when>
    <thetext>The query processor can statically determine that the order by specification will always yield an identical value for each iteration of the loop. This means the order by doesn&apos;t influence the order of the result.

However according to the specification, for a non-stable order, the relative order of two tuples in the tuple stream is undefined if their order by specs are identical (i.e., not &lt; or &gt;).

That means, the processor is free to return the sequence of tuples in any order.

Of course with this query, there isn&apos;t any reason to do that, but if you had a complicated path expression that would require intermediate sorting, this could very well happen.

In any case, this is just a silly query. Making the let clause depend on the for binding will fix the issue and test what the author originally wanted to test, so we should just fix it.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>41477</commentid>
    <comment_count>3</comment_count>
    <who name="Benjamin Nguyen">benjie.nguyen</who>
    <bug_when>2010-10-18 11:16:34 +0000</bug_when>
    <thetext>Tests K2-OrderbyExprWithout-10 and K2-OrderbyExprWithout-40 have been modified in the test suite. Test -10 is the same as the one proposed by Martin, and I wrote -40 like this : 

for $a in (3, 2, 1)
let $a := ($a, 1),
$b := (2, 1),
$c := (2, 1),
$d:= (2, 1)
order by $a
return $a

I think that this fixes the bug.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>41478</commentid>
    <comment_count>4</comment_count>
    <who name="Martin Probst">Probst_Martin</who>
    <bug_when>2010-10-18 11:22:22 +0000</bug_when>
    <thetext>Confirmed.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>41552</commentid>
    <comment_count>5</comment_count>
    <who name="Martin Probst">Probst_Martin</who>
    <bug_when>2010-10-19 15:10:37 +0000</bug_when>
    <thetext>Closing.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>