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/17 12:22:31 UTC

[GitHub] [doris] morrySnow commented on a diff in pull request #10199: [feature](nereids): cost and stats.

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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/cost/CostCalculator.java:
##########
@@ -0,0 +1,107 @@
+// 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.cost;
+
+import org.apache.doris.analysis.SlotId;
+import org.apache.doris.nereids.OperatorVisitor;
+import org.apache.doris.nereids.PlanContext;
+import org.apache.doris.nereids.memo.GroupExpression;
+import org.apache.doris.nereids.operators.Operator;
+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.PhysicalProject;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.statistics.StatsDeriveResult;
+
+import com.google.common.base.Preconditions;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Calculate the cost of a plan.
+ * Inspired by Presto.
+ */
+public class CostCalculator {
+    /**
+     * Constructor.
+     */
+    public double calculateCost(GroupExpression groupExpression) {
+        PlanContext planContext = new PlanContext(groupExpression);
+        CostEstimator costCalculator = new CostEstimator();
+        CostEstimate costEstimate = groupExpression.getOperator()
+                .accept(costCalculator, planContext);
+        return costFormula(costEstimate);
+    }
+
+    private double costFormula(CostEstimate costEstimate) {
+        double cpuCostWeight = 1;
+        double memoryCostWeight = 1;
+        double networkCostWeight = 1;
+        return costEstimate.getCpuCost() * cpuCostWeight + costEstimate.getMemoryCost() * memoryCostWeight
+                + costEstimate.getNetworkCost() * networkCostWeight;
+    }
+
+    private static class CostEstimator extends OperatorVisitor<CostEstimate, PlanContext> {
+        @Override
+        public CostEstimate visitOperator(Operator operator, PlanContext context) {
+            return CostEstimate.zero();
+        }
+
+        @Override
+        public CostEstimate visitPhysicalAggregation(PhysicalAggregation physicalAggregation, PlanContext context) {
+            StatsDeriveResult statistics = context.getStatisticsWithCheck();
+            return CostEstimate.ofCpu(statistics.computeSize());
+        }
+
+        @Override
+        public CostEstimate visitPhysicalOlapScan(PhysicalOlapScan physicalOlapScan, PlanContext context) {
+            StatsDeriveResult statistics = context.getStatisticsWithCheck();
+            return CostEstimate.ofCpu(statistics.computeSize());
+        }
+
+        @Override
+        public CostEstimate visitPhysicalHashJoin(PhysicalHashJoin physicalHashJoin, PlanContext context) {
+            Preconditions.checkState(context.getGroupExpression().arity() == 2);
+            Preconditions.checkState(context.getChildrenStats().size() == 2);
+
+            StatsDeriveResult leftStatistics = context.getChildStatistics(0);
+            StatsDeriveResult rightStatistics = context.getChildStatistics(1);
+
+            // TODO: handle some case
+
+            List<SlotId> leftIds = context.getChildOutputSlots(0).stream().map(Slot::getId).map(SlotId::create)

Review Comment:
   id in Slot should use `ExprId` instead of a int literal



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/cost/CostCalculator.java:
##########
@@ -0,0 +1,107 @@
+// 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.cost;
+
+import org.apache.doris.analysis.SlotId;
+import org.apache.doris.nereids.OperatorVisitor;
+import org.apache.doris.nereids.PlanContext;
+import org.apache.doris.nereids.memo.GroupExpression;
+import org.apache.doris.nereids.operators.Operator;
+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.PhysicalProject;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.statistics.StatsDeriveResult;
+
+import com.google.common.base.Preconditions;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Calculate the cost of a plan.
+ * Inspired by Presto.
+ */
+public class CostCalculator {
+    /**
+     * Constructor.
+     */
+    public double calculateCost(GroupExpression groupExpression) {
+        PlanContext planContext = new PlanContext(groupExpression);
+        CostEstimator costCalculator = new CostEstimator();
+        CostEstimate costEstimate = groupExpression.getOperator()
+                .accept(costCalculator, planContext);
+        return costFormula(costEstimate);
+    }
+
+    private double costFormula(CostEstimate costEstimate) {
+        double cpuCostWeight = 1;
+        double memoryCostWeight = 1;
+        double networkCostWeight = 1;
+        return costEstimate.getCpuCost() * cpuCostWeight + costEstimate.getMemoryCost() * memoryCostWeight
+                + costEstimate.getNetworkCost() * networkCostWeight;
+    }
+
+    private static class CostEstimator extends OperatorVisitor<CostEstimate, PlanContext> {
+        @Override
+        public CostEstimate visitOperator(Operator operator, PlanContext context) {
+            return CostEstimate.zero();
+        }
+
+        @Override
+        public CostEstimate visitPhysicalAggregation(PhysicalAggregation physicalAggregation, PlanContext context) {
+            StatsDeriveResult statistics = context.getStatisticsWithCheck();
+            return CostEstimate.ofCpu(statistics.computeSize());
+        }
+
+        @Override
+        public CostEstimate visitPhysicalOlapScan(PhysicalOlapScan physicalOlapScan, PlanContext context) {
+            StatsDeriveResult statistics = context.getStatisticsWithCheck();
+            return CostEstimate.ofCpu(statistics.computeSize());
+        }
+
+        @Override
+        public CostEstimate visitPhysicalHashJoin(PhysicalHashJoin physicalHashJoin, PlanContext context) {
+            Preconditions.checkState(context.getGroupExpression().arity() == 2);
+            Preconditions.checkState(context.getChildrenStats().size() == 2);
+
+            StatsDeriveResult leftStatistics = context.getChildStatistics(0);
+            StatsDeriveResult rightStatistics = context.getChildStatistics(1);
+
+            // TODO: handle some case
+
+            List<SlotId> leftIds = context.getChildOutputSlots(0).stream().map(Slot::getId).map(SlotId::create)
+                    .collect(Collectors.toList());
+            List<SlotId> rightIds = context.getChildOutputSlots(1).stream().map(Slot::getId).map(SlotId::create)

Review Comment:
   StatsDeriveResult will be modified to adjust for Nereids. After that, it could use both SlotId and ExprId as Column key



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