|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.catalyst.plans.logical |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.analysis.{FieldName, FieldPosition} |
| 21 | +import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec |
| 22 | +import org.apache.spark.sql.catalyst.util.TypeUtils |
| 23 | +import org.apache.spark.sql.connector.catalog.{TableCatalog, TableChange} |
| 24 | +import org.apache.spark.sql.errors.QueryCompilationErrors |
| 25 | +import org.apache.spark.sql.types.DataType |
| 26 | + |
| 27 | +/** |
| 28 | + * The base trait for commands that need to alter a v2 table with [[TableChange]]s. |
| 29 | + */ |
| 30 | +trait AlterTableCommand extends UnaryCommand { |
| 31 | + def changes: Seq[TableChange] |
| 32 | + def table: LogicalPlan |
| 33 | + final override def child: LogicalPlan = table |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * The logical plan that defines or changes the comment of an TABLE for v2 catalogs. |
| 38 | + * |
| 39 | + * {{{ |
| 40 | + * COMMENT ON TABLE tableIdentifier IS ('text' | NULL) |
| 41 | + * }}} |
| 42 | + * |
| 43 | + * where the `text` is the new comment written as a string literal; or `NULL` to drop the comment. |
| 44 | + */ |
| 45 | +case class CommentOnTable(table: LogicalPlan, comment: String) extends AlterTableCommand { |
| 46 | + override def changes: Seq[TableChange] = { |
| 47 | + Seq(TableChange.setProperty(TableCatalog.PROP_COMMENT, comment)) |
| 48 | + } |
| 49 | + override protected def withNewChildInternal(newChild: LogicalPlan): LogicalPlan = |
| 50 | + copy(table = newChild) |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * The logical plan of the ALTER TABLE ... SET LOCATION command. |
| 55 | + */ |
| 56 | +case class SetTableLocation( |
| 57 | + table: LogicalPlan, |
| 58 | + partitionSpec: Option[TablePartitionSpec], |
| 59 | + location: String) extends AlterTableCommand { |
| 60 | + override def changes: Seq[TableChange] = { |
| 61 | + if (partitionSpec.nonEmpty) { |
| 62 | + throw QueryCompilationErrors.alterV2TableSetLocationWithPartitionNotSupportedError() |
| 63 | + } |
| 64 | + Seq(TableChange.setProperty(TableCatalog.PROP_LOCATION, location)) |
| 65 | + } |
| 66 | + override protected def withNewChildInternal(newChild: LogicalPlan): LogicalPlan = |
| 67 | + copy(table = newChild) |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * The logical plan of the ALTER TABLE ... SET TBLPROPERTIES command. |
| 72 | + */ |
| 73 | +case class SetTableProperties( |
| 74 | + table: LogicalPlan, |
| 75 | + properties: Map[String, String]) extends AlterTableCommand { |
| 76 | + override def changes: Seq[TableChange] = { |
| 77 | + properties.map { case (key, value) => |
| 78 | + TableChange.setProperty(key, value) |
| 79 | + }.toSeq |
| 80 | + } |
| 81 | + override protected def withNewChildInternal(newChild: LogicalPlan): LogicalPlan = |
| 82 | + copy(table = newChild) |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * The logical plan of the ALTER TABLE ... UNSET TBLPROPERTIES command. |
| 87 | + */ |
| 88 | +case class UnsetTableProperties( |
| 89 | + table: LogicalPlan, |
| 90 | + propertyKeys: Seq[String], |
| 91 | + ifExists: Boolean) extends AlterTableCommand { |
| 92 | + override def changes: Seq[TableChange] = { |
| 93 | + propertyKeys.map(key => TableChange.removeProperty(key)) |
| 94 | + } |
| 95 | + override protected def withNewChildInternal(newChild: LogicalPlan): LogicalPlan = |
| 96 | + copy(table = newChild) |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * The logical plan of the ALTER TABLE ... ADD COLUMNS command. |
| 101 | + */ |
| 102 | +case class AddColumns( |
| 103 | + table: LogicalPlan, |
| 104 | + columnsToAdd: Seq[QualifiedColType]) extends AlterTableCommand { |
| 105 | + columnsToAdd.foreach { c => |
| 106 | + TypeUtils.failWithIntervalType(c.dataType) |
| 107 | + } |
| 108 | + |
| 109 | + override lazy val resolved: Boolean = table.resolved && columnsToAdd.forall(_.resolved) |
| 110 | + |
| 111 | + override def changes: Seq[TableChange] = { |
| 112 | + columnsToAdd.map { col => |
| 113 | + require(col.path.forall(_.resolved), |
| 114 | + "FieldName should be resolved before it's converted to TableChange.") |
| 115 | + require(col.position.forall(_.resolved), |
| 116 | + "FieldPosition should be resolved before it's converted to TableChange.") |
| 117 | + TableChange.addColumn( |
| 118 | + col.name.toArray, |
| 119 | + col.dataType, |
| 120 | + col.nullable, |
| 121 | + col.comment.orNull, |
| 122 | + col.position.map(_.position).orNull) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + override protected def withNewChildInternal(newChild: LogicalPlan): LogicalPlan = |
| 127 | + copy(table = newChild) |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * The logical plan of the ALTER TABLE ... REPLACE COLUMNS command. |
| 132 | + */ |
| 133 | +case class ReplaceColumns( |
| 134 | + table: LogicalPlan, |
| 135 | + columnsToAdd: Seq[QualifiedColType]) extends AlterTableCommand { |
| 136 | + columnsToAdd.foreach { c => |
| 137 | + TypeUtils.failWithIntervalType(c.dataType) |
| 138 | + } |
| 139 | + |
| 140 | + override lazy val resolved: Boolean = table.resolved && columnsToAdd.forall(_.resolved) |
| 141 | + |
| 142 | + override def changes: Seq[TableChange] = { |
| 143 | + // REPLACE COLUMNS deletes all the existing columns and adds new columns specified. |
| 144 | + require(table.resolved) |
| 145 | + val deleteChanges = table.schema.fieldNames.map { name => |
| 146 | + TableChange.deleteColumn(Array(name)) |
| 147 | + } |
| 148 | + val addChanges = columnsToAdd.map { col => |
| 149 | + assert(col.path.isEmpty) |
| 150 | + assert(col.position.isEmpty) |
| 151 | + TableChange.addColumn( |
| 152 | + col.name.toArray, |
| 153 | + col.dataType, |
| 154 | + col.nullable, |
| 155 | + col.comment.orNull, |
| 156 | + null) |
| 157 | + } |
| 158 | + deleteChanges ++ addChanges |
| 159 | + } |
| 160 | + |
| 161 | + override protected def withNewChildInternal(newChild: LogicalPlan): LogicalPlan = |
| 162 | + copy(table = newChild) |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * The logical plan of the ALTER TABLE ... DROP COLUMNS command. |
| 167 | + */ |
| 168 | +case class DropColumns( |
| 169 | + table: LogicalPlan, |
| 170 | + columnsToDrop: Seq[FieldName]) extends AlterTableCommand { |
| 171 | + override def changes: Seq[TableChange] = { |
| 172 | + columnsToDrop.map { col => |
| 173 | + require(col.resolved, "FieldName should be resolved before it's converted to TableChange.") |
| 174 | + TableChange.deleteColumn(col.name.toArray) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + override protected def withNewChildInternal(newChild: LogicalPlan): LogicalPlan = |
| 179 | + copy(table = newChild) |
| 180 | +} |
| 181 | + |
| 182 | +/** |
| 183 | + * The logical plan of the ALTER TABLE ... RENAME COLUMN command. |
| 184 | + */ |
| 185 | +case class RenameColumn( |
| 186 | + table: LogicalPlan, |
| 187 | + column: FieldName, |
| 188 | + newName: String) extends AlterTableCommand { |
| 189 | + override def changes: Seq[TableChange] = { |
| 190 | + require(column.resolved, "FieldName should be resolved before it's converted to TableChange.") |
| 191 | + Seq(TableChange.renameColumn(column.name.toArray, newName)) |
| 192 | + } |
| 193 | + |
| 194 | + override protected def withNewChildInternal(newChild: LogicalPlan): LogicalPlan = |
| 195 | + copy(table = newChild) |
| 196 | +} |
| 197 | + |
| 198 | +/** |
| 199 | + * The logical plan of the ALTER TABLE ... ALTER COLUMN command. |
| 200 | + */ |
| 201 | +case class AlterColumn( |
| 202 | + table: LogicalPlan, |
| 203 | + column: FieldName, |
| 204 | + dataType: Option[DataType], |
| 205 | + nullable: Option[Boolean], |
| 206 | + comment: Option[String], |
| 207 | + position: Option[FieldPosition]) extends AlterTableCommand { |
| 208 | + override def changes: Seq[TableChange] = { |
| 209 | + require(column.resolved, "FieldName should be resolved before it's converted to TableChange.") |
| 210 | + val colName = column.name.toArray |
| 211 | + val typeChange = dataType.map { newDataType => |
| 212 | + TableChange.updateColumnType(colName, newDataType) |
| 213 | + } |
| 214 | + val nullabilityChange = nullable.map { nullable => |
| 215 | + TableChange.updateColumnNullability(colName, nullable) |
| 216 | + } |
| 217 | + val commentChange = comment.map { newComment => |
| 218 | + TableChange.updateColumnComment(colName, newComment) |
| 219 | + } |
| 220 | + val positionChange = position.map { newPosition => |
| 221 | + require(newPosition.resolved, |
| 222 | + "FieldPosition should be resolved before it's converted to TableChange.") |
| 223 | + TableChange.updateColumnPosition(colName, newPosition.position) |
| 224 | + } |
| 225 | + typeChange.toSeq ++ nullabilityChange ++ commentChange ++ positionChange |
| 226 | + } |
| 227 | + |
| 228 | + override protected def withNewChildInternal(newChild: LogicalPlan): LogicalPlan = |
| 229 | + copy(table = newChild) |
| 230 | +} |
0 commit comments