As a result of
LPS-9903, pagination does not work if the ORDER BY uses column aliases.
LPS-14537 fixed that issue, it introduced a separate defect where pagination failed if the query had a union, because it added the "row_number()" column to the first part, but not on the subsequent parts that were being unioned together.
The solution isn't as straightforward as adding a row_number() to every subquery being unioned, because the resulting row numbers will not match the row numbers we actually want, since what we actually want is the row_number() according to the order by on the unioned result.
Therefore, the natural solution in this case is to do another level of nesting – to re-select the columns and the row number of the unioned result. Though this will result in a reduction in performance, it should still be better than no pagination.