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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java:
##########
@@ -662,18 +663,13 @@ public void mergeGroup(Group source, Group destination, 
HashMap<Long, Group> pla
         if (source.equals(destination)) {
             return;
         }
-        List<GroupExpression> needReplaceChild = Lists.newArrayList();
-        for (GroupExpression dstParent : 
destination.getParentGroupExpressions()) {
-            if (dstParent.getOwnerGroup().equals(source)) {
-                // cycle, we should not merge
-                return;
-            }
+        // Merging an ancestor with a descendant closes a cycle, even when 
they are not direct parent and child.
+        // For example, merging Project(k, Project(k, 1, G)) with G makes 
statistics derivation revisit G forever.
+        if (isReachable(source, destination) || isReachable(destination, 
source)) {

Review Comment:
   [P2] Preserve parent state when a recursive merge is refused
   
   This return can also happen inside the recursive parent deduplication at 
line 699, after the caller has already mutated the parent. For example, this 
acyclic Memo is legal through public `copyIn` calls (all projections reuse the 
same slot `k`; constants have distinct Alias ExprIds):
   
   ```text
   S    = Scan(k, ...)
   A    = Project(k, S)
   E    = Project(k, 1, S)
   B    = Project(k, E)
   Q    = Project(k, B)
   X    = Project(k, 2, Q)
   Root = {Project(k, A), Project(k, X)}
   ```
   
   `MergeProjectsCBO` on B rediscovers A, so `mergeGroup(B, A)` is allowed. 
Replacing B in Q then produces `Project(k, A)`, colliding with Root's first 
expression. The caller removes Q's expression from `groupExpressions` and calls 
`setUnused(true)` before recursively merging Q into Root. This guard now 
refuses because `Root -> X -> Q`, leaving Q's expression reachable with owner Q 
and child A. A subsequent `OptimizeGroupExpressionJob` for it throws at 
`GroupExpression.isUnused()`'s `children.isEmpty() && ownerGroup == null` 
check. Please make recursive refusal preserve/restore usable parent-expression 
state before invalidating it, and add coverage for this 
successful-outer/rejected-inner merge sequence.



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