File tree Expand file tree Collapse file tree 3 files changed +29
-3
lines changed
catalyst/src/main/scala/org/apache/spark/sql/internal
main/scala/org/apache/spark/sql/catalyst/analysis
test/scala/org/apache/spark/sql/sources Expand file tree Collapse file tree 3 files changed +29
-3
lines changed Original file line number Diff line number Diff line change @@ -2925,7 +2925,7 @@ object SQLConf {
2925
2925
val LEGACY_CREATE_HIVE_TABLE_BY_DEFAULT =
2926
2926
buildConf(" spark.sql.legacy.createHiveTableByDefault" )
2927
2927
.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 " +
2929
2929
s " instead of the value of ${DEFAULT_DATA_SOURCE_NAME .key} as the table provider. " )
2930
2930
.version(" 3.1.0" )
2931
2931
.booleanConf
Original file line number Diff line number Diff line change @@ -643,6 +643,9 @@ class ResolveSessionCatalog(
643
643
if (! createHiveTableByDefault || (ctas && conf.convertCTAS)) {
644
644
(nonHiveStorageFormat, conf.defaultDataSourceName)
645
645
} 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." )
646
649
(defaultHiveStorage, DDLUtils .HIVE_PROVIDER )
647
650
}
648
651
}
@@ -659,8 +662,14 @@ class ResolveSessionCatalog(
659
662
comment : Option [String ],
660
663
storageFormat : CatalogStorageFormat ,
661
664
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
+ }
664
673
}
665
674
666
675
val tableType = if (location.isDefined) {
Original file line number Diff line number Diff line change @@ -170,6 +170,23 @@ class CreateTableAsSelectSuite extends DataSourceTest with SharedSparkSession {
170
170
}
171
171
}
172
172
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
+
173
190
test(" create table using as select - with partitioned by" ) {
174
191
val catalog = spark.sessionState.catalog
175
192
withTable(" t" ) {
You can’t perform that action at this time.
0 commit comments