FatLittle commented on a change in pull request #1991:
URL: https://github.com/apache/calcite/pull/1991#discussion_r433051546



##########
File path: 
core/src/main/java/org/apache/calcite/plan/volcano/TopDownRuleDriver.java
##########
@@ -0,0 +1,812 @@
+/*
+ * 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.calcite.plan.volcano;
+
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.rel.PhysicalNode;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.convert.ConverterRule;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.trace.CalciteTrace;
+
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.Stack;
+import java.util.function.Predicate;
+
+class TopDownRuleDriver {
+
+  /**
+   * the Logger
+   */
+  private static final Logger LOGGER = CalciteTrace.getPlannerTaskTracer();
+
+  /**
+   * The planner
+   */
+  private final VolcanoPlanner planner;
+
+  /**
+   * the rule queue designed for top-down rule applying
+   */
+  private final CascadeRuleQueue ruleQueue;
+
+  /**
+   * all tasks waiting for execution
+   */
+  private Stack<Task> tasks = new Stack<>();
+
+  /**
+   * A task that is currently applying and may generate new RelNode.
+   * It provides a callback to schedule tasks for new RelNodes that
+   * are registered during task performing
+   */
+  private GeneratorTask applying = null;
+
+  /**
+   * relnodes that are generated by passThrough or derive
+   * these nodes will not takes part in another passThrough or derive
+   */
+  private Set<Integer> passThroughCache = new HashSet<>();
+
+  //~ Constructors -----------------------------------------------------------
+
+  TopDownRuleDriver(VolcanoPlanner planner) {
+    this.planner = planner;
+    ruleQueue = new CascadeRuleQueue(planner);
+  }
+
+  //~ Methods ----------------------------------------------------------------
+
+  void execute() {
+    TaskDescriptor description = new TaskDescriptor();
+    tasks.push(new OptimizeGroup(planner.root, planner.infCost));
+    exploreMaterializationRoots();
+    while (!tasks.isEmpty()) {
+      Task task = tasks.pop();
+      description.log(task);
+      task.perform();
+    }
+  }
+
+  private void exploreMaterializationRoots() {
+    for (RelSubset extraRoot : planner.explorationRoots) {
+      RelSet rootSet = VolcanoPlanner.equivRoot(extraRoot.set);
+      if (rootSet == planner.root.set) {
+        continue;
+      }
+      for (RelNode rel : extraRoot.set.rels) {
+        if (planner.isLogical(rel)) {
+          tasks.push(new OptimizeMExpr(rel, extraRoot, true));
+        }
+      }
+    }
+  }
+
+  public CascadeRuleQueue ruleQueue() {
+    return ruleQueue;
+  }
+
+  public void reset() {
+    ruleQueue.clear();
+    tasks.clear();
+    passThroughCache.clear();
+    applying = null;
+  }
+
+  private interface Procedure {
+    void exec();
+  }
+
+  private void applyGenerator(GeneratorTask task, Procedure proc) {
+    GeneratorTask applying = this.applying;
+    this.applying = task;
+    try {
+      proc.exec();
+    } finally {
+      this.applying = applying;
+    }
+  }
+
+  void onSetMerged(RelSet set) {
+    applyGenerator(null, () -> clearProcessed(set));
+  }
+
+  private void clearProcessed(RelSet set) {
+    boolean explored = set.exploringState != null;
+    set.exploringState = null;
+
+    for (RelSubset subset : set.subsets) {
+      if (subset.resetOptimizing() || explored) {
+        Collection<RelNode> parentRels = subset.getParentRels();
+        for (RelNode parentRel : parentRels) {
+          clearProcessed(planner.getSet(parentRel));
+        }
+        if (subset == planner.root) {
+          tasks.push(new OptimizeGroup(subset, planner.infCost));

Review comment:
       Task queue cannot be cleared here, because the RuleMatch is already 
popped from rule queue during ApplyRules. So ApplyRule tasks in the queue 
cannot be reproduced after cleaning.
   
   The newly added task may rescan the root RelSet and schedule tasks for new 
relnodes. Duplicate tasks can be avoided as input RelSubsets still hold the 
previous optimizing status. 
   
   The only problem is that it may affect the rule applying sequence. 




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to