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


##########
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:
   Added in 67d8408503: `CriteriaSubqueryScenarioSpec` now runs count (a Long 
against an Integer property), countDistinct, id and property subqueries against 
the database, on both H5 and H7.



##########
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 integration scenarios already cover this: `CriteriaSubqueryScenarioSpec` 
seeds qty 1, 2, 3 with avg 2, so `qty >= avg(qty)` returns b and c while `>` 
returns only c (and the same for `<=`/`<`). It passes on H5 and H7.



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