forked from apache/datafusion-comet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendedExplainInfo.scala
More file actions
229 lines (206 loc) · 7.35 KB
/
ExtendedExplainInfo.scala
File metadata and controls
229 lines (206 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* 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.comet
import scala.collection.mutable
import org.apache.spark.sql.ExtendedExplainGenerator
import org.apache.spark.sql.catalyst.trees.{TreeNode, TreeNodeTag}
import org.apache.spark.sql.comet.{CometColumnarToRowExec, CometNativeColumnarToRowExec, CometPlan, CometSparkToColumnarExec}
import org.apache.spark.sql.execution.{ColumnarToRowExec, InputAdapter, RowToColumnarExec, SparkPlan, WholeStageCodegenExec}
import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanExec, AQEShuffleReadExec, QueryStageExec}
import org.apache.spark.sql.execution.exchange.ReusedExchangeExec
import org.apache.comet.CometExplainInfo.getActualPlan
class ExtendedExplainInfo extends ExtendedExplainGenerator {
override def title: String = "Comet"
def generateExtendedInfo(plan: SparkPlan): String = {
CometConf.COMET_EXTENDED_EXPLAIN_FORMAT.get() match {
case CometConf.COMET_EXTENDED_EXPLAIN_FORMAT_VERBOSE =>
// Generates the extended info in a verbose manner, printing each node along with the
// extended information in a tree display.
val planStats = new CometCoverageStats()
val outString = new StringBuilder()
generateTreeString(getActualPlan(plan), 0, Seq(), 0, outString, planStats)
s"${outString.toString()}\n$planStats"
case CometConf.COMET_EXTENDED_EXPLAIN_FORMAT_FALLBACK =>
// Generates the extended info as a list of fallback reasons
getFallbackReasons(plan).mkString("\n").trim
}
}
def getFallbackReasons(plan: SparkPlan): Seq[String] = {
extensionInfo(plan).toSeq.sorted
}
private[comet] def extensionInfo(node: TreeNode[_]): Set[String] = {
var info = mutable.Seq[String]()
val sorted = sortup(node)
sorted.foreach { p =>
val all: Set[String] =
getActualPlan(p).getTagValue(CometExplainInfo.EXTENSION_INFO).getOrElse(Set.empty[String])
for (s <- all) {
info = info :+ s
}
}
info.toSet
}
// get all plan nodes, breadth first traversal, then returned the reversed list so
// leaf nodes are first
private def sortup(node: TreeNode[_]): mutable.Queue[TreeNode[_]] = {
val ordered = new mutable.Queue[TreeNode[_]]()
val traversed = mutable.Queue[TreeNode[_]](getActualPlan(node))
while (traversed.nonEmpty) {
val s = traversed.dequeue()
ordered += s
if (s.innerChildren.nonEmpty) {
s.innerChildren.foreach {
case c @ (_: TreeNode[_]) => traversed.enqueue(getActualPlan(c))
case _ =>
}
}
if (s.children.nonEmpty) {
s.children.foreach {
case c @ (_: TreeNode[_]) => traversed.enqueue(getActualPlan(c))
case _ =>
}
}
}
ordered.reverse
}
// Simplified generateTreeString from Spark TreeNode. Appends explain info to the node if any
def generateTreeString(
node: TreeNode[_],
depth: Int,
lastChildren: Seq[Boolean],
indent: Int,
outString: StringBuilder,
planStats: CometCoverageStats): Unit = {
node match {
case _: AdaptiveSparkPlanExec | _: InputAdapter | _: QueryStageExec |
_: WholeStageCodegenExec | _: ReusedExchangeExec | _: AQEShuffleReadExec =>
// ignore
case _: RowToColumnarExec | _: ColumnarToRowExec | _: CometColumnarToRowExec |
_: CometNativeColumnarToRowExec | _: CometSparkToColumnarExec =>
planStats.transitions += 1
case _: CometPlan =>
planStats.cometOperators += 1
case _ =>
planStats.sparkOperators += 1
}
outString.append(" " * indent)
if (depth > 0) {
lastChildren.init.foreach { isLast =>
outString.append(if (isLast) " " else ": ")
}
outString.append(if (lastChildren.last) "+- " else ":- ")
}
val tagValue = node.getTagValue(CometExplainInfo.EXTENSION_INFO)
val str = if (tagValue.nonEmpty) {
s" ${node.nodeName} [COMET: ${tagValue.get.mkString(", ")}]"
} else {
node.nodeName
}
outString.append(str)
outString.append("\n")
val innerChildrenLocal = node.innerChildren
if (innerChildrenLocal.nonEmpty) {
innerChildrenLocal.init.foreach {
case c @ (_: TreeNode[_]) =>
generateTreeString(
getActualPlan(c),
depth + 2,
lastChildren :+ node.children.isEmpty :+ false,
indent,
outString,
planStats)
case _ =>
}
generateTreeString(
getActualPlan(innerChildrenLocal.last),
depth + 2,
lastChildren :+ node.children.isEmpty :+ true,
indent,
outString,
planStats)
}
if (node.children.nonEmpty) {
node.children.init.foreach {
case c @ (_: TreeNode[_]) =>
generateTreeString(
getActualPlan(c),
depth + 1,
lastChildren :+ false,
indent,
outString,
planStats)
case _ =>
}
node.children.last match {
case c @ (_: TreeNode[_]) =>
generateTreeString(
getActualPlan(c),
depth + 1,
lastChildren :+ true,
indent,
outString,
planStats)
case _ =>
}
}
}
}
class CometCoverageStats {
var sparkOperators: Int = 0
var cometOperators: Int = 0
var transitions: Int = 0
override def toString(): String = {
val eligible = sparkOperators + cometOperators
val converted =
if (eligible == 0) 0.0 else cometOperators.toDouble / eligible * 100.0
s"Comet accelerated $cometOperators out of $eligible " +
s"eligible operators (${converted.toInt}%). " +
s"Final plan contains $transitions transitions between Spark and Comet."
}
}
object CometCoverageStats {
/**
* Compute coverage stats for a plan without generating explain string.
*/
def forPlan(plan: SparkPlan): CometCoverageStats = {
val stats = new CometCoverageStats()
val explainInfo = new ExtendedExplainInfo()
explainInfo.generateTreeString(
CometExplainInfo.getActualPlan(plan),
0,
Seq(),
0,
new StringBuilder(),
stats)
stats
}
}
object CometExplainInfo {
val EXTENSION_INFO = new TreeNodeTag[Set[String]]("CometExtensionInfo")
def getActualPlan(node: TreeNode[_]): TreeNode[_] = {
node match {
case p: AdaptiveSparkPlanExec => getActualPlan(p.executedPlan)
case p: InputAdapter => getActualPlan(p.child)
case p: QueryStageExec => getActualPlan(p.plan)
case p: WholeStageCodegenExec => getActualPlan(p.child)
case p: ReusedExchangeExec => getActualPlan(p.child)
case p => p
}
}
}