Undefined results order with Order By/Projection/Distinct

Hi all,

Sorry if this is known but was a little surprise to me. Even though
both Projection and Distinct solution modifiers are required to
preserve the order of solutions imposed by Order By (see 18.5), one
can construct an example where the final query results would be
undefined:

SELECT DISTINCT ?a {
    VALUES (?a ?b) { (1 1) (1 2) (2 3) (1 4) (2 5) }
}
ORDER BY DESC(?b)

Solution modifiers are applied in the order of: Order By -> Projection
-> Distinct (15). So after the projection, the solution sequence is:
?a -> 2, ?a -> 1, ?a -> 2, ?a -> 1, ?a -> 1.

Now, the Distinct is only required to keep this order but it's free to
remove any of the duplicate ?a -> 2 or ?a -> 1 solutions. So the final
results could be either ?a -> 2, ?a -> 1 or ?a -> 1, ?a -> 2. Note
that both solution sequences preserve the Order By order!

It's easy to make an extended example with LIMIT where the results
could be completely different based on how Distinct eliminates
duplicates. Given the role preservation requirement one can argue that
Distinct should always keep the first occurrence of each duplicate in
the input, but I don't think it's in the spec.

Am I missing something?
Pavel

Received on Friday, 7 October 2022 14:40:21 UTC