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


##########
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:
   Addressed in 25448d501a: added Hibernate-backed public-API tests for 
`count()`, `countDistinct("category")`, `id()`, and `property("amount")` 
subqueries across `eq`, `gt`, `ge`, `lt`, and `le`, including `Long` 
count/count-distinct projections compared against `Integer` `otherAmount`.



##########
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:
   Addressed in 25448d501a: filtered the `avg("amount")` subquery to 
`eq("name", "C")` (`30.0`, equal to row `C`) so `gt`, `ge`, `lt`, and `le` 
verify all four comparison 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