Repository: spark
Updated Branches:
  refs/heads/master 120723f93 -> 2ab24a7bf


[SPARK-17660][SQL] DESC FORMATTED for VIEW Lacks View Definition

### What changes were proposed in this pull request?
Before this PR, `DESC FORMATTED` does not have a section for the view 
definition. We should add it for permanent views, like what Hive does.

```
+----------------------------+-------------------------------------------------------------------------------------------------------------------------------------+-------+
|col_name                    |data_type                                         
                                                                                
   |comment|
+----------------------------+-------------------------------------------------------------------------------------------------------------------------------------+-------+
|a                           |int                                               
                                                                                
   |null   |
|                            |                                                  
                                                                                
   |       |
|# Detailed Table Information|                                                  
                                                                                
   |       |
|Database:                   |default                                           
                                                                                
   |       |
|Owner:                      |xiaoli                                            
                                                                                
   |       |
|Create Time:                |Sat Sep 24 21:46:19 PDT 2016                      
                                                                                
   |       |
|Last Access Time:           |Wed Dec 31 16:00:00 PST 1969                      
                                                                                
   |       |
|Location:                   |                                                  
                                                                                
   |       |
|Table Type:                 |VIEW                                              
                                                                                
   |       |
|Table Parameters:           |                                                  
                                                                                
   |       |
|  transient_lastDdlTime     |1474778779                                        
                                                                                
   |       |
|                            |                                                  
                                                                                
   |       |
|# Storage Information       |                                                  
                                                                                
   |       |
|SerDe Library:              
|org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe                             
                                                      |       |
|InputFormat:                |org.apache.hadoop.mapred.SequenceFileInputFormat  
                                                                                
   |       |
|OutputFormat:               
|org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat                      
                                                      |       |
|Compressed:                 |No                                                
                                                                                
   |       |
|Storage Desc Parameters:    |                                                  
                                                                                
   |       |
|  serialization.format      |1                                                 
                                                                                
   |       |
|                            |                                                  
                                                                                
   |       |
|# View Information          |                                                  
                                                                                
   |       |
|View Original Text:         |SELECT * FROM tbl                                 
                                                                                
   |       |
|View Expanded Text:         |SELECT `gen_attr_0` AS `a` FROM (SELECT 
`gen_attr_0` FROM (SELECT `a` AS `gen_attr_0` FROM `default`.`tbl`) AS 
gen_subquery_0) AS tbl|       |
+----------------------------+-------------------------------------------------------------------------------------------------------------------------------------+-------+
```

### How was this patch tested?
Added a test case

Author: gatorsmile <gatorsm...@gmail.com>

Closes #15234 from gatorsmile/descFormattedView.


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/2ab24a7b
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/2ab24a7b
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/2ab24a7b

Branch: refs/heads/master
Commit: 2ab24a7bf6687ec238306772c4c7ddef6ac93e99
Parents: 120723f
Author: gatorsmile <gatorsm...@gmail.com>
Authored: Tue Sep 27 10:52:26 2016 -0700
Committer: Herman van Hovell <hvanhov...@databricks.com>
Committed: Tue Sep 27 10:52:26 2016 -0700

----------------------------------------------------------------------
 .../spark/sql/execution/command/tables.scala     |  9 +++++++++
 .../spark/sql/hive/execution/HiveDDLSuite.scala  | 19 +++++++++++++++++++
 2 files changed, 28 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/2ab24a7b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala
----------------------------------------------------------------------
diff --git 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala
index 0f61629..6a91c99 100644
--- 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala
+++ 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala
@@ -462,6 +462,8 @@ case class DescribeTableCommand(table: TableIdentifier, 
isExtended: Boolean, isF
     }
 
     describeStorageInfo(table, buffer)
+
+    if (table.tableType == CatalogTableType.VIEW) describeViewInfo(table, 
buffer)
   }
 
   private def describeStorageInfo(metadata: CatalogTable, buffer: 
ArrayBuffer[Row]): Unit = {
@@ -479,6 +481,13 @@ case class DescribeTableCommand(table: TableIdentifier, 
isExtended: Boolean, isF
     }
   }
 
+  private def describeViewInfo(metadata: CatalogTable, buffer: 
ArrayBuffer[Row]): Unit = {
+    append(buffer, "", "", "")
+    append(buffer, "# View Information", "", "")
+    append(buffer, "View Original Text:", 
metadata.viewOriginalText.getOrElse(""), "")
+    append(buffer, "View Expanded Text:", metadata.viewText.getOrElse(""), "")
+  }
+
   private def describeBucketingInfo(metadata: CatalogTable, buffer: 
ArrayBuffer[Row]): Unit = {
     metadata.bucketSpec match {
       case Some(BucketSpec(numBuckets, bucketColumnNames, sortColumnNames)) =>

http://git-wip-us.apache.org/repos/asf/spark/blob/2ab24a7b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
----------------------------------------------------------------------
diff --git 
a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
 
b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
index c927e5d..751e976 100644
--- 
a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
+++ 
b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
@@ -506,6 +506,25 @@ class HiveDDLSuite
     }
   }
 
+  test("desc formatted table for permanent view") {
+    withTable("tbl") {
+      withView("view1") {
+        sql("CREATE TABLE tbl(a int)")
+        sql("CREATE VIEW view1 AS SELECT * FROM tbl")
+        assert(sql("DESC FORMATTED view1").collect().containsSlice(
+          Seq(
+            Row("# View Information", "", ""),
+            Row("View Original Text:", "SELECT * FROM tbl", ""),
+            Row("View Expanded Text:",
+              "SELECT `gen_attr_0` AS `a` FROM (SELECT `gen_attr_0` FROM " +
+              "(SELECT `a` AS `gen_attr_0` FROM `default`.`tbl`) AS 
gen_subquery_0) AS tbl",
+              "")
+          )
+        ))
+      }
+    }
+  }
+
   test("desc table for data source table using Hive Metastore") {
     assume(spark.sparkContext.conf.get(CATALOG_IMPLEMENTATION) == "hive")
     val tabName = "tab1"


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org

Reply via email to