Skip to content
Snippets Groups Projects
Commit 7d4aba18 authored by gatorsmile's avatar gatorsmile Committed by Michael Armbrust
Browse files

[SPARK-11848][SQL] Support EXPLAIN in DataSet APIs

When debugging DataSet API, I always need to print the logical and physical plans.

I am wondering if we should provide a simple API for EXPLAIN?

Author: gatorsmile <gatorsmile@gmail.com>

Closes #9832 from gatorsmile/explainDS.
parent 276a7e13
No related branches found
No related tags found
No related merge requests found
...@@ -37,7 +37,7 @@ import org.apache.spark.sql.catalyst.expressions.aggregate._ ...@@ -37,7 +37,7 @@ import org.apache.spark.sql.catalyst.expressions.aggregate._
import org.apache.spark.sql.catalyst.plans.logical._ import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.plans.{Inner, JoinType} import org.apache.spark.sql.catalyst.plans.{Inner, JoinType}
import org.apache.spark.sql.catalyst.{CatalystTypeConverters, ScalaReflection, SqlParser} import org.apache.spark.sql.catalyst.{CatalystTypeConverters, ScalaReflection, SqlParser}
import org.apache.spark.sql.execution.{EvaluatePython, ExplainCommand, FileRelation, LogicalRDD, QueryExecution, Queryable, SQLExecution} import org.apache.spark.sql.execution.{EvaluatePython, FileRelation, LogicalRDD, QueryExecution, Queryable, SQLExecution}
import org.apache.spark.sql.execution.datasources.{CreateTableUsingAsSelect, LogicalRelation} import org.apache.spark.sql.execution.datasources.{CreateTableUsingAsSelect, LogicalRelation}
import org.apache.spark.sql.execution.datasources.json.JacksonGenerator import org.apache.spark.sql.execution.datasources.json.JacksonGenerator
import org.apache.spark.sql.sources.HadoopFsRelation import org.apache.spark.sql.sources.HadoopFsRelation
...@@ -308,27 +308,6 @@ class DataFrame private[sql]( ...@@ -308,27 +308,6 @@ class DataFrame private[sql](
def printSchema(): Unit = println(schema.treeString) def printSchema(): Unit = println(schema.treeString)
// scalastyle:on println // scalastyle:on println
/**
* Prints the plans (logical and physical) to the console for debugging purposes.
* @group basic
* @since 1.3.0
*/
def explain(extended: Boolean): Unit = {
val explain = ExplainCommand(queryExecution.logical, extended = extended)
withPlan(explain).queryExecution.executedPlan.executeCollect().foreach {
// scalastyle:off println
r => println(r.getString(0))
// scalastyle:on println
}
}
/**
* Only prints the physical plan to the console for debugging purposes.
* @group basic
* @since 1.3.0
*/
def explain(): Unit = explain(extended = false)
/** /**
* Returns true if the `collect` and `take` methods can be run locally * Returns true if the `collect` and `take` methods can be run locally
* (without any Spark executors). * (without any Spark executors).
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.apache.spark.sql.execution package org.apache.spark.sql.execution
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.types.StructType import org.apache.spark.sql.types.StructType
import scala.util.control.NonFatal import scala.util.control.NonFatal
...@@ -25,6 +26,7 @@ import scala.util.control.NonFatal ...@@ -25,6 +26,7 @@ import scala.util.control.NonFatal
private[sql] trait Queryable { private[sql] trait Queryable {
def schema: StructType def schema: StructType
def queryExecution: QueryExecution def queryExecution: QueryExecution
def sqlContext: SQLContext
override def toString: String = { override def toString: String = {
try { try {
...@@ -34,4 +36,23 @@ private[sql] trait Queryable { ...@@ -34,4 +36,23 @@ private[sql] trait Queryable {
s"Invalid tree; ${e.getMessage}:\n$queryExecution" s"Invalid tree; ${e.getMessage}:\n$queryExecution"
} }
} }
/**
* Prints the plans (logical and physical) to the console for debugging purposes.
* @since 1.3.0
*/
def explain(extended: Boolean): Unit = {
val explain = ExplainCommand(queryExecution.logical, extended = extended)
sqlContext.executePlan(explain).executedPlan.executeCollect().foreach {
// scalastyle:off println
r => println(r.getString(0))
// scalastyle:on println
}
}
/**
* Only prints the physical plan to the console for debugging purposes.
* @since 1.3.0
*/
def explain(): Unit = explain(extended = false)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment