jdaugherty commented on code in PR #16731:
URL: https://github.com/apache/grails-core/pull/16731#discussion_r4361289395


##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/JpaCriteriaQueryCreator.java:
##########
@@ -296,6 +301,17 @@ private void assignOrderBy(CriteriaQuery<?> cq, 
JpaQueryContext context) {
         }
     }
 
+    /**
+     * Whether the query selects nothing but counts, as {@code count()} and 
{@code countDistinct()} do: such a query
+     * has one row, so the criteria's orders do not apply to it. A query that 
groups and counts keeps its orders.
+     */
+    private boolean isCountOnly() {
+        List<Query.Projection> projectionList = 
projections.getProjectionList();
+        return !projectionList.isEmpty() && projectionList.stream()
+                .allMatch(projection -> projection instanceof 
Query.CountProjection ||

Review Comment:
   The predicate covers `count` and `countDistinct`, so a query whose only 
projections are other aggregates still carries the orders and fails the same 
way:
   
   ```
   c.get { projections { max('amount') }; order('amount') }
   
   Column "CTE1_0.AMOUNT" must be in the GROUP BY list
   select max(cte1_0.amount) from criteria_test_entity cte1_0 order by 
cte1_0.amount
   ```
   
   That one is user-written rather than added by the framework, so it is less 
pressing than `count()`, but the rule could be "no group by and every 
projection is an aggregate" (count, countDistinct, max, min, sum, avg) at no 
extra cost. A follow-up PR is fine.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to