-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[fix](partition_prune) Move the pruning of predicates that are always true after partition pruning into the PlanPostProcessor #63111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
feiniaofeiafei
wants to merge
6
commits into
apache:master
Choose a base branch
from
feiniaofeiafei:fix/cir-20138-mv-partition-predicate-compensate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
990efa5
fix
feiniaofeiafei b2a3401
fix
feiniaofeiafei 7836b85
fix
feiniaofeiafei c4285dd
fix
feiniaofeiafei b2a01c9
[refactor](nereids) Carry partition-prunable predicates on the scan i…
feiniaofeiafei 4c371b5
[refactor](nereids) Use Optional<PartitionPrunablePredicate> on scan
feiniaofeiafei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
...e-core/src/main/java/org/apache/doris/nereids/processor/post/PrunePartitionPredicate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| // 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.processor.post; | ||
|
|
||
| import org.apache.doris.analysis.Expr; | ||
| import org.apache.doris.analysis.SlotRef; | ||
| import org.apache.doris.catalog.Column; | ||
| import org.apache.doris.catalog.OlapTable; | ||
| import org.apache.doris.nereids.CascadesContext; | ||
| import org.apache.doris.nereids.rules.expression.rules.PartitionPrunablePredicate; | ||
| import org.apache.doris.nereids.trees.expressions.Expression; | ||
| import org.apache.doris.nereids.trees.expressions.Slot; | ||
| import org.apache.doris.nereids.trees.expressions.SlotReference; | ||
| import org.apache.doris.nereids.trees.plans.Plan; | ||
| import org.apache.doris.nereids.trees.plans.physical.AbstractPhysicalPlan; | ||
| import org.apache.doris.nereids.trees.plans.physical.PhysicalFilter; | ||
| import org.apache.doris.nereids.trees.plans.physical.PhysicalOlapScan; | ||
| import org.apache.doris.nereids.util.ExpressionUtils; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Removes partition-prunable conjuncts that were registered by {@link | ||
| * org.apache.doris.nereids.rules.rewrite.PruneOlapScanPartition} but kept in | ||
| * the logical plan during cascades. Doing the removal here, after | ||
| * materialized-view rewrite has finished, ensures MV matching observes the | ||
| * original predicates; otherwise the MV view-predicate may incorrectly cover | ||
| * the dropped partition predicate and produce extra rows. | ||
| */ | ||
| public class PrunePartitionPredicate extends PlanPostProcessor { | ||
|
|
||
| @Override | ||
| public Plan visitPhysicalFilter(PhysicalFilter<? extends Plan> filter, CascadesContext context) { | ||
| filter = (PhysicalFilter<? extends Plan>) super.visit(filter, context); | ||
| Plan child = filter.child(); | ||
| if (!(child instanceof PhysicalOlapScan)) { | ||
| return filter; | ||
| } | ||
| PhysicalOlapScan scan = (PhysicalOlapScan) child; | ||
| Optional<PartitionPrunablePredicate> entryOpt = scan.getPartitionPrunablePredicates(); | ||
| if (!entryOpt.isPresent()) { | ||
| return filter; | ||
| } | ||
| boolean skipPrunePredicate = context.getConnectContext().getSessionVariable().skipPrunePredicate | ||
| || context.getStatementContext().isDelete(); | ||
| if (skipPrunePredicate) { | ||
| return filter; | ||
| } | ||
| Set<Long> scanPartitions = new HashSet<>(scan.getSelectedPartitionIds()); | ||
| Map<String, Slot> nameToOutputSlot = buildNameToSlotMap(scan); | ||
|
|
||
| Set<Expression> remaining = new LinkedHashSet<>(filter.getConjuncts()); | ||
| boolean changed = false; | ||
| PartitionPrunablePredicate entry = entryOpt.get(); | ||
| if (entry.getSelectedPartitionIds().containsAll(scanPartitions)) { | ||
| Map<Expression, Expression> slotReplaceMap = | ||
| buildSlotReplaceMap(entry.getSnapshotPartitionSlots(), nameToOutputSlot); | ||
| if (slotReplaceMap != null) { | ||
| for (Expression conjunct : entry.getPrunableConjuncts()) { | ||
| Expression rewritten = slotReplaceMap.isEmpty() | ||
| ? conjunct : ExpressionUtils.replace(conjunct, slotReplaceMap); | ||
| if (remaining.remove(rewritten)) { | ||
| changed = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if (!changed) { | ||
| return filter; | ||
| } | ||
| if (remaining.isEmpty()) { | ||
| return scan; | ||
| } | ||
| return filter.withConjunctsAndChild(remaining, scan) | ||
| .copyStatsAndGroupIdFrom((AbstractPhysicalPlan) filter); | ||
| } | ||
|
|
||
| private static Map<String, Slot> buildNameToSlotMap(PhysicalOlapScan scan) { | ||
| OlapTable table = scan.getTable(); | ||
| List<Slot> slots = scan.getOutput(); | ||
| Map<String, Slot> map = new HashMap<>(slots.size()); | ||
| if (scan.getSelectedIndexId() == table.getBaseIndexId()) { | ||
| for (Slot slot : slots) { | ||
| map.put(slot.getName().toLowerCase(), slot); | ||
| } | ||
| } else { | ||
| for (Slot slot : slots) { | ||
| if (!(slot instanceof SlotReference)) { | ||
| continue; | ||
| } | ||
| SlotReference slotReference = (SlotReference) slot; | ||
| Optional<Column> columnOptional = slotReference.getOriginalColumn(); | ||
| if (!columnOptional.isPresent()) { | ||
| continue; | ||
| } | ||
| Expr expr = columnOptional.get().getDefineExpr(); | ||
| if (!(expr instanceof SlotRef)) { | ||
| continue; | ||
| } | ||
| map.put(((SlotRef) expr).getColumnName().toLowerCase(), slot); | ||
| } | ||
| } | ||
| return map; | ||
| } | ||
|
|
||
| /** | ||
| * Map each recorded snapshot slot to the scan's current output slot of the | ||
| * same column name. Returns null when any snapshot slot cannot be located, | ||
| * so the caller can skip the entry. | ||
| */ | ||
| private static Map<Expression, Expression> buildSlotReplaceMap( | ||
| List<Slot> snapshotSlots, Map<String, Slot> nameToOutputSlot) { | ||
| Map<Expression, Expression> replaceMap = new HashMap<>(snapshotSlots.size()); | ||
| for (Slot snapshot : snapshotSlots) { | ||
| Slot current = nameToOutputSlot.get(snapshot.getName().toLowerCase()); | ||
| if (current == null) { | ||
| return null; | ||
| } | ||
| if (!snapshot.equals(current)) { | ||
| replaceMap.put(snapshot, current); | ||
| } | ||
| } | ||
| return replaceMap; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...main/java/org/apache/doris/nereids/rules/expression/rules/PartitionPrunablePredicate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // 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.expression.rules; | ||
|
|
||
| import org.apache.doris.nereids.trees.expressions.Expression; | ||
| import org.apache.doris.nereids.trees.expressions.Slot; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableSet; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Records that, on the scan whose partition list equals {@link | ||
| * #selectedPartitionIds}, the {@link #prunableConjuncts} are guaranteed to | ||
| * evaluate to TRUE for every surviving row. | ||
| * | ||
| * <p>The predicate is registered by {@link | ||
| * org.apache.doris.nereids.rules.rewrite.PruneOlapScanPartition} but kept in | ||
| * the logical filter during cascades. The actual removal happens later in | ||
| * {@link org.apache.doris.nereids.processor.post.PrunePartitionPredicate} so | ||
| * that materialized-view rewrite still sees the original predicates. Keeping | ||
| * the predicate in the plan avoids the wrong-result problem in which the MV | ||
| * view-predicate happens to cover the remaining conjuncts after the partition | ||
| * predicate has been silently dropped. | ||
| * | ||
| * <p>The predicate lives on the scan itself (see {@code LogicalOlapScan} and | ||
| * {@code PhysicalOlapScan}) so we no longer need to match it back to its scan | ||
| * via a table identifier. Because rewrites between recording and removal may | ||
| * rebuild the scan with fresh slot ids, {@link #snapshotPartitionSlots} | ||
| * captures the slots that appear in the recorded conjuncts. The post-processor | ||
| * maps them onto the actual scan's output slots by column name before | ||
| * performing the conjunct removal. | ||
| */ | ||
| public class PartitionPrunablePredicate { | ||
| private final Set<Long> selectedPartitionIds; | ||
| private final List<Slot> snapshotPartitionSlots; | ||
| private final Set<Expression> prunableConjuncts; | ||
|
|
||
| public PartitionPrunablePredicate(Set<Long> selectedPartitionIds, | ||
| List<Slot> snapshotPartitionSlots, | ||
| Set<Expression> prunableConjuncts) { | ||
| this.selectedPartitionIds = ImmutableSet.copyOf(selectedPartitionIds); | ||
| this.snapshotPartitionSlots = ImmutableList.copyOf(snapshotPartitionSlots); | ||
| this.prunableConjuncts = ImmutableSet.copyOf(prunableConjuncts); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| PartitionPrunablePredicate that = (PartitionPrunablePredicate) o; | ||
| return selectedPartitionIds.equals(that.selectedPartitionIds) | ||
| && snapshotPartitionSlots.equals(that.snapshotPartitionSlots) | ||
| && prunableConjuncts.equals(that.prunableConjuncts); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(selectedPartitionIds, snapshotPartitionSlots, prunableConjuncts); | ||
| } | ||
|
|
||
| public Set<Long> getSelectedPartitionIds() { | ||
| return selectedPartitionIds; | ||
| } | ||
|
|
||
| public List<Slot> getSnapshotPartitionSlots() { | ||
| return snapshotPartitionSlots; | ||
| } | ||
|
|
||
| public Set<Expression> getPrunableConjuncts() { | ||
| return prunableConjuncts; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this file should be put into this package.