Copilot commented on code in PR #16817:
URL: https://github.com/apache/grails-core/pull/16817#discussion_r4434003488


##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/PropertyCriterionHandler.java:
##########
@@ -198,14 +214,34 @@ static Subquery<?> buildSubquery(CriterionContext 
context, Expression<?> propert
             projectionList.add((Query.Projection) p);
         }
 
-        Class<?> expectedType = propertyPath != null ? 
propertyPath.getJavaType() : qc.getPersistentEntity().getJavaClass();
+        Class<?> expectedType = resolveSubqueryType(propertyPath, qc);
         Subquery<?> subquery = 
context.getCriteriaQuery().subquery(expectedType);
         var creator = new JpaCriteriaQueryCreator(projectionList, 
context.getCriteriaBuilder(), (GrailsHibernatePersistentEntity) 
qc.getPersistentEntity(), (DetachedCriteria) qc, 
context.getConversionService());
         creator.setParentContext(context.getQueryContext());
         creator.populateSubquery((JpaSubQuery) subquery);
         return subquery;
     }
 
+    static Class<?> resolveSubqueryType(Expression<?> propertyPath, 
QueryableCriteria<?> qc) {
+        if (!qc.getProjections().isEmpty()) {
+            Object first = qc.getProjections().get(0);
+            if (first instanceof Query.CountProjection || first instanceof 
Query.CountDistinctProjection) {
+                return Long.class;

Review Comment:
   The added scalar-comparison tests exercise only `AvgProjection`, leaving the 
other projection types without result-based coverage through the new comparison 
path. Please add Hibernate-backed public-API tests for count, count-distinct, 
identifier, and property subqueries, including a Long count compared with an 
Integer property, and assert the returned rows.



##########
grails-data-hibernate7/core/src/test/groovy/grails/orm/HibernateCriteriaBuilderDirectSpec.groovy:
##########
@@ -246,6 +246,31 @@ class HibernateCriteriaBuilderDirectSpec extends 
HibernateGormDatastoreSpec {
         c.list { notExists { eq("category", "does-not-exist") } }.size() == 4
     }
 
+    void "test scalar subqueries in comparison criteria (gt, ge, lt, le)"() {
+        given:
+        def subquery = new DetachedCriteria(CriteriaTestEntity).build {
+            projections { avg("amount") }
+        }
+
+        expect:
+        c.list { gt("amount", subquery) }*.name.sort() == ["C", "D"]
+        c.list { ge("amount", subquery) }*.name.sort() == ["C", "D"]
+        c.list { lt("amount", subquery) }*.name.sort() == ["A", "B"]
+        c.list { le("amount", subquery) }*.name.sort() == ["A", "B"]

Review Comment:
   The fixture's average is 25, and no row has that amount, so these assertions 
cannot distinguish ge from gt or le from lt. Please include a row equal to the 
subquery result to cover the inclusive comparison branches. Filtering the 
aggregate to C (amount 30) lets this test verify all four operators distinctly.



-- 
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