lsyldliu commented on code in PR #19329:
URL: https://github.com/apache/flink/pull/19329#discussion_r848011475
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/operations/SqlToOperationConverterTest.java:
##########
@@ -1269,6 +1271,112 @@ public void testAlterTable() throws Exception {
.hasMessageContaining("ALTER TABLE RESET does not support
empty key");
}
+ @Test
+ public void testAlterTableRenameColumn() throws Exception {
+ Catalog catalog = new GenericInMemoryCatalog("default", "default");
+ catalogManager.registerCatalog("cat1", catalog);
+ functionCatalog.registerTempCatalogScalarFunction(
+ ObjectIdentifier.of("cat1", "default", "my_udf1"),
Func0$.MODULE$);
+
+ catalog.createDatabase("db1", new CatalogDatabaseImpl(new HashMap<>(),
null), true);
+ CatalogTable catalogTable =
+ CatalogTable.of(
+ Schema.newBuilder()
+ .column("a", DataTypes.STRING().notNull())
+ .column("b", DataTypes.INT().notNull())
+ .column("c", DataTypes.INT())
+ .column("d", DataTypes.INT())
+ .column("e", DataTypes.STRING())
+ .column("f", DataTypes.TIMESTAMP(3).notNull())
+ .columnByExpression("h", "c - 1")
+ .columnByExpression("i",
"cat1.`default`.my_udf1(d) + 1")
+ .columnByMetadata("m", DataTypes.INT(), true)
+ .columnByExpression("g", "TO_TIMESTAMP(e)")
+ .watermark("f", "g - INTERVAL '5' SECOND")
+ .primaryKey("a", "b")
+ .build(),
+ "tb1",
+ Collections.emptyList(),
+ Collections.emptyMap());
+ catalogManager.setCurrentCatalog("cat1");
+ catalogManager.setCurrentDatabase("db1");
+ catalog.createTable(new ObjectPath("db1", "tb1"), catalogTable, true);
+ // Test alter table rename
+ // rename column b
+ Operation operation = parse("alter table tb1 rename b to b1",
SqlDialect.DEFAULT);
+ assert operation instanceof AlterTableSchemaOperation;
+ Schema actual =
+ ((AlterTableSchemaOperation)
operation).getCatalogTable().getUnresolvedSchema();
+ Schema expected =
+ Schema.newBuilder()
+ .column("a", DataTypes.STRING().notNull())
+ .column("b1", DataTypes.INT().notNull())
+ .column("c", DataTypes.INT())
+ .column("d", DataTypes.INT())
+ .column("e", DataTypes.STRING())
+ .column("f", DataTypes.TIMESTAMP(3).notNull())
+ .columnByExpression("h", "c - 1")
+ .columnByExpression("i", "cat1.`default`.my_udf1(d) +
1")
+ .columnByMetadata("m", DataTypes.INT(), true)
+ .columnByExpression("g", "TO_TIMESTAMP(e)")
+ .watermark("f", "g - INTERVAL '5' SECOND")
+ .primaryKeyNamed("PK_a_b", "a", "b1")
+ .build();
+ assertThat(expected).isEqualTo(actual);
+
+ // rename column c test computed column case1
+ assertThatThrownBy(() -> parse("alter table tb1 rename c to c1",
SqlDialect.DEFAULT))
+ .isInstanceOf(ValidationException.class)
+ .hasMessageContaining(
+ "Old column c is referenced by computed column `h` INT
AS c - 1, "
+ + "currently doesn't allow to rename column
which is referenced by computed column.");
+
+ // rename column d test computed column case2
+ assertThatThrownBy(() -> parse("alter table tb1 rename d to d1",
SqlDialect.DEFAULT))
+ .isInstanceOf(ValidationException.class)
+ .hasMessageContaining(
+ "Old column d is referenced by computed column `i` INT
NOT NULL AS cat1.`default`.my_udf1(d) + 1,"
+ + " currently doesn't allow to rename column
which is referenced by computed column.");
+
+ // rename column e test computed column case3
+ CatalogTable catalogTable2 =
Review Comment:
This case in order to validate the computed column `j` is created by
`ApiExpression`, this class doesn't implement the `equals` method, so we can't
reuse the `tb1`, otherwise, the previous test will fail.
--
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]