|
| 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.properties.OrderKey; |
| 21 | +import org.apache.doris.nereids.rules.Rule; |
| 22 | +import org.apache.doris.nereids.rules.RuleType; |
| 23 | +import org.apache.doris.nereids.trees.expressions.Slot; |
| 24 | +import org.apache.doris.nereids.trees.plans.Plan; |
| 25 | +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; |
| 26 | +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; |
| 27 | +import org.apache.doris.nereids.trees.plans.logical.LogicalTopN; |
| 28 | +import org.apache.doris.nereids.util.Utils; |
| 29 | + |
| 30 | +import com.google.common.collect.ImmutableList; |
| 31 | + |
| 32 | +import java.util.List; |
| 33 | +import java.util.Set; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +/** |
| 37 | + * Push down TopN through Outer Join into left child ..... |
| 38 | + */ |
| 39 | +public class PushdownTopNThroughJoin implements RewriteRuleFactory { |
| 40 | + |
| 41 | + @Override |
| 42 | + public List<Rule> buildRules() { |
| 43 | + return ImmutableList.of( |
| 44 | + // topN -> join |
| 45 | + logicalTopN(logicalJoin()) |
| 46 | + // TODO: complex orderby |
| 47 | + .when(topN -> topN.getOrderKeys().stream().map(OrderKey::getExpr) |
| 48 | + .allMatch(Slot.class::isInstance)) |
| 49 | + .then(topN -> { |
| 50 | + LogicalJoin<Plan, Plan> join = topN.child(); |
| 51 | + Plan newJoin = pushLimitThroughJoin(topN, join); |
| 52 | + if (newJoin == null || topN.child().children().equals(newJoin.children())) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + return topN.withChildren(newJoin); |
| 56 | + }) |
| 57 | + .toRule(RuleType.PUSH_TOP_N_THROUGH_JOIN), |
| 58 | + |
| 59 | + // topN -> project -> join |
| 60 | + logicalTopN(logicalProject(logicalJoin()).when(LogicalProject::isAllSlots)) |
| 61 | + // TODO: complex project |
| 62 | + .when(topN -> topN.getOrderKeys().stream().map(OrderKey::getExpr) |
| 63 | + .allMatch(Slot.class::isInstance)) |
| 64 | + .then(topN -> { |
| 65 | + LogicalProject<LogicalJoin<Plan, Plan>> project = topN.child(); |
| 66 | + LogicalJoin<Plan, Plan> join = project.child(); |
| 67 | + |
| 68 | + Plan newJoin = pushLimitThroughJoin(topN, join); |
| 69 | + if (newJoin == null || join.children().equals(newJoin.children())) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + return topN.withChildren(project.withChildren(newJoin)); |
| 73 | + }).toRule(RuleType.PUSH_TOP_N_THROUGH_PROJECT_JOIN) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + private Plan pushLimitThroughJoin(LogicalTopN<? extends Plan> topN, LogicalJoin<Plan, Plan> join) { |
| 78 | + switch (join.getJoinType()) { |
| 79 | + case LEFT_OUTER_JOIN: |
| 80 | + Set<Slot> rightOutputSet = join.right().getOutputSet(); |
| 81 | + if (topN.getOrderKeys().stream().map(OrderKey::getExpr) |
| 82 | + .anyMatch(e -> Utils.isIntersecting(rightOutputSet, e.getInputSlots()))) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + return join.withChildren(topN.withChildren(join.left()), join.right()); |
| 86 | + case RIGHT_OUTER_JOIN: |
| 87 | + Set<Slot> leftOutputSet = join.left().getOutputSet(); |
| 88 | + if (topN.getOrderKeys().stream().map(OrderKey::getExpr) |
| 89 | + .anyMatch(e -> Utils.isIntersecting(leftOutputSet, e.getInputSlots()))) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + return join.withChildren(join.left(), topN.withChildren(join.right())); |
| 93 | + case CROSS_JOIN: |
| 94 | + List<Slot> orderbySlots = topN.getOrderKeys().stream().map(OrderKey::getExpr) |
| 95 | + .flatMap(e -> e.getInputSlots().stream()).collect(Collectors.toList()); |
| 96 | + if (join.left().getOutputSet().containsAll(orderbySlots)) { |
| 97 | + return join.withChildren(topN.withChildren(join.left()), join.right()); |
| 98 | + } else if (join.right().getOutputSet().containsAll(orderbySlots)) { |
| 99 | + return join.withChildren(join.left(), topN.withChildren(join.right())); |
| 100 | + } else { |
| 101 | + return null; |
| 102 | + } |
| 103 | + default: |
| 104 | + // don't push limit. |
| 105 | + return null; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments