Skip to content

Commit 29e8d50

Browse files
chenghao-intelmarmbrus
authored andcommitted
[SPARK-2918] [SQL] Support the CTAS in EXPLAIN command
Hive supports the `explain` the CTAS, which was supported by Spark SQL previously, however, seems it was reverted after the code refactoring in HiveQL. Author: Cheng Hao <hao.cheng@intel.com> Closes apache#3357 from chenghao-intel/explain and squashes the following commits: 7aace63 [Cheng Hao] Support the CTAS in EXPLAIN command (cherry picked from commit 6aa0fc9) Signed-off-by: Michael Armbrust <michael@databricks.com>
1 parent 1d7ee2b commit 29e8d50

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ private[hive] object HiveQl {
124124

125125
// Commands that we do not need to explain.
126126
protected val noExplainCommands = Seq(
127-
"TOK_CREATETABLE",
128127
"TOK_DESCTABLE",
129128
"TOK_TRUNCATETABLE" // truncate table" is a NativeCommand, does not need to explain.
130129
) ++ nativeCommands
@@ -421,6 +420,11 @@ private[hive] object HiveQl {
421420
case Token("TOK_EXPLAIN", explainArgs)
422421
if noExplainCommands.contains(explainArgs.head.getText) =>
423422
ExplainCommand(NoRelation)
423+
case Token("TOK_EXPLAIN", explainArgs)
424+
if "TOK_CREATETABLE" == explainArgs.head.getText =>
425+
val Some(crtTbl) :: _ :: extended :: Nil =
426+
getClauses(Seq("TOK_CREATETABLE", "FORMATTED", "EXTENDED"), explainArgs)
427+
ExplainCommand(nodeToPlan(crtTbl), extended != None)
424428
case Token("TOK_EXPLAIN", explainArgs) =>
425429
// Ignore FORMATTED if present.
426430
val Some(query) :: _ :: extended :: Nil =

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,40 @@ class HiveExplainSuite extends QueryTest {
3838
"== Physical Plan ==",
3939
"Code Generation", "== RDD ==")
4040
}
41+
42+
test("explain create table command") {
43+
checkExistence(sql("explain create table temp__b as select * from src limit 2"), true,
44+
"== Physical Plan ==",
45+
"InsertIntoHiveTable",
46+
"Limit",
47+
"src")
48+
49+
checkExistence(sql("explain extended create table temp__b as select * from src limit 2"), true,
50+
"== Parsed Logical Plan ==",
51+
"== Analyzed Logical Plan ==",
52+
"== Optimized Logical Plan ==",
53+
"== Physical Plan ==",
54+
"CreateTableAsSelect",
55+
"InsertIntoHiveTable",
56+
"Limit",
57+
"src")
58+
59+
checkExistence(sql(
60+
"""
61+
| EXPLAIN EXTENDED CREATE TABLE temp__b
62+
| ROW FORMAT SERDE "org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe"
63+
| WITH SERDEPROPERTIES("serde_p1"="p1","serde_p2"="p2")
64+
| STORED AS RCFile
65+
| TBLPROPERTIES("tbl_p1"="p11", "tbl_p2"="p22")
66+
| AS SELECT * FROM src LIMIT 2
67+
""".stripMargin), true,
68+
"== Parsed Logical Plan ==",
69+
"== Analyzed Logical Plan ==",
70+
"== Optimized Logical Plan ==",
71+
"== Physical Plan ==",
72+
"CreateTableAsSelect",
73+
"InsertIntoHiveTable",
74+
"Limit",
75+
"src")
76+
}
4177
}

0 commit comments

Comments
 (0)