imback82 commented on a change in pull request #27444: [SPARK-30614][SQL] The
native ALTER COLUMN syntax should change one property at a time
URL: https://github.com/apache/spark/pull/27444#discussion_r374361369
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
##########
@@ -2940,30 +2940,61 @@ class AstBuilder(conf: SQLConf) extends
SqlBaseBaseVisitor[AnyRef] with Logging
}
/**
- * Parse a [[AlterTableAlterColumnStatement]] command.
+ * Parse a [[AlterTableAlterColumnStatement]] command to change column type
*
* For example:
* {{{
* ALTER TABLE table1 ALTER COLUMN a.b.c TYPE bigint
- * ALTER TABLE table1 ALTER COLUMN a.b.c TYPE bigint COMMENT 'new comment'
+ * }}}
+ */
+ override def visitAlterTableColumnType(
+ ctx: AlterTableColumnTypeContext): LogicalPlan = withOrigin(ctx) {
+ AlterTableAlterColumnStatement(
+ visitMultipartIdentifier(ctx.table),
+ typedVisit[Seq[String]](ctx.column),
+ dataType = Some(typedVisit[DataType](ctx.dataType)),
+ nullable = None,
+ comment = None,
+ position = None)
+ }
+
+ /**
+ * Parse a [[AlterTableAlterColumnStatement]] command to change column
comment
+ *
+ * For example:
+ * {{{
* ALTER TABLE table1 ALTER COLUMN a.b.c COMMENT 'new comment'
* }}}
*/
- override def visitAlterTableColumn(
- ctx: AlterTableColumnContext): LogicalPlan = withOrigin(ctx) {
- val verb = if (ctx.CHANGE != null) "CHANGE" else "ALTER"
- if (ctx.dataType == null && ctx.commentSpec() == null && ctx.colPosition
== null) {
- operationNotAllowed(
- s"ALTER TABLE table $verb COLUMN requires a TYPE or a COMMENT or a
FIRST/AFTER", ctx)
- }
+ override def visitAlterTableColumnComment(
+ ctx: AlterTableColumnCommentContext): LogicalPlan = withOrigin(ctx) {
+ AlterTableAlterColumnStatement(
+ visitMultipartIdentifier(ctx.table),
+ typedVisit[Seq[String]](ctx.column),
+ dataType = None,
+ nullable = None,
+ comment = Some(visitCommentSpec(ctx.commentSpec())),
+ position = None)
+ }
+ /**
+ * Parse a [[AlterTableAlterColumnStatement]] command to change column
position
+ *
+ * For example:
+ * {{{
+ * ALTER TABLE table1 ALTER COLUMN a.b.c FIRST
+ * ALTER TABLE table1 ALTER COLUMN a.b.c AFTER x
+ * }}}
+ */
+ override def visitAlterTableColumnPosition(
+ ctx: AlterTableColumnPositionContext): LogicalPlan = withOrigin(ctx) {
AlterTableAlterColumnStatement(
visitMultipartIdentifier(ctx.table),
typedVisit[Seq[String]](ctx.column),
- dataType = Option(ctx.dataType).map(typedVisit[DataType]),
+ dataType = None,
nullable = None,
- comment = Option(ctx.commentSpec()).map(visitCommentSpec),
- position = Option(ctx.colPosition).map(typedVisit[ColumnPosition]))
+ comment = None,
+ position = Some(typedVisit[ColumnPosition](ctx.colPosition)))
}
Review comment:
I could also update `AlterTAbleAlterColumnStatement` to be like:
```Scala
case class AlterTableAlterColumnStatement(
tableName: Seq[String],
column: Seq[String],
dataType: Option[DataType],
nullable: Option[Boolean],
comment: Option[String],
position: Option[ColumnPosition],
isHive: Boolean) extends ParsedStatement {
require(
isHive || Seq(dataType, nullable, comment,
position).filter(_.nonEmpty).length == 1,
"Only one property can be changed at a time.")
}
```
to be safe, but I didn't add it because of `isHive` hack.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]