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/06/08 02:12:24 UTC

[GitHub] [incubator-doris] Kikyou1997 commented on a diff in pull request #9993: [feature](nereids) Plan Translator

Kikyou1997 commented on code in PR #9993:
URL: https://github.com/apache/incubator-doris/pull/9993#discussion_r891852178


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PhysicalPlanTranslator.java:
##########
@@ -0,0 +1,309 @@
+// 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.trees.plans;
+
+import org.apache.doris.analysis.AggregateInfo;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.FunctionCallExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.SortInfo;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.nereids.PlanOperatorVisitor;
+import org.apache.doris.nereids.operators.AbstractOperator;
+import org.apache.doris.nereids.operators.plans.JoinType;
+import org.apache.doris.nereids.operators.plans.physical.PhysicalAggregation;
+import org.apache.doris.nereids.operators.plans.physical.PhysicalHashJoin;
+import org.apache.doris.nereids.operators.plans.physical.PhysicalOlapScan;
+import org.apache.doris.nereids.operators.plans.physical.PhysicalOperator;
+import org.apache.doris.nereids.operators.plans.physical.PhysicalSort;
+import org.apache.doris.nereids.properties.OrderKey;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.ExpressionConverter;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan;
+import org.apache.doris.nereids.util.Utils;
+import org.apache.doris.planner.AggregationNode;
+import org.apache.doris.planner.CrossJoinNode;
+import org.apache.doris.planner.DataPartition;
+import org.apache.doris.planner.ExchangeNode;
+import org.apache.doris.planner.HashJoinNode;
+import org.apache.doris.planner.OlapScanNode;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.planner.PlanNode;
+import org.apache.doris.planner.SortNode;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+@SuppressWarnings("rawtypes")
+public class PhysicalPlanTranslator extends PlanOperatorVisitor<PlanFragment, PlanContext> {
+
+    public void translatePlan(PhysicalPlan<? extends PhysicalPlan, ? extends AbstractOperator> physicalPlan,
+            PlanContext context) {
+        visit(physicalPlan, context);
+    }
+
+    @Override
+    public PlanFragment visit(PhysicalPlan<? extends PhysicalPlan, ? extends PhysicalOperator> physicalPlan,
+            PlanContext context) {
+        PhysicalOperator<?> operator = physicalPlan.getOperator();
+        return operator.accept(this, physicalPlan, context);
+    }
+
+    @Override
+    public PlanFragment visitPhysicalAggregationPlan(
+            PhysicalPlan<? extends PhysicalPlan, ? extends PhysicalOperator> physicalPlan, PlanContext context) {
+
+        PlanFragment inputPlanFragment = visit(
+                (PhysicalPlan<? extends PhysicalPlan, ? extends PhysicalOperator>) physicalPlan.child(0), context);
+
+        AggregationNode aggregationNode = null;
+        List<Slot> slotList = physicalPlan.getOutput();
+        TupleDescriptor outputTupleDesc = generateTupleDesc(slotList, context, null);
+        PhysicalAggregation physicalAggregation = (PhysicalAggregation) physicalPlan.getOperator();
+        AggregateInfo.AggPhase phase = physicalAggregation.getAggPhase();
+
+        List<Expression> groupByExpressionList = physicalAggregation.getGroupByExprList();
+        ArrayList<Expr> execGroupingExpressions = groupByExpressionList.stream()
+                .map(e -> ExpressionConverter.converter.convert(e)).collect(Collectors.toCollection(ArrayList::new));
+
+        List<Expression> aggExpressionList = physicalAggregation.getAggExprList();
+        // TODO: agg function could be other expr type either
+        ArrayList<FunctionCallExpr> execAggExpressions = aggExpressionList.stream()
+                .map(e -> (FunctionCallExpr) ExpressionConverter.converter.convert(e))
+                .collect(Collectors.toCollection(ArrayList::new));
+
+        List<Expression> partitionExpressionList = physicalAggregation.getPartitionExprList();
+        List<Expr> execPartitionExpressions = partitionExpressionList.stream()
+                .map(e -> (FunctionCallExpr) ExpressionConverter.converter.convert(e)).collect(Collectors.toList());
+        // todo: support DISTINCT
+        AggregateInfo aggInfo = null;
+        switch (phase) {
+            case FIRST:
+                aggInfo = AggregateInfo.create(execGroupingExpressions, execAggExpressions, outputTupleDesc,
+                        outputTupleDesc, AggregateInfo.AggPhase.FIRST, context.getAnalyzer());
+                aggregationNode = new AggregationNode(context.nextNodeId(), inputPlanFragment.getPlanRoot(), aggInfo);
+                aggregationNode.unsetNeedsFinalize();
+                aggregationNode.setUseStreamingPreagg(physicalAggregation.isUsingStream());
+                aggregationNode.setIntermediateTuple();
+                if (!partitionExpressionList.isEmpty()) {
+                    inputPlanFragment.setOutputPartition(DataPartition.hashPartitioned(execPartitionExpressions));
+                }
+                break;
+            case FIRST_MERGE:
+                aggInfo = AggregateInfo.create(execGroupingExpressions, execAggExpressions, outputTupleDesc,

Review Comment:
   exactly



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