FINERACT-2622: Preserve top-level ORDER BY when wrapping report SQL#6058
FINERACT-2622: Preserve top-level ORDER BY when wrapping report SQL#6058heissenberg06 wants to merge 1 commit into
Conversation
|
The failing |
|
I wonder whether instead of this, we should revisit whether the
|
|
Thanks @adamsaghy , that's a good challenge — I dug into the three justifications in the wrapSQL comment:
So I agree it's worth testing whether wrapSQL can simply be dropped. If it can, that removes the root cause and this ORDER BY issue disappears without the SQL-parsing logic in this PR. Proposed plan: I'll run the reporting tests with wrapSQL turned into a no-op to see what (if anything) fails — column-label handling and any dialect-specific behavior in particular. If it's clean, I'll repurpose this PR to remove the wrapping (and simplify the call sites) instead of adding the ORDER BY lifter. Does that direction work for you? Any specific cases (DB dialects, label edge cases) you'd want covered before removing it? |
Description
Fixes FINERACT-2622.
Problem
Report SQL is wrapped as
select x.* from (<sql>) xinGenericDataServiceImpl#wrapSQL. A derived table'sORDER BYis not guaranteed to be honored by the outer query, so a user report ending in e.g.ORDER BY account_no DESCreturns rows in an arbitrary order.Fix
Detect a top-level trailing
ORDER BY(with any trailingLIMIT/OFFSET) and re-apply it on the outer wrapper, where it resolves against the columns exposed byx.*. The wrapping itself is kept (theCachedRowSetImplcolumn-label workaround).The scan ignores
ORDER BYthat appears inside parentheses (subqueries / window functions), string literals, and SQL comments. As a safeguard against regressions, lifting is skipped when the trailing clause contains a dot-qualified reference (alias.column) that would not resolve in the outer scope, leaving today's behavior for that case.Scope check:
wrapSQLhas three call sites inReadReportingServiceImpl—buildPreparedQuery(user report SQL, the target of this fix) andgetSql/getReportType(fixed internal queries with noORDER BY, so their behavior is unchanged).Tests
Unit tests on
wrapSQLcover: ORDER BY preserved, no-ORDER-BY unchanged, subquery ORDER BY not lifted, window-function ORDER BY not lifted, dot-qualified skipped, string-literal ORDER BY ignored, trailing LIMIT carried.Note on pagination
When pagination is enabled (
IS_PAGINATION_ALLOWED=true), the wrapped SQL is passed topaginationHelper.fetchPage. If a user report SQL ends with a top-levelORDER BY ... LIMIT, the carriedLIMITcould interact with pagination's own limiting. Flagging for reviewer confirmation on whetherPaginationHelperappends or re-wraps; happy to skipLIMIT-carrying on that path if preferred. Also happy to add a DB-backed integration test if desired.Checklist