Errors and Optimization: re-ordering predicates

The spec has always been clear that it is permissible for an optimizer to rearrange the order of predicates, so that

e[@a castable as xs:integer][xs:integer(@a) = 3]

is not guaranteed to be error-free.

In the latest Saxon release I have started taking advantage of this freedom, and inevitably users have tripped up over this: see https://saxonica.plan.io/boards/3/topics/6270

Now the supplementary question raised in that thread is, does this freedom still apply when the expression is rewritten with variables, as

let $x := e[@a castable as xs:integer]
return $x[xs:integer(@a) = 3]

I think the answer is that yes, it does. The ONLY way to ensure the expression is error-free is to use a clumsy conditional

e[if (@a castable as xs:integer) then xs:integer(@a) = 3 else false()]

I am torn here. Are other products exploiting the freedom which we give to implementors in this area? Are users of those products complaining? Or are implementors showing restraint by finding a compromise between performance and usability?

Michael Kay
Saxonica

Received on Sunday, 3 January 2016 18:30:32 UTC