-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support limit->proj, support topn-agg
- Loading branch information
Showing
8 changed files
with
296 additions
and
9 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
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
135 changes: 135 additions & 0 deletions
135
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// 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.expressions.Expression; | ||
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.TopnPushInfo; | ||
import org.apache.doris.nereids.trees.plans.physical.PhysicalLimit; | ||
import org.apache.doris.nereids.trees.plans.physical.PhysicalProject; | ||
import org.apache.doris.nereids.trees.plans.physical.PhysicalTopN; | ||
|
||
import org.apache.hadoop.util.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) | ||
Pattern2: topn orderkeys are the prefix of 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 topnChild = topN.child(); | ||
if (topnChild instanceof PhysicalProject) { | ||
topnChild = topnChild.child(0); | ||
} | ||
if (topnChild instanceof PhysicalHashAggregate) { | ||
PhysicalHashAggregate<? extends Plan> upperAgg = (PhysicalHashAggregate<? extends Plan>) topnChild; | ||
if (upperAgg.getAggPhase().isGlobal() | ||
&& upperAgg.child() instanceof PhysicalDistribute | ||
&& upperAgg.child().child(0) instanceof PhysicalHashAggregate) { | ||
List<OrderKey> orderKeys = tryGenerateOrderKeyByGroupKeyAndTopnKey(topN, upperAgg); | ||
if (!orderKeys.isEmpty()) { | ||
upperAgg.setTopnPushInfo(new TopnPushInfo( | ||
orderKeys, | ||
topN.getLimit() + topN.getOffset())); | ||
PhysicalHashAggregate<? extends Plan> bottomAgg = | ||
(PhysicalHashAggregate<? extends Plan>) upperAgg.child().child(0); | ||
bottomAgg.setTopnPushInfo(new TopnPushInfo( | ||
orderKeys, | ||
topN.getLimit() + topN.getOffset())); | ||
} | ||
} | ||
} | ||
topN.child().accept(this, ctx); | ||
return topN; | ||
} | ||
|
||
@Override | ||
public Plan visitPhysicalLimit(PhysicalLimit<? extends Plan> limit, CascadesContext ctx) { | ||
Plan limitChild = limit.child(); | ||
if (limitChild instanceof PhysicalProject) { | ||
limitChild = limitChild.child(0); | ||
} | ||
if (limitChild instanceof PhysicalHashAggregate) { | ||
PhysicalHashAggregate<? extends Plan> upperAgg = (PhysicalHashAggregate<? extends Plan>) limitChild; | ||
if (upperAgg.getAggPhase().isGlobal() | ||
&& upperAgg.child() instanceof PhysicalDistribute | ||
&& upperAgg.child().child(0) instanceof PhysicalHashAggregate) { | ||
upperAgg.setTopnPushInfo(new TopnPushInfo( | ||
generateOrderKeyByGroupKey(upperAgg), | ||
limit.getLimit() + limit.getOffset())); | ||
PhysicalHashAggregate<? extends Plan> bottomAgg = | ||
(PhysicalHashAggregate<? extends Plan>) upperAgg.child().child(0); | ||
bottomAgg.setTopnPushInfo(new TopnPushInfo( | ||
generateOrderKeyByGroupKey(bottomAgg), | ||
limit.getLimit() + limit.getOffset())); | ||
} | ||
} | ||
limit.child().accept(this, ctx); | ||
|
||
return limit; | ||
} | ||
|
||
private List<OrderKey> generateOrderKeyByGroupKey(PhysicalHashAggregate<? extends Plan> agg) { | ||
return agg.getGroupByExpressions().stream() | ||
.map(key -> new OrderKey(key, true, false)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
return true, if topn order-key is prefix of agg group-key, ignore asc/desc and null_first | ||
TODO order-key can be subset of group-key. BE does not support now. | ||
*/ | ||
private List<OrderKey> tryGenerateOrderKeyByGroupKeyAndTopnKey(PhysicalTopN<? extends Plan> topN, | ||
PhysicalHashAggregate<? extends Plan> agg) { | ||
List<OrderKey> orderKeys = Lists.newArrayListWithCapacity(agg.getGroupByExpressions().size()); | ||
if (topN.getOrderKeys().size() > agg.getGroupByExpressions().size()) { | ||
return orderKeys; | ||
} | ||
List<Expression> topnKeys = topN.getOrderKeys().stream() | ||
.map(OrderKey::getExpr).collect(Collectors.toList()); | ||
for (int i = 0; i < topN.getOrderKeys().size(); i++) { | ||
// prefix check | ||
if (!topnKeys.get(i).equals(agg.getGroupByExpressions().get(i))) { | ||
return Lists.newArrayList(); | ||
} | ||
orderKeys.add(topN.getOrderKeys().get(i)); | ||
} | ||
for (int i = topN.getOrderKeys().size(); i < agg.getGroupByExpressions().size(); i++) { | ||
orderKeys.add(new OrderKey(agg.getGroupByExpressions().get(i), true, false)); | ||
} | ||
return orderKeys; | ||
} | ||
} |
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
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
Oops, something went wrong.