jamesfredley opened a new issue, #16712:
URL: https://github.com/apache/grails-core/issues/16712
## Summary
#16597 taught HQL projection analysis to ignore a `SELECT` nested inside
parentheses, so a real subquery is no longer stripped. The same depth check
treats parentheses around the root query as a subquery. A valid parenthesized
query, including one with no subquery, is then given the domain class as its
result type instead of the scalar type.
## What goes wrong
`HqlQueryContext.indexOfTopLevel()` increments depth on every `(`. For
`(select b.title from Book b)` the root `select` is therefore not visible.
`countHqlProjections()` returns 0, and `getTarget()` returns the domain class
(`Book`) rather than `Object`. `HibernateHqlQueryCreator` passes that class to
`session.createQuery()`, and a query that previously returned titles fails with
a result-type mismatch.
```groovy
Book.executeQuery("(select b.title from Book b)")
```
Before the merge, normalization left this query alone and projection
analysis returned `Object.class`. A fully parenthesized scalar `UNION` branch
has the same problem. Queries that are not wrapped in parentheses are
unaffected.
## Introduced by
- https://github.com/apache/grails-core/pull/16597
- Merge commit `85d7c8621c912513be9916a98a96224e0758f4b5` on `8.0.x`
-
`grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HqlQueryContext.java`
lines 313-317, with the bad target chosen at line 171
## Fix
Distinguish parentheses that wrap the root query from parentheses that wrap
a nested subquery. For a parenthesized root query, keep the original HQL and
let Hibernate infer the result type (the existing null target-class path)
instead of falling back to the domain class. Add a regression for `(select
b.title from Book b)` and for a parenthesized scalar union branch.
--
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]