morrySnow commented on code in PR #33808:
URL: https://github.com/apache/doris/pull/33808#discussion_r1569855216


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java:
##########
@@ -351,19 +353,69 @@ protected Set<Long> calcInvalidPartitions(Plan 
rewrittenPlan, MaterializationCon
         if (relatedPartitionTable == null) {
             return ImmutableSet.of();
         }
-        // get mv valid partitions
+        // Check base table partition if update or not, if updated, will calc 
the invalid mv partitions which
+        // are updated and then return
         Set<Long> mvDataValidPartitionIdSet = 
MTMVRewriteUtil.getMTMVCanRewritePartitions(mtmv,
                         cascadesContext.getConnectContext(), 
System.currentTimeMillis()).stream()
                 .map(Partition::getId)
                 .collect(Collectors.toSet());
-        Set<Long> queryUsedPartitionIdSet = rewrittenPlan.collectToList(node 
-> node instanceof LogicalOlapScan
+        Set<Long> mvPartitionIdSetQueryUsed = rewrittenPlan.collectToList(node 
-> node instanceof LogicalOlapScan
                         && Objects.equals(((CatalogRelation) 
node).getTable().getName(), mtmv.getName()))
                 .stream()
                 .map(node -> ((LogicalOlapScan) 
node).getSelectedPartitionIds())
                 .flatMap(Collection::stream)
+                .collect(ImmutableSet.toImmutableSet());
+
+        Set<Long> invalidMvPartitionIdSet = new 
HashSet<>(mvPartitionIdSetQueryUsed);
+        invalidMvPartitionIdSet.removeAll(mvDataValidPartitionIdSet);
+        Set<PartitionKeyDesc> needUnionPartitions = 
invalidMvPartitionIdSet.stream()
+                .map(invalidPartitionId -> 
mvPartitionInfo.getItem(invalidPartitionId).toPartitionKeyDesc())
+                .collect(Collectors.toSet());
+        if (!needUnionPartitions.isEmpty()) {
+            return needUnionPartitions;
+        }
+        // check partitions used is valid or not when base table create or 
delete partitions
+        // or partition in mv is not ready but base table is ready
+        Map<Long, Set<PartitionItem>> baseTablePartitionMap = new 
LinkedHashMap<>();
+        baseTablePartitionMap.put(relatedPartitionTable.getTableId(), new 
HashSet<>());
+        cascadesContext.getRewritePlan().accept(new 
QueryScanPartitionsCollector(), baseTablePartitionMap);
+        Set<PartitionKeyDesc> baseTablePartitionsQueryUsed = 
baseTablePartitionMap.getOrDefault(
+                        relatedPartitionTable.getTableId(),
+                        ImmutableSet.of()).stream()
+                .map(PartitionItem::toPartitionKeyDesc)
+                .collect(Collectors.toSet());

Review Comment:
   do not use stream api. it hurt perfermance



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to