Skip to content
Snippets Groups Projects
Commit 0a8e51a5 authored by gatorsmile's avatar gatorsmile Committed by Wenchen Fan
Browse files

[SPARK-17657][SQL] Disallow Users to Change Table Type

### What changes were proposed in this pull request?
Hive allows users to change the table type from `Managed` to `External` or from `External` to `Managed` by altering table's property `EXTERNAL`. See the JIRA: https://issues.apache.org/jira/browse/HIVE-1329

So far, Spark SQL does not correctly support it, although users can do it. Many assumptions are broken in the implementation. Thus, this PR is to disallow users to change it.

In addition, we also do not allow users to set the property `EXTERNAL` when creating a table.

### How was this patch tested?
Added test cases

Author: gatorsmile <gatorsmile@gmail.com>

Closes #15230 from gatorsmile/alterTableSetExternal.
parent 7bf8a404
No related branches found
No related tags found
No related merge requests found
......@@ -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'")
}
}
// --------------------------------------------------------------------------
......
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment