jackwener commented on code in PR #20209:
URL: https://github.com/apache/doris/pull/20209#discussion_r1211529951


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/joinorder/hypergraph/receiver/PlanReceiver.java:
##########
@@ -335,58 +336,106 @@ private void makeLogicalExpression(Group root) {
         }
     }
 
+    /**
+     * The top project of (T1, T2, T3) is different after reorder
+     * we need merge Project1 and Project2 as Project4 after reorder
+     * T1 join T2 join T3:
+     *    Project1(a, e + f)
+     *        join(a = e)
+     *            Project2(a, b + d as e)
+     *                join(a = c)
+     *                    T1(a, b)
+     *                    T2(c, d)
+     *        T3(e, f)
+     *
+     * after reorder:
+     * T1 join T3 join T2:
+     *    Project4(a, b + d + f)
+     *        join(a = c)
+     *            Project3(a, b, f)
+     *                join(a = e)
+     *                    T1(a, b)
+     *                    T3(e, f)
+     *        T2(c, d)
+     */
+    private List<NamedExpression> mergeProjections(List<NamedExpression> 
childProjects,
+            List<NamedExpression> parentProjects) {
+        Map<Expression, Alias> replaceMap = childProjects.stream().filter(e -> 
e instanceof Alias)
+                .collect(Collectors.toMap(NamedExpression::toSlot, e -> 
(Alias) e));
+        return parentProjects.stream().map(expr -> {
+            if (expr instanceof Alias) {
+                Alias alias = (Alias) expr;
+                Expression insideExpr = alias.child();
+                Expression newInsideExpr = insideExpr.rewriteUp(e -> {
+                    Alias getAlias = replaceMap.get(e);
+                    return getAlias == null ? e : getAlias.child();
+                });
+                return newInsideExpr == insideExpr ? expr
+                        : alias.withChildren(ImmutableList.of(newInsideExpr));
+            } else {
+                Alias getAlias = replaceMap.get(expr);
+                return getAlias == null ? expr : getAlias;
+            }
+        }).collect(ImmutableList.toImmutableList());
+    }

Review Comment:
   Already exist `mergeProjections(Project childProject)`



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