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 @@ -21,6 +21,7 @@ import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.trees.{CurrentOrigin, TreeNode}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{DataType, StructType}
import org.apache.spark.sql.AnalysisException

abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] extends TreeNode[PlanType] {
self: PlanType =>
Expand Down Expand Up @@ -267,6 +268,21 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] extends TreeNode[PlanT
* All the attributes that are used for this plan.
*/
lazy val allAttributes: AttributeSeq = children.flatMap(_.output)

/**
* Converts the query plan to string and appends it via provided function.
*/
def appendOrError(
append: String => Unit,
verbose: Boolean,
addSuffix: Boolean,
maxFields: Int = SQLConf.get.maxToStringFields): Unit = {
try {
treeString(append, verbose, addSuffix, maxFields)
} catch {
case e: AnalysisException => append(e.toString)
}
}
}

object QueryPlan extends PredicateHelper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,38 +196,31 @@ class QueryExecution(
val rope = new StringRope()

rope.append("== Physical Plan ==\n")
appendOrError(rope.append)(
executedPlan.treeString(_, false, false, SQLConf.get.maxToStringFields))
executedPlan.appendOrError(rope.append, verbose = false, addSuffix = false)
rope.append("\n")

rope.toString
}

private def appendOrError(append: String => Unit)(f: (String => Unit) => Unit): Unit = {
try f(append)
catch {
case e: AnalysisException => append(e.toString)
}
}

private def writePlans(append: String => Unit, maxFields: Int): Unit = {
def stringOrError[A](f: => A): String = {
try f.toString catch { case e: AnalysisException => e.toString }
}
val (verbose, addSuffix) = (true, false)

append("== Parsed Logical Plan ==\n")
appendOrError(append)(logical.treeString(_, verbose, addSuffix, maxFields))
logical.appendOrError(append, verbose, addSuffix, maxFields)
append("\n== Analyzed Logical Plan ==\n")
val analyzedOutput = stringOrError(truncatedString(
analyzed.output.map(o => s"${o.name}: ${o.dataType.simpleString}"), ", ", maxFields))
val analyzedOutput = try {
truncatedString(
analyzed.output.map(o => s"${o.name}: ${o.dataType.simpleString}"), ", ", maxFields)
} catch {
case e: AnalysisException => e.toString
}
append(analyzedOutput)
append("\n")
appendOrError(append)(analyzed.treeString(_, verbose, addSuffix, maxFields))
analyzed.appendOrError(append, verbose, addSuffix, maxFields)
append("\n== Optimized Logical Plan ==\n")
appendOrError(append)(optimizedPlan.treeString(_, verbose, addSuffix, maxFields))
optimizedPlan.appendOrError(append, verbose, addSuffix, maxFields)
append("\n== Physical Plan ==\n")
appendOrError(append)(executedPlan.treeString(_, verbose, addSuffix, maxFields))
executedPlan.appendOrError(append, verbose, addSuffix, maxFields)
}

override def toString: String = withRedaction {
Expand All @@ -239,16 +232,15 @@ class QueryExecution(

def stringWithStats: String = withRedaction {
val rope = new StringRope()
val maxFields = SQLConf.get.maxToStringFields

// trigger to compute stats for logical plans
optimizedPlan.stats

// only show optimized logical plan and physical plan
rope.append("== Optimized Logical Plan ==\n")
appendOrError(rope.append)(optimizedPlan.treeString(_, true, true, maxFields))
optimizedPlan.appendOrError(rope.append, verbose = true, addSuffix = true)
rope.append("\n== Physical Plan ==\n")
appendOrError(rope.append)(executedPlan.treeString(_, true, false, maxFields))
executedPlan.appendOrError(rope.append, verbose = true, addSuffix = false)
rope.append("\n")

rope.toString
Expand Down