@@ -24,7 +24,7 @@ import scala.collection.mutable.ArrayBuffer
24
24
import scala .util .Random
25
25
26
26
import org .apache .spark .sql .AnalysisException
27
- import org .apache .spark .sql .catalog .v2 .{CatalogNotFoundException , CatalogPlugin , LookupCatalog }
27
+ import org .apache .spark .sql .catalog .v2 .{CatalogNotFoundException , CatalogPlugin , LookupCatalog , TableChange }
28
28
import org .apache .spark .sql .catalyst ._
29
29
import org .apache .spark .sql .catalyst .catalog ._
30
30
import org .apache .spark .sql .catalyst .encoders .OuterScopes
@@ -34,6 +34,7 @@ import org.apache.spark.sql.catalyst.expressions.aggregate._
34
34
import org .apache .spark .sql .catalyst .expressions .objects ._
35
35
import org .apache .spark .sql .catalyst .plans ._
36
36
import org .apache .spark .sql .catalyst .plans .logical ._
37
+ import org .apache .spark .sql .catalyst .plans .logical .sql .{AlterTableAddColumnsStatement , AlterTableAlterColumnStatement , AlterTableDropColumnsStatement , AlterTableRenameColumnStatement , AlterTableSetLocationStatement , AlterTableSetPropertiesStatement , AlterTableUnsetPropertiesStatement }
37
38
import org .apache .spark .sql .catalyst .rules ._
38
39
import org .apache .spark .sql .catalyst .trees .TreeNodeRef
39
40
import org .apache .spark .sql .catalyst .util .toPrettySQL
@@ -165,6 +166,7 @@ class Analyzer(
165
166
new SubstituteUnresolvedOrdinals (conf)),
166
167
Batch (" Resolution" , fixedPoint,
167
168
ResolveTableValuedFunctions ::
169
+ ResolveAlterTable ::
168
170
ResolveTables ::
169
171
ResolveRelations ::
170
172
ResolveReferences ::
@@ -787,6 +789,86 @@ class Analyzer(
787
789
}
788
790
}
789
791
792
+ /**
793
+ * Resolve ALTER TABLE statements that use a DSv2 catalog.
794
+ *
795
+ * This rule converts unresolved ALTER TABLE statements to v2 when a v2 catalog is responsible
796
+ * for the table identifier. A v2 catalog is responsible for an identifier when the identifier
797
+ * has a catalog specified, like prod_catalog.db.table, or when a default v2 catalog is set and
798
+ * the table identifier does not include a catalog.
799
+ */
800
+ object ResolveAlterTable extends Rule [LogicalPlan ] {
801
+ import org .apache .spark .sql .catalog .v2 .CatalogV2Implicits ._
802
+ override def apply (plan : LogicalPlan ): LogicalPlan = plan resolveOperators {
803
+ case alter @ AlterTableAddColumnsStatement (
804
+ CatalogObjectIdentifier (Some (v2Catalog), ident), cols) =>
805
+ val changes = cols.map { col =>
806
+ TableChange .addColumn(col.name.toArray, col.dataType, true , col.comment.orNull)
807
+ }
808
+
809
+ AlterTable (
810
+ v2Catalog.asTableCatalog, ident,
811
+ UnresolvedRelation (alter.tableName),
812
+ changes)
813
+
814
+ case alter @ AlterTableAlterColumnStatement (
815
+ CatalogObjectIdentifier (Some (v2Catalog), ident), colName, dataType, comment) =>
816
+ val typeChange = dataType.map { newDataType =>
817
+ TableChange .updateColumnType(colName.toArray, newDataType, true )
818
+ }
819
+
820
+ val commentChange = comment.map { newComment =>
821
+ TableChange .updateColumnComment(colName.toArray, newComment)
822
+ }
823
+
824
+ AlterTable (
825
+ v2Catalog.asTableCatalog, ident,
826
+ UnresolvedRelation (alter.tableName),
827
+ typeChange.toSeq ++ commentChange.toSeq)
828
+
829
+ case alter @ AlterTableRenameColumnStatement (
830
+ CatalogObjectIdentifier (Some (v2Catalog), ident), col, newName) =>
831
+ AlterTable (
832
+ v2Catalog.asTableCatalog, ident,
833
+ UnresolvedRelation (alter.tableName),
834
+ Seq (TableChange .renameColumn(col.toArray, newName)))
835
+
836
+ case alter @ AlterTableDropColumnsStatement (
837
+ CatalogObjectIdentifier (Some (v2Catalog), ident), cols) =>
838
+ val changes = cols.map(col => TableChange .deleteColumn(col.toArray))
839
+ AlterTable (
840
+ v2Catalog.asTableCatalog, ident,
841
+ UnresolvedRelation (alter.tableName),
842
+ changes)
843
+
844
+ case alter @ AlterTableSetPropertiesStatement (
845
+ CatalogObjectIdentifier (Some (v2Catalog), ident), props) =>
846
+ val changes = props.map {
847
+ case (key, value) =>
848
+ TableChange .setProperty(key, value)
849
+ }
850
+
851
+ AlterTable (
852
+ v2Catalog.asTableCatalog, ident,
853
+ UnresolvedRelation (alter.tableName),
854
+ changes.toSeq)
855
+
856
+ case alter @ AlterTableUnsetPropertiesStatement (
857
+ CatalogObjectIdentifier (Some (v2Catalog), ident), keys, _) =>
858
+ AlterTable (
859
+ v2Catalog.asTableCatalog, ident,
860
+ UnresolvedRelation (alter.tableName),
861
+ keys.map(key => TableChange .removeProperty(key)))
862
+
863
+ case alter @ AlterTableSetLocationStatement (
864
+ CatalogObjectIdentifier (Some (v2Catalog), ident), newLoc) =>
865
+ AlterTable (
866
+ v2Catalog.asTableCatalog, ident,
867
+ UnresolvedRelation (alter.tableName),
868
+ Seq (TableChange .setProperty(" location" , newLoc)))
869
+ }
870
+ }
871
+
790
872
/**
791
873
* Replaces [[UnresolvedAttribute ]]s with concrete [[AttributeReference ]]s from
792
874
* a logical plan node's children.
0 commit comments