Skip to content

Add regression tests for sorting native queries with a derived table containing ORDER BY … LIMIT#4276

Open
jewoodev wants to merge 1 commit into
spring-projects:mainfrom
jewoodev:gh-2395-native-derived-table-sort-regression-test
Open

Add regression tests for sorting native queries with a derived table containing ORDER BY … LIMIT#4276
jewoodev wants to merge 1 commit into
spring-projects:mainfrom
jewoodev:gh-2395-native-derived-table-sort-regression-test

Conversation

@jewoodev
Copy link
Copy Markdown
Contributor

@jewoodev jewoodev commented Jun 6, 2026

The broken sort fragment reported in #2395 reproduces on the version it was filed against (2.3.9): QueryUtils.applySorting matched the derived table's inner ORDER BY and appended the primary sort after the WHERE clause without an order by keyword (… = :status, n.name asc), which becomes invalid SQL once pagination adds its LIMIT. It is no longer reproducible on main — both native query enhancers append the primary ORDER BY to the outer query while preserving the derived table's inner ordering.

This PR adds a regression test for each native enhancer: the regex-based DefaultQueryEnhancer, where the original breakage lived, and the JSqlParser-based JSqlParserQueryEnhancer, the default once JSqlParser is on the classpath. Each sits next to the existing shouldApplySorting coverage in its test class; both classes pass locally.

Closes #2395


  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 6, 2026
…containing `ORDER BY … LIMIT`.

Applying a `Sort` to a native query whose `FROM` clause contains a derived table with its own `ORDER BY … LIMIT` used to corrupt the generated sort fragment: `QueryUtils.applySorting` matched the derived table's inner `ORDER BY` and appended the primary sort after the `WHERE` clause without an `order by` keyword (`… WHERE … = :status, n.name asc`), producing invalid SQL once pagination applied its `LIMIT`. Both native query enhancers now append the primary `ORDER BY` to the outer query while preserving the derived table's inner ordering, so this adds regression tests for the regex-based `DefaultQueryEnhancer` and the JSqlParser-based `JSqlParserQueryEnhancer`.

Closes spring-projects#2395

Signed-off-by: jewoodev <jewoos15@naver.com>
@jewoodev jewoodev force-pushed the gh-2395-native-derived-table-sort-regression-test branch from 73eb853 to 0c77518 Compare June 6, 2026 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pageable - incorrect query for complex join for nativeQuery=true

2 participants