From 6b77e1338ef4f11bdb584dbecb9814f57f3924ef Mon Sep 17 00:00:00 2001 From: yantian Date: Mon, 16 Dec 2024 18:02:46 +0800 Subject: [PATCH] add alter database doc for flink and spark and support comment when create database in flink --- docs/content/flink/sql-alter.md | 8 ++++++++ docs/content/spark/sql-alter.md | 10 ++++++++++ .../java/org/apache/paimon/flink/FlinkCatalog.java | 12 ++++++------ .../org/apache/paimon/flink/FlinkCatalogTest.java | 9 +++++---- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/docs/content/flink/sql-alter.md b/docs/content/flink/sql-alter.md index 877995cc631b..645cb06ef365 100644 --- a/docs/content/flink/sql-alter.md +++ b/docs/content/flink/sql-alter.md @@ -227,3 +227,11 @@ The following SQL modifies the watermark strategy to `ts - INTERVAL '2' HOUR`. ```sql ALTER TABLE my_table MODIFY WATERMARK FOR ts AS ts - INTERVAL '2' HOUR ``` + +# ALTER DATABASE + +The following SQL sets one or more properties in the specified database. If a particular property is already set in the database, override the old value with the new one. + +```sql +ALTER DATABASE [catalog_name.]db_name SET (key1=val1, key2=val2, ...) +``` diff --git a/docs/content/spark/sql-alter.md b/docs/content/spark/sql-alter.md index 3ad72048029b..839f0d2f8ce4 100644 --- a/docs/content/spark/sql-alter.md +++ b/docs/content/spark/sql-alter.md @@ -240,3 +240,13 @@ The following SQL changes the type of a nested column `f2` to `BIGINT` in a stru -- column v previously has type MAP> ALTER TABLE my_table ALTER COLUMN v.value.f2 TYPE BIGINT; ``` + + +# ALTER DATABASE + +The following SQL sets one or more properties in the specified database. If a particular property is already set in the database, override the old value with the new one. + +```sql +ALTER { DATABASE | SCHEMA | NAMESPACE } my_database + SET { DBPROPERTIES | PROPERTIES } ( property_name = property_value [ , ... ] ) +``` \ No newline at end of file diff --git a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/FlinkCatalog.java b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/FlinkCatalog.java index 7a2e996d44a1..2d57b2ef59ca 100644 --- a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/FlinkCatalog.java +++ b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/FlinkCatalog.java @@ -239,19 +239,19 @@ public CatalogDatabase getDatabase(String databaseName) @Override public void createDatabase(String name, CatalogDatabase database, boolean ignoreIfExists) throws DatabaseAlreadyExistException, CatalogException { + Map properties; if (database != null) { + properties = new HashMap<>(database.getProperties()); if (database.getDescription().isPresent() && !database.getDescription().get().equals("")) { - throw new UnsupportedOperationException( - "Create database with description is unsupported."); + properties.put(Catalog.COMMENT_PROP, database.getDescription().get()); } + } else { + properties = Collections.emptyMap(); } try { - catalog.createDatabase( - name, - ignoreIfExists, - database == null ? Collections.emptyMap() : database.getProperties()); + catalog.createDatabase(name, ignoreIfExists, properties); } catch (Catalog.DatabaseAlreadyExistException e) { throw new DatabaseAlreadyExistException(getName(), e.database()); } diff --git a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/FlinkCatalogTest.java b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/FlinkCatalogTest.java index c0f81828e1d3..fb36f7ad77b1 100644 --- a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/FlinkCatalogTest.java +++ b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/FlinkCatalogTest.java @@ -99,6 +99,7 @@ import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatCollection; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.spy; @@ -581,11 +582,9 @@ public void testCreateDb_DatabaseWithProperties() throws Exception { } @Test - public void testCreateDb_DatabaseWithCommentException() { + public void testCreateDb_DatabaseWithCommentSuccessful() throws DatabaseAlreadyExistException { CatalogDatabaseImpl database = new CatalogDatabaseImpl(Collections.emptyMap(), "haha"); - assertThatThrownBy(() -> catalog.createDatabase(path1.getDatabaseName(), database, false)) - .isInstanceOf(UnsupportedOperationException.class) - .hasMessage("Create database with description is unsupported."); + assertDoesNotThrow(() -> catalog.createDatabase(path1.getDatabaseName(), database, false)); } @ParameterizedTest @@ -612,6 +611,8 @@ public void testAlterDb() throws DatabaseAlreadyExistException, DatabaseNotExist catalog.createDatabase(path1.getDatabaseName(), database, false); Map properties = Collections.singletonMap("haa", "ccc"); CatalogDatabaseImpl newDatabase = new CatalogDatabaseImpl(properties, "haha"); + // as file system catalog don't support alter database, so we have to use mock to overview + // this method to test Catalog mockCatalog = spy(catalog); doNothing().when(mockCatalog).alterDatabase(path1.getDatabaseName(), newDatabase, false); when(mockCatalog.getDatabase(path1.getDatabaseName())).thenReturn(database);