seawinde commented on code in PR #59972:
URL: https://github.com/apache/doris/pull/59972#discussion_r3679783639


##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVRefreshContext.java:
##########
@@ -58,9 +59,10 @@ public Map<BaseTableInfo, MTMVSnapshotIf> 
getBaseTableSnapshotCache() {
         return baseTableSnapshotCache;
     }
 
-    public static MTMVRefreshContext buildContext(MTMV mtmv) throws 
AnalysisException {
+    public static MTMVRefreshContext buildContext(MTMV mtmv, Map<List<String>, 
Set<String>> queryUsedPartitions)
+            throws AnalysisException {
         MTMVRefreshContext context = new MTMVRefreshContext(mtmv);
-        context.partitionMappings = mtmv.calculatePartitionMappings();
+        context.partitionMappings = 
mtmv.calculatePartitionMappings(queryUsedPartitions);
         context.baseVersions = MTMVPartitionUtil.getBaseVersions(mtmv);

Review Comment:
   Valid performance follow-up, especially for cloud. Restricting 
visible-version snapshots affects shared refresh/rewrite paths and needs 
separate cloud and non-cloud validation.



##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVRelatedPartitionDescSyncLimitGenerator.java:
##########
@@ -45,7 +46,8 @@ public class MTMVRelatedPartitionDescSyncLimitGenerator 
implements MTMVRelatedPa
 
     @Override
     public void apply(MTMVPartitionInfo mvPartitionInfo, Map<String, String> 
mvProperties,
-            RelatedPartitionDescResult lastResult, List<Column> 
partitionColumns) throws AnalysisException {
+            RelatedPartitionDescResult lastResult, List<Column> 
partitionColumns,
+                      Map<List<String>, Set<String>> queryUsedPartitionMap) 
throws AnalysisException {

Review Comment:
   Valid performance follow-up for sync-limit plus sparse filters. We will move 
filtering before conversion separately, with tests for missing, null, and empty 
filter semantics.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AsyncMaterializationContext.java:
##########
@@ -56,6 +57,7 @@ public class AsyncMaterializationContext extends 
MaterializationContext {
     private static final Logger LOG = 
LogManager.getLogger(AsyncMaterializationContext.class);
     private final MTMV mtmv;
     private Map<MTMVRelatedTableIf, Map<String, Set<String>>> 
partitionMultiFlatMap;

Review Comment:
   AsyncMaterializationContext is created per query/CascadesContext, and the 
same instance is used serially during rewrite. It is not shared across 
concurrent queries, so synchronization is not needed.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AsyncMaterializationContext.java:
##########
@@ -202,6 +211,58 @@ public Map<MTMVRelatedTableIf, Map<String, Set<String>>> 
calculatePartitionMappi
                         .addAll(set);
             }
         }
+        mergeCoveredPartitions(effectiveQueryUsedBaseTablePartitionMap, 
pctTables);
         return partitionMultiFlatMap;
     }
+
+    private boolean isPartitionMappingsCovered(Map<List<String>, Set<String>> 
effectiveQueryUsedBaseTablePartitionMap,
+            Set<MTMVRelatedTableIf> pctTables) {
+        if (partitionMultiFlatMap == null) {
+            return false;
+        }
+        for (MTMVRelatedTableIf pctTable : pctTables) {
+            List<String> tableQualifiers = pctTable.getFullQualifiers();
+            Set<String> queryUsedPartitions = 
effectiveQueryUsedBaseTablePartitionMap.containsKey(tableQualifiers)
+                    ? 
effectiveQueryUsedBaseTablePartitionMap.get(tableQualifiers)
+                    : null;
+            if (!isTableCovered(tableQualifiers, queryUsedPartitions)) {
+                return false;
+            }
+        }
+        return true;
+    }

Review Comment:
   Null and empty cannot share a sentinel: null means all partitions/full 
coverage, while empty means no partitions. The context is query-scoped, so the 
TOCTOU concern does not apply.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AsyncMaterializationContext.java:
##########
@@ -202,6 +211,58 @@ public Map<MTMVRelatedTableIf, Map<String, Set<String>>> 
calculatePartitionMappi
                         .addAll(set);
             }
         }
+        mergeCoveredPartitions(effectiveQueryUsedBaseTablePartitionMap, 
pctTables);
         return partitionMultiFlatMap;
     }
+
+    private boolean isPartitionMappingsCovered(Map<List<String>, Set<String>> 
effectiveQueryUsedBaseTablePartitionMap,
+            Set<MTMVRelatedTableIf> pctTables) {
+        if (partitionMultiFlatMap == null) {
+            return false;
+        }

Review Comment:
   Direct get is equivalent here because absent and present-null intentionally 
use the same full-mapping path. This is only a style cleanup, so we will keep 
this PR focused.



##########
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());

Review Comment:
   This invariant is enforced upstream: date_trunc EXPR partition analysis 
accepts only RANGE PCT tables, and this helper is only called on that path. No 
defensive runtime branch is needed.



-- 
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]

Reply via email to