Skip to content

fix(hibernate7): support where-query functions and refactor PredicateGenerator - #16816

Open
borinquenkid wants to merge 34 commits into
8.0.xfrom
fix/h7-where-query-functions-8.0.x
Open

borinquenkid wants to merge 34 commits into
8.0.xfrom
fix/h7-where-query-functions-8.0.x

Conversation

@borinquenkid

@borinquenkid borinquenkid commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

Summary of Changes

  1. Where-query functions (Fixes Hibernate 7: Support where-query functions (upper, lower, trim, length, year) #16811): a where query that calls a function on a property (lower, upper, trim, length, second, hour, minute, day, month, year) runs on Hibernate 7, in a read and in the bulk statements updateAll and deleteAll. WhereQueryFunctionsSpec covers the string and date functions, in, null operands, paging and the bulk statements.
  2. PredicateGenerator refactoring: the criterion evaluation state is in CriterionContext, InternalCriterionHandler is the strategy interface, and each criterion has a package-private handler. PredicateGenerator dispatches through a concurrent map and the nearest-superclass lookup, as the instanceof chain it replaces did, and keeps the public SPI (CriterionHandler, CriterionHandlerProvider, CUSTOM_HANDLERS, registerCriterionHandler, clearCustomCriterionHandlers).
  3. Scalar subqueries in comparisons (fix(hibernate7): support scalar subqueries in comparison criteria #16817) and subquery projections, unresolved paths and nulls in inList (fix(hibernate7): preserve subquery projections, fail fast on unresolved paths, and filter nulls in inList #16818): a comparison of a property or function with a scalar subquery, a subquery without projection compared by the property it names, an unresolved property comparison failing with a ConfigurationException instead of being dropped, and a null in an in list no longer throws.

Results that change, and how to keep the old ones

  • !=, ne and the NotEqual finders leave out the rows whose value is null on Hibernate 7, as Hibernate 5 always has and as Grails 7 did (upgrade note 26.14). qty != 2 || qty == null keeps them.
  • Bulk statements: criteria of a subquery are joined with AND, the parameter positions after a subquery are right, an empty IN list is written as a condition that matches nothing, and a subquery without projection is compared by the property of the function.
  • Only the ten where-query functions are accepted, in a read and in a bulk statement, and a name outside them is rejected before a query is built. A function over an rlike is rejected in a bulk statement.
  • A fraction compared with a function is kept as it is, and the date functions are Hibernate's own.
  • An unresolved property comparison in a subquery fails with a ConfigurationException; before, it was dropped. The feature that compares with a path on the root alias of the enclosing query (SubqueryAliasSpec, H7 SubqueryAliasSpec 'root alias' fails on the PredicateGenerator chain until #16821 #16884) is marked pending until Fix root alias registration and nested alias path resolution in subqueries #16821 registers the root alias.

There is no setting that restores the old result of these: each of them contradicted the call.

Verification

The tests of grails-datastore-core, grails-datamapping-core, grails-data-hibernate5-core and grails-data-hibernate7-core pass, and codeStyle passes.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Copilot AI balanced review requested due to automatic review settings November 1, 2026 00:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved query-generation defects produce invalid IN clauses, mishandle subqueries and null/case-insensitive comparisons, and can select the wrong criterion handler.

6 open issues
What changed in this PR

This PR addresses #16811 by adding where-query function support to Hibernate 7 and the shared JPA query builder. It also separates predicate generation into dedicated handlers while retaining the custom-handler extension point.

Changes:

  • Adds function-based comparisons for string and date properties.
  • Extracts criterion handlers and shared evaluation context from PredicateGenerator.
  • Adds Hibernate-backed function-query tests and predicate-generation checks.
File Description
grails-datastore-core/​src/​main/​groovy/​org/​grails/​datastore/​mapping/​query/​jpa/​JpaQueryBuilder.java Adds parameterized function comparisons to generated queries.
grails-data-hibernate7/​core/​src/​test/​groovy/​grails/​gorm/​tests/​hibernatequery/​PredicateGeneratorSpec.groovy Tests function predicates and invalid inputs.
grails-data-hibernate7/​core/​src/​test/​groovy/​grails/​gorm/​tests/​detachedcriteria/​WhereQueryFunctionsSpec.groovy Tests string and year functions through where-queries.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​SubqueryCriterionHandler.java Extracts quantified subquery comparisons.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​SqlRestrictionCriterionHandler.java Extracts native SQL restriction handling.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​PropertyNameCriterionHandler.java Extracts nullness and emptiness checks.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​PropertyCriterionHandler.java Extracts property comparisons and subquery construction.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​PropertyComparisonCriterionHandler.java Extracts property-to-property comparisons.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​PredicateGenerator.java Introduces handler registration, lookup, and dispatch.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​NotExistsCriterionHandler.java Extracts negated existence predicates.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​JunctionCriterionHandler.java Extracts logical predicate combinations.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​InternalCriterionHandler.java Defines the internal handler strategy interface.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​FunctionCallingCriterionHandler.java Builds function expressions and comparison predicates.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​ExistsCriterionHandler.java Extracts existence subquery predicates.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​CriterionContext.java Centralizes query state, path resolution, and conversion helpers.
grails-data-hibernate7/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​query/​AssociationCriterionHandler.java Extracts association navigation and nested predicate handling.

Review effort: Balanced

- Add FunctionCallingCriterion handling in JpaQueryBuilder and PredicateGenerator, including null, subquery, and case-insensitive ILike operands
- Encapsulate criterion evaluation context and helpers into CriterionContext
- Introduce InternalCriterionHandler strategy interface and dedicated criterion delegates
- Refactor PredicateGenerator to dispatch criteria via CriterionContext with nearest-superclass fallback lookup
- Add WhereQueryFunctionsSpec, JpaQueryBuilderFunctionSpec, and criterion handler coverage in PredicateGeneratorSpec

Fixes #16811
…e on H5 and H7

The PredicateGenerator refactor was covered by specs that check the shape of the
predicate built, so a wrong operator or a dropped null branch passed. Add integration
scenarios to the Hibernate 5 and Hibernate 7 grails-hibernate test apps that execute
each comparison, null, in, range, and/or/not criterion through where queries, the
criteria builder and dynamic finders, on simple, enum, association and embedded
properties, and assert the rows that come back.

!= and ne include rows whose value is null on Hibernate 7, as the TCK defines;
Hibernate 5 leaves them out, as its NullValueEqualSpec documents. Each copy states
its own behaviour in the feature name.
…ernate 5 does

Hibernate 7 OR-ed an IS NULL into NotEquals, so qty != 2 returned the rows where
qty is null. Hibernate 5 maps NotEquals to Restrictions.ne, which is plain SQL and
leaves them out. An application moving from Hibernate 5 to Hibernate 7 got different
rows for the same query. Match Hibernate 5 for property comparisons, comparisons
against a subquery, and function comparisons.

The Hibernate 7 copy of NullValueEqualSpec now carries the same documented exception
to the TCK that the Hibernate 5 copy has. ne with a null value still means IS NOT NULL;
Hibernate 5 turns that into '<> NULL', which matches nothing.
@borinquenkid

Copy link
Copy Markdown
Member Author

For the team: != / ne and null rows

This PR adds integration scenarios to grails-test-examples/hibernate5/grails-hibernate and .../hibernate7/grails-hibernate, the same spec on both. Running it showed Hibernate 7 and Hibernate 5 disagreed on one thing:

  • where { age != 11 }, ne 'age', 11 and countByAgeNotEqual(11) leave out rows where age is null on Hibernate 5 (Restrictions.ne, plain SQL <>).
  • Hibernate 7 included them (an OR … IS NULL added in the April query-engine rewrite), so the same query returned different rows after an upgrade.

781d0f3 makes H7 match H5. The H7 NullValueEqualSpec now carries the same documented exception to the TCK that the H5 copy has.

Why H5 is the reference, not the TCK line: the TCK assertion countByAgeNotEqual(11) == 2 came in with grails-data-mapping #1220 (2019). It never passed on Hibernate: Graeme disabled it for Hibernate as "not yet implemented", and a contributor noted that making NotEqual null-inclusive "may break existing application code". The docs never promise it. So Hibernate has always left null rows out of !=.

Worth sharing with the team

  • Neo4j and the other datastores still count null rows, so the datastores differ here. That was already true before this PR; I did not change it.
  • ne 'age', null stays IS NOT NULL on H7, as the dynamic finder does (countByAgeNotEqual(null) == 1 on both). Only H5's criteria-builder path turns it into <> NULL, which matches nothing.
  • ! / not {} already left null rows out on both.

Full grails-data-hibernate7-core suite: 4351 tests, 0 failures. Integration spec: H5 38/38, H7 39/39.

@borinquenkid

Copy link
Copy Markdown
Member Author

Heads-up for reviewers: #16573 (the createAlias fix for #16562) changes the same two files this chain refactors, PredicateGenerator and JpaCriteriaQueryCreator. It is further along in review, approved and with its requested changes fixed, and it merges cleanly into 8.0.x today. So this chain will rebase onto #16573 after it merges, and its alias changes will be carried over into the criterion handlers here. Nothing is needed from the #16573 author, and there is nothing to do on this PR until then.

@borinquenkid borinquenkid added this to the grails:8.0.1 milestone Nov 4, 2026

@jdaugherty jdaugherty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran this at 781d0f3. The full grails-data-hibernate7-core suite passes, and a throwaway spec run on this head and on 8803d54, on Hibernate 7 and Hibernate 5, confirms the fix: every where-query function the guide documents now runs on Hibernate 7, including year(pets.birthDate), in, null operands, paging and updateAll / deleteAll. The dispatch rewrite keeps the old order: every instanceof target of the old chain is a class, and the nearest-superclass lookup resolves them the way the chain did.

The inline comments are, in order of weight: a spec that expects a query that cannot be parsed, the function name going into the query unchecked, the javadoc of the public SPI, an upgrade note for !=, the visibility of the new types, executed tests for what changes here, and two small ones.

For the rebase onto #16573: only PredicateGenerator.java shows a conflict, but AssociationCriterionHandler is a new file that holds the association code from before that change, so it merges silently. It needs resolveAssociation, the QueryAliasRegistrar context and the null for a block without criteria ported by hand.

def info = new JpaQueryBuilder(entity, criteria).buildUpdate(age: 30)

then:
info.query == "UPDATE ${JpaFunctionTestPerson.name} jpaFunctionTestPerson SET jpaFunctionTestPerson.age=:p1 WHERE (upper(jpaFunctionTestPerson.name) IN (SELECT jpaFunctionTestPerson1.name FROM ${JpaFunctionTestPerson.name} jpaFunctionTestPerson1 WHERE jpaFunctionTestPerson1.name=:p2jpaFunctionTestPerson1.age > :p3) AND lower(jpaFunctionTestPerson.name) = :p4)"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected query has no AND between the two criteria of the subquery (...name=:p2jpaFunctionTestPerson1.age > :p3), so this pins a query that Hibernate cannot parse. Executed at this head, inList('name', sub) with two criteria in sub, followed by updateAll, throws a SyntaxException on Hibernate 7 and a QuerySyntaxException on Hibernate 5, and a subquery without criteria renders WHERE ).

buildSubQuery appends the criteria with nothing between them. This PR already changes that loop to carry position, so please join them here as well: #16822 (e036996) does it with buildWhereClauseForCriterion and a Query.Conjunction, and leaves WHERE out for an empty list. Then this expectation can be a query that runs. If that change stays in #16822, give this stub a single criterion so the spec does not assert broken output until then.

parameters.add(wrapped.getValue());
return position;
}
whereClause.append(functionCriterion.getFunctionName())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The property goes through validateProperty, but the function name is appended to the query as it is. On Hibernate 5, list() looks the name up in the dialect and rejects one it does not know (Unsupported function defined in query [x]). Here a FunctionCallingCriterion whose function name is not a plain identifier changes the WHERE clause: in a probe it made updateAll update every row, on Hibernate 5 and on Hibernate 7. The same holds for list() on Hibernate 7, where the default branch of FunctionCallingCriterionHandler.applyFunction hands any name to criteriaBuilder.function.

That branch also types every other function as Integer, so a function that returns a string fails with a ConversionFailedException on the value it is compared with.

Please accept only the ten functions the where DSL supports (SUPPORTED_FUNCTIONS in DetachedCriteriaTransformer) in both places, or at least require a plain identifier, and throw InvalidDataAccessResourceUsageException for anything else, as Hibernate 5 does.

*
* @since 8.0
* @author Graeme Rocher
* @since 6.0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PredicateGenerator was added for Hibernate 7 (52e87b8), and 841f6fb set @since 8.0 on it on purpose. Please keep that rather than @author Graeme Rocher / @since 6.0.

The rewrite also drops the javadoc of the public SPI, which is the only documentation it has on 8.0.x (the guide does not cover it):

  • CriterionHandler: registered handlers run before the built-in handling and can override it. The example that is left no longer compiles, since c is undefined without the def c = criterion as MyCustomCriterion line.
  • CriterionHandlerProvider: the META-INF/services/org.grails.orm.hibernate.query.PredicateGenerator$CriterionHandlerProvider file name and the example.
  • registerCriterionHandler: the type has to match exactly, and a programmatic registration wins over a ServiceLoader one.
  • clearCustomCriterionHandlers.

The comments that explain the moved code are gone as well: convertComparisonValue (why a plain value is not wrapped in a literal, which is what keeps comparisons on a UserType property working), asExpression, handleEqualsIgnoreCase, aliasColumn and nativeSqlArguments. Please carry them over to where the code lives now.

Subquery<?> subquery = buildSubquery(context, propertyPath, qc, pc.getProperty());
return criteriaBuilder.notEqual(propertyPath, subquery);
}
return criteriaBuilder.notEqual(propertyPath, context.convertComparisonValue(pc.getProperty(), value, propertyPath));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the question for the team: I agree with leaving the null rows out. At 8803d54 the same where query picked different rows depending on the operation. With qty null on one row, where { qty != 2 }.list(), count(), ne 'qty', 2 and findAllByQtyNotEqual(2) returned that row, while where { qty != 2 }.updateAll(..) and deleteAll() left it alone (JpaQueryBuilder renders a plain !=), and so did !(qty == 2), not { eq 'qty', 2 }, !(qty in [2]) and findAllByQtyNotInList([2]). With this commit they all agree.

It does change the rows an application on 8.0.0 gets back from !=, ne and the NotEqual finders, also inside an ||. Please add a section to upgrading80x.adoc ("As of Grails 8.0.1 ...") that says so and shows how to keep the null rows (qty != 2 || qty == null), and a sentence next to != / ne in the Hibernate 7 whereQueries.adoc and criteria docs.

* @since 8.0.1
*/
@FunctionalInterface
public interface InternalCriterionHandler<T extends Query.QueryElement> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InternalCriterionHandler, CriterionContext and the ten handler classes are public types with public constructors and static methods, new in a patch release. Nothing outside org.grails.orm.hibernate.query uses them (the specs go through PredicateGenerator), so they can be package-private, as QueryAliasRegistrar is. That keeps them free to change, and it avoids a second public context type next to QueryPathContext, which 8.1.x already hands to custom handlers.

Optional: findHandler walks the superclass chain, so registering PropertyCriterion, SubqueryCriterion, PropertyNameCriterion, PropertyComparisonCriterion and Junction covers their subclasses. The 42 entries for the subclasses each create another handler instance and change nothing.

matches == ['a', 'b']
}

void "bulk updateAll and deleteAll support function IN, ILike, and subquery criteria"() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature runs two updateAll calls; there is no deleteAll and no subquery in it. Several things this PR changes are covered only by specs that compare query strings or count predicates. All of them pass when executed, so this is about pinning them:

  • deleteAll with a function, and a function in over a subquery in a bulk operation.
  • Parameter positions after a subquery in a bulk operation: inList('name', sub); eq('qty', 1) followed by updateAll fails at 8803d54 (SemanticException on Hibernate 7, Could not locate named parameter [p3] on Hibernate 5) and works here, for notIn too. That fix reaches Hibernate 5, which has no executed test for it.
  • == and != against a projected subquery, such as where { qty == max(qty) }: a ClassCastException at 8803d54, right here because buildSubquery now hands the subquery's projections to the creator. PredicateGeneratorSpec only checks predicates.length == 13 for it.
  • The association form the guide documents, year(pets.birthDate) == 2009, and month, day, hour, minute and second.

if (wrapped instanceof Query.LessThanEquals) {
return " <= ";
}
if (wrapped instanceof Query.Like) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RLike extends Like, so a function over an rlike is rendered as LIKE in a bulk query: lower(name) with rlike '^r.x$' matches a row through list() and updates none through updateAll. A plain RLike is rejected there as unsupported; returning null for it here does the same.

names(rows) == ['a', 'c']
}

void "where: negation follows SQL three-valued logic, unlike != which includes null rows"() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name still says != includes null rows. The Hibernate 5 copy has the same line.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A where query can call a function in a comparison, and a non-null != comparison leaves out nulls, as Hibernate 5 does. Not approving. updateAll and deleteAll write a regular expression as LIKE, and they bind a one-row subquery as a parameter instead of rendering it.

if (wrapped instanceof Query.LessThanEquals) {
return " <= ";
}
if (wrapped instanceof Query.Like) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A function around an rlike comparison, then deleteAll(), deletes nothing when the name matches the regular expression. RLike is a Like, so this branch emits LIKE. Handle the regular expression before this branch. A read of the same criterion already uses the regular expression.

throw new InvalidDataAccessResourceUsageException("Unsupported criterion in a function call: " + wrapped.getClass().getSimpleName());
}
whereClause.append(operator).append(PARAMETER_PREFIX).append(++position);
parameters.add(wrapped.getValue());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A function around an equality whose value is a one-row detached criteria, then deleteAll(), binds the criteria object as the parameter and the update fails. Render that value as a subquery, as the read path does.

The association code that #16573 changed in PredicateGenerator now lives in AssociationCriterionHandler: a block resolves
its association through the context, takes the context of a QueryAliasRegistrar, and yields no predicate when it has
no criteria.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…orted functions and render a subquery value in a bulk statement

A subquery in a bulk statement wrote its criteria one after another with nothing between them, so a subquery with
two criteria did not parse and one with none ended in WHERE. The criteria are joined with AND and WHERE is left out
for none.

A function name was written into the statement as it was given, so a name that was not an identifier changed the WHERE
clause of updateAll and deleteAll, and Hibernate 7 passed any name to the database. Only the ten functions the where DSL
accepts are run, in the bulk statement and in Hibernate 7, and any other name throws InvalidDataAccessResourceUsageException.
The list is declared once, in FunctionCallingCriterion, and the DSL transform uses it.

A function over an rlike was written as LIKE in a bulk statement, which matched differently from the read of the same
criterion. A bulk statement has no regular expression operator, so it is rejected, as a plain rlike is.

A function over an equality or a comparison with a one-row detached criteria bound the criteria object as the parameter.
The criteria is written as a subquery, as the read does.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…6817)

* fix(hibernate7): support scalar subqueries in comparison criteria

Handle QueryableCriteria values in GreaterThan, GreaterThanEquals,
LessThan, and LessThanEquals criteria within PropertyCriterionHandler
by building a JPA Subquery expression instead of converting the criteria
as a literal value. Infer the subquery return type from its projections
for aggregate, identifier, and property projections.

Fixes #16812

* test(hibernate): run scalar subquery comparisons against the database on H5 and H7

Add integration scenarios to the Hibernate 5 and Hibernate 7 grails-hibernate test apps
that compare a property with avg, max and min subqueries (>, >=, <, <=, ==, !=), use a
subquery in an in list, and combine one with another criterion, through where queries
and the criteria builder. Both modules return the same rows, and the scenarios fail
when the greater-than, less-than, equal or not-equal subquery branch is changed.

* test(hibernate): a scalar subquery that returns several rows is a database error on H5 and H7

* test(hibernate): run count, countDistinct, id and property subqueries on H5 and H7

Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX

@jamesfredley jamesfredley left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An empty list inside a function comparison still produces IN ().

…hing

Answers the review of #16816: where { upper(name) in [] } built IN (), which a database that is sent the HQL as written rejects. The function form and the plain Query.In form now render 1 = 0, and a negation of it NOT(1 = 0).

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

Answers the empty-list comment (JpaQueryBuilder). An empty literal IN list now renders 1 = 0 in the function form and in the plain Query.In form, and a negation of it renders NOT(1 = 0).

Hibernate 7 already coped on Oracle and Db2, because it renders an empty IN as a false condition itself; the HQL text still said IN (), which a database that is sent the HQL as written (Hibernate 5) rejects, so the change is in the shared builder. JpaQueryBuilderFunctionSpec has the rows (alone, beside a criterion, under a negation, and the one-value controls); 6 fail without the change. WhereQueryFunctionsSpec has an end-to-end row for a read and for updateAll and deleteAll. datastore-core 470, in-memory 725, Hibernate 5 1180, Hibernate 7 5276 tests, 0 failures; codeStyle clean.

Not changed here: the Hibernate 5 builder on 7.0.x is a different file.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The items this review found have been addressed. An empty list inside a function comparison no longer produces IN ().

@matrei matrei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at b3554b4 (base 8.0.x). The merge base is d71bc1a, 94 commits behind 8.0.x, and the branch now conflicts with it in upgrading80x.adoc only. CI has never run on this branch: it carries no ci.

What I ran locally:

  • This head merged with today's 8.0.x (the only conflict is the upgrade guide; I kept both sides), then the suites of the four modules it changes: grails-datastore-core (479), grails-datamapping-core (1816), grails-data-hibernate7-core (5325) and grails-data-hibernate5-core (1220). All pass, so the 94 commits on 8.0.x since the merge base don't break anything here; CI still has to confirm it (item 2).
  • The PR's Hibernate 5 WhereQueryBulkStatementSpec on 7.0.x (171db20): 6 of its 7 features fail there (item 3).

jdaugherty's 11-05 points are answered in the commits of 11-07: I checked the restored SPI Javadoc, @since 8.0, the package-private handlers, the function list and the upgrade note. James's last round says his are all addressed.

Needs a fix

  1. #16818 is not in this branch. #16817 was squashed into fix/h7-where-query-functions-8.0.x on 11-15 at 00:38 (e2e5bdc). #16818 was squashed into #16817's branch, fix/h7-scalar-subqueries-comparisons-8.0.x, eight minutes later (10531d1), after that branch had already been merged here. So "preserve subquery projections, fail fast on unresolved paths, and filter nulls in inList" shows as merged on GitHub but is in no branch that reaches 8.0.x. It now only travels inside #16820, whose base is #16818's head branch: once #16820 is retargeted to this branch, #16818's changes appear in #16820's diff as if they were new. Please cherry-pick 10531d1 onto this branch (James approved it on #16818), so that this PR holds #16817 and #16818 as intended and #16820's diff stays its own.

  2. CI and the conflict. no ci keeps every check off a PR that targets 8.0.x itself; the reason given on the stacked PRs ("until the chain reaches 8.0.x") does not apply to the root. Please merge 8.0.x (the conflict is the numbering in upgrading80x.adoc), remove the label, and let CI run before this is approved.

  3. Branch: the subquery fix of the bulk statements belongs on 7.0.x as well. JpaQueryBuilder writes updateAll and deleteAll for Hibernate 5 too, and the subquery part of this PR fixes a call that throws there today. The first feature of the PR's own WhereQueryBulkStatementSpec, a criterion after an inList subquery in updateAll, fails on 7.0.x with IllegalArgumentException: Could not locate named parameter [p3], expecting one of [p1, p2], and a subquery with two criteria renders them without AND. Those changes do not depend on the function work: buildSubQuery joining the criteria through a Query.Conjunction and leaving out WHERE for none, and position = parameters.size() after it in the In handler and in handleSubQuery. A small PR against 7.0.x with them and that feature, merged up, gets the fix to Hibernate 5 applications on 7.x; this PR keeps the rest. Whether functions in Hibernate 5 bulk statements (they throw "Queries of type FunctionCallingCriterion are not supported" on 7.0.x) should follow is a separate question.

  4. Description. It still describes the first commit. The PR now also changes results and adds checks, and the rule in the dev@ thread asks the description to say which results change and how to get the old ones:

    • !=, ne and the NotEqual finders leave out rows whose value is null on Hibernate 7, as Hibernate 5 does (upgrade note 26.14; qty != 2 || qty == null keeps them).
    • Bulk statements: subquery criteria joined with AND, parameter positions after a subquery, an empty IN list written as a condition that matches nothing, a subquery without projection compared by the property of the function.
    • Only the ten where-query functions are accepted, in a read and in a bulk statement; a function over an rlike is rejected in a bulk statement.
    • A fraction compared with a function is kept as it is; the date functions are Hibernate's own.
    • #16817 (scalar subqueries in comparisons), and #16818 once item 1 is done.

    Also, "(1)$ concurrent map lookup" in the second section has lost its O.

Should fix

  1. Small API points on a patch line:

    • JpaQueryBuilder.buildSubQuery(..., String comparedProperty) is a new protected static overload on a public class, and only the class itself calls it. It can be private; the existing protected overload delegates to it.
    • FunctionComparison (with its two enums and UnsupportedComparisonException) and the new public static members of FunctionCallingCriterion are new public API in grails-datastore-core. The Hibernate 7 handler and JpaQueryBuilder need them across modules, so they cannot be package-private, but a Javadoc line saying they are internal to GORM and may change would keep 8.1.x free to change them.
  2. Upgrade note 26.14 says nulls are left out "on Hibernate 7 and on Hibernate 5", which reads as if Hibernate 5 changed. This PR does not change Hibernate 5's reads; Hibernate 5 already left them out. "on Hibernate 7, as Hibernate 5 always has" says what changed, and that the change restores the Grails 7 result.

Verified as correct

  • Both paths check the function name before anything is rendered: FunctionComparison.of calls FunctionCallingCriterion.requireSupported, and the bulk statement and the Hibernate 7 handler both go through it, so a name outside the ten no longer reaches the query.
  • An empty IN list becomes 1 = 0, and its negation NOT (1 = 0) matches every row. notIn takes only a subquery, so it has no empty-list case.
  • The handlers are package-private, and the public SPI (CriterionHandler, CriterionHandlerProvider, CUSTOM_HANDLERS, registerCriterionHandler, clearCustomCriterionHandlers) keeps its Javadoc and its example, which compiles again.
  • The != change restores the Hibernate 5 result on Hibernate 7, which is what 8.0.x is for under the rule on dev@, and it has its upgrade note.

@matrei

matrei commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

A correction to item 1 of my review. Cherry-picking 10531d1 here on its own does not work:

So #16818 can come here only together with the repair from #16821. Or it stays in the chain, and #16820 and #16821 say in their descriptions that they carry #16818 and land together. The point of item 1 stands either way: GitHub shows #16818 as merged, and its change is in no branch that reaches 8.0.x yet.

…ed paths, and filter nulls in inList (#16818)

* fix(hibernate7): preserve subquery projections, fail fast on unresolved paths, and filter nulls in inList

- Copy subquery projections into a local ProjectionList in PropertyCriterionHandler and SubqueryCriterionHandler instead of mutating the caller's QueryableCriteria in-place
- Throw ConfigurationException when property paths cannot be resolved in PropertyComparisonCriterionHandler, PropertyNameCriterionHandler, and SubqueryCriterionHandler
- Filter null elements in In criteria across PropertyCriterionHandler and FunctionCallingCriterionHandler and return a false predicate (disjunction) when no non-null values remain

Fixes #16815

* test(hibernate): run inList with nulls, reused subquery criteria and unknown properties on H5 and H7

Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
The only conflict is the numbering of the upgrade guide: the sections 26.11 to 26.14 of this branch are kept after 26.10.
Also, for the review of the branch:
- the protected buildSubQuery overload that takes the compared property is private, since only JpaQueryBuilder calls it;
- FunctionComparison and the public static members of FunctionCallingCriterion say they are internal to GORM;
- the upgrade note on not-equal comparisons says it restores the result of Hibernate 5 and of Grails 7 on Hibernate 7.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…16821

Preserving the failure of an unresolved property comparison in a subquery (#16818) makes the feature that compares with a
path on the root alias of the enclosing query fail where the comparison used to be dropped without an error. #16821
registers the root alias and rewrites the feature; #16884 tracks it.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

Thank you, all six are done in the new commits: d8f0b90 (#16818), 7499148 (merge of 8.0.x) and 895101b.

  1. fix(hibernate7): preserve subquery projections, fail fast on unresolved paths, and filter nulls in inList #16818. I cherry-picked 10531d1 onto this branch (d8f0b90), so this pull request holds fix(hibernate7): support scalar subqueries in comparison criteria #16817 and fix(hibernate7): preserve subquery projections, fail fast on unresolved paths, and filter nulls in inList #16818 as intended and the diff of Materialize deferred criteria on a copy before translating queries and subqueries #16820 stays its own. One file conflicted, FunctionCallingCriterionHandler: this branch had already restructured it around FunctionComparison, which carries the null filtering, the empty list and the subquery property, so I kept this branch's version. One consequence: SubqueryAliasSpec > Test subquery with root alias fails once fix(hibernate7): preserve subquery projections, fail fast on unresolved paths, and filter nulls in inList #16818 is in, because an unresolved property comparison in a subquery now fails with a ConfigurationException where it used to be dropped. That is H7 SubqueryAliasSpec 'root alias' fails on the PredicateGenerator chain until #16821 #16884, which Fix root alias registration and nested alias path resolution in subqueries #16821 fixes by registering the root alias, and it rewrites that spec. I marked the feature @PendingFeature with that reason, so the module is green and the failure is visible; Fix root alias registration and nested alias path resolution in subqueries #16821 removes the marker when it rewrites the spec.
  2. CI and the conflict. 8.0.x is merged (the only conflict was the numbering in upgrading80x.adoc, kept after 26.10), and I removed the no ci label, so the next push runs CI.
  3. Branch. The subquery part of the bulk statements is now fix(datastore): a subquery in updateAll or deleteAll takes its parameters in order and joins its criteria with AND #17049 on 7.0.x: buildSubQuery joins its criteria as a conjunction and has no WHERE for none, and the position continues from the number of parameters after a subquery in the In handler and in handleSubQuery. Its spec has four features, all failing without the change; the one you named is the first. This pull request keeps the rest, and the merge-up will conflict with it on those lines: the version here is a superset. I did not move functions in Hibernate 5 bulk statements; that stays the separate question you describe.
  4. Description. Rewritten: the three parts, the results that change and how to get the old ones, and the verification. The typo is gone.
  5. API points. The buildSubQuery overload that takes the compared property is private. FunctionComparison and the public static members of FunctionCallingCriterion say in their Javadoc that they are internal to GORM and may change in a minor release.
  6. Upgrade note 26.14 says that on Hibernate 7 the change restores the result of Hibernate 5 and of Grails 7, which always left the nulls out, and that Hibernate 5 is not changed.

The tests of grails-datastore-core (479), grails-datamapping-core (1816), grails-data-hibernate7-core (5337) and grails-data-hibernate5-core (1220) pass, as in your run, and codeStyle is clean.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A negated in list that contains null no longer means what SQL means.

…as on Hibernate 5

ne no longer returns rows whose value is null on Hibernate 7 (matching Hibernate 5), so
a club without a team is left out of ne on a property of a left alias, as on Hibernate 5
(#16905). The scenario merged in #16907 expected the earlier behaviour.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

CI on 895101b had one failure on every failing job: QueryEntryPointScenariosSpec > not over a left-joined association: a missing association matches ne but not not-eq in the Hibernate 7 gorm example (from #16907, which merged on 8.0.x after the earlier null decision). #16816 makes ne leave out rows whose value is null, as Hibernate 5 does, so a club without a team is left out of ne on a property of a left alias. On Hibernate 5 the same scenario is already @PendingFeature for that reason (#16905), so 2c6b447 marks the Hibernate 7 one the same way, with the same reference. It passes with that (7 tests, 1 pending).

The branch also merges 8.0.x (no conflicts) so the run is on a current base. Everything else in the run passed.

borinquenkid added a commit that referenced this pull request Nov 19, 2026
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

# Conflicts:
#	grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/FunctionCallingCriterionHandler.java
#	grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/PropertyCriterionHandler.java
#	grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/query/jpa/JpaQueryBuilder.java

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A negated in list that contains null still does not mean what SQL means.

borinquenkid added a commit that referenced this pull request Nov 19, 2026
…p the registry layout feature

The merge of #16816 had added null filtering for a function's in list and a disjunction for an empty one in
FunctionComparison and FunctionCallingCriterionHandler. Neither parent has it, so it goes back to
#16816's version of both files; a function's list follows whatever #16816 decides for a plain in.

PredicateGeneratorHandlerRegistrySpec no longer has the feature that pins how the registration map is laid
out, which no query depends on.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

The note "Not-Equal Comparisons Leave Out Null Rows" is now in the Grails 8.0.1 section of upgrading80x.adoc, not in the 8.0.0 list, because 8.0.0 is released. WhereQueryBulkStatementSpec no longer names this PR in @Issue; it names #16811.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@matrei matrei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Round 2, reviewed at caf243f. Thanks for the quick turnaround. CI passes at caf243f.

Round 1

  1. #16818: d8f0b90 is 10531d1 except for the FunctionCallingCriterionHandler hunk, where this branch's FunctionComparison path is kept (I compared them with git range-diff). On the pending SubqueryAliasSpec feature: 8.0.x today drops the comparison with the root alias and returns both teams, and this branch throws a ConfigurationException instead. That is the better failure, but #16816 and #16821 should go out in the same release.
  2. CI and the conflict: done.
  3. Branch: done as #17049, which I'll review on its own.
  4. Description: done, apart from one line that changes with item A below.
  5. API: done. The changes are in the merge commit 7499148, not in a commit of their own, which makes them easy to miss, but they are right.
  6. Upgrade note: done, and it's now in the 8.0.1 section.

Needs a fix

A. A negated in with a null in the list (James's point). He's right. I ran the forms on Hibernate 5 on 7.0.x, on Hibernate 7 on 8.0.x and at this head, with three rows: qty 1, 2 and null.

Query Hibernate 5, 7.0.x Hibernate 7, 8.0.x This head
where { qty in [1, null] }, inList, findAllByQtyInList [one] ConfigurationException [one]
where { !(qty in [1, null]) }, not { inList(...) }, findAllByQtyNotInList([1, null]) [] ConfigurationException [two]
where { !(qty in [null]) } [] ConfigurationException [none, one, two]
where { !(qty in [1, null]) }.updateAll(...) 0 rows 0 rows 0 rows
where { !(qty in [null]) }.updateAll(...) 0 rows 0 rows 0 rows
where { !(lower(name) in ['one', null]) } lower not supported UnsupportedOperationException []

On 8.0.x a null anywhere in the list throws, which is a regression from Grails 7, so fixing it here is right. But dropping the null restores the Hibernate 5 result only for in, not for its negations:

  • !(qty in [null]) returns the row whose qty is null. That contradicts the PR's own upgrade note, which says a not-equal comparison leaves those rows out.
  • The read and the bulk statement of the same where query disagree again. list() returns two, but updateAll changes no row, because JpaQueryBuilder keeps the null. The note says that mismatch is gone.

The function path of this PR already keeps the null, and gives SQL's result (last row). Please keep the null in PropertyCriterionHandler.handleIn too, so that SQL decides there, as it does on Hibernate 5, in the bulk statements and for the functions. Also add the negated forms to CriteriaInListScenarioSpec, in both example applications. The description's "nulls filtered out of an in list" then becomes "a null in an in list no longer throws".

Should fix

B. The ne scenario through a left alias (2c6b447). @PendingFeature marks the expectation as the result we want, but this one says "a missing association matches ne", which is what this PR decides against. With the PR, Hibernate 5's result is the reference for ne, as your correction on #16905 says. So the feature in both example applications should expect ['c2', 'c3'] for ne and for not { eq }, without @PendingFeature, and get a name to match. That also answers point 2 of #16905.

Verified

  • The != change: qty != 2, ne('qty', 2) and findAllByQtyNotEqual(2) return [one] on Hibernate 5 on 7.0.x and at this head, and [none, one] on 8.0.x. !(qty == 2), not { eq(...) } and findAllByQtyNotInList([2]) return [one] on all three.
  • A null in a positive in, and an empty list: they give the Hibernate 5 result. !(qty in []) returns every row on all three.

…result for ne through a left alias

A null in an in list stays in the list, as it does on Hibernate 5 and in the bulk statements, so SQL
decides what it matches: a negated list that holds a null matches no row, the read and the bulk
statement of the same where query agree, and a negated empty list still matches every row.
The ne scenario through a left alias in both example applications expects c2 and c3, as Hibernate 5
returns, and is no longer pending.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@matrei: round 2.

A. PropertyCriterionHandler.handleIn keeps the null in the list now, so SQL decides, as on Hibernate 5 and in the bulk statements. CriteriaInListScenarioSpec in both example applications has the negated forms: !(qty in [1, null]) through where, not { inList } and findAllByQtyNotInList match no row, !(qty in [null]) matches no row, !(qty in []) matches every row, and the updateAll of !(qty in [1, null]) writes 0 rows, the same as the read. The two rows with a null fail without the change. The description now says a null in an in list no longer throws.

B. The ne scenario through a left alias expects ['c2', 'c3'] for ne and for not { eq } in both example applications, without @PendingFeature.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The items this review found have been addressed. A negated in list that contains null keeps the null.

@testlens-app

testlens-app Bot commented Nov 20, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 85c8df3
▶️ Tests: 121907 executed
⚪️ Checks: 99/99 completed


Learn more about TestLens at testlens.app/docs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Hibernate 7: Support where-query functions (upper, lower, trim, length, year)

5 participants