Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,37 +200,37 @@ class QueryExecution(val sparkSession: SparkSession, val logical: LogicalPlan) {
""".stripMargin.trim
}

override def toString: String = completeString(appendStats = false)

def toStringWithStats: String = completeString(appendStats = true)

private def completeString(appendStats: Boolean): String = {
override def toString: String = {
def output = Utils.truncatedString(
analyzed.output.map(o => s"${o.name}: ${o.dataType.simpleString}"), ", ")
val analyzedPlan = Seq(
stringOrError(output),
stringOrError(analyzed.treeString(verbose = true))
).filter(_.nonEmpty).mkString("\n")

val optimizedPlanString = if (appendStats) {
// trigger to compute stats for logical plans
optimizedPlan.stats(sparkSession.sessionState.conf)
optimizedPlan.treeString(verbose = true, addSuffix = true)
} else {
optimizedPlan.treeString(verbose = true)
}

s"""== Parsed Logical Plan ==
|${stringOrError(logical.treeString(verbose = true))}
|== Analyzed Logical Plan ==
|$analyzedPlan
|== Optimized Logical Plan ==
|${stringOrError(optimizedPlanString)}
|${stringOrError(optimizedPlan.treeString(verbose = true))}
|== Physical Plan ==
|${stringOrError(executedPlan.treeString(verbose = true))}
""".stripMargin.trim
}

def stringWithStats: String = {
// trigger to compute stats for logical plans
optimizedPlan.stats(sparkSession.sessionState.conf)

// only show optimized logical plan and physical plan
s"""== Optimized Logical Plan ==
|${stringOrError(optimizedPlan.treeString(verbose = true, addSuffix = true))}
|== Physical Plan ==
|${stringOrError(executedPlan.treeString(verbose = true))}
""".stripMargin.trim
}

/** A special namespace for commands that can be used to debug query execution. */
// scalastyle:off
object debug {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ case class ExplainCommand(
} else if (extended) {
queryExecution.toString
} else if (cost) {
queryExecution.toStringWithStats
queryExecution.stringWithStats
} else {
queryExecution.simpleString
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class HiveExplainSuite extends QueryTest with SQLTestUtils with TestHiveSingleto
import testImplicits._

test("show cost in explain command") {
// For readability, we only show optimized plan and physical plan in explain cost command
checkKeywordsExist(sql("EXPLAIN COST SELECT * FROM src "),
"Optimized Logical Plan", "Physical Plan")
checkKeywordsNotExist(sql("EXPLAIN COST SELECT * FROM src "),
"Parsed Logical Plan", "Analyzed Logical Plan")

// Only has sizeInBytes before ANALYZE command
checkKeywordsExist(sql("EXPLAIN COST SELECT * FROM src "), "sizeInBytes")
checkKeywordsNotExist(sql("EXPLAIN COST SELECT * FROM src "), "rowCount")
Expand Down