Skip to content

Commit

Permalink
optimize limit + distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
englefly committed Feb 5, 2025
1 parent 8c45784 commit 50088a5
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
import org.apache.doris.nereids.rules.rewrite.PushDownTopNThroughUnion;
import org.apache.doris.nereids.rules.rewrite.PushDownTopNThroughWindow;
import org.apache.doris.nereids.rules.rewrite.PushFilterInsideJoin;
import org.apache.doris.nereids.rules.rewrite.PushLimitThroughDistinctAgg;
import org.apache.doris.nereids.rules.rewrite.PushProjectIntoOneRowRelation;
import org.apache.doris.nereids.rules.rewrite.PushProjectIntoUnion;
import org.apache.doris.nereids.rules.rewrite.PushProjectThroughUnion;
Expand Down Expand Up @@ -401,7 +402,8 @@ public class Rewriter extends AbstractBatchJobExecutor {
new PushDownTopNDistinctThroughUnion(),
new PushDownTopNThroughJoin(),
new PushDownTopNThroughWindow(),
new PushDownTopNThroughUnion()
new PushDownTopNThroughUnion(),
new PushLimitThroughDistinctAgg()
),
topDown(new CreatePartitionTopNFromWindow()),
topDown(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ public enum RuleType {
PUSH_DOWN_LIMIT_DISTINCT_THROUGH_JOIN(RuleTypeClass.REWRITE),
PUSH_DOWN_LIMIT_DISTINCT_THROUGH_PROJECT_JOIN(RuleTypeClass.REWRITE),
PUSH_DOWN_LIMIT_DISTINCT_THROUGH_UNION(RuleTypeClass.REWRITE),
PUSH_DOWN_LIMIT_THROUGH_DISTINCT_AGG(RuleTypeClass.REWRITE),
// adjust nullable
ADJUST_NULLABLE(RuleTypeClass.REWRITE),
ADJUST_CONJUNCTS_RETURN_TYPE(RuleTypeClass.REWRITE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public List<Rule> buildRules() {
>= limit.getLimit() + limit.getOffset())
.when(limit -> {
LogicalAggregate<? extends Plan> agg = limit.child();
return !agg.getGroupByExpressions().isEmpty() && !agg.getSourceRepeat().isPresent();
return !agg.getGroupByExpressions().isEmpty() && !agg.getSourceRepeat().isPresent()
&& !agg.isDistinct();
})
.then(limit -> {
LogicalAggregate<? extends Plan> agg = limit.child();
Expand All @@ -76,7 +77,8 @@ public List<Rule> buildRules() {
>= limit.getLimit() + limit.getOffset())
.when(limit -> {
LogicalAggregate<? extends Plan> agg = limit.child().child();
return !agg.getGroupByExpressions().isEmpty() && !agg.getSourceRepeat().isPresent();
return !agg.getGroupByExpressions().isEmpty() && !agg.getSourceRepeat().isPresent()
&& !agg.isDistinct();
})
.then(limit -> {
LogicalProject<? extends Plan> project = limit.child();
Expand All @@ -95,7 +97,8 @@ public List<Rule> buildRules() {
>= topn.getLimit() + topn.getOffset())
.when(topn -> {
LogicalAggregate<? extends Plan> agg = topn.child();
return !agg.getGroupByExpressions().isEmpty() && !agg.getSourceRepeat().isPresent();
return !agg.getGroupByExpressions().isEmpty() && !agg.getSourceRepeat().isPresent()
&& !agg.isDistinct();
})
.then(topn -> {
LogicalAggregate<? extends Plan> agg = topn.child();
Expand All @@ -116,7 +119,8 @@ public List<Rule> buildRules() {
>= topn.getLimit() + topn.getOffset())
.when(topn -> {
LogicalAggregate<? extends Plan> agg = topn.child().child();
return !agg.getGroupByExpressions().isEmpty() && !agg.getSourceRepeat().isPresent();
return !agg.getGroupByExpressions().isEmpty() && !agg.getSourceRepeat().isPresent()
&& !agg.isDistinct();
})
.then(topn -> {
LogicalTopN originTopn = topn;
Expand Down
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 regression-test/data/nereids_tpch_p0/tpch/push_topn_to_agg.out
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]

Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,10 @@ suite("push_topn_to_agg") {
sql "select sum(ps_availqty), ps_partkey, ps_suppkey from partsupp group by ps_partkey, ps_suppkey order by ps_partkey, ps_suppkey limit 18;"
contains("sortByGroupKey:true")
}

sql "select o_custkey, o_shippriority from orders group by o_custkey, o_shippriority limit 1"
qt_limit_agg_distinct "explain shape plan select o_custkey, o_shippriority from orders group by o_custkey, o_shippriority limit 1"

sql "select o_custkey from orders group by o_custkey, o_shippriority limit 1"
qt_limit_project_agg_distinct "explain shape plan select o_custkey from orders group by o_custkey, o_shippriority limit 1"
}

0 comments on commit 50088a5

Please sign in to comment.