Skip to content

Commit 68260f5

Browse files
committed
revert [SPARK-29680][SQL] Remove ALTER TABLE CHANGE COLUMN syntax
### What changes were proposed in this pull request? Revert #26338 , as the syntax is actually the [hive style ALTER COLUMN](https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-ChangeColumnName/Type/Position/Comment). This PR brings it back, and make it support multi-catalog: 1. renaming is not allowed as `AlterTableAlterColumnStatement` can't do renaming. 2. column name should be multi-part ### Why are the changes needed? to not break hive compatibility. ### Does this PR introduce any user-facing change? no, as the removal was merged in 3.0. ### How was this patch tested? new parser tests Closes #27076 from cloud-fan/alter. Authored-by: Wenchen Fan <wenchen@databricks.com> Signed-off-by: Wenchen Fan <wenchen@databricks.com>
1 parent 1743d5b commit 68260f5

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ statement
161161
| ALTER TABLE table=multipartIdentifier
162162
(ALTER | CHANGE) COLUMN? column=multipartIdentifier
163163
(TYPE dataType)? (COMMENT comment=STRING)? colPosition? #alterTableColumn
164+
| ALTER TABLE table=multipartIdentifier partitionSpec?
165+
CHANGE COLUMN?
166+
colName=multipartIdentifier colType colPosition? #hiveChangeColumn
164167
| ALTER TABLE multipartIdentifier (partitionSpec)?
165168
SET SERDE STRING (WITH SERDEPROPERTIES tablePropertyList)? #setTableSerDe
166169
| ALTER TABLE multipartIdentifier (partitionSpec)?

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,6 +2872,34 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
28722872
Option(ctx.colPosition).map(typedVisit[ColumnPosition]))
28732873
}
28742874

2875+
/**
2876+
* Parse a [[AlterTableAlterColumnStatement]] command. This is Hive SQL syntax.
2877+
*
2878+
* For example:
2879+
* {{{
2880+
* ALTER TABLE table [PARTITION partition_spec]
2881+
* CHANGE [COLUMN] column_old_name column_new_name column_dataType [COMMENT column_comment]
2882+
* [FIRST | AFTER column_name];
2883+
* }}}
2884+
*/
2885+
override def visitHiveChangeColumn(ctx: HiveChangeColumnContext): LogicalPlan = withOrigin(ctx) {
2886+
if (ctx.partitionSpec != null) {
2887+
operationNotAllowed("ALTER TABLE table PARTITION partition_spec CHANGE COLUMN", ctx)
2888+
}
2889+
val columnNameParts = typedVisit[Seq[String]](ctx.colName)
2890+
if (!conf.resolver(columnNameParts.last, ctx.colType().colName.getText)) {
2891+
throw new AnalysisException("Renaming column is not supported in Hive-style ALTER COLUMN, " +
2892+
"please run RENAME COLUMN instead.")
2893+
}
2894+
2895+
AlterTableAlterColumnStatement(
2896+
typedVisit[Seq[String]](ctx.table),
2897+
columnNameParts,
2898+
Option(ctx.colType().dataType()).map(typedVisit[DataType]),
2899+
Option(ctx.colType().STRING()).map(string),
2900+
Option(ctx.colPosition).map(typedVisit[ColumnPosition]))
2901+
}
2902+
28752903
/**
28762904
* Parse a [[AlterTableDropColumnsStatement]] command.
28772905
*

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/DDLParserSuite.scala

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,46 @@ class DDLParserSuite extends AnalysisTest {
664664
}
665665
}
666666

667+
test("alter table: hive style") {
668+
val sql1 = "ALTER TABLE table_name CHANGE COLUMN a.b.c c INT"
669+
val sql2 = "ALTER TABLE table_name CHANGE COLUMN a.b.c c INT COMMENT 'new_comment'"
670+
val sql3 = "ALTER TABLE table_name CHANGE COLUMN a.b.c c INT AFTER other_col"
671+
672+
comparePlans(
673+
parsePlan(sql1),
674+
AlterTableAlterColumnStatement(
675+
Seq("table_name"),
676+
Seq("a", "b", "c"),
677+
Some(IntegerType),
678+
None,
679+
None))
680+
681+
comparePlans(
682+
parsePlan(sql2),
683+
AlterTableAlterColumnStatement(
684+
Seq("table_name"),
685+
Seq("a", "b", "c"),
686+
Some(IntegerType),
687+
Some("new_comment"),
688+
None))
689+
690+
comparePlans(
691+
parsePlan(sql3),
692+
AlterTableAlterColumnStatement(
693+
Seq("table_name"),
694+
Seq("a", "b", "c"),
695+
Some(IntegerType),
696+
None,
697+
Some(after("other_col"))))
698+
699+
// renaming column not supported in hive style ALTER COLUMN.
700+
intercept("ALTER TABLE table_name CHANGE COLUMN a.b.c new_name INT",
701+
"please run RENAME COLUMN instead")
702+
703+
// ALTER COLUMN for a partition is not supported.
704+
intercept("ALTER TABLE table_name PARTITION (a='1') CHANGE COLUMN a.b.c c INT")
705+
}
706+
667707
test("alter table/view: rename table/view") {
668708
comparePlans(
669709
parsePlan("ALTER TABLE a.b.c RENAME TO x.y.z"),

0 commit comments

Comments
 (0)