Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ statement
| ALTER TABLE table=multipartIdentifier
(ALTER | CHANGE) COLUMN? column=multipartIdentifier
(TYPE dataType)? (COMMENT comment=STRING)? colPosition? #alterTableColumn
| ALTER TABLE table=multipartIdentifier partitionSpec?
CHANGE COLUMN?
colName=multipartIdentifier colType colPosition? #hiveChangeColumn
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

colName was errorCapturingIdentifier before. We want it to be multipartIdentifier?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though it is fine because ResolveSessionCatalog will detect and throw AnalysisException("ALTER COLUMN with qualified column is only supported with v2 tables.").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to be consistent with the other ALTER COLUMN syntax.

| ALTER TABLE multipartIdentifier (partitionSpec)?
SET SERDE STRING (WITH SERDEPROPERTIES tablePropertyList)? #setTableSerDe
| ALTER TABLE multipartIdentifier (partitionSpec)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2872,6 +2872,34 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
Option(ctx.colPosition).map(typedVisit[ColumnPosition]))
}

/**
* Parse a [[AlterTableAlterColumnStatement]] command. This is Hive SQL syntax.
*
* For example:
* {{{
* ALTER TABLE table [PARTITION partition_spec]
* CHANGE [COLUMN] column_old_name column_new_name column_dataType [COMMENT column_comment]
* [FIRST | AFTER column_name];
* }}}
*/
override def visitHiveChangeColumn(ctx: HiveChangeColumnContext): LogicalPlan = withOrigin(ctx) {
if (ctx.partitionSpec != null) {
operationNotAllowed("ALTER TABLE table PARTITION partition_spec CHANGE COLUMN", ctx)
}
val columnNameParts = typedVisit[Seq[String]](ctx.colName)
if (!conf.resolver(columnNameParts.last, ctx.colType().colName.getText)) {
throw new AnalysisException("Renaming column is not supported in Hive-style ALTER COLUMN, " +
"please run RENAME COLUMN instead.")
}

AlterTableAlterColumnStatement(
typedVisit[Seq[String]](ctx.table),
columnNameParts,
Option(ctx.colType().dataType()).map(typedVisit[DataType]),
Option(ctx.colType().STRING()).map(string),
Option(ctx.colPosition).map(typedVisit[ColumnPosition]))
}

/**
* Parse a [[AlterTableDropColumnsStatement]] command.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,46 @@ class DDLParserSuite extends AnalysisTest {
}
}

test("alter table: hive style") {
val sql1 = "ALTER TABLE table_name CHANGE COLUMN a.b.c c INT"
val sql2 = "ALTER TABLE table_name CHANGE COLUMN a.b.c c INT COMMENT 'new_comment'"
val sql3 = "ALTER TABLE table_name CHANGE COLUMN a.b.c c INT AFTER other_col"

comparePlans(
parsePlan(sql1),
AlterTableAlterColumnStatement(
Seq("table_name"),
Seq("a", "b", "c"),
Some(IntegerType),
None,
None))

comparePlans(
parsePlan(sql2),
AlterTableAlterColumnStatement(
Seq("table_name"),
Seq("a", "b", "c"),
Some(IntegerType),
Some("new_comment"),
None))

comparePlans(
parsePlan(sql3),
AlterTableAlterColumnStatement(
Seq("table_name"),
Seq("a", "b", "c"),
Some(IntegerType),
None,
Some(after("other_col"))))

// renaming column not supported in hive style ALTER COLUMN.
intercept("ALTER TABLE table_name CHANGE COLUMN a.b.c new_name INT",
"please run RENAME COLUMN instead")

// ALTER COLUMN for a partition is not supported.
intercept("ALTER TABLE table_name PARTITION (a='1') CHANGE COLUMN a.b.c c INT")
}

test("alter table/view: rename table/view") {
comparePlans(
parsePlan("ALTER TABLE a.b.c RENAME TO x.y.z"),
Expand Down