|
| 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 | + |
| 18 | +package org.apache.doris.nereids.rules.rewrite; |
| 19 | + |
| 20 | +import org.apache.doris.nereids.rules.Rule; |
| 21 | +import org.apache.doris.nereids.rules.RuleType; |
| 22 | +import org.apache.doris.nereids.trees.expressions.Slot; |
| 23 | +import org.apache.doris.nereids.trees.plans.Plan; |
| 24 | +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; |
| 25 | +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; |
| 26 | +import org.apache.doris.nereids.trees.plans.logical.LogicalLimit; |
| 27 | +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; |
| 28 | + |
| 29 | +import com.google.common.collect.ImmutableList; |
| 30 | + |
| 31 | +import java.util.List; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +/** |
| 35 | + * Same with PushdownLimit |
| 36 | + */ |
| 37 | +public class PushdownLimitDistinctThroughJoin implements RewriteRuleFactory { |
| 38 | + |
| 39 | + @Override |
| 40 | + public List<Rule> buildRules() { |
| 41 | + return ImmutableList.of( |
| 42 | + // limit -> distinct -> join |
| 43 | + logicalLimit(logicalAggregate(logicalJoin()) |
| 44 | + .when(LogicalAggregate::isDistinct)) |
| 45 | + .then(limit -> { |
| 46 | + LogicalAggregate<LogicalJoin<Plan, Plan>> agg = limit.child(); |
| 47 | + LogicalJoin<Plan, Plan> join = agg.child(); |
| 48 | + |
| 49 | + Plan newJoin = pushLimitThroughJoin(limit, join); |
| 50 | + if (newJoin == null || join.children().equals(newJoin.children())) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + return limit.withChildren(agg.withChildren(newJoin)); |
| 54 | + }) |
| 55 | + .toRule(RuleType.PUSH_LIMIT_DISTINCT_THROUGH_JOIN), |
| 56 | + |
| 57 | + // limit -> distinct -> project -> join |
| 58 | + logicalLimit(logicalAggregate(logicalProject(logicalJoin()).when(LogicalProject::isAllSlots)) |
| 59 | + .when(LogicalAggregate::isDistinct)) |
| 60 | + .then(limit -> { |
| 61 | + LogicalAggregate<LogicalProject<LogicalJoin<Plan, Plan>>> agg = limit.child(); |
| 62 | + LogicalProject<LogicalJoin<Plan, Plan>> project = agg.child(); |
| 63 | + LogicalJoin<Plan, Plan> join = project.child(); |
| 64 | + |
| 65 | + Plan newJoin = pushLimitThroughJoin(limit, join); |
| 66 | + if (newJoin == null || join.children().equals(newJoin.children())) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + return limit.withChildren(agg.withChildren(project.withChildren(newJoin))); |
| 70 | + }).toRule(RuleType.PUSH_LIMIT_DISTINCT_THROUGH_JOIN) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + private Plan pushLimitThroughJoin(LogicalLimit<?> limit, LogicalJoin<Plan, Plan> join) { |
| 75 | + LogicalAggregate<?> agg = (LogicalAggregate<?>) limit.child(); |
| 76 | + List<Slot> groupBySlots = agg.getGroupByExpressions().stream() |
| 77 | + .flatMap(e -> e.getInputSlots().stream()).collect(Collectors.toList()); |
| 78 | + switch (join.getJoinType()) { |
| 79 | + case LEFT_OUTER_JOIN: |
| 80 | + if (join.left().getOutputSet().containsAll(groupBySlots) |
| 81 | + && join.left().getOutputSet().equals(agg.getOutputSet())) { |
| 82 | + return join.withChildren(limit.withLimitChild(limit.getLimit() + limit.getOffset(), 0, |
| 83 | + agg.withChildren(join.left())), join.right()); |
| 84 | + } |
| 85 | + return null; |
| 86 | + case RIGHT_OUTER_JOIN: |
| 87 | + if (join.right().getOutputSet().containsAll(groupBySlots) |
| 88 | + && join.right().getOutputSet().equals(agg.getOutputSet())) { |
| 89 | + return join.withChildren(join.left(), limit.withLimitChild(limit.getLimit() + limit.getOffset(), 0, |
| 90 | + agg.withChildren(join.right()))); |
| 91 | + } |
| 92 | + return null; |
| 93 | + case CROSS_JOIN: |
| 94 | + if (join.left().getOutputSet().containsAll(groupBySlots) |
| 95 | + && join.left().getOutputSet().equals(agg.getOutputSet())) { |
| 96 | + return join.withChildren(limit.withLimitChild(limit.getLimit() + limit.getOffset(), 0, |
| 97 | + agg.withChildren(join.left())), join.right()); |
| 98 | + } else if (join.right().getOutputSet().containsAll(groupBySlots) |
| 99 | + && join.right().getOutputSet().equals(agg.getOutputSet())) { |
| 100 | + return join.withChildren(join.left(), limit.withLimitChild(limit.getLimit() + limit.getOffset(), 0, |
| 101 | + agg.withChildren(join.right()))); |
| 102 | + } else { |
| 103 | + return null; |
| 104 | + } |
| 105 | + default: |
| 106 | + return null; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments