EmmyMiao87 commented on code in PR #9807: URL: https://github.com/apache/incubator-doris/pull/9807#discussion_r885306346
########## fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java: ########## @@ -17,65 +17,142 @@ package org.apache.doris.nereids.memo; +import org.apache.doris.nereids.trees.TreeNode; +import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; +import com.google.common.base.Preconditions; import com.google.common.collect.Lists; -import com.google.common.collect.Sets; +import com.google.common.collect.Maps; import java.util.List; -import java.util.Set; +import java.util.Map; /** * Representation for memo in cascades optimizer. + * + * @param <NODE_TYPE> should be {@link Plan} or {@link Expression} */ -public class Memo { +public class Memo<NODE_TYPE extends TreeNode> { private final List<Group> groups = Lists.newArrayList(); - private final Set<GroupExpression> groupExpressions = Sets.newHashSet(); - private Group rootSet; + // we could not use Set, because Set has no get method. + private final Map<GroupExpression, GroupExpression> groupExpressions = Maps.newHashMap(); + private Group root; - public void initialize(LogicalPlan plan) { - rootSet = newGroupExpression(plan, null).getParent(); + public void initialize(NODE_TYPE node) { + root = copyIn(node, null, false).getParent(); } - public Group getRootSet() { - return rootSet; + public Group getRoot() { + return root; } /** - * Add plan to Memo. + * Add node to Memo. * - * @param plan {@link Plan} to be added - * @param target target group to add plan. null to generate new Group - * @return Reference of plan in Memo + * @param node {@link Plan} or {@link Expression} to be added + * @param target target group to add node. null to generate new Group + * @param rewrite whether to rewrite the node to the target group + * @return Reference of node in Memo */ - // TODO: need to merge PlanRefSet if new PlanRef is same with some one already in memo - public GroupExpression newGroupExpression(Plan<?, ?> plan, Group target) { - List<GroupExpression> childGroupExpr = Lists.newArrayList(); - for (Plan<?, ?> childrenPlan : plan.children()) { - childGroupExpr.add(newGroupExpression(childrenPlan, null)); + public GroupExpression copyIn(NODE_TYPE node, Group target, boolean rewrite) { Review Comment: This is a very logic-heavy function, if you add ut later, please write TODO -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org