Skip to content

When using a native SQL query and applying a Pageable with sorting, the generated SQL can become malformed with missing order by clause when inner order by clauses exist in query #3823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jon-nielsen opened this issue Mar 31, 2025 · 2 comments
Labels
status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged

Comments

@jon-nielsen
Copy link

Description:
When using a native SQL query and applying a Pageable with sorting, the generated SQL can become malformed with missing order by clause when inner order by clauses exist in query

To reproduce:

  1. Create a native query string with some inner selects with order clauses
  2. End inner select like " order by something desc)"
  3. Run query and get error like "ORA-00933: SQL command not properly ended..." in Oracle
  4. Notice logged SQL end with: "where (...), something desc fetch first ? rows only"

Details:
I had a look at JPA repository query package to look for hints for problems with my SQL. Found this:

https://github.com/spring-projects/spring-data-jpa/blame/dbd4e532d2eb229b8449458f7851e9ec6e814cfc/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java#L301

In QueryUtils

  • applySorting is used to either append ", " or " order by " to a query.
  • hasOrderByClause is used to determine if top-level "order by" exists: countOccurrences(ORDER_BY, query) > countOccurrences(ORDER_BY_IN_WINDOW_OR_SUBSELECT, query).
  • ORDER_BY_IN_WINDOW_OR_SUBSELECT only matches order by within ( ).

Workaround:

  1. Added "order by null" to native query
  2. Run query
  3. Notice logged SQL end with: "where (...) order by null, something desc fetch first ? rows only"
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 31, 2025
@mp911de
Copy link
Member

mp911de commented Apr 7, 2025

Thanks for reaching out. Our own support is limited to simple queries only as we do not maintain a fully SQL compliant parser, instead we apply limited regex replacements. If you want to use more complex queries, please add JSqlParser (see reference docs) or provide the count query yourself.

@mp911de mp911de added the status: waiting-for-feedback We need additional information before we can continue label Apr 7, 2025
@spring-projects-issues
Copy link

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants