Github user andrewor14 commented on a diff in the pull request:
https://github.com/apache/spark/pull/7774#discussion_r36132403
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/ui/SparkPlanGraph.scala ---
@@ -0,0 +1,111 @@
+/*
+ * 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.spark.sql.ui
+
+import java.util.concurrent.atomic.AtomicLong
+
+import scala.collection.mutable
+
+import org.apache.spark.AccumulatorParam
+import org.apache.spark.sql.execution.SparkPlan
+
+/**
+ * A graph used for storing information of an executionPlan of DataFrame.
+ *
+ * Each graph is defined with a set of nodes and a set of edges. Each node
represents a node in the
+ * SparkPlan tree, and each edge represents a parent-child relationship
between two nodes.
+ */
+private[ui] case class SparkPlanGraph(
+ nodes: Seq[SparkPlanGraphNode], edges: Seq[SparkPlanGraphEdge]) {
+
+ def makeDotFile(metrics: Map[Long, Any]): String = {
+ val dotFile = new StringBuilder
+ dotFile.append("digraph G {\n")
+ nodes.foreach(node => dotFile.append(node.makeDotNode(metrics) + "\n"))
+ edges.foreach(edge => dotFile.append(edge.makeDotEdge + "\n"))
+ dotFile.append("}")
+ dotFile.toString()
+ }
+}
+
+private[ui] object SparkPlanGraph {
+
+ /**
+ * Build a SparkPlanGraph from the root of a SparkPlan tree.
+ */
+ def apply(plan: SparkPlan): SparkPlanGraph = {
+ val nodeIdGenerator = new AtomicLong(0)
+ val nodes = mutable.ArrayBuffer[SparkPlanGraphNode]()
+ val edges = mutable.ArrayBuffer[SparkPlanGraphEdge]()
+ buildSparkPlanGraphNode(plan, nodeIdGenerator, nodes, edges)
+ new SparkPlanGraph(nodes, edges)
+ }
+
+ private def buildSparkPlanGraphNode(
+ plan: SparkPlan,
+ nodeIdGenerator: AtomicLong,
+ nodes: mutable.ArrayBuffer[SparkPlanGraphNode],
+ edges: mutable.ArrayBuffer[SparkPlanGraphEdge]): SparkPlanGraphNode
= {
+ val metrics = plan.accumulators.toSeq.map { case (key, accumulator) =>
+ SQLPlanMetric(accumulator.name.getOrElse(key), accumulator.id,
+ accumulator.param.asInstanceOf[AccumulatorParam[Any]])
+ }
+ val node = SparkPlanGraphNode(
+ nodeIdGenerator.getAndIncrement(), plan.nodeName, plan.simpleString,
metrics)
+ nodes += node
+ val childrenNodes = plan.children.map(
+ child => buildSparkPlanGraphNode(child, nodeIdGenerator, nodes,
edges))
+ for (child <- childrenNodes) {
+ edges += SparkPlanGraphEdge(child.id, node.id)
+ }
+ node
+ }
+}
+
+/**
+ * Represent a node in the SparkPlan tree, along with its metrics.
+ *
+ * @param id generated by "SparkPlanGraph". There is no duplicate id in a
graph
+ * @param name the name of this SparkPlan node
+ * @param metrics metrics that this SparkPlan node will track
+ */
+private[ui] case class SparkPlanGraphNode(
+ id: Long, name: String, desc: String, metrics: Seq[SQLPlanMetric]) {
+
+ def makeDotNode(metricsValue: Map[Long, Any]): String = {
+ val values =
+ for (metric <- metrics;
+ value <- metricsValue.get(metric.accumulatorId))
+ yield metric.name + ": " + value
--- End diff --
style: need `{ } around the for loop body
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]