github-actions[bot] commented on code in PR #62828:
URL: https://github.com/apache/doris/pull/62828#discussion_r3146455379


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Optimizer.java:
##########
@@ -166,14 +167,29 @@ private Plan generateSelectiveCTEInline() {
         if (inlinedPlan != null) {
             inlinedPlan = rewriteInlinedPlan(inlinedPlan);
             if (inlinedPlan.anyMatch(p -> p instanceof LogicalEmptyRelation)) {
-                inlinedPlan = eliminateEmptyRelation(inlinedPlan);
+                inlinedPlan = normalizeCtePlan(inlinedPlan);
                 cascadesContext.setRewritePlan(inlinedPlan);
                 return null;
             }
         }
         return null;
     }
 
+    private Plan normalizeCtePlan(Plan plan) {
+        Plan currentPlan = plan;
+        while (true) {
+            CTEInliner cteInliner = new 
CTEInliner(cascadesContext.getStatementContext());
+            Plan normalizedPlan = 
cteInliner.inlineByCurrentConsumerCount(currentPlan);
+            if (normalizedPlan.anyMatch(p -> p instanceof 
LogicalEmptyRelation)) {
+                normalizedPlan = eliminateEmptyRelation(normalizedPlan);

Review Comment:
   `Plan.equals()` is not a safe fixpoint test here. Several logical plan nodes 
in Nereids ignore their children in `equals()` (for example `LogicalCTEAnchor` 
only compares `cteId`, and `LogicalSubQueryAlias` only compares `qualifier`). 
That means an inner CTE can be pruned under a still-kept outer anchor, 
`currentPlan.equals(normalizedPlan)` becomes `true` after the first pass, and 
the outer anchor is never revisited. A query where removing one nested CTE 
drops an outer producer from 2 consumers to 1 will still miss the cascading 
inline this method is trying to apply. Please track an explicit `changed` flag 
(or compare a deep tree fingerprint) instead of using shallow structural 
`equals()`.



##########
fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java:
##########
@@ -2552,7 +2552,7 @@ public static boolean isEagerAggregationOnJoin() {
     @VarAttrDef.VarAttr(name = ENABLE_ORDERED_SCAN_RANGE_LOCATIONS)
     public boolean enableOrderedScanRangeLocations = false;
 
-    @VarAttrDef.VarAttr(name = CTE_INLINE_MODE, alias = "cbo_cte_inline_mode", 
description = {
+    @VarAttrDef.VarAttr(name = CTE_INLINE_MODE, description = {

Review Comment:
   `VariableMgr` registers every `@VarAttrDef.VarAttr.alias()` as an accepted 
variable name, so removing `alias = "cbo_cte_inline_mode"` is a user-visible 
compatibility break. Existing scripts/sessions that still do `SET 
cbo_cte_inline_mode = ...` will start failing after this PR, even though the 
change is otherwise just an optimizer fix. Unless there is an intentional 
deprecation/migration plan for that legacy name, the alias should stay.



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