Skip to content

Order by wrongly added to a native query #2620

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

Closed
michal-trnka opened this issue Aug 16, 2022 · 8 comments
Closed

Order by wrongly added to a native query #2620

michal-trnka opened this issue Aug 16, 2022 · 8 comments
Labels
type: bug A general bug

Comments

@michal-trnka
Copy link

michal-trnka commented Aug 16, 2022

Hello,
I have found an issue in 2.7.1 with adding order by by sort to a query that already has order by.
It happens only in specific scenarios/combinations of the clauses. See the test method bellow.

@Test
void testOrderBy(){
    Sort sort = Sort.by(Sort.Order.desc("age"));

    //works
    assertThat(QueryUtils.applySorting("SELECT * FROM t \n" //
            + "WHERE true \n"
            + "ORDER BY (case when t.id in :priorityIds then 1 else 2 end)\n" //
            + "", sort))
            .endsWith("ORDER BY (case when t.id in :priorityIds then 1 else 2 end)\n" + ", age desc");

    //works
    assertThat(QueryUtils.applySorting("SELECT * FROM t \n" //
            + "WHERE (true OR false) \n"
            + "ORDER BY t.id\n" //
            + "", sort))
            .endsWith("ORDER BY t.id\n" + ", age desc");

    //not working
    assertThat(QueryUtils.applySorting("SELECT * FROM t \n" //
            + "WHERE (true OR false) \n"
            + "ORDER BY (case when t.id in :priorityIds then 1 else 2 end)\n" //
            + "", sort))
            .endsWith("ORDER BY (case when t.id in :priorityIds then 1 else 2 end)\n" + ", age desc");

}

It seems to be related to this change of the regex pattern: 6fd829c .

The issue is not present in 2.7.0.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 16, 2022
@grzegorz-aniol
Copy link

the valid workaround is to remove parenthesis after ORDER BY
ORDER BY (case when t.id in :priorityIds then 1 else 2 end) => ORDER BY case when t.id in :priorityIds then 1 else 2 end

@schauder schauder added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 29, 2022
@lith-imad
Copy link

another case where a workaround is not as trivial as when using native database functions:

select e from SampleEntity e where function('nativeFunc', ?1) > 'testVal' order by function('nativeFunc', ?1);

spring data will add extra order by as it incorrectly resolves subsequent function presence as subquery or window query.

@gregturn
Copy link
Contributor

We are working on a parsing solution to better handle such situations that QueryUtils accommodates. For the meantime, we are trying to avoid any more changes to QueryParser.

@gregturn
Copy link
Contributor

@lith-imad Your query appears to be Hibernate while the original reporter appears to be commenting on a SQL query. I have opened #2862 to separately track your issue.

@gregturn
Copy link
Contributor

Since this involves native queries and not the new parsers (HQL/JPQL), I'm dropping the revisit-after-parser-rewrite label.

@gregturn gregturn changed the title Order by wrongly added to a query Order by wrongly added to a native query Mar 22, 2023
@gregturn gregturn removed the in: query-parser Everything related to parsing JPQL or SQL label Apr 28, 2023
@gregturn
Copy link
Contributor

I'm removing the query-parser tag since this issue involves native queries, which we currently have limited ability to deal with.

Frankly, this situation appears too complicated for QueryUtils to handle, and we aren't likely to approve a change there that would meet your needs. Your best best moving forward may be to implement a custom implementation. Check out https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations for more details on how to hook such a solution into your repository.

@gregturn gregturn removed their assignment Apr 28, 2023
@gregturn gregturn added the status: waiting-for-feedback We need additional information before we can continue label Apr 28, 2023
@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 May 5, 2023
@spring-projects-issues
Copy link

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues closed this as not planned Won't fix, can't repro, duplicate, stale May 12, 2023
@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

6 participants