Skip to content

Commit 73c82fc

Browse files
committed
modify syntax
1 parent 57934d4 commit 73c82fc

File tree

6 files changed

+139
-159
lines changed

6 files changed

+139
-159
lines changed

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ statement
9090
| ALTER (TABLE | VIEW) tableIdentifier
9191
UNSET TBLPROPERTIES (IF EXISTS)? tablePropertyList #unsetTableProperties
9292
| ALTER TABLE tableIdentifier partitionSpec?
93-
CHANGE COLUMN? expandColTypeList #changeColumns
93+
CHANGE COLUMN? identifier colType colPosition? #changeColumn
9494
| ALTER TABLE tableIdentifier (partitionSpec)?
9595
SET SERDE STRING (WITH SERDEPROPERTIES tablePropertyList)? #setTableSerDe
9696
| ALTER TABLE tableIdentifier (partitionSpec)?
@@ -598,14 +598,6 @@ colType
598598
: identifier dataType (COMMENT STRING)?
599599
;
600600

601-
expandColTypeList
602-
: expandColType (',' expandColType)*
603-
;
604-
605-
expandColType
606-
: identifier colType colPosition?
607-
;
608-
609601
complexColTypeList
610602
: complexColType (',' complexColType)*
611603
;

sql/core/src/main/scala/org/apache/spark/sql/execution/SparkSqlParser.scala

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -877,32 +877,30 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder {
877877
}
878878

879879
/**
880-
* Create a [[AlterTableChangeColumnsCommand]] command.
880+
* Create a [[AlterTableChangeColumnCommand]] command.
881881
*
882882
* For example:
883883
* {{{
884884
* ALTER TABLE table [PARTITION partition_spec]
885-
* CHANGE [COLUMN] `col` `col` dataType [COMMENT "comment"] [FIRST | AFTER `otherCol`]
886-
* [, `col2` `col2` dataType [COMMENT "comment"] [FIRST | AFTER `otherCol`], ...]
885+
* CHANGE [COLUMN] column_old_name column_new_name column_dataType [COMMENT column_comment]
886+
* [FIRST | AFTER column_name];
887887
* }}}
888888
*/
889-
override def visitChangeColumns(ctx: ChangeColumnsContext): LogicalPlan = withOrigin(ctx) {
889+
override def visitChangeColumn(ctx: ChangeColumnContext): LogicalPlan = withOrigin(ctx) {
890890
if (ctx.partitionSpec != null) {
891891
operationNotAllowed("ALTER TABLE table PARTITION partition_spec CHANGE COLUMN", ctx)
892892
}
893893

894-
val tableName = visitTableIdentifier(ctx.tableIdentifier)
895-
896-
val columns = ctx.expandColTypeList.expandColType.asScala.map { col =>
897-
if (col.colPosition != null) {
898-
operationNotAllowed(
899-
"ALTER TABLE table [PARTITION partition_spec] CHANGE COLUMN ... FIRST | AFTER otherCol",
900-
ctx)
901-
}
902-
col.identifier.getText -> visitColType(col.colType)
903-
}.toMap
894+
if (ctx.colPosition != null) {
895+
operationNotAllowed(
896+
"ALTER TABLE table [PARTITION partition_spec] CHANGE COLUMN ... FIRST | AFTER otherCol",
897+
ctx)
898+
}
904899

905-
AlterTableChangeColumnsCommand(tableName, columns)
900+
AlterTableChangeColumnCommand(
901+
tableName = visitTableIdentifier(ctx.tableIdentifier),
902+
columnName = ctx.identifier.getText,
903+
newColumn = visitColType(ctx.colType))
906904
}
907905

908906
/**

sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ case class AlterTableUnsetPropertiesCommand(
276276

277277

278278
/**
279-
* A command to change the columns for a table, only support changing the comments of non-partition
280-
* columns for now.
279+
* A command to change the column for a table, only support changing the comment of a non-partition
280+
* column for now.
281281
*
282282
* The syntax of using this command in SQL is:
283283
* {{{
@@ -286,9 +286,10 @@ case class AlterTableUnsetPropertiesCommand(
286286
* [FIRST | AFTER column_name];
287287
* }}}
288288
*/
289-
case class AlterTableChangeColumnsCommand(
289+
case class AlterTableChangeColumnCommand(
290290
tableName: TableIdentifier,
291-
columns: Map[String, StructField]) extends RunnableCommand {
291+
columnName: String,
292+
newColumn: StructField) extends RunnableCommand {
292293

293294
// TODO: support change column name/dataType/metadata/position.
294295
override def run(sparkSession: SparkSession): Seq[Row] = {
@@ -297,26 +298,24 @@ case class AlterTableChangeColumnsCommand(
297298
val resolver = sparkSession.sessionState.conf.resolver
298299
DDLUtils.verifyAlterTableType(catalog, table, isView = false)
299300

300-
// Create a map that converts the origin column to the new column with changed comment, throw
301-
// an AnalysisException if the column reference is invalid or the column name/dataType is
302-
// changed.
303-
val columnsMap = columns.map { case (oldName: String, newField: StructField) =>
304-
// Find the origin column from schema by column name.
305-
val originColumn = findColumnByName(table.schema, oldName, resolver)
306-
// Throw an AnalysisException if the column name/dataType is changed.
307-
if (!columnEqual(originColumn, newField, resolver)) {
308-
throw new AnalysisException(
309-
"ALTER TABLE CHANGE COLUMN is not supported for changing column " +
310-
s"'${originColumn.name}' with type '${originColumn.dataType}' to " +
311-
s"'${newField.name}' with type '${newField.dataType}'")
312-
}
313-
// Create a new column from the origin column with the new comment.
314-
val newColumn = addComment(originColumn, newField.getComment)
315-
// Create the map from origin column to changed column
316-
originColumn.name -> newColumn
301+
// Find the origin column from schema by column name.
302+
val originColumn = findColumnByName(table.schema, columnName, resolver)
303+
// Throw an AnalysisException if the column name/dataType is changed.
304+
if (!columnEqual(originColumn, newColumn, resolver)) {
305+
throw new AnalysisException(
306+
"ALTER TABLE CHANGE COLUMN is not supported for changing column " +
307+
s"'${originColumn.name}' with type '${originColumn.dataType}' to " +
308+
s"'${newColumn.name}' with type '${newColumn.dataType}'")
317309
}
318310

319-
val newSchema = table.schema.fields.map(field => columnsMap.getOrElse(field.name, field))
311+
val newSchema = table.schema.fields.map { field =>
312+
if (field.name == originColumn.name) {
313+
// Create a new column from the origin column with the new comment.
314+
addComment(field, newColumn.getComment)
315+
} else {
316+
field
317+
}
318+
}
320319
val newTable = table.copy(schema = StructType(newSchema))
321320
catalog.alterTable(newTable)
322321

sql/core/src/test/resources/sql-tests/inputs/change-column.sql

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@ DESC test_change;
44

55
-- Change column name (not supported yet)
66
ALTER TABLE test_change CHANGE a a1 INT;
7-
ALTER TABLE test_change CHANGE b b1 STRING, c c1 INT;
87
DESC test_change;
98

109
-- Change column dataType (not supported yet)
1110
ALTER TABLE test_change CHANGE a a STRING;
12-
ALTER TABLE test_change CHANGE b b INT, c c DOUBLE;
1311
DESC test_change;
1412

1513
-- Change column position (not supported yet)
1614
ALTER TABLE test_change CHANGE a a INT AFTER b;
17-
ALTER TABLE test_change CHANGE b b STRING FIRST, c c INT AFTER b;
15+
ALTER TABLE test_change CHANGE b b STRING FIRST;
1816
DESC test_change;
1917

2018
-- Change column comment
2119
ALTER TABLE test_change CHANGE a a INT COMMENT 'this is column a';
22-
ALTER TABLE test_change CHANGE b b STRING COMMENT '#*02?`', c c INT COMMENT '';
20+
ALTER TABLE test_change CHANGE b b STRING COMMENT '#*02?`';
21+
ALTER TABLE test_change CHANGE c c INT COMMENT '';
2322
DESC test_change;
2423

2524
-- Don't change anything.

0 commit comments

Comments
 (0)