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


##########
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:
   补充完善于 7c76f722b61:移除了含义容易误解的 isSame helper,直接在入口表达支持前提:唯一 Global 
Limit/TopN、类型一致、两侧 offset 一致;前提破坏时放弃 rewrite。对应边界均已补单测。



##########
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:
   补充完善于 7c76f722b61:query、rewritten 和 base-table plan 现在都会收集全部 Global 
Limit/TopN。query 无全局算子时要求两个分支也没有;有全局算子时要求三棵 plan 各自恰好一个、类型一致,并同时校验 
rewritten/base 两侧 offset。任一前提不满足均返回 null 放弃 MV 
rewrite。已补充无预期算子、多个算子、混合类型、类型不一致和 base offset 不一致测试。



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