Branch: refs/heads/master
Home:
https://github.com/hibernate/hibernate-orm
Commit: 21e79125e601160101f1f27e1aea026ede9c36a9
https://github.com/hibernate/hibernate-orm/commit/21e79125e601160101f1f27...
Author: Jan-Willem Gmelig Meyling <jan-willem(a)youngmediaexperts.nl>
Date: 2019-11-25 (Mon, 25 Nov 2019)
Changed paths:
M hibernate-core/src/main/antlr/hql-sql.g
M hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java
M hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java
M hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java
M hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java
M hibernate-core/src/main/java/org/hibernate/hql/internal/ast/HqlSqlWalker.java
A hibernate-core/src/test/java/org/hibernate/query/GroupByAliasTest.java
M hibernate-testing/src/main/java/org/hibernate/testing/DialectChecks.java
Log Message:
-----------
HHH-9301 - Support select variable refs in group by for DBMS dialects that support it
While not strictly compliant with the SQL specification, the MySQL, PostgreSQL and H2
support the use of select aliases in the GROUP BY clause. An obvious benefit is that
produced SQL queries will be easier to read, because complex select expressions from
aggregrate queries will have to be included in the group by clause as well. These can now
simply reference the aliases of the respective columns for the tuple element. However,
there is also a functional difference. For function invocations that have parameterized
arguments, the query optimizer can't guarantee the that the function result between
the selection projection and grouping process are equal. This results in an error because
a value is projected which is not grouped by. An example where this for example becomes
relevant, is when parameterizing TimeScaleDB's
[`time_bucket_gapfill()`](https://docs.timescale.com/latest/api#time_bucket_gapfill-examples)
function.
For example, using `time_bucket_gapfill()` one might want to produce a criteria query that
produces the following SQL:
```sql
SELECT
time_bucket_gapfill(?, time, ?, ?) AS ts,
avg(tg) as tg
FROM iaqmeasurement
GROUP BY ts
ORDER BY ts;
```
When the alias is not used as grouping value, the query will yield an error:
```sql
SELECT
time_bucket_gapfill(?, time, ?, ?) AS ts,
avg(tg) as tg
FROM iaqmeasurement
GROUP BY time_bucket_gapfill(?, time, ?, ?)
ORDER BY ts;
```
Of course the parameter values can just be stored in a CTE as well, but I think we should
consider support for variable refs in group by for DBMS dialects that support it. This
pull request implements the feature equivalently to how its currently done for order by
clauses, and only enables the feature for known supported dialects (H2, PostgreSQL and
MySQL - based on
[
https://stackoverflow.com/a/3841804/2104280](https://stackoverflow.com/a/...).
Jira:
https://hibernate.atlassian.net/browse/HHH-9301
Co-authored-by: Sayra Ranjha <S.S.Ranjha(a)student.tudelft.nl>