@@ -735,4 +735,77 @@ void countQueryUsesCorrectVariable() {
735
735
assertThat (countQueryFor )
736
736
.isEqualTo ("select count(us) FROM users_statuses us WHERE (user_created_at BETWEEN :fromDate AND :toDate)" );
737
737
}
738
+
739
+ @ Test // GH-2496, GH-2522, GH-2537, GH-2045
740
+ void orderByShouldWorkWithSubSelectStatements () {
741
+
742
+ Sort sort = Sort .by (Order .desc ("age" ));
743
+
744
+ assertThat (QueryUtils .applySorting ("SELECT\n " //
745
+ + " foo_bar.*\n " //
746
+ + "FROM\n " //
747
+ + " foo foo\n " //
748
+ + "INNER JOIN\n " //
749
+ + " foo_bar_dnrmv foo_bar ON\n " //
750
+ + " foo_bar.foo_id = foo.foo_id\n " //
751
+ + "INNER JOIN\n " //
752
+ + " (\n " //
753
+ + " SELECT\n " //
754
+ + " foo_bar_action.*,\n " //
755
+ + " RANK() OVER (PARTITION BY \" foo_bar_action\" .attributes->>'baz' ORDER BY \" foo_bar_action\" .attributes->>'qux' DESC) AS ranking\n " //
756
+ + " FROM\n " //
757
+ + " foo_bar_action\n " //
758
+ + " WHERE\n " //
759
+ + " foo_bar_action.deleted_ts IS NULL)\n " //
760
+ + " foo_bar_action ON\n " //
761
+ + " foo_bar.foo_bar_id = foo_bar_action.foo_bar_id\n " //
762
+ + " AND ranking = 1\n " //
763
+ + "INNER JOIN\n " //
764
+ + " bar bar ON\n " //
765
+ + " foo_bar.bar_id = bar.bar_id\n " //
766
+ + "INNER JOIN\n " //
767
+ + " bar_metadata bar_metadata ON\n " //
768
+ + " bar.bar_metadata_key = bar_metadata.bar_metadata_key\n " //
769
+ + "WHERE\n " //
770
+ + " foo.tenant_id =:tenantId\n " //
771
+ + "AND (foo.attributes ->> :serialNum IN (:serialNumValue))" , sort )).endsWith ("order by foo.age desc" );
772
+
773
+ assertThat (QueryUtils .applySorting ("select r " //
774
+ + "From DataRecord r " //
775
+ + "where " //
776
+ + " ( " //
777
+ + " r.adusrId = :userId " //
778
+ + " or EXISTS( select 1 FROM DataRecordDvsRight dr WHERE dr.adusrId = :userId AND dr.dataRecord = r ) " //
779
+ + ")" , sort )).endsWith ("order by r.age desc" );
780
+
781
+ assertThat (QueryUtils .applySorting ("select distinct u " //
782
+ + "from FooBar u " //
783
+ + "where [REDACTED] " //
784
+ + "and (" //
785
+ + " not exists (" //
786
+ + " from FooBarGroup group " //
787
+ + " where group in :excludedGroups " //
788
+ + " and group in elements(u.groups)" //
789
+ + " )" //
790
+ + ")" , sort )).endsWith ("order by u.age desc" );
791
+
792
+ assertThat (QueryUtils .applySorting ("SELECT i " //
793
+ + "FROM Item i " //
794
+ + "FETCH ALL PROPERTIES \" " //
795
+ + "+ \" WHERE i.id IN (\" " //
796
+ + "+ \" SELECT max(i2.id) FROM Item i2 \" " //
797
+ + "+ \" WHERE i2.field.id = :fieldId \" " //
798
+ + "+ \" GROUP BY i2.field.id, i2.version)" , sort )).endsWith ("order by i.age desc" );
799
+
800
+ assertThat (QueryUtils .applySorting ("select \n " //
801
+ + " f.id,\n " //
802
+ + " (\n " //
803
+ + " select timestamp from bar\n " //
804
+ + " where date(bar.timestamp) > '2022-05-21'\n " //
805
+ + " and bar.foo_id = f.id \n " //
806
+ + " order by date(bar.timestamp) desc\n " //
807
+ + " limit 1\n " //
808
+ + ") as timestamp\n " //
809
+ + "from foo f" , sort )).endsWith ("order by f.age desc" );
810
+ }
738
811
}
0 commit comments