forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
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
109 additions
and
15 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
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
68 changes: 68 additions & 0 deletions
68
...ore/src/main/java/org/apache/doris/nereids/rules/rewrite/PushLimitThroughDistinctAgg.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,68 @@ | ||
// 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. | ||
|
||
package org.apache.doris.nereids.rules.rewrite; | ||
|
||
import org.apache.doris.nereids.rules.Rule; | ||
import org.apache.doris.nereids.rules.RuleType; | ||
import org.apache.doris.nereids.trees.plans.Plan; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalLimit; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalProject; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* limit ->agg(without agg functions) -> any(not limit) | ||
* => | ||
* limit -> agg -> limit -> any | ||
*/ | ||
public class PushLimitThroughDistinctAgg implements RewriteRuleFactory { | ||
@Override | ||
public List<Rule> buildRules() { | ||
return ImmutableList.of( | ||
logicalLimit(logicalAggregate().when(LogicalAggregate::isDistinct)) | ||
.then(limit -> { | ||
LogicalAggregate<? extends Plan> aggregate = limit.child(); | ||
Plan aggChild = aggregate.child(); | ||
if (aggChild instanceof LogicalLimit) { | ||
return null; | ||
} | ||
LogicalLimit childLimit = new LogicalLimit(limit.getLimit() + limit.getOffset(), | ||
0, limit.getPhase(), aggChild); | ||
aggregate = aggregate.withChildren(ImmutableList.of(childLimit)); | ||
return limit.withChildren(ImmutableList.of(aggregate)); | ||
}).toRule(RuleType.PUSH_DOWN_LIMIT_THROUGH_DISTINCT_AGG), | ||
logicalLimit(logicalProject(logicalAggregate().when(LogicalAggregate::isDistinct))) | ||
.then(limit -> { | ||
LogicalProject<? extends Plan> project = limit.child(); | ||
LogicalAggregate<? extends Plan> aggregate = (LogicalAggregate) project.child(); | ||
Plan aggChild = aggregate.child(); | ||
if (aggChild instanceof LogicalLimit) { | ||
return null; | ||
} | ||
LogicalLimit childLimit = new LogicalLimit(limit.getLimit() + limit.getOffset(), | ||
0, limit.getPhase(), aggChild); | ||
aggregate = aggregate.withChildren(ImmutableList.of(childLimit)); | ||
project = project.withChildren(ImmutableList.of(aggregate)); | ||
return limit.withChildren(ImmutableList.of(project)); | ||
}).toRule(RuleType.PUSH_DOWN_LIMIT_THROUGH_DISTINCT_AGG) | ||
); | ||
} | ||
} |
33 changes: 23 additions & 10 deletions
33
regression-test/data/nereids_tpch_p0/tpch/push_topn_to_agg.out
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,13 +1,26 @@ | ||
-- This file is automatically generated. You should know what you did if you want to edit this | ||
-- !stable_1 -- | ||
0 | ||
1 | ||
-- !limit_agg_distinct -- | ||
PhysicalResultSink | ||
--PhysicalLimit[GLOBAL] | ||
----PhysicalDistribute[DistributionSpecGather] | ||
------PhysicalLimit[LOCAL] | ||
--------hashAgg[GLOBAL] | ||
----------PhysicalDistribute[DistributionSpecHash] | ||
------------hashAgg[LOCAL] | ||
--------------PhysicalLimit[LOCAL] | ||
----------------PhysicalProject | ||
------------------PhysicalOlapScan[orders] | ||
|
||
-- !stable_2 -- | ||
0 | ||
1 | ||
|
||
-- !stable_3 -- | ||
0 | ||
1 | ||
-- !limit_project_agg_distinct -- | ||
PhysicalResultSink | ||
--PhysicalLimit[GLOBAL] | ||
----PhysicalDistribute[DistributionSpecGather] | ||
------PhysicalLimit[LOCAL] | ||
--------PhysicalProject | ||
----------hashAgg[GLOBAL] | ||
------------PhysicalDistribute[DistributionSpecHash] | ||
--------------hashAgg[LOCAL] | ||
----------------PhysicalLimit[LOCAL] | ||
------------------PhysicalProject | ||
--------------------PhysicalOlapScan[orders] | ||
|
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