You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/12/01 10:31:43 UTC

[GitHub] [doris] morrySnow commented on a diff in pull request #14730: [feature](nereids)merge proj-proj in post process

morrySnow commented on code in PR #14730:
URL: https://github.com/apache/doris/pull/14730#discussion_r1036937981


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/MergeProjectPostProcessor.java:
##########
@@ -0,0 +1,60 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.processor.post;
+
+import org.apache.doris.nereids.CascadesContext;
+import org.apache.doris.nereids.rules.rewrite.logical.MergeProjects;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalProject;
+
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * merge consecutive projects
+ */
+public class MergeProjectPostProcessor extends PlanPostProcessor {
+
+    @Override
+    public PhysicalProject visitPhysicalProject(PhysicalProject<? extends Plan> project, CascadesContext ctx) {
+        Plan child = project.child();
+        child = child.accept(this, ctx);
+        if (child instanceof PhysicalProject) {
+            Map<Expression, Expression> aliasMap = getAliasProjections((PhysicalProject) child);
+            List<NamedExpression> projections = project.getProjects();
+            projections = projections.stream()
+                    .map(e -> MergeProjects.ExpressionReplacer.INSTANCE.visit(e, aliasMap))
+                    .map(NamedExpression.class::cast)
+                    .collect(Collectors.toList());
+            return project.withProjectionsAndChild(projections, child.child(0));
+        }
+        return project;

Review Comment:
   could we abstract a function for merging project list? this function looks like
   ```
   List< NamedExpression> func(List< NamedExpression> top, List< NamedExpression> bottom)
   ```
   and then both merge logical and physical project to call it to do merge job



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/MergeProjects.java:
##########
@@ -92,7 +95,7 @@ public Expression visit(Expression expr, Map<Expression, Expression> substitutio
                 Slot ref = ((SlotReference) expr).withQualifier(Collections.emptyList());
                 if (substitutionMap.containsKey(ref)) {
                     Alias res = (Alias) substitutionMap.get(ref);
-                    return res.child();
+                    return new Alias(res.child(), ref.getName());

Review Comment:
   the new Alias is useless and will intro more complexity to expression process. because when we come here the slot is not the root of this expr tree.



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org