|
| 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.analysis.Expr; |
| 21 | +import org.apache.doris.analysis.SlotRef; |
| 22 | +import org.apache.doris.catalog.Column; |
| 23 | +import org.apache.doris.catalog.OlapTable; |
| 24 | +import org.apache.doris.nereids.CascadesContext; |
| 25 | +import org.apache.doris.nereids.trees.expressions.Expression; |
| 26 | +import org.apache.doris.nereids.trees.expressions.Slot; |
| 27 | +import org.apache.doris.nereids.trees.expressions.SlotReference; |
| 28 | +import org.apache.doris.nereids.trees.plans.PartitionPrunablePredicate; |
| 29 | +import org.apache.doris.nereids.trees.plans.Plan; |
| 30 | +import org.apache.doris.nereids.trees.plans.physical.AbstractPhysicalPlan; |
| 31 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalFilter; |
| 32 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalOlapScan; |
| 33 | +import org.apache.doris.nereids.util.ExpressionUtils; |
| 34 | + |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.HashSet; |
| 37 | +import java.util.LinkedHashSet; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Optional; |
| 41 | +import java.util.Set; |
| 42 | + |
| 43 | +/** |
| 44 | + * Removes partition-prunable conjuncts that were registered by {@link |
| 45 | + * org.apache.doris.nereids.rules.rewrite.PruneOlapScanPartition} but kept in |
| 46 | + * the logical plan during cascades. Doing the removal here, after |
| 47 | + * materialized-view rewrite has finished, ensures MV matching observes the |
| 48 | + * original predicates; otherwise the MV view-predicate may incorrectly cover |
| 49 | + * the dropped partition predicate and produce extra rows. |
| 50 | + */ |
| 51 | +public class PrunePartitionPredicate extends PlanPostProcessor { |
| 52 | + |
| 53 | + @Override |
| 54 | + public Plan visitPhysicalFilter(PhysicalFilter<? extends Plan> filter, CascadesContext context) { |
| 55 | + filter = (PhysicalFilter<? extends Plan>) super.visit(filter, context); |
| 56 | + Plan child = filter.child(); |
| 57 | + if (!(child instanceof PhysicalOlapScan)) { |
| 58 | + return filter; |
| 59 | + } |
| 60 | + PhysicalOlapScan scan = (PhysicalOlapScan) child; |
| 61 | + Optional<PartitionPrunablePredicate> entryOpt = scan.getPartitionPrunablePredicates(); |
| 62 | + if (!entryOpt.isPresent()) { |
| 63 | + return filter; |
| 64 | + } |
| 65 | + boolean skipPrunePredicate = context.getConnectContext().getSessionVariable().skipPrunePredicate |
| 66 | + || context.getStatementContext().isSkipPrunePredicate(); |
| 67 | + if (skipPrunePredicate) { |
| 68 | + return filter; |
| 69 | + } |
| 70 | + Set<Long> scanPartitions = new HashSet<>(scan.getSelectedPartitionIds()); |
| 71 | + Map<String, Slot> nameToOutputSlot = buildNameToSlotMap(scan); |
| 72 | + |
| 73 | + Set<Expression> remaining = new LinkedHashSet<>(filter.getConjuncts()); |
| 74 | + boolean changed = false; |
| 75 | + PartitionPrunablePredicate entry = entryOpt.get(); |
| 76 | + if (entry.getSelectedPartitionIds().containsAll(scanPartitions)) { |
| 77 | + Map<Expression, Expression> slotReplaceMap = |
| 78 | + buildSlotReplaceMap(entry.getSnapshotPartitionSlots(), nameToOutputSlot); |
| 79 | + if (slotReplaceMap != null) { |
| 80 | + for (Expression conjunct : entry.getPrunableConjuncts()) { |
| 81 | + Expression rewritten = slotReplaceMap.isEmpty() |
| 82 | + ? conjunct : ExpressionUtils.replace(conjunct, slotReplaceMap); |
| 83 | + if (remaining.remove(rewritten)) { |
| 84 | + changed = true; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + if (!changed) { |
| 90 | + return filter; |
| 91 | + } |
| 92 | + if (remaining.isEmpty()) { |
| 93 | + return scan; |
| 94 | + } |
| 95 | + return filter.withConjunctsAndChild(remaining, scan) |
| 96 | + .copyStatsAndGroupIdFrom((AbstractPhysicalPlan) filter); |
| 97 | + } |
| 98 | + |
| 99 | + private static Map<String, Slot> buildNameToSlotMap(PhysicalOlapScan scan) { |
| 100 | + OlapTable table = scan.getTable(); |
| 101 | + List<Slot> slots = scan.getOutput(); |
| 102 | + Map<String, Slot> map = new HashMap<>(slots.size()); |
| 103 | + if (scan.getSelectedIndexId() == table.getBaseIndexId()) { |
| 104 | + for (Slot slot : slots) { |
| 105 | + map.put(slot.getName().toLowerCase(), slot); |
| 106 | + } |
| 107 | + } else { |
| 108 | + for (Slot slot : slots) { |
| 109 | + if (!(slot instanceof SlotReference)) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + SlotReference slotReference = (SlotReference) slot; |
| 113 | + Optional<Column> columnOptional = slotReference.getOriginalColumn(); |
| 114 | + if (!columnOptional.isPresent()) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + Expr expr = columnOptional.get().getDefineExpr(); |
| 118 | + if (!(expr instanceof SlotRef)) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + map.put(((SlotRef) expr).getColumnName().toLowerCase(), slot); |
| 122 | + } |
| 123 | + } |
| 124 | + return map; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Map each recorded snapshot slot to the scan's current output slot of the |
| 129 | + * same column name. Returns null when any snapshot slot cannot be located, |
| 130 | + * so the caller can skip the entry. |
| 131 | + */ |
| 132 | + private static Map<Expression, Expression> buildSlotReplaceMap( |
| 133 | + List<Slot> snapshotSlots, Map<String, Slot> nameToOutputSlot) { |
| 134 | + Map<Expression, Expression> replaceMap = new HashMap<>(snapshotSlots.size()); |
| 135 | + for (Slot snapshot : snapshotSlots) { |
| 136 | + Slot current = nameToOutputSlot.get(snapshot.getName().toLowerCase()); |
| 137 | + if (current == null) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + if (!snapshot.equals(current)) { |
| 141 | + replaceMap.put(snapshot, current); |
| 142 | + } |
| 143 | + } |
| 144 | + return replaceMap; |
| 145 | + } |
| 146 | +} |
0 commit comments