-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
179 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 81 additions & 17 deletions
98
fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/PushLimitToLocalAgg.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,100 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
// This file is copied from | ||
// https://github.com/apache/impala/blob/branch-2.9.0/fe/src/main/java/org/apache/impala/AggregationNode.java | ||
// and modified by Doris | ||
|
||
package org.apache.doris.nereids.processor.post; | ||
|
||
import org.apache.doris.nereids.CascadesContext; | ||
import org.apache.doris.nereids.properties.OrderKey; | ||
import org.apache.doris.nereids.trees.plans.Plan; | ||
import org.apache.doris.nereids.trees.plans.physical.PhysicalDistribute; | ||
import org.apache.doris.nereids.trees.plans.physical.PhysicalHashAggregate; | ||
import org.apache.doris.nereids.trees.plans.physical.PhysicalHashAggregate.TopNOptInfo; | ||
import org.apache.doris.nereids.trees.plans.physical.PhysicalLimit; | ||
import org.apache.doris.nereids.trees.plans.physical.PhysicalTopN; | ||
|
||
import com.google.common.collect.Lists; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
Pattern1: | ||
limit(n) -> aggGlobal -> distribute -> aggLocal | ||
=> | ||
limit(n) -> aggGlobal(topn=n) -> distribute -> aggLocal(topn=n) | ||
public class PushLimitToLocalAgg extends PlanPostProcessor{ | ||
Pattern2: topn orderkeys are the same as group keys | ||
topn -> aggGlobal -> distribute -> aggLocal | ||
=> | ||
topn(n) -> aggGlobal(topn=n) -> distribute -> aggLocal(topn=n) | ||
*/ | ||
public class PushLimitToLocalAgg extends PlanPostProcessor { | ||
@Override | ||
public Plan visitPhysicalTopN(PhysicalTopN<? extends Plan> topN, CascadesContext ctx) { | ||
Plan newChild = this.visit(topN.child(), ctx); | ||
if (newChild != topN.child()) { | ||
topN = (PhysicalTopN<? extends Plan>) topN.withChildren(Lists.newArrayList(newChild)) | ||
.copyStatsAndGroupIdFrom(topN); | ||
} | ||
if (topN.child() instanceof PhysicalHashAggregate) { | ||
PhysicalHashAggregate agg1 = (PhysicalHashAggregate) topN.child(); | ||
if (agg1.getAggPhase().isGlobal()) { | ||
if (agg1.child(0) instanceof PhysicalDistribute | ||
&& agg1.child(0).child(0) instanceof PhysicalHashAggregate) { | ||
PhysicalDistribute dist = (PhysicalDistribute) agg1.child(0); | ||
PhysicalHashAggregate agg2 = (PhysicalHashAggregate) agg1.child(0).child(0); | ||
PhysicalTopN topnForLocalAgg = (PhysicalTopN) topN.withChildren(agg2); | ||
dist = (PhysicalDistribute) dist.withChildren(topnForLocalAgg); | ||
agg1 = (PhysicalHashAggregate) agg1.withChildren(dist); | ||
topN = (PhysicalTopN<? extends Plan>) topN.withChildren(agg1); | ||
PhysicalHashAggregate<? extends Plan> upperAgg = (PhysicalHashAggregate<? extends Plan>) topN.child(); | ||
upperAgg.setTopn(new TopNOptInfo( | ||
topN.getOrderKeys(), | ||
topN.getLimit() + topN.getOffset())); | ||
if (upperAgg.getAggPhase().isGlobal()) { | ||
if (upperAgg.child() instanceof PhysicalDistribute | ||
&& upperAgg.child().child(0) instanceof PhysicalHashAggregate) { | ||
PhysicalHashAggregate<? extends Plan> bottomAgg = | ||
(PhysicalHashAggregate<? extends Plan>) upperAgg.child().child(0); | ||
bottomAgg.setTopn(new TopNOptInfo( | ||
topN.getOrderKeys(), | ||
topN.getLimit() + topN.getOffset())); | ||
bottomAgg.child().accept(this, ctx); | ||
} | ||
} | ||
} else { | ||
topN.child().accept(this, ctx); | ||
} | ||
return topN; | ||
} | ||
|
||
@Override | ||
public Plan visitPhysicalLimit(PhysicalLimit<? extends Plan> limit, CascadesContext ctx) { | ||
if (limit.child() instanceof PhysicalHashAggregate) { | ||
PhysicalHashAggregate<? extends Plan> upperAgg = (PhysicalHashAggregate<? extends Plan>) limit.child(); | ||
upperAgg.setTopn(new TopNOptInfo( | ||
generateOrderKeysByGroupKeys(upperAgg), | ||
limit.getLimit() + limit.getOffset())); | ||
if (upperAgg.getAggPhase().isGlobal()) { | ||
if (upperAgg.child() instanceof PhysicalDistribute | ||
&& upperAgg.child().child(0) instanceof PhysicalHashAggregate) { | ||
PhysicalHashAggregate<? extends Plan> bottomAgg = | ||
(PhysicalHashAggregate<? extends Plan>) upperAgg.child().child(0); | ||
bottomAgg.setTopn(new TopNOptInfo( | ||
generateOrderKeysByGroupKeys(bottomAgg), | ||
limit.getLimit() + limit.getOffset())); | ||
bottomAgg.child().accept(this, ctx); | ||
} | ||
} | ||
} else { | ||
limit.child().accept(this, ctx); | ||
} | ||
return limit; | ||
} | ||
|
||
private List<OrderKey> generateOrderKeysByGroupKeys(PhysicalHashAggregate<? extends Plan> agg) { | ||
return agg.getGroupByExpressions().stream() | ||
.map(key -> new OrderKey(key, true, false)) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
regression-test/suites/nereids_tpch_p0/tpch/push_limit_to_local_agg.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
suite("push_limit_to_local_agg") { | ||
String db = context.config.getDbNameByFile(new File(context.file.parent)) | ||
sql "use ${db}" | ||
explain{ | ||
sql "select o_custkey, sum(o_shippriority) from orders group by o_custkey order by o_custkey limit 4;" | ||
multiContains ("sortByGroupKey:true", 2) | ||
} | ||
} |