Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public List<PlanPostProcessor> getProcessors() {
// add processor if we need
Builder<PlanPostProcessor> builder = ImmutableList.builder();
builder.add(new PushDownFilterThroughProject());
builder.add(new PrunePartitionPredicate());
builder.add(new RemoveUselessProjectPostProcessor());
builder.add(new ShuffleKeyPruner());
builder.add(new RecomputeLogicalPropertiesProcessor());
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,18 @@ public Plan getScanPlan(StructInfo queryStructInfo, CascadesContext cascadesCont
return scanPlan.accept(new DefaultPlanRewriter<Void>() {
@Override
public Plan visitLogicalOlapScan(LogicalOlapScan olapScan, Void context) {
if (!queryStructInfoRelations.get(0).getTable().getFullQualifiers().equals(
LogicalOlapScan queryScan = (LogicalOlapScan) queryStructInfoRelations.get(0);
if (!queryScan.getTable().getFullQualifiers().equals(
olapScan.getTable().getFullQualifiers())) {
// Only the same table, we can do partition prue
return olapScan;
}
return olapScan.withSelectedPartitionIds(
((LogicalOlapScan) queryStructInfoRelations.get(0)).getSelectedPartitionIds());
// Carry partition-prunable predicates from the original query scan onto
// the rewritten MV scan so the post-processor can still drop the
// predicates that have already been enforced by partition pruning.
return olapScan
.withSelectedPartitionIds(queryScan.getSelectedPartitionIds())
.withPartitionPrunablePredicates(queryScan.getPartitionPrunablePredicates());
}
}, null);
}
Expand Down
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 {
Copy link
Copy Markdown
Contributor

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.

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@
import org.apache.doris.nereids.trees.expressions.literal.DateTimeLiteral;
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
import org.apache.doris.nereids.trees.plans.logical.LogicalRelation;
import org.apache.doris.nereids.types.DateTimeType;
import org.apache.doris.nereids.util.ExpressionUtils;
import org.apache.doris.nereids.util.Utils;

import com.google.common.collect.ImmutableList;
Expand All @@ -51,7 +47,6 @@
import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -390,22 +385,4 @@ private static <K> Pair<Boolean, Boolean> canBePrunedOut(Expression partitionPre
return Pair.of(true, false);
}
}

/** remove predicates that are always true*/
public static Plan prunePredicate(boolean skipPrunePredicate, Optional<Expression> prunedPredicates,
LogicalFilter<? extends Plan> filter, LogicalRelation scan) {
if (!skipPrunePredicate && prunedPredicates.isPresent()) {
Set<Expression> conjuncts = new LinkedHashSet<>(filter.getConjuncts());
Expression deletedPredicate = prunedPredicates.get();
Set<Expression> deletedPredicateSet = ExpressionUtils.extractConjunctionToSet(deletedPredicate);
conjuncts.removeAll(deletedPredicateSet);
if (conjuncts.isEmpty()) {
return scan;
} else {
return filter.withConjunctsAndChild(conjuncts, scan);
}
} else {
return filter.withChildren(ImmutableList.of(scan));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public Rule build() {
olapScan.getScoreRangeInfo(),
olapScan.getAnnOrderKeys(),
olapScan.getAnnLimit(),
olapScan.getTableAlias())
olapScan.getTableAlias(),
olapScan.getPartitionPrunablePredicates())
).toRule(RuleType.LOGICAL_OLAP_SCAN_TO_PHYSICAL_OLAP_SCAN_RULE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.doris.nereids.pattern.MatchingContext;
import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.rules.expression.rules.PartitionPrunablePredicate;
import org.apache.doris.nereids.rules.expression.rules.PartitionPruner;
import org.apache.doris.nereids.rules.expression.rules.PartitionPruner.PartitionPruneResult;
import org.apache.doris.nereids.rules.expression.rules.PartitionPruner.PartitionTableType;
Expand All @@ -40,13 +41,15 @@
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
import org.apache.doris.nereids.trees.plans.logical.LogicalRelation;
import org.apache.doris.nereids.util.ExpressionUtils;
import org.apache.doris.nereids.util.Utils;
import org.apache.doris.qe.ConnectContext;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -92,12 +95,29 @@ public List<Rule> buildRules() {
}
if (rewrittenLogicalRelation instanceof LogicalEmptyRelation) {
return rewrittenLogicalRelation;
} else {
return PartitionPruner.prunePredicate(
ctx.connectContext.getSessionVariable().skipPrunePredicate
|| ctx.statementContext.isDelete(),
prunedRes.second, filter, rewrittenLogicalRelation);
}
boolean skipPrunePredicate = ctx.connectContext.getSessionVariable().skipPrunePredicate
|| ctx.statementContext.isDelete();
if (!skipPrunePredicate && prunedRes.second.isPresent()) {
// Defer the predicate removal to PlanPostProcessor so that materialized-view
// rewrite still sees the original predicates. Otherwise, partition predicates
// that are equivalent to the surviving partition list would be silently
// dropped, leading to wrong results when an MV definition predicate matches
// the remaining conjuncts.
LogicalOlapScan prunedScan = (LogicalOlapScan) rewrittenLogicalRelation;
Set<Expression> prunableConjuncts = ExpressionUtils.extractConjunctionToSet(
prunedRes.second.get());
List<Slot> partitionSlots = getPartitionSlots(prunedScan, prunedScan.getTable());
if (partitionSlots != null) {
PartitionPrunablePredicate entry = new PartitionPrunablePredicate(
new HashSet<>(prunedScan.getSelectedPartitionIds()),
partitionSlots,
prunableConjuncts);
rewrittenLogicalRelation = prunedScan.withPartitionPrunablePredicates(
Optional.of(entry));
}
}
return filter.withChildren(ImmutableList.of(rewrittenLogicalRelation));
}).toRule(RuleType.OLAP_SCAN_PARTITION_PRUNE)
);
}
Expand Down
Loading
Loading