englefly commented on code in PR #11717: URL: https://github.com/apache/doris/pull/11717#discussion_r944188694
########## fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java: ########## @@ -169,7 +169,34 @@ private Pair<Boolean, GroupExpression> rewriteGroupExpression( GroupExpression groupExpression, Group target, LogicalProperties logicalProperties) { boolean newGroupExpressionGenerated = true; GroupExpression existedGroupExpression = groupExpressions.get(groupExpression); - if (existedGroupExpression != null) { + /* + * here we need to handle one situation that original target is not the same with + * existedGroupExpression.getOwnerGroup(). In this case, if we change target to + * existedGroupExpression.getOwnerGroup(), we could not rewrite plan as we expected and the plan + * will not be changed anymore. + * Think below example: + * We have a plan like this: + * Original (Group 2 is root): + * Group2: Project(outside) + * Group1: |---Project(inside) + * Group0: |---UnboundRelation + * + * and we want to rewrite group 2 by Project(inside, GroupPlan(group 0)) + * + * After rewriting we should get (Group 2 is root): + * Group2: Project(inside) + * Group0: |---UnboundRelation + * + * Group1: Project(inside) + * + * After rewriting, Group 1's GroupExpression is not in GroupExpressionsMap anymore and Group 1 is unreachable. + * Merge Group 1 into Group 2 is better, but in consideration of there is others way to let a Group take into + * unreachable. There's no need to complicate to add a merge step. Instead, we need to have a clear step to + * remove unreachable groups and GroupExpressions after rewrite. + * TODO: add a clear groups function to memo. + */ + if (existedGroupExpression != null + && (target == null || target.equals(existedGroupExpression.getOwnerGroup()))) { target = existedGroupExpression.getOwnerGroup(); Review Comment: if target is unreachable, it should be removed, `groups.remove(target)`, right? But if this target is a child of other GroupExpression, it is still reachable. -- 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