foxtail463 commented on code in PR #64032: URL: https://github.com/apache/doris/pull/64032#discussion_r3433862088
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/algebra/ExternalPartitionSelection.java: ########## @@ -0,0 +1,112 @@ +// 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.trees.plans.algebra; + +import org.apache.doris.catalog.PartitionItem; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.Slot; + +import com.google.common.collect.ImmutableMap; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +/** + * Partition selection state for external file scans. + */ +public class ExternalPartitionSelection extends PartitionSelection<String> { + // NOT_PRUNED means the Nereids planner does not handle the partition pruning. + // This can be treated as the initial value of ExternalPartitionSelection. + // Or used to indicate that the partition pruning is not processed. + public static final ExternalPartitionSelection NOT_PRUNED = + new ExternalPartitionSelection(0, ImmutableMap.of(), false, false); + + /** + * total partition number before pruning. + */ + public final long totalPartitionNum; + + /** + * partition name -> partition item + */ + public final Map<String, PartitionItem> selectedPartitionItems; + + /** + * Constructor for ExternalPartitionSelection. + */ + public ExternalPartitionSelection(long totalPartitionNum, Map<String, PartitionItem> selectedPartitionItems, + boolean partitionPruned, boolean hasPartitionConstraint) { + super(partitionPruned, hasPartitionConstraint); + this.totalPartitionNum = totalPartitionNum; + this.selectedPartitionItems = ImmutableMap.copyOf(Objects.requireNonNull(selectedPartitionItems, + "selectedPartitionItems is null")); + } + + /** + * Constructor for ExternalPartitionSelection. + */ + public ExternalPartitionSelection(long totalPartitionNum, Map<String, PartitionItem> selectedPartitionItems, + boolean partitionPruned, boolean hasPartitionConstraint, List<Slot> partitionSlots, + Set<Expression> conjuncts) { + super(partitionPruned, hasPartitionConstraint, selectedPartitionItems.keySet(), partitionSlots, conjuncts); + this.totalPartitionNum = totalPartitionNum; + this.selectedPartitionItems = ImmutableMap.copyOf(Objects.requireNonNull(selectedPartitionItems, + "selectedPartitionItems is null")); + } + + public boolean isPruned() { + return partitionPruned && selectedPartitionItems.size() < totalPartitionNum; + } + + /** + * Returns partition conjuncts that have already been applied to the selected partition row count. + */ + public Set<Expression> getAppliedPartitionConjuncts(List<Slot> output) { + return rewriteAppliedPartitionConjuncts(selectedPartitionItems.keySet(), buildNameToSlotMap(output)); + } + + private static Map<String, Slot> buildNameToSlotMap(List<Slot> output) { + Map<String, Slot> map = new HashMap<>(output.size()); + for (Slot slot : output) { + map.put(slot.getName().toLowerCase(), slot); + } + return map; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ExternalPartitionSelection that = (ExternalPartitionSelection) o; + return totalPartitionNum == that.totalPartitionNum + && Objects.equals(selectedPartitionItems.keySet(), that.selectedPartitionItems.keySet()) Review Comment: The old `SelectedPartitions.equals()` also compared only `selectedPartitions.keySet()`, not the full map values. The old `hashCode()` used the full map, which was actually inconsistent with `equals()` when two maps had the same partition names but different `PartitionItem` instances. <img width="680" height="404" alt="image" src="https://github.com/user-attachments/assets/eedfb575-4ee0-4aab-9bf2-ab42dc721920" /> Here I keep the same equality semantics: partition selection identity is based on the selected partition names, while `PartitionItem` values are metadata payloads used for pruning/scan planning. I also make `hashCode()` consistent with `equals()` by hashing the key set. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
