org.hibernate.dialect.function
public Class AggregateWindowEmulationQueryTransformer
extends Object
implements QueryTransformer
- Class Inheritance
-
- java.lang.Object
- org.hibernate.dialect.function.AggregateWindowEmulationQueryTransformer
- All Implemented Interfaces
- org.hibernate.sql.ast.tree.expression.QueryTransformer
- Imports
- java.util.ArrayList, .Collections, .HashMap, .List, .Map, org.hibernate.engine.spi.SessionFactoryImplementor, org.hibernate.metamodel.mapping.BasicValuedMapping, .JdbcMapping, org.hibernate.query.ReturnableType, org.hibernate.spi.NavigablePath, org.hibernate.query.sqm.ComparisonOperator, org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression, org.hibernate.query.sqm.sql.SqmToSqlAstConverter, org.hibernate.query.sqm.sql.internal.SqmPathInterpretation, org.hibernate.sql.ast.spi.ExpressionReplacementWalker, .SqlSelection, org.hibernate.sql.ast.tree.SqlAstNode, org.hibernate.sql.ast.tree.cte.CteContainer, org.hibernate.sql.ast.tree.expression.ColumnReference, .Expression, .JdbcParameter, .Literal, .Over, .QueryTransformer, .SqlSelectionExpression, .SqlTuple, org.hibernate.sql.ast.tree.from.QueryPartTableGroup, org.hibernate.sql.ast.tree.predicate.ComparisonPredicate, .Predicate, org.hibernate.sql.ast.tree.select.QuerySpec, .SelectClause, .SelectStatement, .SortSpecification, org.hibernate.sql.results.internal.ResolvedSqlSelection, org.hibernate.type.BasicType
Since the query spec will render a hypothetical set window function instead of an aggregate,
the following query transformer will wrap the query spec and apply aggregation in the outer query.
We can do this because these functions can only be used in the select clause.
A hypothetical set aggregate function like e.g. "rank" returns the rank of a passed value within
the ordered set as defined through the WITHIN GROUP clause.
When used as window function, the function provides the rank of the current row within the ordered set
as defined for the window frame through the OVER clause, but does not do aggregation.
The aggregation effect can be achieved by:
1. Selecting the elements by which the ordered set is sorted
2. In the outer query, add a comparison predicate `function_args`=`sort_expressions`
3. Use an arbitrary row produced by the inner query by using e.g. the "min" function
The following query
select rank(5) within group (order by e.num)
from (values (1), (2), (5)) e(num)
can be rewritten to
select min(t.c1) from (
select rank() over (order by e.num), e.num
from (values (1), (2), (5)) e(num)
) t(c1,c2)
where t.c2 = 5
- Author
- Christian Beikov
Constructor Summary
| Access | Constructor and Description
|
|---|
| public |
|