You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by "morrySnow (via GitHub)" <gi...@apache.org> on 2023/06/14 02:33:01 UTC

[GitHub] [doris] morrySnow commented on a diff in pull request #20746: [feature](Nereids) add cbo rewrite framework

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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/CostBasedRewriteJob.java:
##########
@@ -0,0 +1,90 @@
+// 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.jobs.rewrite;
+
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.CascadesContext;
+import org.apache.doris.nereids.cost.Cost;
+import org.apache.doris.nereids.jobs.JobContext;
+import org.apache.doris.nereids.jobs.executor.Optimizer;
+import org.apache.doris.nereids.jobs.executor.Rewriter;
+import org.apache.doris.nereids.memo.GroupExpression;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Cost based rewrite job.
+ * This job do
+ */
+public class CostBasedRewriteJob implements RewriteJob {
+
+    private final List<RewriteJob> rewriteJobs;
+
+    public CostBasedRewriteJob(List<RewriteJob> rewriteJobs) {
+        this.rewriteJobs = rewriteJobs;
+        // need to generate real rewrite job list
+    }
+
+    @Override
+    public void execute(JobContext jobContext) {
+        CascadesContext cascadesContext = jobContext.getCascadesContext();
+        CascadesContext skipCboRuleCtx = CascadesContext.newRewriteContext(
+                cascadesContext, cascadesContext.getRewritePlan(),
+                cascadesContext.getCurrentJobContext().getRequiredProperties());
+        CascadesContext applyCboRuleCtx = CascadesContext.newRewriteContext(
+                cascadesContext, cascadesContext.getRewritePlan(),
+                cascadesContext.getCurrentJobContext().getRequiredProperties());
+
+        // Do rewrite on 2 candidates
+        List<RewriteJob> withCboJobs = ImmutableList.<RewriteJob>builder()
+                .addAll(rewriteJobs)
+                .addAll(jobContext.getRemainJobs())
+                .build();
+        new Rewriter(skipCboRuleCtx, jobContext.getRemainJobs()).execute();
+        new Rewriter(applyCboRuleCtx, withCboJobs).execute();
+        if (skipCboRuleCtx.getRewritePlan().deepEquals(applyCboRuleCtx.getRewritePlan())) {
+            // this means rewrite do not do anything
+            return;
+        }
+        // Do optimize on 2 candidates
+        new Optimizer(skipCboRuleCtx).execute();
+        new Optimizer(applyCboRuleCtx).execute();
+        Optional<Pair<Cost, GroupExpression>> skipCboRuleCost = skipCboRuleCtx.getMemo().getRoot()
+                .getLowestCostPlan(PhysicalProperties.GATHER);
+        Optional<Pair<Cost, GroupExpression>> appliedCboRuleCost = applyCboRuleCtx.getMemo().getRoot()
+                .getLowestCostPlan(PhysicalProperties.GATHER);
+        // If one of them optimize failed, just return
+        if (!skipCboRuleCost.isPresent() || !appliedCboRuleCost.isPresent()) {
+            return;

Review Comment:
   right, wo should do some log here



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