This bug entry describes a number of separate but related problems with optimization of FLWOR expressions in 9.4.
(A) when a variable declared outside the FLWOR expression is referenced within it, the reference may be inlined when this is inappropriate, because the value of the variable is used more than once.
(B) a variable declared in a let clause may fail to be inlined when it should be. In turn this may result in a failure to convert where clauses to predicates, which may prevent successful join optimization.
(C) when a variable is inlined and the return clause is a reference to that variable, the expression is not rewritten correctly, and the result is an internal run-time error reporting that no slot number has been allocated to the variable
(D) when a variable reference used as an argument to a user function call is inlined, the evaluation mode of the argument is not recomputed, leading to a ClassCastException trying to cast some other kind of expression to a VariableReference
(E) inlining of variables does not work in the case of variables that are referenced in an order-by clause. These rely on the semantics of FLWOR expressions in terms of "tuples of variable bindings" and the variable references must not be rewritten. (This manifests itself as a ClassCastException when attepting to replace a subexpression of a TupleExpression, which only allows LocalVariableReferences as its subexpressions).
A patch is being added to Subversion