924060929 commented on code in PR #12147:
URL: https://github.com/apache/doris/pull/12147#discussion_r957314021


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java:
##########
@@ -129,91 +111,178 @@ public CascadesContext 
newCascadesContext(StatementContext statementContext) {
     }
 
     /**
-     * Insert groupExpression to target group.
-     * If group expression is already in memo and target group is not null, we 
merge two groups.
-     * If target is null, generate new group.
-     * If target is not null, add group expression to target group
+     * init memo by a first plan.
+     * @param plan first plan
+     * @return plan's corresponding group
+     */
+    private Group init(Plan plan) {
+        Preconditions.checkArgument(!(plan instanceof GroupPlan), "Cannot init 
memo by a GroupPlan");
+        Preconditions.checkArgument(!plan.getGroupExpression().isPresent(),
+                "Cannot init memo by a plan which contains a groupExpression");
+
+        // initialize children recursively
+        List<Group> childrenGroups = plan.children()
+                .stream()
+                .map(this::init)
+                .collect(ImmutableList.toImmutableList());
+
+        plan = replaceChildrenToGroupPlan(plan, childrenGroups);
+        GroupExpression newGroupExpression = new GroupExpression(plan, 
childrenGroups);
+        Group group = new Group(groupIdGenerator.getNextId(), 
newGroupExpression, plan.getLogicalProperties());
+
+        groups.put(group.getGroupId(), group);
+        if (groupExpressions.containsKey(newGroupExpression)) {
+            throw new IllegalStateException("groupExpression already exists in 
memo, maybe a bug");
+        }
+        groupExpressions.put(newGroupExpression, newGroupExpression);
+        return group;
+    }
+
+    /**
+     * add or replace the plan into the target group.
      *
-     * @param groupExpression groupExpression to insert
-     * @param target target group to insert groupExpression

Review Comment:
   ok



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