Class MultipleDistinctAggregationsToSubqueries

java.lang.Object
io.trino.sql.planner.iterative.rule.MultipleDistinctAggregationsToSubqueries
All Implemented Interfaces:
Rule<AggregationNode>

public class MultipleDistinctAggregationsToSubqueries extends Object implements Rule<AggregationNode>
Transforms plans of the following shape:
 - Aggregation
        GROUP BY (k)
        F1(DISTINCT a0, a1, ...)
        F2(DISTINCT b0, b1, ...)
        F3(DISTINCT c0, c1, ...)
     - X
 
into
 - Join
     on left.k = right.k
     - Aggregation
         GROUP BY (k)
         F1(DISTINCT a0, a1, ...)
         F2(DISTINCT b0, b1, ...)
       - X
     - Aggregation
         GROUP BY (k)
         F3(DISTINCT c0, c1, ...)
       - X
 

This improves plan parallelism and allows SingleDistinctAggregationToGroupBy to optimize the single input distinct aggregation further. The cost is we calculate X and GROUP BY (k) multiple times, so this rule is only beneficial if the calculations are cheap compared to other distinct aggregation strategies.