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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java:
##########
@@ -471,6 +470,49 @@ protected List<Plan> doRewrite(StructInfo queryStructInfo, 
CascadesContext casca
         return rewriteResults;
     }
 
+    Plan buildPartitionCompensationPlan(Plan rewrittenPlan, Plan 
baseTablePlan, Plan queryPlan) {
+        Plan queryGlobalLimit = queryPlan.<Plan>collectFirst(
+                node -> isGlobalLimitOrTopN((Plan) node)).orElse(null);
+        if (queryGlobalLimit == null) {
+            return buildCompensationUnion(queryPlan, 
Lists.newArrayList(rewrittenPlan, baseTablePlan));
+        }
+        Plan rewrittenGlobalLimit = rewrittenPlan.<Plan>collectFirst(
+                node -> isSameGlobalLimitOrTopN((Plan) node, 
queryGlobalLimit)).orElse(null);
+        Plan baseTableGlobalLimit = baseTablePlan.<Plan>collectFirst(
+                node -> isSameGlobalLimitOrTopN((Plan) node, 
queryGlobalLimit)).orElse(null);
+        if (rewrittenGlobalLimit == null || baseTableGlobalLimit == null
+                || getOffset(rewrittenGlobalLimit) != 
getOffset(queryGlobalLimit)) {
+            return null;
+        }
+        Plan compensationUnion = 
buildCompensationUnion(queryGlobalLimit.child(0), Lists.newArrayList(
+                rewrittenGlobalLimit.child(0), baseTableGlobalLimit.child(0)));
+        return queryPlan.rewriteDownShortCircuit(plan -> plan == 
queryGlobalLimit
+                ? queryGlobalLimit.withChildren(compensationUnion) : plan);
+    }
+
+    private boolean isGlobalLimitOrTopN(Plan plan) {
+        return plan instanceof LogicalTopN
+                || plan instanceof LogicalLimit && ((LogicalLimit<?>) 
plan).getPhase() == LimitPhase.GLOBAL;
+    }
+
+    private boolean isSameGlobalLimitOrTopN(Plan plan, Plan queryGlobalLimit) {
+        return plan.getType() == queryGlobalLimit.getType() && 
isGlobalLimitOrTopN(plan);
+    }

Review Comment:
   为什么这就可以判定是same?需要注释说明。比如,如果一个plan中有多个 global limit 或者 topn 
会怎么样?当前支持哪些情况,有什么限制和前提假设。如果前提假设被破坏会怎么样



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java:
##########
@@ -471,6 +470,49 @@ protected List<Plan> doRewrite(StructInfo queryStructInfo, 
CascadesContext casca
         return rewriteResults;
     }
 
+    Plan buildPartitionCompensationPlan(Plan rewrittenPlan, Plan 
baseTablePlan, Plan queryPlan) {
+        Plan queryGlobalLimit = queryPlan.<Plan>collectFirst(
+                node -> isGlobalLimitOrTopN((Plan) node)).orElse(null);
+        if (queryGlobalLimit == null) {
+            return buildCompensationUnion(queryPlan, 
Lists.newArrayList(rewrittenPlan, baseTablePlan));
+        }
+        Plan rewrittenGlobalLimit = rewrittenPlan.<Plan>collectFirst(
+                node -> isSameGlobalLimitOrTopN((Plan) node, 
queryGlobalLimit)).orElse(null);
+        Plan baseTableGlobalLimit = baseTablePlan.<Plan>collectFirst(
+                node -> isSameGlobalLimitOrTopN((Plan) node, 
queryGlobalLimit)).orElse(null);
+        if (rewrittenGlobalLimit == null || baseTableGlobalLimit == null
+                || getOffset(rewrittenGlobalLimit) != 
getOffset(queryGlobalLimit)) {
+            return null;

Review Comment:
   这里的判断方式比较tricky,需要注释详细说清楚能这么做的原因,前提假设有哪些等,以防止以后功能扩展破坏这里的行为



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