cloud-fan commented on a change in pull request #33200:
URL: https://github.com/apache/spark/pull/33200#discussion_r674761697
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/v2Commands.scala
##########
@@ -1099,21 +1066,83 @@ case class UnsetTableProperties(
copy(table = newChild)
}
-trait AlterTableCommand extends UnaryCommand {
+trait AlterTableColumnCommand extends UnaryCommand {
def table: LogicalPlan
- def operation: String
def changes: Seq[TableChange]
override def child: LogicalPlan = table
}
+/**
+ * The logical plan of the ALTER TABLE ... ADD COLUMNS command.
+ */
+case class AlterTableAddColumns(
+ table: LogicalPlan,
+ columnsToAdd: Seq[QualifiedColType]) extends AlterTableColumnCommand {
+ import org.apache.spark.sql.connector.catalog.CatalogV2Util._
+ columnsToAdd.foreach { c =>
+ failNullType(c.dataType)
+ TypeUtils.failWithIntervalType(c.dataType)
+ }
+
+ override def changes: Seq[TableChange] = {
+ columnsToAdd.map { col =>
+ require(col.position.forall(_.resolved),
+ "FieldPosition should be resolved before it's converted to
TableChange.")
+ TableChange.addColumn(
+ col.name.toArray,
+ col.dataType,
+ col.nullable,
+ col.comment.orNull,
+ col.position.map(_.position).orNull)
+ }
+ }
+
+ override protected def withNewChildInternal(newChild: LogicalPlan):
LogicalPlan =
+ copy(table = newChild)
+}
+
+/**
+ * The logical plan of the ALTER TABLE ... REPLACE COLUMNS command.
+ */
+case class AlterTableReplaceColumns(
+ table: LogicalPlan,
+ columnsToAdd: Seq[QualifiedColType]) extends AlterTableColumnCommand {
+ import org.apache.spark.sql.connector.catalog.CatalogV2Util._
+ columnsToAdd.foreach { c =>
+ failNullType(c.dataType)
+ TypeUtils.failWithIntervalType(c.dataType)
+ }
+
+ override def changes: Seq[TableChange] = {
+ // REPLACE COLUMNS deletes all the existing columns and adds new columns
specified.
+ require(table.resolved)
+ val deleteChanges = table.schema.fieldNames.map { name =>
+ TableChange.deleteColumn(Array(name))
+ }
+ val addChanges = columnsToAdd.map { col =>
+ // Cannot add nested columns when replacing columns.
+ assert(col.path.name.isEmpty)
Review comment:
SGTM. We can probably remove the `isRoot` flag from `ResolvedFieldName`
as well.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]