[SPARK-27480][SQL] Improve EXPLAIN DESC QUERY to show the input SQL statement

Currently running explain on describe query gives a little confusing output. This is a minor pr that improves the output of explain.

Before
```
1.EXPLAIN DESCRIBE WITH s AS (SELECT 'hello' as col1) SELECT * FROM s;
== Physical Plan ==
Execute DescribeQueryCommand
   +- DescribeQueryCommand CTE [s]
2.EXPLAIN EXTENDED DESCRIBE SELECT * from s1 where c1 > 0;
== Physical Plan ==
Execute DescribeQueryCommand
   +- DescribeQueryCommand 'Project [*]
```
After
```
1. EXPLAIN DESCRIBE WITH s AS (SELECT 'hello' as col1) SELECT * FROM s;
== Physical Plan ==
Execute DescribeQueryCommand
   +- DescribeQueryCommand WITH s AS (SELECT 'hello' as col1) SELECT * FROM s
2. EXPLAIN DESCRIBE SELECT * from s1 where c1 > 0;
== Physical Plan ==
Execute DescribeQueryCommand
   +- DescribeQueryCommand SELECT * from s1 where c1 > 0
```
Added a couple of tests in describe-query.sql under SQLQueryTestSuite.

Closes #24385 from dilipbiswal/describe_query_explain.

Authored-by: Dilip Biswal <dbiswal@us.ibm.com>
Signed-off-by: Dongjoon Hyun <dhyun@apple.com>
This commit is contained in:
Dilip Biswal 2019-04-21 15:34:16 -07:00 committed by Dongjoon Hyun
parent 9793d9ec22
commit 8a8643c28d
5 changed files with 47 additions and 11 deletions

View file

@ -373,7 +373,7 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder(conf) {
* Create a [[DescribeQueryCommand]] logical command.
*/
override def visitDescribeQuery(ctx: DescribeQueryContext): LogicalPlan = withOrigin(ctx) {
DescribeQueryCommand(visitQuery(ctx.query))
DescribeQueryCommand(source(ctx.query), visitQuery(ctx.query))
}
/**

View file

@ -639,12 +639,14 @@ case class DescribeTableCommand(
* select * from (from a select * select *)
* 7. Common table expressions (CTEs)
*/
case class DescribeQueryCommand(query: LogicalPlan)
case class DescribeQueryCommand(queryText: String, plan: LogicalPlan)
extends DescribeCommandBase {
override def simpleString(maxFields: Int): String = s"$nodeName $queryText".trim
override def run(sparkSession: SparkSession): Seq[Row] = {
val result = new ArrayBuffer[Row]
val queryExecution = sparkSession.sessionState.executePlan(query)
val queryExecution = sparkSession.sessionState.executePlan(plan)
describeSchema(queryExecution.analyzed.schema, result, header = false)
result
}

View file

@ -22,6 +22,10 @@ DESCRIBE
insert into desc_temp1 select *
insert into desc_temp2 select *;
-- Explain
EXPLAIN DESC QUERY SELECT * FROM desc_temp2 WHERE key > 0;
EXPLAIN EXTENDED DESC WITH s AS (SELECT 'hello' as col1) SELECT * FROM s;
-- cleanup
DROP TABLE desc_temp1;
DROP TABLE desc_temp2;

View file

@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 17
-- Number of queries: 19
-- !query 0
@ -154,16 +154,46 @@ DESCRIBE
-- !query 15
DROP TABLE desc_temp1
EXPLAIN DESC QUERY SELECT * FROM desc_temp2 WHERE key > 0
-- !query 15 schema
struct<>
struct<plan:string>
-- !query 15 output
== Physical Plan ==
Execute DescribeQueryCommand
+- DescribeQueryCommand SELECT * FROM desc_temp2 WHERE key > 0
-- !query 16
DROP TABLE desc_temp2
EXPLAIN EXTENDED DESC WITH s AS (SELECT 'hello' as col1) SELECT * FROM s
-- !query 16 schema
struct<>
struct<plan:string>
-- !query 16 output
== Parsed Logical Plan ==
DescribeQueryCommand WITH s AS (SELECT 'hello' as col1) SELECT * FROM s
== Analyzed Logical Plan ==
col_name: string, data_type: string, comment: string
DescribeQueryCommand WITH s AS (SELECT 'hello' as col1) SELECT * FROM s
== Optimized Logical Plan ==
DescribeQueryCommand WITH s AS (SELECT 'hello' as col1) SELECT * FROM s
== Physical Plan ==
Execute DescribeQueryCommand
+- DescribeQueryCommand WITH s AS (SELECT 'hello' as col1) SELECT * FROM s
-- !query 17
DROP TABLE desc_temp1
-- !query 17 schema
struct<>
-- !query 17 output
-- !query 18
DROP TABLE desc_temp2
-- !query 18 schema
struct<>
-- !query 18 output

View file

@ -228,8 +228,8 @@ class SparkSqlParserSuite extends AnalysisTest {
test("describe query") {
val query = "SELECT * FROM t"
assertEqual("DESCRIBE QUERY " + query, DescribeQueryCommand(parser.parsePlan(query)))
assertEqual("DESCRIBE " + query, DescribeQueryCommand(parser.parsePlan(query)))
assertEqual("DESCRIBE QUERY " + query, DescribeQueryCommand(query, parser.parsePlan(query)))
assertEqual("DESCRIBE " + query, DescribeQueryCommand(query, parser.parsePlan(query)))
}
test("describe table column") {