aboutsummaryrefslogtreecommitdiff
path: root/sql/hive
diff options
context:
space:
mode:
authorgatorsmile <gatorsmile@gmail.com>2016-04-14 08:34:11 -0700
committerYin Huai <yhuai@databricks.com>2016-04-14 08:34:11 -0700
commit0d22092cd9c8876a7f226add578ff1c025012fe9 (patch)
tree5ae02c9621897418c34ca5c63f13662cc0c21b58 /sql/hive
parentf83ba454a507bec0cc389d9a382cd71add7f17c1 (diff)
downloadspark-0d22092cd9c8876a7f226add578ff1c025012fe9.tar.gz
spark-0d22092cd9c8876a7f226add578ff1c025012fe9.tar.bz2
spark-0d22092cd9c8876a7f226add578ff1c025012fe9.zip
[SPARK-14125][SQL] Native DDL Support: Alter View
#### What changes were proposed in this pull request? This PR is to provide a native DDL support for the following three Alter View commands: Based on the Hive DDL document: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL ##### 1. ALTER VIEW RENAME **Syntax:** ```SQL ALTER VIEW view_name RENAME TO new_view_name ``` - to change the name of a view to a different name - not allowed to rename a view's name by ALTER TABLE ##### 2. ALTER VIEW SET TBLPROPERTIES **Syntax:** ```SQL ALTER VIEW view_name SET TBLPROPERTIES ('comment' = new_comment); ``` - to add metadata to a view - not allowed to set views' properties by ALTER TABLE - ignore it if trying to set a view's existing property key when the value is the same - overwrite the value if trying to set a view's existing key to a different value ##### 3. ALTER VIEW UNSET TBLPROPERTIES **Syntax:** ```SQL ALTER VIEW view_name UNSET TBLPROPERTIES [IF EXISTS] ('comment', 'key') ``` - to remove metadata from a view - not allowed to unset views' properties by ALTER TABLE - issue an exception if trying to unset a view's non-existent key #### How was this patch tested? Added test cases to verify if it works properly. Author: gatorsmile <gatorsmile@gmail.com> Author: xiaoli <lixiao1983@gmail.com> Author: Xiao Li <xiaoli@Xiaos-MacBook-Pro.local> Closes #12324 from gatorsmile/alterView.
Diffstat (limited to 'sql/hive')
-rw-r--r--sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala112
1 files changed, 112 insertions, 0 deletions
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 c82c7f6ca6..249dcdfff5 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
@@ -147,6 +147,118 @@ class HiveDDLSuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
}
}
+ test("alter views - rename") {
+ val tabName = "tab1"
+ withTable(tabName) {
+ sqlContext.range(10).write.saveAsTable(tabName)
+ val oldViewName = "view1"
+ val newViewName = "view2"
+ withView(oldViewName, newViewName) {
+ val catalog = hiveContext.sessionState.catalog
+ sql(s"CREATE VIEW $oldViewName AS SELECT * FROM $tabName")
+
+ assert(catalog.tableExists(TableIdentifier(oldViewName)))
+ assert(!catalog.tableExists(TableIdentifier(newViewName)))
+ sql(s"ALTER VIEW $oldViewName RENAME TO $newViewName")
+ assert(!catalog.tableExists(TableIdentifier(oldViewName)))
+ assert(catalog.tableExists(TableIdentifier(newViewName)))
+ }
+ }
+ }
+
+ test("alter views - set/unset tblproperties") {
+ val tabName = "tab1"
+ withTable(tabName) {
+ sqlContext.range(10).write.saveAsTable(tabName)
+ val viewName = "view1"
+ withView(viewName) {
+ val catalog = hiveContext.sessionState.catalog
+ sql(s"CREATE VIEW $viewName AS SELECT * FROM $tabName")
+
+ assert(catalog.getTableMetadata(TableIdentifier(viewName))
+ .properties.filter(_._1 != "transient_lastDdlTime") == Map())
+ sql(s"ALTER VIEW $viewName SET TBLPROPERTIES ('p' = 'an')")
+ assert(catalog.getTableMetadata(TableIdentifier(viewName))
+ .properties.filter(_._1 != "transient_lastDdlTime") == Map("p" -> "an"))
+
+ // no exception or message will be issued if we set it again
+ sql(s"ALTER VIEW $viewName SET TBLPROPERTIES ('p' = 'an')")
+ assert(catalog.getTableMetadata(TableIdentifier(viewName))
+ .properties.filter(_._1 != "transient_lastDdlTime") == Map("p" -> "an"))
+
+ // the value will be updated if we set the same key to a different value
+ sql(s"ALTER VIEW $viewName SET TBLPROPERTIES ('p' = 'b')")
+ assert(catalog.getTableMetadata(TableIdentifier(viewName))
+ .properties.filter(_._1 != "transient_lastDdlTime") == Map("p" -> "b"))
+
+ sql(s"ALTER VIEW $viewName UNSET TBLPROPERTIES ('p')")
+ assert(catalog.getTableMetadata(TableIdentifier(viewName))
+ .properties.filter(_._1 != "transient_lastDdlTime") == Map())
+
+ val message = intercept[AnalysisException] {
+ sql(s"ALTER VIEW $viewName UNSET TBLPROPERTIES ('p')")
+ }.getMessage
+ assert(message.contains(
+ "attempted to unset non-existent property 'p' in table '`view1`'"))
+ }
+ }
+ }
+
+ test("alter views and alter table - misuse") {
+ val tabName = "tab1"
+ withTable(tabName) {
+ sqlContext.range(10).write.saveAsTable(tabName)
+ val oldViewName = "view1"
+ val newViewName = "view2"
+ withView(oldViewName, newViewName) {
+ val catalog = hiveContext.sessionState.catalog
+ sql(s"CREATE VIEW $oldViewName AS SELECT * FROM $tabName")
+
+ assert(catalog.tableExists(TableIdentifier(tabName)))
+ assert(catalog.tableExists(TableIdentifier(oldViewName)))
+
+ var message = intercept[AnalysisException] {
+ sql(s"ALTER VIEW $tabName RENAME TO $newViewName")
+ }.getMessage
+ assert(message.contains(
+ "Cannot alter a table with ALTER VIEW. Please use ALTER TABLE instead"))
+
+ message = intercept[AnalysisException] {
+ sql(s"ALTER VIEW $tabName SET TBLPROPERTIES ('p' = 'an')")
+ }.getMessage
+ assert(message.contains(
+ "Cannot alter a table with ALTER VIEW. Please use ALTER TABLE instead"))
+
+ message = intercept[AnalysisException] {
+ sql(s"ALTER VIEW $tabName UNSET TBLPROPERTIES ('p')")
+ }.getMessage
+ assert(message.contains(
+ "Cannot alter a table with ALTER VIEW. Please use ALTER TABLE instead"))
+
+ message = intercept[AnalysisException] {
+ sql(s"ALTER TABLE $oldViewName RENAME TO $newViewName")
+ }.getMessage
+ assert(message.contains(
+ "Cannot alter a view with ALTER TABLE. Please use ALTER VIEW instead"))
+
+ message = intercept[AnalysisException] {
+ sql(s"ALTER TABLE $oldViewName SET TBLPROPERTIES ('p' = 'an')")
+ }.getMessage
+ assert(message.contains(
+ "Cannot alter a view with ALTER TABLE. Please use ALTER VIEW instead"))
+
+ message = intercept[AnalysisException] {
+ sql(s"ALTER TABLE $oldViewName UNSET TBLPROPERTIES ('p')")
+ }.getMessage
+ assert(message.contains(
+ "Cannot alter a view with ALTER TABLE. Please use ALTER VIEW instead"))
+
+ assert(catalog.tableExists(TableIdentifier(tabName)))
+ assert(catalog.tableExists(TableIdentifier(oldViewName)))
+ }
+ }
+ }
+
test("drop table using drop view") {
withTable("tab1") {
sql("CREATE TABLE tab1(c1 int)")