|
40 | 40 | * @author Grégoire Druant
|
41 | 41 | * @author Mohammad Hewedy
|
42 | 42 | * @author Greg Turnquist
|
| 43 | + * @author Jędrzej Biedrzycki |
43 | 44 | */
|
44 | 45 | class QueryUtilsUnitTests {
|
45 | 46 |
|
@@ -552,4 +553,53 @@ void countProjectionDistinctQueryIncludesNewLineAfterEntityAndBeforeWhere() {
|
552 | 553 | private static void assertCountQuery(String originalQuery, String countQuery) {
|
553 | 554 | assertThat(createCountQueryFor(originalQuery)).isEqualTo(countQuery);
|
554 | 555 | }
|
| 556 | + |
| 557 | + @Test |
| 558 | + void applySortingAccountsForNativeWindowFunction() { |
| 559 | + Sort sort = Sort.by(Order.desc("age")); |
| 560 | + |
| 561 | + // order by absent |
| 562 | + assertThat(QueryUtils.applySorting("select * from user u", sort)) |
| 563 | + .isEqualTo("select * from user u order by u.age desc"); |
| 564 | + // order by present |
| 565 | + assertThat(QueryUtils.applySorting("select * from user u order by u.lastname", sort)) |
| 566 | + .isEqualTo("select * from user u order by u.lastname, u.age desc"); |
| 567 | + // partition by |
| 568 | + assertThat(QueryUtils.applySorting("select dense_rank() over (partition by age) from user u", sort)) |
| 569 | + .isEqualTo("select dense_rank() over (partition by age) from user u order by u.age desc"); |
| 570 | + // order by in over clause |
| 571 | + assertThat(QueryUtils.applySorting("select dense_rank() over (order by lastname) from user u", sort)) |
| 572 | + .isEqualTo("select dense_rank() over (order by lastname) from user u order by u.age desc"); |
| 573 | + // order by in over clause (additional spaces) |
| 574 | + assertThat(QueryUtils.applySorting("select dense_rank() over ( order by lastname ) from user u", sort)) |
| 575 | + .isEqualTo("select dense_rank() over ( order by lastname ) from user u order by u.age desc"); |
| 576 | + // order by in over clause + at the end |
| 577 | + assertThat(QueryUtils.applySorting("select dense_rank() over (order by lastname) from user u order by u.lastname", sort)) |
| 578 | + .isEqualTo("select dense_rank() over (order by lastname) from user u order by u.lastname, u.age desc"); |
| 579 | + // partition by + order by in over clause |
| 580 | + assertThat(QueryUtils.applySorting("select dense_rank() over (partition by active, age order by lastname) from user u", sort)) |
| 581 | + .isEqualTo("select dense_rank() over (partition by active, age order by lastname) from user u order by u.age desc"); |
| 582 | + // partition by + order by in over clause + order by at the end |
| 583 | + assertThat(QueryUtils.applySorting("select dense_rank() over (partition by active, age order by lastname) from user u order by active", sort)) |
| 584 | + .isEqualTo("select dense_rank() over (partition by active, age order by lastname) from user u order by active, u.age desc"); |
| 585 | + // partition by + order by in over clause + frame clause |
| 586 | + assertThat(QueryUtils.applySorting("select dense_rank() over ( partition by active, age order by username rows between current row and unbounded following ) from user u", sort)) |
| 587 | + .isEqualTo("select dense_rank() over ( partition by active, age order by username rows between current row and unbounded following ) from user u order by u.age desc"); |
| 588 | + // partition by + order by in over clause + frame clause + order by at the end |
| 589 | + assertThat(QueryUtils.applySorting("select dense_rank() over ( partition by active, age order by username rows between current row and unbounded following ) from user u order by active", sort)) |
| 590 | + .isEqualTo("select dense_rank() over ( partition by active, age order by username rows between current row and unbounded following ) from user u order by active, u.age desc"); |
| 591 | + // order by in subselect (select expression) |
| 592 | + assertThat(QueryUtils.applySorting("select lastname, (select i.id from item i order by i.id limit 1) from user u", sort)) |
| 593 | + .isEqualTo("select lastname, (select i.id from item i order by i.id limit 1) from user u order by u.age desc"); |
| 594 | + // order by in subselect (select expression) + at the end |
| 595 | + assertThat(QueryUtils.applySorting("select lastname, (select i.id from item i order by 1 limit 1) from user u order by active", sort)) |
| 596 | + .isEqualTo("select lastname, (select i.id from item i order by 1 limit 1) from user u order by active, u.age desc"); |
| 597 | + // order by in subselect (from expression) |
| 598 | + assertThat(QueryUtils.applySorting("select * from (select * from user order by age desc limit 10) u", sort)) |
| 599 | + .isEqualTo("select * from (select * from user order by age desc limit 10) u order by age desc"); |
| 600 | + // order by in subselect (from expression) + at the end |
| 601 | + assertThat(QueryUtils.applySorting("select * from (select * from user order by 1, 2, 3 desc limit 10) u order by u.active asc", sort)) |
| 602 | + .isEqualTo("select * from (select * from user order by 1, 2, 3 desc limit 10) u order by u.active asc, age desc"); |
| 603 | + } |
| 604 | + |
555 | 605 | }
|
0 commit comments