Skip to content

Commit bff924d

Browse files
committed
address comments
1 parent ada2bd8 commit bff924d

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2925,7 +2925,7 @@ object SQLConf {
29252925
val LEGACY_CREATE_HIVE_TABLE_BY_DEFAULT =
29262926
buildConf("spark.sql.legacy.createHiveTableByDefault")
29272927
.internal()
2928-
.doc("When set to true, CREATE TABLE syntax without a table provider will use hive " +
2928+
.doc("When set to true, CREATE TABLE syntax without USING or STORED AS will use Hive " +
29292929
s"instead of the value of ${DEFAULT_DATA_SOURCE_NAME.key} as the table provider.")
29302930
.version("3.1.0")
29312931
.booleanConf

sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveSessionCatalog.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,9 @@ class ResolveSessionCatalog(
643643
if (!createHiveTableByDefault || (ctas && conf.convertCTAS)) {
644644
(nonHiveStorageFormat, conf.defaultDataSourceName)
645645
} else {
646+
logWarning("A Hive serde table will be created as there is no table provider " +
647+
s"specified. You can set ${SQLConf.LEGACY_CREATE_HIVE_TABLE_BY_DEFAULT.key} to false " +
648+
"so that native data source table will be created instead.")
646649
(defaultHiveStorage, DDLUtils.HIVE_PROVIDER)
647650
}
648651
}
@@ -659,8 +662,14 @@ class ResolveSessionCatalog(
659662
comment: Option[String],
660663
storageFormat: CatalogStorageFormat,
661664
external: Boolean): CatalogTable = {
662-
if (external && location.isEmpty) {
663-
throw new AnalysisException(s"CREATE EXTERNAL TABLE must be accompanied by LOCATION")
665+
if (external) {
666+
if (DDLUtils.isHiveTable(Some(provider))) {
667+
if (location.isEmpty) {
668+
throw new AnalysisException(s"CREATE EXTERNAL TABLE must be accompanied by LOCATION")
669+
}
670+
} else {
671+
throw new AnalysisException(s"Operation not allowed: CREATE EXTERNAL TABLE ... USING")
672+
}
664673
}
665674

666675
val tableType = if (location.isDefined) {

sql/core/src/test/scala/org/apache/spark/sql/sources/CreateTableAsSelectSuite.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,23 @@ class CreateTableAsSelectSuite extends DataSourceTest with SharedSparkSession {
170170
}
171171
}
172172

173+
test("disallows CREATE EXTERNAL TABLE ... USING ... AS query") {
174+
withTable("t") {
175+
val error = intercept[AnalysisException] {
176+
sql(
177+
s"""
178+
|CREATE EXTERNAL TABLE t USING PARQUET
179+
|OPTIONS (PATH '${path.toURI}')
180+
|AS SELECT 1 AS a, 2 AS b
181+
""".stripMargin
182+
)
183+
}.getMessage
184+
185+
assert(error.contains("Operation not allowed") &&
186+
error.contains("CREATE EXTERNAL TABLE ..."))
187+
}
188+
}
189+
173190
test("create table using as select - with partitioned by") {
174191
val catalog = spark.sessionState.catalog
175192
withTable("t") {

0 commit comments

Comments
 (0)