perf: Eliminate duplicate aggregations by KKould · Pull Request #132 · KipData/KiteSQL

When Distinct and GroupBy exist at the same time, and groupby_exprs are consistent, Distinct can be eliminated.

28156=> explain select distinct a, b from t1 group by a, b; 
                       PLAN                                                                                                                                                                                                        
---------------------------------------------------
 Projection [a, b] [Project]                      +
   Aggregate [] -> Group By [a, b] [HashAggregate]+
     Scan t1 -> [a, b] [SeqScan]
(1 row)

28156=> explain select distinct a, b, sum(k) from t1 group by a, b; 
                           PLAN                                                                                                                                                                                                    
-----------------------------------------------------------
 Projection [a, b, Sum(k)] [Project]                      +
   Aggregate [] -> Group By [a, b, Sum(k)] [HashAggregate]+
     Aggregate [Sum(k)] -> Group By [a, b] [HashAggregate]+
       Scan t1 -> [a, b, k] [SeqScan]
(1 row)