Skip to content

Commit

Permalink
[hive] Improve hive database is exists yet create/drop database (apac…
Browse files Browse the repository at this point in the history
  • Loading branch information
XuQianJin-Stars authored Jan 3, 2024
1 parent 09f0292 commit b3fd931
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void dropPartition(Identifier identifier, Map<String, String> partitionSp
Collections.singletonList(partitionSpec), BatchWriteBuilder.COMMIT_IDENTIFIER);
}

protected abstract void createDatabaseImpl(String name);
protected abstract void createDatabaseImpl(String name) throws DatabaseAlreadyExistException;

@Override
public void dropDatabase(String name, boolean ignoreIfNotExists, boolean cascade)
Expand All @@ -134,14 +134,14 @@ public void dropDatabase(String name, boolean ignoreIfNotExists, boolean cascade
throw new DatabaseNotExistException(name);
}

if (!cascade && listTables(name).size() > 0) {
if (!cascade && !listTables(name).isEmpty()) {
throw new DatabaseNotEmptyException(name);
}

dropDatabaseImpl(name);
}

protected abstract void dropDatabaseImpl(String name);
protected abstract void dropDatabaseImpl(String name) throws DatabaseNotExistException;

@Override
public List<String> listTables(String databaseName) throws DatabaseNotExistException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ protected boolean databaseExistsImpl(String databaseName) {
}

@Override
protected void createDatabaseImpl(String name) {
public void createDatabaseImpl(String name) {
uncheck(() -> fileIO.mkdirs(newDatabasePath(name)));
}

@Override
protected void dropDatabaseImpl(String name) {
public void dropDatabaseImpl(String name) {
uncheck(() -> fileIO.delete(newDatabasePath(name), true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public abstract class CatalogTestBase {
@TempDir java.nio.file.Path tempFile;
protected String warehouse;
protected FileIO fileIO;
protected Catalog catalog;
protected AbstractCatalog catalog;
protected static final Schema DEFAULT_TABLE_SCHEMA =
new Schema(
Lists.newArrayList(
Expand Down Expand Up @@ -140,6 +140,21 @@ public void testCreateDatabase() throws Exception {
.doesNotThrowAnyException();
}

@Test
public void testCreateDatabaseImpl() throws Exception {
// Create database creates a new database when it does not exist
catalog.createDatabaseImpl("new_db");
boolean exists = catalog.databaseExists("new_db");
assertThat(exists).isTrue();

catalog.createDatabaseImpl("existing_db");

// Create database throws DatabaseAlreadyExistException when database already exists
assertThatExceptionOfType(Catalog.DatabaseAlreadyExistException.class)
.isThrownBy(() -> catalog.createDatabaseImpl("existing_db"))
.withMessage("Database existing_db already exists.");
}

@Test
public void testDropDatabase() throws Exception {
// Drop database deletes the database when it exists and there are no tables
Expand Down Expand Up @@ -173,6 +188,29 @@ public void testDropDatabase() throws Exception {
.withMessage("Database db_with_tables is not empty.");
}

@Test
public void testDropDatabaseImpl() throws Exception {
// Drop database deletes the database when it exists and there are no tables
catalog.createDatabase("db_to_drop", false);
catalog.dropDatabase("db_to_drop", false, false);
boolean exists = catalog.databaseExists("db_to_drop");
assertThat(exists).isFalse();

// Drop database does throw exception when database does not exist
assertThatExceptionOfType(Catalog.DatabaseNotExistException.class)
.isThrownBy(() -> catalog.dropDatabaseImpl("non_existing_db"))
.withMessage("Database non_existing_db does not exist.");

// Drop database deletes all tables in the database
catalog.createDatabase("db_to_drop", false);
catalog.createTable(Identifier.create("db_to_drop", "table1"), DEFAULT_TABLE_SCHEMA, false);
catalog.createTable(Identifier.create("db_to_drop", "table2"), DEFAULT_TABLE_SCHEMA, false);

catalog.dropDatabaseImpl("db_to_drop");
exists = catalog.databaseExists("db_to_drop");
assertThat(exists).isFalse();
}

@Test
public void testListTables() throws Exception {
// List tables returns an empty list when there are no tables in the database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,43 +208,60 @@ public List<String> listDatabases() {
@Override
protected boolean databaseExistsImpl(String databaseName) {
try {
client.getDatabase(databaseName);
return true;
} catch (NoSuchObjectException e) {
return false;
return getDatabase(databaseName) != null;
} catch (TException e) {
throw new RuntimeException(
"Failed to determine if database " + databaseName + " exists", e);
}
}

@Override
protected void createDatabaseImpl(String name) {
protected void createDatabaseImpl(String name) throws DatabaseAlreadyExistException {
try {
Path databasePath = newDatabasePath(name);
locationHelper.createPathIfRequired(databasePath, fileIO);

Database database = new Database();
database.setName(name);
locationHelper.specifyDatabaseLocation(databasePath, database);
client.createDatabase(database);
Database database = getDatabase(name);
if (database == null) {
Path databasePath = newDatabasePath(name);
locationHelper.createPathIfRequired(databasePath, fileIO);

database = new Database();
database.setName(name);
locationHelper.specifyDatabaseLocation(databasePath, database);
client.createDatabase(database);
} else {
throw new DatabaseAlreadyExistException(name);
}
} catch (TException | IOException e) {
throw new RuntimeException("Failed to create database " + name, e);
}
}

@Override
protected void dropDatabaseImpl(String name) {
protected void dropDatabaseImpl(String name) throws DatabaseNotExistException {
try {
Database database = client.getDatabase(name);
String location = locationHelper.getDatabaseLocation(database);
locationHelper.dropPathIfRequired(new Path(location), fileIO);
client.dropDatabase(name, true, false, true);
Database database = getDatabase(name);
if (database != null) {
String location = locationHelper.getDatabaseLocation(database);
locationHelper.dropPathIfRequired(new Path(location), fileIO);
client.dropDatabase(name, true, false, true);
} else {
throw new DatabaseNotExistException(name);
}
} catch (TException | IOException e) {
throw new RuntimeException("Failed to drop database " + name, e);
}
}

private Database getDatabase(String databaseName) throws TException {
try {
return client.getDatabase(databaseName);
} catch (NoSuchObjectException e) {
return null;
} catch (TException e) {
throw new RuntimeException(
"Failed to determine if database " + databaseName + " exists", e);
}
}

@Override
protected List<String> listTablesImpl(String databaseName) {
try {
Expand Down

0 comments on commit b3fd931

Please sign in to comment.