Skip to content

Commit

Permalink
update alter database test and add alter column type in RESTCatalogIT…
Browse files Browse the repository at this point in the history
…Case
  • Loading branch information
jerry-024 committed Jan 9, 2025
1 parent 1fc3b37 commit feef4c2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.paimon.options.Options;
import org.apache.paimon.schema.Schema;
import org.apache.paimon.schema.SchemaChange;
import org.apache.paimon.table.FileStoreTable;
import org.apache.paimon.table.FormatTable;
import org.apache.paimon.table.Table;
import org.apache.paimon.types.DataField;
Expand Down Expand Up @@ -146,6 +147,57 @@ public void testCreateDatabase() throws Exception {
.doesNotThrowAnyException();
}

@Test
public void testAlterDatabase() throws Exception {
if (supportsAlterDatabase()) {
// Alter database
String databaseName = "db_to_alter";
catalog.createDatabase(databaseName, false);
String key = "key1";
String key2 = "key2";
// Add property
catalog.alterDatabase(
databaseName,
Lists.newArrayList(
PropertyChange.setProperty(key, "value"),
PropertyChange.setProperty(key2, "value")),
false);
Database db = catalog.getDatabase(databaseName);
assertEquals("value", db.options().get(key));
assertEquals("value", db.options().get(key2));
// Update property
catalog.alterDatabase(
databaseName,
Lists.newArrayList(
PropertyChange.setProperty(key, "value1"),
PropertyChange.setProperty(key2, "value1")),
false);
db = catalog.getDatabase(databaseName);
assertEquals("value1", db.options().get(key));
assertEquals("value1", db.options().get(key2));
// remove property
catalog.alterDatabase(
databaseName,
Lists.newArrayList(
PropertyChange.removeProperty(key),
PropertyChange.removeProperty(key2)),
false);
db = catalog.getDatabase(databaseName);
assertEquals(false, db.options().containsKey(key));
assertEquals(false, db.options().containsKey(key2));
// Remove non-existent property
catalog.alterDatabase(
databaseName,
Lists.newArrayList(
PropertyChange.removeProperty(key),
PropertyChange.removeProperty(key2)),
false);
db = catalog.getDatabase(databaseName);
assertEquals(false, db.options().containsKey(key));
assertEquals(false, db.options().containsKey(key2));
}
}

@Test
public void testDropDatabase() throws Exception {
// Drop database deletes the database when it exists and there are no tables
Expand Down Expand Up @@ -193,6 +245,10 @@ public void testListTables() throws Exception {

tables = catalog.listTables("test_db");
assertThat(tables).containsExactlyInAnyOrder("table1", "table2", "table3");

// List tables throws DatabaseNotExistException when the database does not exist
assertThatExceptionOfType(Catalog.DatabaseNotExistException.class)
.isThrownBy(() -> catalog.listTables("non_existing_db"));
}

@Test
Expand Down Expand Up @@ -317,8 +373,8 @@ public void testGetTable() throws Exception {
catalog.createTable(identifier, DEFAULT_TABLE_SCHEMA, false);
Table systemTable = catalog.getTable(Identifier.create("test_db", "test_table$snapshots"));
assertThat(systemTable).isNotNull();
Table dataTable = catalog.getTable(identifier);
assertThat(dataTable).isNotNull();
FileStoreTable dataTable = (FileStoreTable) catalog.getTable(identifier);
assertThat(dataTable.schema().toSchema().fields()).isEqualTo(DEFAULT_TABLE_SCHEMA.fields());

// Get system table throws Exception when table contains multiple '$' separator
assertThatExceptionOfType(IllegalArgumentException.class)
Expand Down Expand Up @@ -956,49 +1012,7 @@ public void testTableUUID() throws Exception {
.isGreaterThan(0);
}

protected void alterDatabaseWhenSupportAlter() throws Exception {
// Alter database
String databaseName = "db_to_alter";
catalog.createDatabase(databaseName, false);
String key = "key1";
String key2 = "key2";
// Add property
catalog.alterDatabase(
databaseName,
Lists.newArrayList(
PropertyChange.setProperty(key, "value"),
PropertyChange.setProperty(key2, "value")),
false);
Database db = catalog.getDatabase(databaseName);
assertEquals("value", db.options().get(key));
assertEquals("value", db.options().get(key2));
// Update property
catalog.alterDatabase(
databaseName,
Lists.newArrayList(
PropertyChange.setProperty(key, "value1"),
PropertyChange.setProperty(key2, "value1")),
false);
db = catalog.getDatabase(databaseName);
assertEquals("value1", db.options().get(key));
assertEquals("value1", db.options().get(key2));
// remove property
catalog.alterDatabase(
databaseName,
Lists.newArrayList(
PropertyChange.removeProperty(key), PropertyChange.removeProperty(key2)),
false);
db = catalog.getDatabase(databaseName);
assertEquals(false, db.options().containsKey(key));
assertEquals(false, db.options().containsKey(key2));
// Remove non-existent property
catalog.alterDatabase(
databaseName,
Lists.newArrayList(
PropertyChange.removeProperty(key), PropertyChange.removeProperty(key2)),
false);
db = catalog.getDatabase(databaseName);
assertEquals(false, db.options().containsKey(key));
assertEquals(false, db.options().containsKey(key2));
protected boolean supportsAlterDatabase() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public void testSerializeTable() throws Exception {
});
}

@Test
public void testAlterDatabase() throws Exception {
this.alterDatabaseWhenSupportAlter();
@Override
protected boolean supportsAlterDatabase() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void testInitFailWhenDefineWarehouse() {
}

@Test
public void testAuthFail() {
void testAuthFail() {
Options options = new Options();
options.set(RESTCatalogOptions.URI, restCatalogServer.getUrl());
options.set(RESTCatalogOptions.TOKEN, "aaaaa");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class RESTCatalogITCase extends CatalogITCaseBase {
private static final String databaseName = "mydb";
private static final String tableName = "t1";

RESTCatalogServer RESTCatalogServer;
RESTCatalogServer restCatalogServer;
private String serverUrl;
protected String warehouse;
@TempDir java.nio.file.Path tempFile;
Expand All @@ -49,9 +49,9 @@ public class RESTCatalogITCase extends CatalogITCaseBase {
public void before() throws IOException {
String initToken = "init_token";
warehouse = tempFile.toUri().toString();
RESTCatalogServer = new RESTCatalogServer(warehouse, initToken);
RESTCatalogServer.start();
serverUrl = RESTCatalogServer.getUrl();
restCatalogServer = new RESTCatalogServer(warehouse, initToken);
restCatalogServer.start();
serverUrl = restCatalogServer.getUrl();
super.before();
sql(String.format("CREATE DATABASE %s", databaseName));
sql(String.format("CREATE TABLE %s.%s (a STRING, b DOUBLE)", databaseName, tableName));
Expand All @@ -61,7 +61,7 @@ public void before() throws IOException {
public void after() throws IOException {
sql(String.format("DROP TABLE %s.%s", databaseName, tableName));
sql(String.format("DROP DATABASE %s", databaseName));
RESTCatalogServer.shutdown();
restCatalogServer.shutdown();
}

@Test
Expand All @@ -81,13 +81,14 @@ public void testAlterTable() {
sql(String.format("ALTER TABLE %s.%s ADD e INT AFTER b", databaseName, tableName));
sql(String.format("ALTER TABLE %s.%s DROP b", databaseName, tableName));
sql(String.format("ALTER TABLE %s.%s RENAME a TO a1", databaseName, tableName));
sql(String.format("ALTER TABLE %s.%s MODIFY e DOUBLE", databaseName, tableName));
List<Row> result = sql(String.format("SHOW CREATE TABLE %s.%s", databaseName, tableName));
assertThat(result.toString())
.contains(
String.format(
"CREATE TABLE `PAIMON`.`%s`.`%s` (\n"
+ " `a1` VARCHAR(2147483647),\n"
+ " `e` INT",
+ " `e` DOUBLE",
databaseName, tableName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@ private void testHiveConfDirFromEnvImpl() {
assertThat(hiveConf.get("hive.metastore.uris")).isEqualTo("dummy-hms");
}

@Test
public void testAlterDatabase() throws Exception {
this.alterDatabaseWhenSupportAlter();
}

@Test
public void testAddHiveTableParameters() {
try {
Expand Down Expand Up @@ -503,4 +498,9 @@ public void testPartitionTable() throws Exception {
// hive catalog list partitions from filesystem, so here return empty.
assertThat(catalog.listPartitions(identifier)).isEmpty();
}

@Override
protected boolean supportsAlterDatabase() {
return true;
}
}

0 comments on commit feef4c2

Please sign in to comment.