@@ -632,15 +632,15 @@ class ResolveSessionCatalog(
632632 c : CreateTableAsSelectStatement ): Option [CatalogTable ] = {
633633 buildV1Table(
634634 ident, new StructType , c.partitioning, c.bucketSpec, c.properties, c.provider, c.serde,
635- c.options, c.location, c.comment)
635+ c.options, c.location, c.comment, c.external )
636636 }
637637
638638 private def buildV1Table (
639639 ident : TableIdentifier ,
640640 c : CreateTableStatement ): Option [CatalogTable ] = {
641641 buildV1Table(
642642 ident, c.tableSchema, c.partitioning, c.bucketSpec, c.properties, c.provider, c.serde,
643- c.options, c.location, c.comment)
643+ c.options, c.location, c.comment, c.external )
644644 }
645645
646646 private def buildV1Table (
@@ -653,7 +653,8 @@ class ResolveSessionCatalog(
653653 serdeInfo : Option [SerdeInfo ],
654654 options : Map [String , String ],
655655 location : Option [String ],
656- comment : Option [String ]): Option [CatalogTable ] = {
656+ comment : Option [String ],
657+ external : Boolean ): Option [CatalogTable ] = {
657658 (provider, serdeInfo) match {
658659 case (Some (provider), Some (serde)) =>
659660 throw new AnalysisException (
@@ -662,22 +663,22 @@ class ResolveSessionCatalog(
662663 case (None , Some (serde)) =>
663664 Some (buildHiveCatalogTable(
664665 table, schema, partitioning, bucketSpec, properties, serde, options, location,
665- comment))
666+ comment, external ))
666667
667668 case (None , None ) if conf.createHiveTableByDefaultEnabled =>
668669 Some (buildHiveCatalogTable(
669670 table, schema, partitioning, bucketSpec, properties, SerdeInfo .empty, options, location,
670- comment))
671+ comment, external ))
671672
672673 case (Some (provider), None ) if ! isV2Provider(provider) =>
673674 Some (buildCatalogTable(
674675 table, schema, partitioning, bucketSpec, properties, provider, options, location,
675- comment))
676+ comment, external ))
676677
677678 case (None , None ) if ! isV2Provider(conf.defaultDataSourceName) =>
678679 Some (buildCatalogTable(
679680 table, schema, partitioning, bucketSpec, properties, conf.defaultDataSourceName, options,
680- location, comment))
681+ location, comment, external ))
681682
682683 case _ =>
683684 None
@@ -693,8 +694,12 @@ class ResolveSessionCatalog(
693694 provider : String ,
694695 options : Map [String , String ],
695696 location : Option [String ],
696- comment : Option [String ]): CatalogTable = {
697+ comment : Option [String ],
698+ external : Boolean ): CatalogTable = {
697699 assertNoCharTypeInSchema(schema)
700+ if (external) {
701+ throw new AnalysisException (s " Operation not allowed: CREATE EXTERNAL TABLE ... USING " )
702+ }
698703
699704 val storage = CatalogStorageFormat .empty.copy(
700705 locationUri = location.map(CatalogUtils .stringToURI),
@@ -727,10 +732,12 @@ class ResolveSessionCatalog(
727732 serdeInfo : SerdeInfo ,
728733 options : Map [String , String ],
729734 location : Option [String ],
730- comment : Option [String ]): CatalogTable = {
731- val baseStorage = HiveSerDe .getDefaultStorage(conf).copy(
735+ comment : Option [String ],
736+ external : Boolean ): CatalogTable = {
737+ val defaultStorage = HiveSerDe .getDefaultStorage(conf)
738+ val baseStorage = defaultStorage.copy(
732739 locationUri = location.map(CatalogUtils .stringToURI),
733- serde = serdeInfo.serde,
740+ serde = serdeInfo.serde.orElse(defaultStorage.serde) ,
734741 properties = options ++ serdeInfo.serdeProperties)
735742
736743 val storage = (serdeInfo.storedAs, serdeInfo.formatClasses) match {
@@ -739,7 +746,8 @@ class ResolveSessionCatalog(
739746 case Some (hiveSerDe) =>
740747 baseStorage.copy(
741748 inputFormat = hiveSerDe.inputFormat,
742- outputFormat = hiveSerDe.outputFormat)
749+ outputFormat = hiveSerDe.outputFormat,
750+ serde = serdeInfo.serde.orElse(hiveSerDe.serde))
743751 case _ =>
744752 baseStorage
745753 }
@@ -752,6 +760,10 @@ class ResolveSessionCatalog(
752760 baseStorage
753761 }
754762
763+ if (external && location.isEmpty) {
764+ throw new AnalysisException (s " CREATE EXTERNAL TABLE must be accompanied by LOCATION " )
765+ }
766+
755767 val tableType = if (location.isDefined) {
756768 CatalogTableType .EXTERNAL
757769 } else {
@@ -763,6 +775,7 @@ class ResolveSessionCatalog(
763775 tableType = tableType,
764776 storage = storage,
765777 schema = schema,
778+ provider = Some (DDLUtils .HIVE_PROVIDER ),
766779 partitionColumnNames = partitioning.asPartitionColumns,
767780 bucketSpec = bucketSpec,
768781 properties = properties,
0 commit comments