You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Specifically, eXist-db's implementation of the XQuery order by clause does not follow the W3C specification. The W3C XQuery 3.1 Order By Clause specification states:
The purpose of an order by clause is to impose a value-based ordering on the tuples in the tuple stream. The output tuple stream of the order by clause contains the same tuples as its input tuple stream, but the tuples may be in a different order.
This means that any tuples in a tuple stream after an Order By Clause should be in the order defined by the order by clause. In the eXist-db implementation that is not the case. In eXist-db any expressions/clauses within a FLWOR expression that occur after the order by clause, but before the return clause, will not see an ordered tuple stream; instead they see the tuple stream prior to the order by clause. The reason for this is that in eXist-db the operation of executing the sort for the Order By clause is always deferred until just before the return clause of the FLWOR expression. In eXist-db this is known as a postEval phase, see:
Modifying eXist-db to correctly implement the Order By Clause semantics is a large task, and may require a rewrite of all FLWOR expressions implementations. The issue was discovered during development of this PR - #4530
See the pending test ct:order-alpha-ascending-indexes#0 in exist-core/src/test/xquery/xquery3/count.xqm.
On an tangentially related note, we suspect there may be other problems with all other FLWOR expressions in eXist-db that implement postEval with regards to compliance with W3C XQuery 3.1.
In Further Detail
Given the XQuery:
for $x in ('a', 'b')
count $index1
let $remainder := $index1 mod2orderby $remainder, $index1
count $index2
return
<x index1="{$index1}"index2="{$index2}">{$x}</x>
Specifically, eXist-db's implementation of the XQuery
order by
clause does not follow the W3C specification. The W3C XQuery 3.1 Order By Clause specification states:This means that any tuples in a tuple stream after an Order By Clause should be in the order defined by the order by clause. In the eXist-db implementation that is not the case. In eXist-db any expressions/clauses within a FLWOR expression that occur after the order by clause, but before the return clause, will not see an ordered tuple stream; instead they see the tuple stream prior to the order by clause. The reason for this is that in eXist-db the operation of executing the
sort
for the Order By clause is always deferred until just before the return clause of the FLWOR expression. In eXist-db this is known as apostEval
phase, see:FLWORClause#postEval(Sequence)
ForExpr#eval(Sequence, Item)
OrderByClause#postEval(Sequence)
Modifying eXist-db to correctly implement the
Order By Clause
semantics is a large task, and may require a rewrite of all FLWOR expressions implementations. The issue was discovered during development of this PR - #4530See the pending test
ct:order-alpha-ascending-indexes#0
inexist-core/src/test/xquery/xquery3/count.xqm
.On an tangentially related note, we suspect there may be other problems with all other FLWOR expressions in eXist-db that implement
postEval
with regards to compliance with W3C XQuery 3.1.In Further Detail
Given the XQuery:
The result should be computed as:
However eXist-db produces the incorrect result due to its incorrect implementation of the
order by
clause:This occurs because eXist-db evaluates the query by effectively executing the following steps:
$x := 'a'
$index1 := 1
$remainder := 1
$index2 := 1
<x index1="1" index2="1">a</x>
to the result sequence.$x := 'b'
$index1 := 2
$remainder := 0
$index2 := 2
<x index1="2" index2="2">b</x>
to the result sequence.$remainder ascending
and then$index1 ascending
The problem is that eXist-db applies the sorting (i.e. the
order by
clause) after the fact.I have created a detailed UML Sequence Diagram of the above XQuery execution in eXist-db that shows the exact problem:
In case it is helpful for anyone else, the code for the UML Sequence Diagram:
The text was updated successfully, but these errors were encountered: