PruneFileScanPartition.java
// 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.rewrite;
import org.apache.doris.catalog.PartitionItem;
import org.apache.doris.datasource.ExternalTable;
import org.apache.doris.nereids.CascadesContext;
import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
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;
import org.apache.doris.nereids.rules.expression.rules.SortedPartitionRanges;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.plans.logical.LogicalFileScan;
import org.apache.doris.nereids.trees.plans.logical.LogicalFileScan.SelectedPartitions;
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Used to prune partition of file scan. For different external tables, there is no unified partition prune method.
* For example, Hive is using hive meta store api to get partitions. Iceberg is using Iceberg api to get FileScanTask,
* which doesn't return a partition list.
* So here we only support Hive table partition prune.
* For other external table, simply pass the conjuncts to LogicalFileScan, so that different
* external file ScanNode could do the partition filter by themselves.
*/
public class PruneFileScanPartition extends OneRewriteRuleFactory {
private static final Logger LOG = LogManager.getLogger(PruneFileScanPartition.class);
@Override
public Rule build() {
return logicalFilter(logicalFileScan()).whenNot(p -> p.child().getSelectedPartitions().isPruned)
.thenApply(ctx -> {
LogicalFilter<LogicalFileScan> filter = ctx.root;
LogicalFileScan scan = filter.child();
ExternalTable tbl = scan.getTable();
SelectedPartitions selectedPartitions;
if (tbl.supportInternalPartitionPruned()) {
selectedPartitions = pruneExternalPartitions(tbl, filter, scan, ctx.cascadesContext);
} else {
// set isPruned so that it won't go pass the partition prune again
selectedPartitions = new SelectedPartitions(0, ImmutableMap.of(), true);
}
LogicalFileScan rewrittenScan = scan.withSelectedPartitions(selectedPartitions);
return new LogicalFilter<>(filter.getConjuncts(), rewrittenScan);
}).toRule(RuleType.FILE_SCAN_PARTITION_PRUNE);
}
private SelectedPartitions pruneExternalPartitions(ExternalTable externalTable,
LogicalFilter<LogicalFileScan> filter, LogicalFileScan scan, CascadesContext ctx) {
Map<String, PartitionItem> selectedPartitionItems = Maps.newHashMap();
if (CollectionUtils.isEmpty(externalTable.getPartitionColumns(
ctx.getStatementContext().getSnapshot(externalTable,
scan.getTableSnapshot(), scan.getScanParams())))) {
// non partitioned table, return NOT_PRUNED.
// non partition table will be handled in HiveScanNode.
return SelectedPartitions.NOT_PRUNED;
}
Map<String, Slot> scanOutput = scan.getOutput()
.stream()
.collect(Collectors.toMap(slot -> slot.getName().toLowerCase(), Function.identity()));
List<Slot> partitionSlots = externalTable.getPartitionColumns(
ctx.getStatementContext().getSnapshot(externalTable,
scan.getTableSnapshot(), scan.getScanParams()))
.stream()
.map(column -> scanOutput.get(column.getName().toLowerCase()))
.collect(Collectors.toList());
// The pruner's input contract is positional AND injective: PartitionPruner zips each partition
// slot with the partition key's value at the same index, and OneListPartitionEvaluator collects
// (slot -> literal) into an ImmutableMap. A repeated slot therefore aborts planning with
// "Multiple entries with same key", and a null slot (a declared partition column missing from the
// scan output) would NPE. Either shape means the table's partition model is not expressible as a
// Doris partition-column set — e.g. an iceberg spec with two partition FIELDS over one source
// column. Decline to prune instead of failing the query: NOT_PRUNED leaves isPruned=false, so
// PluginDrivenScanNode.resolveRequiredPartitions returns scan-all and the query reads every
// partition — never fewer rows, only a lost optimization.
if (partitionSlots.contains(null) || Sets.newHashSet(partitionSlots).size() != partitionSlots.size()) {
LOG.warn("skip partition pruning for {}.{}: partition columns {} do not map to a distinct,"
+ " fully-resolved scan slot set", externalTable.getDbName(),
externalTable.getName(), partitionSlots);
return SelectedPartitions.NOT_PRUNED;
}
Map<String, PartitionItem> nameToPartitionItem = scan.getSelectedPartitions().selectedPartitions;
Optional<SortedPartitionRanges<String>> sortedPartitionRanges = Optional.empty();
boolean enableBinarySearch = ctx.getConnectContext() == null
|| ctx.getConnectContext().getSessionVariable().enableBinarySearchFilteringPartitions;
if (enableBinarySearch && !nameToPartitionItem.isEmpty()) {
sortedPartitionRanges = scan.getSelectedPartitions().sortedPartitionRanges
.or(() -> (Optional) externalTable.getSortedPartitionRanges(scan))
.or(() -> Optional.ofNullable(SortedPartitionRanges.build(nameToPartitionItem)));
}
PartitionPruneResult<String> result = PartitionPruner.pruneWithResult(
partitionSlots, filter.getPredicate(), nameToPartitionItem, ctx,
PartitionTableType.EXTERNAL, sortedPartitionRanges);
List<String> prunedPartitions = new ArrayList<>(result.partitions);
for (String name : prunedPartitions) {
PartitionItem item = nameToPartitionItem.get(name);
// Within THIS query, nameToPartitionItem and sortedPartitionRanges are built from the same
// frozen map. On a cross-query cache HIT, sortedPartitionRanges instead reuses ranges built by
// an earlier query keyed by the identical (snapshotId, schemaId) version token -- content is
// identical only via the MVCC determinism premise (same version token => same partition set),
// not because the two maps are literally the same object. A missing item here means that
// premise was violated, so fail loud rather than silently returning a partial scan.
Preconditions.checkState(item != null,
"pruned partition %s is missing in the selected partitions snapshot", name);
selectedPartitionItems.put(name, item);
}
return new SelectedPartitions(nameToPartitionItem.size(), selectedPartitionItems, true,
result.hasPartitionPredicate);
}
}