gustavoharff opened a new pull request, #16597:
URL: https://github.com/apache/grails-core/pull/16597

   ## Description
   
   On Hibernate 7, an HQL query whose only `select` is in a subquery is 
rewritten into an invalid one before it reaches Hibernate:
   
   ```groovy
   Book.findAll('from Book where title in (select name from Author where name 
like :p)', [p: 'A%'])
   ```
   
   is sent as `select e.name from Author e where name like :p)` and fails with 
a `SyntaxException`. The same happens to an `update` or a `delete` with a 
subquery, and to a query with `select ` inside a string literal. An `insert ... 
select` loses its `insert into` and fails with `IllegalMutationQueryException: 
Expecting a mutation query`. Hibernate 7 itself accepts all of these queries, 
and they ran on Hibernate 5.
   
   `HqlQueryContext` looked for the first `"select "` and `" from "` anywhere 
in the HQL. `normalizeNonAliasedSelect`, which gives an unaliased query the 
alias `e`, then rebuilt the query from that `select` and dropped what came 
before it. `countHqlProjections`, `getSingleProjectionClause` and 
`getRootAlias` read the same positions, so such a query also got `Object` 
instead of the entity as its result type.
   
   Changes:
   
   - `indexOfTopLevel` finds a keyword outside parentheses, string literals and 
quoted identifiers. The four methods use it for the `select` and `from` of the 
query, so those of a subquery or a literal are not taken for them.
   - `normalizeNonAliasedSelect` keeps what precedes the `select`, such as the 
`insert into` of an insert-select.
   
   ## Testing
   
   - `HibernateGormStaticApiSpec`, end to end through the static API: `findAll` 
and `executeQuery` with a subquery and no select clause, `executeUpdate` with 
an `update` and a `delete` with a subquery, an `insert ... select`, and a 
subquery in the select clause.
   - `HqlQueryContextSpec`: new rows for `getTarget`, 
`normalizeNonAliasedSelect` and `countHqlProjections`, and a table for 
`indexOfTopLevel` (a subquery, a string literal with a parenthesis, the column 
list of an insert).
   - 12 of the new cases fail on `8.0.x` without this change (9 in 
`HqlQueryContextSpec`, 3 in `HibernateGormStaticApiSpec`). The others cover the 
new helper, or guard queries that already worked, such as a subquery in the 
select clause.
   - `./gradlew :grails-data-hibernate7-core:codeStyle`: no violations.
   - All passing:
     - `:grails-data-hibernate7-core:test`: 3725 tests, 0 failures
     - `:grails-data-hibernate7:test`
     - `:grails-data-hibernate7-spring-boot:test`
     - `:grails-data-hibernate7-dbmigration:test`
     - `:grails-data-hibernate7-dbmigration-core:test`
     - `:grails-test-examples-hibernate7-criteria-extension:integrationTest`
     - `:grails-test-examples-hibernate7-gorm:integrationTest`
   - The full `./gradlew build --rerun-tasks` was not run; the change is 
confined to `grails-data-hibernate7`.
   
   ## Contributor Checklist
   
   ### Issue and Scope
   
   - [ ] This PR is linked to an existing issue that has been **acknowledged or 
approved** by the project team. If no approved issue exists, please give 
background on why this change is necessary.  Tickets are preferred for release 
change log history.
   - [ ] This PR addresses the **complete scope** of the linked issue. Partial 
implementations or unfinished work should not be submitted for review.
   - [x] This PR contains a **single, focused change**. Unrelated changes 
should be submitted as separate pull requests.
   - [x] This PR targets the **correct branch** for the type of change:
       - **Patch release branches** (e.g., `7.0.x`): Bug fixes only. No new 
features or API changes.
       - **Minor release branches** (e.g., `7.1.x`): New features are welcome, 
but breaking existing APIs must be avoided.
       - **Major release branches** (e.g., `8.0.x`): Reserved for major 
changes. Breaking API changes are permitted.
   
   ### Code Quality
   
   - [x] I have **added or updated tests** that cover the changes introduced in 
this PR. All code contributions are expected to include appropriate test 
coverage.
   - [ ] I have verified that all existing tests pass by running `./gradlew 
build --rerun-tasks`.
   - [x] My code follows the project's **code style** guidelines. I have run 
`./gradlew codeStyle` and resolved any violations. See [Code 
Style](../CONTRIBUTING.md#code-style) for details.
   - [x] This PR does **not** include mass reformatting, style-only changes, or 
large-scale refactoring unless it was **explicitly approved** in the linked 
issue. Unsolicited reformatting will not be accepted.
   - [x] If generative AI tooling was used in preparing this contribution, a 
quality model was used to ensure contributions are **consistent with the 
project's quality standards**.
   
   ### Licensing and Attribution
   
   - [x] All contributed code is provided under the [Apache License 
2.0](https://www.apache.org/licenses/LICENSE-2.0), and new source files include 
the appropriate **Apache license header**.
   - [x] I have the necessary rights to submit this contribution and confirm it 
is my own original work (see [Legal 
Notice](../CONTRIBUTING.md#i-want-to-contribute)).
   - [x] If generative AI tooling was used in preparing this contribution, I 
have followed the [Apache Software Foundation's policy on generative 
tooling](https://www.apache.org/legal/generative-tooling.html) and have 
properly attributed its use.
   
   ### Documentation
   
   - [ ] If this PR introduces user-facing changes, I have included or updated 
the relevant documentation.
   - [ ] If this PR adds a new feature, I have updated the **What's New** 
section of the Grails Guide.
   - [ ] If this PR introduces breaking changes or changes that require user 
action during an upgrade, I have updated the **Upgrade Notes** for the 
corresponding version in the Grails Guide.
   - [x] The PR description clearly explains **what** was changed and **why**.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   
   


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