seawinde commented on code in PR #59972: URL: https://github.com/apache/doris/pull/59972#discussion_r3679782904
########## 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: Thanks. Fixed on the current head: null still means all partitions and uses the full-mapping path. ########## 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: Thanks. Fixed on the current head: cache coverage is tracked per PCT table, and uncovered partitions are calculated and merged. ########## fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPartitionExpander.java: ########## @@ -0,0 +1,122 @@ +// 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 expand query-used partition filters to MV partition granularity + * using Range.encloses(), avoiding expensive dateTrunc / strToDate / dateIncrement + * per-partition operations in the rollup pipeline. + * 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 { + + /** + * Expand queryUsedPartitions to MV partition granularity for RANGE base tables. + * For example, if MV is monthly partitioned (date_trunc(month)) and base table is daily: + * - Query uses p_20250115 (Jan 15) + * - Find MV partition p_202501 that encloses [20250115, 20250116) + * - Expand to ALL daily partitions within p_202501's range [20250101, 20250201) + * - Result: {p_20250101, p_20250102, ..., p_20250131} + */ + public static Map<List<String>, Set<String>> expandToMvPartitionGranularity( + Map<List<String>, Set<String>> queryUsedBaseTablePartitionMap, + Map<String, PartitionItem> mvPartitionItems, + Set<MTMVRelatedTableIf> pctTables) throws AnalysisException { + List<Range<PartitionKey>> mvRanges = new ArrayList<>(mvPartitionItems.size()); + for (PartitionItem item : mvPartitionItems.values()) { + mvRanges.add(((RangePartitionItem) item).getItems()); + } + + Map<List<String>, Set<String>> expanded = Maps.newHashMap(); + for (MTMVRelatedTableIf pctTable : pctTables) { + List<String> qualifiers = pctTable.getFullQualifiers(); + Set<String> queryUsedPartitions = queryUsedBaseTablePartitionMap.get(qualifiers); + if (queryUsedPartitions == null) { + continue; + } + + Optional<MvccSnapshot> snapshot = MvccUtil.getSnapshotFromContext(pctTable); + if (pctTable.getPartitionType(snapshot) != PartitionType.RANGE) { + expanded.put(qualifiers, queryUsedPartitions); + continue; + } + + Map<String, PartitionItem> basePartitionItems = pctTable.getAndCopyPartitionItems(snapshot); + + List<Range<PartitionKey>> relevantMvRanges = new ArrayList<>(); + for (String queriedBasePartition : queryUsedPartitions) { + PartitionItem baseItem = basePartitionItems.get(queriedBasePartition); + if (baseItem == null) { + continue; + } + Range<PartitionKey> baseRange = ((RangePartitionItem) baseItem).getItems(); Review Comment: Valid performance follow-up. This PR already delivers the target sparse-query QPS gain; broad-query indexing and an all-partitions fast path will be benchmarked separately. ########## fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPartitionExpander.java: ########## @@ -0,0 +1,122 @@ +// 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 expand query-used partition filters to MV partition granularity + * using Range.encloses(), avoiding expensive dateTrunc / strToDate / dateIncrement + * per-partition operations in the rollup pipeline. + * 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 { + + /** + * Expand queryUsedPartitions to MV partition granularity for RANGE base tables. + * For example, if MV is monthly partitioned (date_trunc(month)) and base table is daily: + * - Query uses p_20250115 (Jan 15) + * - Find MV partition p_202501 that encloses [20250115, 20250116) + * - Expand to ALL daily partitions within p_202501's range [20250101, 20250201) + * - Result: {p_20250101, p_20250102, ..., p_20250131} + */ + public static Map<List<String>, Set<String>> expandToMvPartitionGranularity( + Map<List<String>, Set<String>> queryUsedBaseTablePartitionMap, + Map<String, PartitionItem> mvPartitionItems, + Set<MTMVRelatedTableIf> pctTables) throws AnalysisException { + List<Range<PartitionKey>> mvRanges = new ArrayList<>(mvPartitionItems.size()); + for (PartitionItem item : mvPartitionItems.values()) { + mvRanges.add(((RangePartitionItem) item).getItems()); + } + + Map<List<String>, Set<String>> expanded = Maps.newHashMap(); + for (MTMVRelatedTableIf pctTable : pctTables) { Review Comment: Rechecked: independent per-table filters currently require an unsupported UNION ALL-root transparent rewrite, and the existing test also accepts MV not chose. Supported multi-PCT joins equality-relate PCT keys, so disjoint buckets do not establish a current correctness regression. We will address complete mapping when that rewrite shape is supported. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AsyncMaterializationContext.java: ########## @@ -184,12 +186,19 @@ public boolean isSuccess() { /** * Calculate partition mappings and cache */ - public Map<MTMVRelatedTableIf, Map<String, Set<String>>> calculatePartitionMappings() throws AnalysisException { - if (partitionMultiFlatMap != null) { + public Map<MTMVRelatedTableIf, Map<String, Set<String>>> calculatePartitionMappings( + Map<List<String>, Set<String>> queryUsedBaseTablePartitionMap) throws AnalysisException { + Map<List<String>, Set<String>> effectiveQueryUsedBaseTablePartitionMap Review Comment: Valid performance follow-up. Avoiding normalization on hits and duplicate expansion on misses needs a cache/API change, so it will be handled separately with benchmarks. ########## fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java: ########## @@ -530,15 +553,25 @@ public Map<String, PartitionKeyDesc> generateMvPartitionDescs() { * @return mvPartitionName ==> pctTable ==> pctPartitionName * @throws AnalysisException */ - public Map<String, Map<MTMVRelatedTableIf, Set<String>>> calculatePartitionMappings() throws AnalysisException { + public Map<String, Map<MTMVRelatedTableIf, Set<String>>> calculatePartitionMappings( + Map<List<String>, Set<String>> queryUsedBaseTablePartitionMap) throws AnalysisException { if (mvPartitionInfo.getPartitionType() == MTMVPartitionType.SELF_MANAGE) { return Maps.newHashMap(); } long start = System.currentTimeMillis(); + // For EXPR-type partitions with RANGE base tables, expand the query-used partition + // filter to MV partition granularity. This ensures complete partition mappings per + // MV partition (needed for isSyncWithPartitions correctness) while skipping + // irrelevant MV partitions entirely (the performance optimization). + // For nested MVs where pctTable is not in the filter, the expanded map is empty, + // so the pipeline runs without filtering (full computation) — correct behavior. + Map<String, PartitionItem> mvPartitionItems = getAndCopyPartitionItems(); + Map<List<String>, Set<String>> effectiveFilter + = getEffectiveQueryUsedBaseTablePartitionMap(queryUsedBaseTablePartitionMap, mvPartitionItems); Map<String, Map<MTMVRelatedTableIf, Set<String>>> res = Maps.newHashMap(); Map<PartitionKeyDesc, Map<MTMVRelatedTableIf, Set<String>>> pctPartitionDescs = MTMVPartitionUtil - .generateRelatedPartitionDescs(mvPartitionInfo, mvProperties, getPartitionColumns()); - Map<String, PartitionItem> mvPartitionItems = getAndCopyPartitionItems(); + .generateRelatedPartitionDescs(mvPartitionInfo, mvProperties, getPartitionColumns(), + effectiveFilter); for (Entry<String, PartitionItem> entry : mvPartitionItems.entrySet()) { Review Comment: Valid performance follow-up. Returning only relevant MV partitions changes the filtered result contract; we will optimize it separately while keeping refresh callers on the full result. -- 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]
