LadyForest commented on code in PR #20652:
URL: https://github.com/apache/flink/pull/20652#discussion_r990178647
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/operations/SqlToOperationConverterTest.java:
##########
@@ -1466,6 +1477,404 @@ public void
testAlterTableCompactOnManagedPartitionedTable() throws Exception {
parse("alter table tb1 compact", SqlDialect.DEFAULT),
staticPartitions);
}
+ @Test
+ public void testAlterTableAddColumn() throws Exception {
+ prepareNonManagedTable(false);
+ ObjectIdentifier tableIdentifier = ObjectIdentifier.of("cat1", "db1",
"tb1");
+ Schema originalSchema =
+
catalogManager.getTable(tableIdentifier).get().getTable().getUnresolvedSchema();
+
+ // test duplicated column name
+ assertThatThrownBy(() -> parse("alter table tb1 add a bigint",
SqlDialect.DEFAULT))
+ .isInstanceOf(ValidationException.class)
+ .hasMessageContaining("Try to add a column 'a' which already
exists in the table.");
+
+ // test reference nonexistent column name
+ assertThatThrownBy(() -> parse("alter table tb1 add x bigint after y",
SqlDialect.DEFAULT))
+ .isInstanceOf(ValidationException.class)
+ .hasMessageContaining(
+ "Referenced column 'y' by 'AFTER' does not exist in
the table.");
+
+ // test add a single column
+ Operation operation =
+ parse(
+ "alter table tb1 add d double not null comment 'd is
double not null'",
+ SqlDialect.DEFAULT);
+ assertAlterTableSchema(
+ operation,
+ tableIdentifier,
+ Schema.newBuilder()
+ .fromSchema(originalSchema)
+ .column("d", DataTypes.DOUBLE().notNull())
+ .withComment("d is double not null")
+ .build());
+
+ // test add multiple columns with pk
+ operation =
Review Comment:
MySQL will throw the exception, and we've aligned the behavior. I can add a
case to verify.
```sql
mysql> show create table foo;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| foo | CREATE TABLE `foo` (
`a` int DEFAULT NULL,
`b` varchar(20) DEFAULT NULL,
`c` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
mysql> alter table foo add e int after f, add f double;
ERROR 1054 (42S22): Unknown column 'f' in 'foo'
```
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]