diff --git a/presto-main-base/src/main/java/com/facebook/presto/sql/planner/iterative/rule/MinMaxByToWindowFunction.java b/presto-main-base/src/main/java/com/facebook/presto/sql/planner/iterative/rule/MinMaxByToWindowFunction.java index c8acb0b6a833f..b10d9ddd27e7c 100644 --- a/presto-main-base/src/main/java/com/facebook/presto/sql/planner/iterative/rule/MinMaxByToWindowFunction.java +++ b/presto-main-base/src/main/java/com/facebook/presto/sql/planner/iterative/rule/MinMaxByToWindowFunction.java @@ -47,6 +47,34 @@ import static com.facebook.presto.sql.relational.Expressions.comparisonExpression; import static com.google.common.collect.ImmutableMap.toImmutableMap; +/** + * For queries with min_by/max_by functions on map, rewrite it with top n window functions. + * For example, for query `select id, max(ds), max_by(feature, ds) from t group by id`, + * it will be rewritten from: + *
+ * - Aggregation
+ *      ds_0 := max(ds)
+ *      feature_0 := max_by(feature, ds)
+ *      group by id
+ *      - scan t
+ *          ds
+ *          feature
+ *          id
+ * 
+ * into: + *
+ *     - Filter
+ *          row_num = 1
+ *          - TopNRow
+ *              partition by id
+ *              order by ds desc
+ *              maxRowCountPerPartition = 1
+ *              - scan t
+ *                  ds
+ *                  feature
+ *                  id
+ * 
+ */ public class MinMaxByToWindowFunction implements Rule {