Class AggregateWindowEmulationQueryTransformer

  • All Implemented Interfaces:
    QueryTransformer

    public class AggregateWindowEmulationQueryTransformer
    extends Object
    implements QueryTransformer
    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