924060929 commented on code in PR #12147:
URL: https://github.com/apache/doris/pull/12147#discussion_r957336047
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java:
##########
@@ -277,12 +346,172 @@ public void addEnforcerPlan(GroupExpression
groupExpression, Group group) {
groupExpression.setOwnerGroup(group);
}
+ private CopyInResult rewriteByExistsPlan(Group targetGroup, Plan
existsPlan) {
+ GroupExpression existedLogicalExpression = existsPlan instanceof
GroupPlan
+ ? ((GroupPlan) existsPlan).getGroup().getLogicalExpression()
// get first logicalGroupExpression
+ : existsPlan.getGroupExpression().get();
+ if (targetGroup != null) {
+ Group existedGroup = existedLogicalExpression.getOwnerGroup();
+ // clear targetGroup, from exist group move all logical
groupExpression
+ // and logicalProperties to target group
+ eliminateFromGroupAndMoveToTargetGroup(existedGroup, targetGroup,
existsPlan.getLogicalProperties());
+ }
+ return CopyInResult.of(false, existedLogicalExpression);
+ }
+
+ private CopyInResult rewriteByNewGroupExpression(Group targetGroup, Plan
newPlan,
+ GroupExpression newGroupExpression) {
+ if (targetGroup == null) {
+ // case 2:
+ // if not exist target group and not exist the same group
expression,
+ // then create new group with the newGroupExpression
+ Group newGroup = new Group(groupIdGenerator.getNextId(),
newGroupExpression,
+ newPlan.getLogicalProperties());
+ groups.put(newGroup.getGroupId(), newGroup);
+ groupExpressions.put(newGroupExpression, newGroupExpression);
+ } else {
+ // case 3:
+ // if exist the target group, clear all origin group expressions
in the
+ // existedExpression's owner group and reset logical properties,
the
+ // newGroupExpression is the init logical group expression
+ reInitGroup(targetGroup, newGroupExpression,
newPlan.getLogicalProperties());
+ }
+ return CopyInResult.of(true, newGroupExpression);
+ }
+
+ private CopyInResult rewriteByExistedGroupExpression(Group targetGroup,
Plan existedPlan,
+ GroupExpression existedExpression, GroupExpression newExpression) {
+ if (targetGroup != null &&
!targetGroup.equals(existedExpression.getOwnerGroup())) {
+ // case 4:
+ existedExpression.propagateApplied(newExpression);
+ moveParentExpressionsReference(existedExpression.getOwnerGroup(),
targetGroup);
+ recycleGroup(existedExpression.getOwnerGroup());
+ reInitGroup(targetGroup, newExpression,
existedPlan.getLogicalProperties());
+ return CopyInResult.of(true, newExpression);
+ } else {
+ // case 5:
+ // if targetGroup is null or targetGroup equal to the
existedExpression's ownerGroup,
+ // then recycle the temporary new group expression
+ recycleExpression(newExpression);
+ return CopyInResult.of(false, existedExpression);
+ }
+ }
+
+ /**
+ * eliminate fromGroup, clear targetGroup, then move the logical group
expressions in the fromGroup to the toGroup.
+ *
+ * the scenario is:
+ * ```
+ * Group 1(project, the targetGroup) Group
1(logicalOlapScan, the targetGroup)
+ * | =>
+ * Group 0(logicalOlapScan, the fromGroup)
+ * ```
+ *
+ * we should recycle the group 0, and recycle all group expressions in
group 1, then move the logicalOlapScan to
+ * the group 1, and reset logical properties of the group 1.
+ */
+ private void eliminateFromGroupAndMoveToTargetGroup(Group fromGroup, Group
targetGroup,
+ LogicalProperties logicalProperties) {
+ if (fromGroup == targetGroup) {
+ return;
+ }
+ // simple check targetGroup is the ancestors of the fromGroup, not
check completely because of performance
+ if (fromGroup == root) {
+ throw new IllegalStateException(
+ "TargetGroup should be ancestors of fromGroup, but
fromGroup is root. Maybe a bug");
+ }
+
+ List<GroupExpression> logicalExpressions =
fromGroup.clearLogicalExpressions();
+ recycleGroup(fromGroup);
Review Comment:
you can see the `clearLogicalExpressions` method remove all logical group
expressions from the fromGroup, when we recycle the fromGroup, there is no any
logical group expression will be recycle. so we can simple move all the logical
group expressions to the targetGroup
--
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]