spring-data-jpa icon indicating copy to clipboard operation
spring-data-jpa copied to clipboard

Order by wrongly added to a query

Open michal-trnka opened this issue 3 years ago • 2 comments
trafficstars

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: https://github.com/spring-projects/spring-data-jpa/commit/6fd829c3ca98a9cd76f4df5e1fdbf4d163fd6dc1 .

The issue is not present in 2.7.0.

michal-trnka avatar Aug 16 '22 11:08 michal-trnka

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

grzegorz-aniol avatar Aug 16 '22 13:08 grzegorz-aniol

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.

lith-imad avatar Oct 01 '22 12:10 lith-imad

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 avatar Feb 25 '23 04:02 gregturn

@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 avatar Mar 15 '23 14:03 gregturn

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

gregturn avatar Mar 22 '23 15:03 gregturn

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 avatar Apr 28 '23 15:04 gregturn

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 avatar May 05 '23 15:05 spring-projects-issues

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 avatar May 12 '23 15:05 spring-projects-issues