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