Omar,
Thanks for your observation and the reproducible query. The bug turns out to be an intricate one, so I’ve opened an issue for it [1].
Meanwhile, you can circumvent the problem by suppressing the inlining of the function …
declare %basex:inline(0) function _:query-index___( ...
… and (if that doesn’t suffice) by replacing …
let $rs := (# db:enforceindex #) { ... } return $rs
… by …
(# db:enforceindex #) { ... }
Best, Christian