github-actions[bot] commented on code in PR #59972: URL: https://github.com/apache/doris/pull/59972#discussion_r3061928841
########## fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPartitionExpander.java: ########## @@ -0,0 +1,209 @@ +// 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.mtmv; + +import org.apache.doris.catalog.PartitionItem; +import org.apache.doris.catalog.PartitionKey; +import org.apache.doris.catalog.PartitionType; +import org.apache.doris.catalog.RangePartitionItem; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.datasource.mvcc.MvccSnapshot; +import org.apache.doris.datasource.mvcc.MvccUtil; + +import com.google.common.collect.Maps; +import com.google.common.collect.Range; +import com.google.common.collect.Sets; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.Set; + +/** + * Utility to build MV partition mappings using Range.encloses(), bypassing the + * expensive rollup pipeline (dateTrunc / strToDate / dateIncrement per-partition). + * + * Separated from MTMV to keep a lightweight dependency tree for testability — + * loading this class does not trigger MTMV → OlapTable → CloudReplica class loading. + */ +public class MTMVPartitionExpander { + + /** + * Build partition mappings directly using Range.encloses(), bypassing the rollup pipeline. + * + * For EXPR-type MVs (e.g., date_trunc(month)) with RANGE base tables: + * 1. Find which MV partition ranges enclose the queried base partitions + * 2. Collect ALL base partitions within those relevant MV ranges + * 3. Return the complete mapping directly — no dateTrunc/rollup computation needed + * + * Returns null if any pctTable is non-RANGE (caller should fall back to pipeline). + * + * @return mvPartitionName ==> pctTable ==> Set of pctPartitionNames, or null if unsupported + */ + public static Map<String, Map<MTMVRelatedTableIf, Set<String>>> buildDirectMappings( + Map<List<String>, Set<String>> queryUsedBaseTablePartitionMap, + Map<String, PartitionItem> mvPartitionItems, + Set<MTMVRelatedTableIf> pctTables) throws AnalysisException { + // Pre-extract MV ranges into parallel lists for fast indexed access in inner loops. + int mvCount = mvPartitionItems.size(); + List<String> mvNames = new ArrayList<>(mvCount); + List<Range<PartitionKey>> mvRanges = new ArrayList<>(mvCount); + for (Entry<String, PartitionItem> mvEntry : mvPartitionItems.entrySet()) { + if (!(mvEntry.getValue() instanceof RangePartitionItem)) { + return null; + } + mvNames.add(mvEntry.getKey()); + mvRanges.add(((RangePartitionItem) mvEntry.getValue()).getItems()); + } + + // Initialize result: all MV partitions with empty mappings. + // Non-relevant MV partitions keep empty maps — downstream code + // (getMtmvPartitionsByRelatedPartitions) correctly skips them. + Map<String, Map<MTMVRelatedTableIf, Set<String>>> result = + Maps.newHashMapWithExpectedSize(mvCount); + for (String mvName : mvNames) { + result.put(mvName, Maps.newHashMap()); + } + + for (MTMVRelatedTableIf pctTable : pctTables) { + List<String> qualifiers = pctTable.getFullQualifiers(); + Set<String> queryUsedPartitions = queryUsedBaseTablePartitionMap.get(qualifiers); Review Comment: `queryUsedBaseTablePartitionMap` uses `null` to mean "the query touches all partitions of this table" (`PartitionCompensator.getQueryUsedPartitions()` builds that contract, and `MTMVRewriteUtil.getMtmvPartitionsByRelatedPartitions()` already treats `entry.getValue() == null` that way). Here `get()` returning `null` is treated the same as "table not present in the filter", so for EXPR+RANGE MVs the fast path skips that PCT table entirely and leaves every MV partition with an empty mapping. Downstream `isMTMVPartitionSync()` then compares those empty sets with the refresh snapshot and will mark all MV partitions unsynchronized, so rewrite eligibility regresses for queries that scan all partitions of a related table. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AsyncMaterializationContext.java: ########## @@ -184,12 +184,14 @@ public boolean isSuccess() { /** * Calculate partition mappings and cache */ - public Map<MTMVRelatedTableIf, Map<String, Set<String>>> calculatePartitionMappings() throws AnalysisException { + public Map<MTMVRelatedTableIf, Map<String, Set<String>>> calculatePartitionMappings( + Map<List<String>, Set<String>> queryUsedBaseTablePartitionMap) throws AnalysisException { if (partitionMultiFlatMap != null) { Review Comment: This cache is no longer keyed by the actual `queryUsedBaseTablePartitionMap`, but the result now depends on that argument. The same `AsyncMaterializationContext` instance is reused while `AbstractMaterializedViewRule` iterates different `queryStructInfo` / `relationBitSet` combinations, so a mapping computed for one partition subset can be returned for a later subset and make `PartitionCompensator.calcInvalidPartitions()` compensate against stale partitions. -- 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]
