Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cloud-fan committed Dec 2, 2020
1 parent ada2bd8 commit bff924d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2925,7 +2925,7 @@ object SQLConf {
val LEGACY_CREATE_HIVE_TABLE_BY_DEFAULT =
buildConf("spark.sql.legacy.createHiveTableByDefault")
.internal()
.doc("When set to true, CREATE TABLE syntax without a table provider will use hive " +
.doc("When set to true, CREATE TABLE syntax without USING or STORED AS will use Hive " +
s"instead of the value of ${DEFAULT_DATA_SOURCE_NAME.key} as the table provider.")
.version("3.1.0")
.booleanConf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,9 @@ class ResolveSessionCatalog(
if (!createHiveTableByDefault || (ctas && conf.convertCTAS)) {
(nonHiveStorageFormat, conf.defaultDataSourceName)
} else {
logWarning("A Hive serde table will be created as there is no table provider " +
s"specified. You can set ${SQLConf.LEGACY_CREATE_HIVE_TABLE_BY_DEFAULT.key} to false " +
"so that native data source table will be created instead.")
(defaultHiveStorage, DDLUtils.HIVE_PROVIDER)
}
}
Expand All @@ -659,8 +662,14 @@ class ResolveSessionCatalog(
comment: Option[String],
storageFormat: CatalogStorageFormat,
external: Boolean): CatalogTable = {
if (external && location.isEmpty) {
throw new AnalysisException(s"CREATE EXTERNAL TABLE must be accompanied by LOCATION")
if (external) {
if (DDLUtils.isHiveTable(Some(provider))) {
if (location.isEmpty) {
throw new AnalysisException(s"CREATE EXTERNAL TABLE must be accompanied by LOCATION")
}
} else {
throw new AnalysisException(s"Operation not allowed: CREATE EXTERNAL TABLE ... USING")
}
}

val tableType = if (location.isDefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ class CreateTableAsSelectSuite extends DataSourceTest with SharedSparkSession {
}
}

test("disallows CREATE EXTERNAL TABLE ... USING ... AS query") {
withTable("t") {
val error = intercept[AnalysisException] {
sql(
s"""
|CREATE EXTERNAL TABLE t USING PARQUET
|OPTIONS (PATH '${path.toURI}')
|AS SELECT 1 AS a, 2 AS b
""".stripMargin
)
}.getMessage

assert(error.contains("Operation not allowed") &&
error.contains("CREATE EXTERNAL TABLE ..."))
}
}

test("create table using as select - with partitioned by") {
val catalog = spark.sessionState.catalog
withTable("t") {
Expand Down

0 comments on commit bff924d

Please sign in to comment.