|
| 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.processor.post; |
| 19 | + |
| 20 | +import org.apache.doris.nereids.CascadesContext; |
| 21 | +import org.apache.doris.nereids.annotation.DependsRules; |
| 22 | +import org.apache.doris.nereids.trees.expressions.NamedExpression; |
| 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.physical.AbstractPhysicalJoin; |
| 26 | +import org.apache.doris.nereids.trees.plans.physical.AbstractPhysicalPlan; |
| 27 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalDistribute; |
| 28 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalProject; |
| 29 | + |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Set; |
| 33 | +import java.util.stream.Collectors; |
| 34 | + |
| 35 | +/** |
| 36 | + * Prune column for Join-Cluster |
| 37 | + */ |
| 38 | +@DependsRules({ |
| 39 | + MergeProjectPostProcessor.class |
| 40 | +}) |
| 41 | +public class ColumnPruningPostProcessor extends PlanPostProcessor { |
| 42 | + @Override |
| 43 | + public PhysicalProject visitPhysicalProject(PhysicalProject<? extends Plan> project, CascadesContext ctx) { |
| 44 | + Plan child = project.child(); |
| 45 | + Plan newChild = child.accept(this, ctx); |
| 46 | + if (newChild instanceof AbstractPhysicalJoin) { |
| 47 | + AbstractPhysicalJoin<? extends Plan, ? extends Plan> join = (AbstractPhysicalJoin) newChild; |
| 48 | + Plan left = join.left(); |
| 49 | + Plan right = join.right(); |
| 50 | + Set<Slot> leftOutput = left.getOutputSet(); |
| 51 | + Set<Slot> rightOutput = right.getOutputSet(); |
| 52 | + |
| 53 | + Set<Slot> usedSlots = project.getProjects().stream().flatMap(ne -> ne.getInputSlots().stream()) |
| 54 | + .collect(Collectors.toSet()); |
| 55 | + usedSlots.addAll(join.getConditionSlot()); |
| 56 | + |
| 57 | + List<NamedExpression> leftNewProjections = new ArrayList<>(); |
| 58 | + List<NamedExpression> rightNewProjections = new ArrayList<>(); |
| 59 | + |
| 60 | + for (Slot usedSlot : usedSlots) { |
| 61 | + if (leftOutput.contains(usedSlot)) { |
| 62 | + leftNewProjections.add(usedSlot); |
| 63 | + } else if (rightOutput.contains(usedSlot)) { |
| 64 | + rightNewProjections.add(usedSlot); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + Plan newLeft; |
| 69 | + if (left instanceof PhysicalDistribute) { |
| 70 | + newLeft = leftNewProjections.size() != leftOutput.size() && !leftNewProjections.isEmpty() |
| 71 | + ? left.withChildren(new PhysicalProject<>(leftNewProjections, |
| 72 | + left.getLogicalProperties(), left.child(0))) |
| 73 | + : left; |
| 74 | + } else { |
| 75 | + newLeft = leftNewProjections.size() != leftOutput.size() && !leftNewProjections.isEmpty() |
| 76 | + ? new PhysicalProject<>(leftNewProjections, left.getLogicalProperties(), |
| 77 | + left).copyStatsAndGroupIdFrom((AbstractPhysicalPlan) left) |
| 78 | + : left; |
| 79 | + } |
| 80 | + Plan newRight; |
| 81 | + if (right instanceof PhysicalDistribute) { |
| 82 | + newRight = rightNewProjections.size() != rightOutput.size() && !rightNewProjections.isEmpty() |
| 83 | + ? right.withChildren(new PhysicalProject<>(rightNewProjections, |
| 84 | + right.getLogicalProperties(), right.child(0))) |
| 85 | + : right; |
| 86 | + } else { |
| 87 | + newRight = rightNewProjections.size() != rightOutput.size() && !rightNewProjections.isEmpty() |
| 88 | + ? new PhysicalProject<>(rightNewProjections, right.getLogicalProperties(), |
| 89 | + right).copyStatsAndGroupIdFrom((AbstractPhysicalPlan) right) |
| 90 | + : right; |
| 91 | + } |
| 92 | + |
| 93 | + if (newLeft != left || newRight != right) { |
| 94 | + return (PhysicalProject) project.withChildren(join.withChildren(newLeft, newRight)); |
| 95 | + } else { |
| 96 | + return project; |
| 97 | + } |
| 98 | + } |
| 99 | + return project; |
| 100 | + } |
| 101 | +} |
| 102 | + |
0 commit comments