aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveExternalCatalog.scala5
-rw-r--r--sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala32
2 files changed, 37 insertions, 0 deletions
diff --git a/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveExternalCatalog.scala b/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveExternalCatalog.scala
index ed189724a2..237b829da8 100644
--- a/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveExternalCatalog.scala
+++ b/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveExternalCatalog.scala
@@ -112,6 +112,11 @@ private[spark] class HiveExternalCatalog(conf: SparkConf, hadoopConf: Configurat
s"as table property keys may not start with '$DATASOURCE_PREFIX' or '$STATISTICS_PREFIX':" +
s" ${invalidKeys.mkString("[", ", ", "]")}")
}
+ // External users are not allowed to set/switch the table type. In Hive metastore, the table
+ // type can be switched by changing the value of a case-sensitive table property `EXTERNAL`.
+ if (table.properties.contains("EXTERNAL")) {
+ throw new AnalysisException("Cannot set or change the preserved property key: 'EXTERNAL'")
+ }
}
// --------------------------------------------------------------------------
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 8bff6de008..3d1712e435 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
@@ -315,6 +315,38 @@ class HiveDDLSuite
assert(message.contains("Cannot alter a table with ALTER VIEW. Please use ALTER TABLE instead"))
}
+ test("create table - SET TBLPROPERTIES EXTERNAL to TRUE") {
+ val tabName = "tab1"
+ withTable(tabName) {
+ val message = intercept[AnalysisException] {
+ sql(s"CREATE TABLE $tabName (height INT, length INT) TBLPROPERTIES('EXTERNAL'='TRUE')")
+ }.getMessage
+ assert(message.contains("Cannot set or change the preserved property key: 'EXTERNAL'"))
+ }
+ }
+
+ test("alter table - SET TBLPROPERTIES EXTERNAL to TRUE") {
+ val tabName = "tab1"
+ withTable(tabName) {
+ val catalog = spark.sessionState.catalog
+ sql(s"CREATE TABLE $tabName (height INT, length INT)")
+ assert(
+ catalog.getTableMetadata(TableIdentifier(tabName)).tableType == CatalogTableType.MANAGED)
+ val message = intercept[AnalysisException] {
+ sql(s"ALTER TABLE $tabName SET TBLPROPERTIES ('EXTERNAL' = 'TRUE')")
+ }.getMessage
+ assert(message.contains("Cannot set or change the preserved property key: 'EXTERNAL'"))
+ // The table type is not changed to external
+ assert(
+ catalog.getTableMetadata(TableIdentifier(tabName)).tableType == CatalogTableType.MANAGED)
+ // The table property is case sensitive. Thus, external is allowed
+ sql(s"ALTER TABLE $tabName SET TBLPROPERTIES ('external' = 'TRUE')")
+ // The table type is not changed to external
+ assert(
+ catalog.getTableMetadata(TableIdentifier(tabName)).tableType == CatalogTableType.MANAGED)
+ }
+ }
+
test("alter views and alter table - misuse") {
val tabName = "tab1"
withTable(tabName) {