Skip to content

Commit 6de41e9

Browse files
wzhfycloud-fan
authored andcommitted
[SPARK-17078][SQL][FOLLOWUP] Simplify explain cost command
## What changes were proposed in this pull request? Usually when using explain cost command, users want to see the stats of plan. Since stats is only showed in optimized plan, it is more direct and convenient to include only optimized plan and physical plan in the output. ## How was this patch tested? Enhanced existing test. Author: Zhenhua Wang <[email protected]> Closes #18190 from wzhfy/simplifyExplainCost.
1 parent 0eb1fc6 commit 6de41e9

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/QueryExecution.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,37 +200,37 @@ class QueryExecution(val sparkSession: SparkSession, val logical: LogicalPlan) {
200200
""".stripMargin.trim
201201
}
202202

203-
override def toString: String = completeString(appendStats = false)
204-
205-
def toStringWithStats: String = completeString(appendStats = true)
206-
207-
private def completeString(appendStats: Boolean): String = {
203+
override def toString: String = {
208204
def output = Utils.truncatedString(
209205
analyzed.output.map(o => s"${o.name}: ${o.dataType.simpleString}"), ", ")
210206
val analyzedPlan = Seq(
211207
stringOrError(output),
212208
stringOrError(analyzed.treeString(verbose = true))
213209
).filter(_.nonEmpty).mkString("\n")
214210

215-
val optimizedPlanString = if (appendStats) {
216-
// trigger to compute stats for logical plans
217-
optimizedPlan.stats(sparkSession.sessionState.conf)
218-
optimizedPlan.treeString(verbose = true, addSuffix = true)
219-
} else {
220-
optimizedPlan.treeString(verbose = true)
221-
}
222-
223211
s"""== Parsed Logical Plan ==
224212
|${stringOrError(logical.treeString(verbose = true))}
225213
|== Analyzed Logical Plan ==
226214
|$analyzedPlan
227215
|== Optimized Logical Plan ==
228-
|${stringOrError(optimizedPlanString)}
216+
|${stringOrError(optimizedPlan.treeString(verbose = true))}
229217
|== Physical Plan ==
230218
|${stringOrError(executedPlan.treeString(verbose = true))}
231219
""".stripMargin.trim
232220
}
233221

222+
def stringWithStats: String = {
223+
// trigger to compute stats for logical plans
224+
optimizedPlan.stats(sparkSession.sessionState.conf)
225+
226+
// only show optimized logical plan and physical plan
227+
s"""== Optimized Logical Plan ==
228+
|${stringOrError(optimizedPlan.treeString(verbose = true, addSuffix = true))}
229+
|== Physical Plan ==
230+
|${stringOrError(executedPlan.treeString(verbose = true))}
231+
""".stripMargin.trim
232+
}
233+
234234
/** A special namespace for commands that can be used to debug query execution. */
235235
// scalastyle:off
236236
object debug {

sql/core/src/main/scala/org/apache/spark/sql/execution/command/commands.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ case class ExplainCommand(
127127
} else if (extended) {
128128
queryExecution.toString
129129
} else if (cost) {
130-
queryExecution.toStringWithStats
130+
queryExecution.stringWithStats
131131
} else {
132132
queryExecution.simpleString
133133
}

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveExplainSuite.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ class HiveExplainSuite extends QueryTest with SQLTestUtils with TestHiveSingleto
2929
import testImplicits._
3030

3131
test("show cost in explain command") {
32+
// For readability, we only show optimized plan and physical plan in explain cost command
33+
checkKeywordsExist(sql("EXPLAIN COST SELECT * FROM src "),
34+
"Optimized Logical Plan", "Physical Plan")
35+
checkKeywordsNotExist(sql("EXPLAIN COST SELECT * FROM src "),
36+
"Parsed Logical Plan", "Analyzed Logical Plan")
37+
3238
// Only has sizeInBytes before ANALYZE command
3339
checkKeywordsExist(sql("EXPLAIN COST SELECT * FROM src "), "sizeInBytes")
3440
checkKeywordsNotExist(sql("EXPLAIN COST SELECT * FROM src "), "rowCount")

0 commit comments

Comments
 (0)