chunweilei commented on a change in pull request #2128:
URL: https://github.com/apache/calcite/pull/2128#discussion_r481577477



##########
File path: 
core/src/main/java/org/apache/calcite/rel/externalize/RelDotWriter.java
##########
@@ -0,0 +1,210 @@
+/*
+ * 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.rel.externalize;
+
+import org.apache.calcite.plan.hep.HepRelVertex;
+import org.apache.calcite.plan.volcano.RelSubset;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelWriter;
+import org.apache.calcite.sql.SqlExplainLevel;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Util;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Multimap;
+
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Utility to dump a rel node plan in dot format.
+ */
+public class RelDotWriter extends RelWriterImpl {
+
+  //~ Instance fields --------------------------------------------------------
+
+  /**
+   * Adjacent list of the plan graph.
+   */
+  private final Map<RelNode, List<RelNode>> outArcTable = new 
LinkedHashMap<>();
+
+  private Map<RelNode, String> nodeLabels = new HashMap<>();
+
+  private Multimap<RelNode, String> nodeStyles = HashMultimap.create();
+
+  private final WriteOption option;
+
+  //~ Constructors -----------------------------------------------------------
+
+  public RelDotWriter(
+      PrintWriter pw, SqlExplainLevel detailLevel,
+      boolean withIdPrefix) {
+    this(pw, detailLevel, withIdPrefix, new WriteOption());
+  }
+
+  public RelDotWriter(
+      PrintWriter pw, SqlExplainLevel detailLevel,
+      boolean withIdPrefix, WriteOption option) {
+    super(pw, detailLevel, withIdPrefix);
+    this.option = option;
+  }
+
+  //~ Methods ----------------------------------------------------------------
+
+  protected void explain_(RelNode rel,
+                          List<Pair<String, Object>> values) {
+    // get inputs
+    List<RelNode> inputs = getInputs(rel);
+    outArcTable.put(rel, inputs);
+
+    // generate node label
+    StringBuilder relDesc = new StringBuilder();
+    populateRelNodeLabel(rel, values, relDesc);
+    nodeLabels.put(rel, option.formatNodeLabel(relDesc.toString()));
+
+    if (option.highlightNode(rel)) {
+      nodeStyles.put(rel, "bold");
+    }
+
+    explainInputs(inputs);
+  }
+
+  private List<RelNode> getInputs(RelNode parent) {
+    return parent.getInputs().stream().map(child -> {
+      if (child instanceof HepRelVertex) {
+        return ((HepRelVertex) child).getCurrentRel();
+      } else if (child instanceof RelSubset) {
+        RelSubset subset = (RelSubset) child;
+        return Util.first(subset.getBest(), subset.getOriginal());
+      } else {
+        return child;
+      }
+    }).collect(Collectors.toList());
+  }
+
+  private void explainInputs(List<RelNode> inputs) {
+    for (RelNode input : inputs) {
+      if (input == null || nodeLabels.containsKey(input)) {
+        continue;
+      }
+      input.explain(this);
+    }
+  }
+
+  public RelWriter done(RelNode node) {
+    int numOfVisitedNodes = nodeLabels.size();
+    super.done(node);
+    if (numOfVisitedNodes == 0) {
+      // When we enter this method call, no node
+      // has been visited. So the current node must be the root of the plan.
+      // Now we are exiting the method, all nodes in the plan
+      // have been visited, so it is time to dump the plan
+

Review comment:
       Minor: it is time to dump the plan -> `it is time to dump the plan.`

##########
File path: core/src/test/java/org/apache/calcite/plan/RelWriterTest.java
##########
@@ -658,14 +658,25 @@
     final RelJsonWriter jsonWriter = new RelJsonWriter();
     rel.explain(jsonWriter);
     final String relJson = jsonWriter.asString();
-    String s = deserializeAndDumpToTextFormat(getSchema(rel), relJson);
-    final String expected = ""
+    String s = deserializeAndDumpToTextFormat(getSchema(rel), relJson, format);
+    assertThat(s, isLinux(expected));
+  }
+
+  @Test void testAggregateWithAlias() {
+    testAggregateWithAlias0(SqlExplainFormat.TEXT, ""
         + "LogicalProject(max_sal=[$1])\n"
         + "  LogicalAggregate(group=[{0}], max_sal=[MAX($1)])\n"
         + "    LogicalProject(JOB=[$2], SAL=[$5])\n"
-        + "      LogicalTableScan(table=[[scott, EMP]])\n";
+        + "      LogicalTableScan(table=[[scott, EMP]])\n");
 
-    assertThat(s, isLinux(expected));
+    testAggregateWithAlias0(SqlExplainFormat.DOT, "digraph {\n"
+        + "\"LogicalProject(max_s\\nal=[$1])\" -> 
\"LogicalAggregate(gro\\nup=[{0}], "
+        + "max_sal=[M\\nAX($1)])\" [label=\"0\"]\n"
+        + "\"LogicalAggregate(gro\\nup=[{0}], max_sal=[M\\nAX($1)])\" -> 
\"LogicalProject"

Review comment:
       Text such as `LogicalAggregate(gro\\nup=[{0}]` and 
`LogicalTableScan(tab\\nle=` is a little weird. Maybe we can change it to 
LogicalAggregate\n(group=[{0}]`. What do you think?




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