So the problem is, when you have a multi-level query e.g. a query(L0) with nested subqueries and a subquery(L1) appears in the ORDER BY clause. If that subquery(L1) uses a function e.g. in it's WHERE clause, which receives another subquery(L2) and also has an ORDER BY clause, the generated SQL is wrong. The reason for that is, that the SQL generation does not handle the capturing of the generated SQL expressions properly in the case of nested ORDER BY s.