|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +// This file is copied from |
| 18 | +// https://github.com/apache/impala/blob/branch-2.9.0/fe/src/main/java/org/apache/impala/AggregationNode.java |
| 19 | +// and modified by Doris |
| 20 | + |
| 21 | +package org.apache.doris.nereids.processor.post; |
| 22 | + |
| 23 | +import org.apache.doris.nereids.CascadesContext; |
| 24 | +import org.apache.doris.nereids.properties.DistributionSpecGather; |
| 25 | +import org.apache.doris.nereids.properties.OrderKey; |
| 26 | +import org.apache.doris.nereids.trees.expressions.Expression; |
| 27 | +import org.apache.doris.nereids.trees.plans.AggMode; |
| 28 | +import org.apache.doris.nereids.trees.plans.Plan; |
| 29 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalDistribute; |
| 30 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalHashAggregate; |
| 31 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalHashAggregate.TopnPushInfo; |
| 32 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalLimit; |
| 33 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalProject; |
| 34 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalTopN; |
| 35 | +import org.apache.doris.qe.ConnectContext; |
| 36 | + |
| 37 | +import org.apache.hadoop.util.Lists; |
| 38 | + |
| 39 | +import java.util.List; |
| 40 | +import java.util.stream.Collectors; |
| 41 | + |
| 42 | +/** |
| 43 | + * Add SortInfo to Agg. This SortInfo is used as boundary, not used to sort elements. |
| 44 | + * example |
| 45 | + * sql: select count(*) from orders group by o_clerk order by o_clerk limit 1; |
| 46 | + * plan: topn(1) -> aggGlobal -> shuffle -> aggLocal -> scan |
| 47 | + * optimization: aggLocal and aggGlobal only need to generate the smallest row with respect to o_clerk. |
| 48 | + * |
| 49 | + * TODO: the following case is not covered: |
| 50 | + * sql: select sum(o_shippriority) from orders group by o_clerk limit 1; |
| 51 | + * plan: limit -> aggGlobal -> shuffle -> aggLocal -> scan |
| 52 | + * aggGlobal may receive partial aggregate results, and hence is not supported now |
| 53 | + * instance1: input (key=2, v=1) => localAgg => (2, 1) => aggGlobal inst1 => (2, 1) |
| 54 | + * instance2: input (key=1, v=1), (key=2, v=2) => localAgg inst2 => (1, 1) |
| 55 | + * (2,1),(1,1) => limit => may output (2, 1), which is not complete, missing (2, 2) in instance2 |
| 56 | + * |
| 57 | + *TOPN: |
| 58 | + * Precondition: topn orderkeys are the prefix of group keys |
| 59 | + * TODO: topnKeys could be subset of groupKeys. This will be implemented in future |
| 60 | + * Pattern 2-phase agg: |
| 61 | + * topn -> aggGlobal -> distribute -> aggLocal |
| 62 | + * => |
| 63 | + * topn(n) -> aggGlobal(topn=n) -> distribute -> aggLocal(topn=n) |
| 64 | + * Pattern 1-phase agg: |
| 65 | + * topn->agg->Any(not agg) -> topn -> agg(topn=n) -> any |
| 66 | + * |
| 67 | + * LIMIT: |
| 68 | + * Pattern 1: limit->agg(1phase)->any |
| 69 | + * Pattern 2: limit->agg(global)->gather->agg(local) |
| 70 | + */ |
| 71 | +public class PushTopnToAgg extends PlanPostProcessor { |
| 72 | + @Override |
| 73 | + public Plan visitPhysicalTopN(PhysicalTopN<? extends Plan> topN, CascadesContext ctx) { |
| 74 | + topN.child().accept(this, ctx); |
| 75 | + if (ConnectContext.get().getSessionVariable().topnOptLimitThreshold <= topN.getLimit() + topN.getOffset()) { |
| 76 | + return topN; |
| 77 | + } |
| 78 | + Plan topnChild = topN.child(); |
| 79 | + if (topnChild instanceof PhysicalProject) { |
| 80 | + topnChild = topnChild.child(0); |
| 81 | + } |
| 82 | + if (topnChild instanceof PhysicalHashAggregate) { |
| 83 | + PhysicalHashAggregate<? extends Plan> upperAgg = (PhysicalHashAggregate<? extends Plan>) topnChild; |
| 84 | + List<OrderKey> orderKeys = tryGenerateOrderKeyByGroupKeyAndTopnKey(topN, upperAgg); |
| 85 | + if (!orderKeys.isEmpty()) { |
| 86 | + |
| 87 | + if (upperAgg.getAggPhase().isGlobal() && upperAgg.getAggMode() == AggMode.BUFFER_TO_RESULT) { |
| 88 | + upperAgg.setTopnPushInfo(new TopnPushInfo( |
| 89 | + orderKeys, |
| 90 | + topN.getLimit() + topN.getOffset())); |
| 91 | + if (upperAgg.child() instanceof PhysicalDistribute |
| 92 | + && upperAgg.child().child(0) instanceof PhysicalHashAggregate) { |
| 93 | + PhysicalHashAggregate<? extends Plan> bottomAgg = |
| 94 | + (PhysicalHashAggregate<? extends Plan>) upperAgg.child().child(0); |
| 95 | + bottomAgg.setTopnPushInfo(new TopnPushInfo( |
| 96 | + orderKeys, |
| 97 | + topN.getLimit() + topN.getOffset())); |
| 98 | + } |
| 99 | + } else if (upperAgg.getAggPhase().isLocal() && upperAgg.getAggMode() == AggMode.INPUT_TO_RESULT) { |
| 100 | + // one phase agg |
| 101 | + upperAgg.setTopnPushInfo(new TopnPushInfo( |
| 102 | + orderKeys, |
| 103 | + topN.getLimit() + topN.getOffset())); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + return topN; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + return true, if topn order-key is prefix of agg group-key, ignore asc/desc and null_first |
| 112 | + TODO order-key can be subset of group-key. BE does not support now. |
| 113 | + */ |
| 114 | + private List<OrderKey> tryGenerateOrderKeyByGroupKeyAndTopnKey(PhysicalTopN<? extends Plan> topN, |
| 115 | + PhysicalHashAggregate<? extends Plan> agg) { |
| 116 | + List<OrderKey> orderKeys = Lists.newArrayListWithCapacity(agg.getGroupByExpressions().size()); |
| 117 | + if (topN.getOrderKeys().size() > agg.getGroupByExpressions().size()) { |
| 118 | + return orderKeys; |
| 119 | + } |
| 120 | + List<Expression> topnKeys = topN.getOrderKeys().stream() |
| 121 | + .map(OrderKey::getExpr).collect(Collectors.toList()); |
| 122 | + for (int i = 0; i < topN.getOrderKeys().size(); i++) { |
| 123 | + // prefix check |
| 124 | + if (!topnKeys.get(i).equals(agg.getGroupByExpressions().get(i))) { |
| 125 | + return Lists.newArrayList(); |
| 126 | + } |
| 127 | + orderKeys.add(topN.getOrderKeys().get(i)); |
| 128 | + } |
| 129 | + for (int i = topN.getOrderKeys().size(); i < agg.getGroupByExpressions().size(); i++) { |
| 130 | + orderKeys.add(new OrderKey(agg.getGroupByExpressions().get(i), true, false)); |
| 131 | + } |
| 132 | + return orderKeys; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public Plan visitPhysicalLimit(PhysicalLimit<? extends Plan> limit, CascadesContext ctx) { |
| 137 | + limit.child().accept(this, ctx); |
| 138 | + if (ConnectContext.get().getSessionVariable().topnOptLimitThreshold <= limit.getLimit() + limit.getOffset()) { |
| 139 | + return limit; |
| 140 | + } |
| 141 | + Plan limitChild = limit.child(); |
| 142 | + if (limitChild instanceof PhysicalProject) { |
| 143 | + limitChild = limitChild.child(0); |
| 144 | + } |
| 145 | + if (limitChild instanceof PhysicalHashAggregate) { |
| 146 | + PhysicalHashAggregate<? extends Plan> upperAgg = (PhysicalHashAggregate<? extends Plan>) limitChild; |
| 147 | + if (upperAgg.getAggPhase().isGlobal() && upperAgg.getAggMode() == AggMode.BUFFER_TO_RESULT) { |
| 148 | + Plan child = upperAgg.child(); |
| 149 | + Plan grandChild = child.child(0); |
| 150 | + if (child instanceof PhysicalDistribute |
| 151 | + && ((PhysicalDistribute<?>) child).getDistributionSpec() instanceof DistributionSpecGather |
| 152 | + && grandChild instanceof PhysicalHashAggregate) { |
| 153 | + upperAgg.setTopnPushInfo(new TopnPushInfo( |
| 154 | + generateOrderKeyByGroupKey(upperAgg), |
| 155 | + limit.getLimit() + limit.getOffset())); |
| 156 | + PhysicalHashAggregate<? extends Plan> bottomAgg = |
| 157 | + (PhysicalHashAggregate<? extends Plan>) grandChild; |
| 158 | + bottomAgg.setTopnPushInfo(new TopnPushInfo( |
| 159 | + generateOrderKeyByGroupKey(bottomAgg), |
| 160 | + limit.getLimit() + limit.getOffset())); |
| 161 | + } |
| 162 | + } else if (upperAgg.getAggMode() == AggMode.INPUT_TO_RESULT) { |
| 163 | + // 1-phase agg |
| 164 | + upperAgg.setTopnPushInfo(new TopnPushInfo( |
| 165 | + generateOrderKeyByGroupKey(upperAgg), |
| 166 | + limit.getLimit() + limit.getOffset())); |
| 167 | + } |
| 168 | + } |
| 169 | + return limit; |
| 170 | + } |
| 171 | + |
| 172 | + private List<OrderKey> generateOrderKeyByGroupKey(PhysicalHashAggregate<? extends Plan> agg) { |
| 173 | + return agg.getGroupByExpressions().stream() |
| 174 | + .map(key -> new OrderKey(key, true, false)) |
| 175 | + .collect(Collectors.toList()); |
| 176 | + } |
| 177 | +} |
0 commit comments