From de07967e89b69d016fcee9df222d0d351357bbd0 Mon Sep 17 00:00:00 2001 From: yuqi Date: Wed, 20 Mar 2024 21:18:59 +0800 Subject: [PATCH 1/7] Remove depends on Jdbc system table. --- .../operation/JdbcDatabaseOperations.java | 6 +++ .../operation/MysqlDatabaseOperations.java | 45 ++++++++----------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java index 6c0be7351c3..b7edcb0d259 100644 --- a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java +++ b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java @@ -77,6 +77,12 @@ public List listDatabases() { } } + protected ResultSet getDatabase(Connection connection, String databaseName) throws SQLException { + final DatabaseMetaData metaData = connection.getMetaData(); + // It's indeed the catalog name, not the schema name. + return metaData.getCatalogs(); + } + /** * @param databaseName The name of the database. * @param comment The comment of the database. diff --git a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java index ca6a6eb74fc..6fc72f8634c 100644 --- a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java +++ b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java @@ -9,16 +9,16 @@ import com.datastrato.gravitino.catalog.jdbc.operation.JdbcDatabaseOperations; import com.datastrato.gravitino.exceptions.NoSuchSchemaException; import com.datastrato.gravitino.meta.AuditInfo; +import com.google.common.collect.ImmutableMap; import java.sql.Connection; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Set; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; @@ -93,33 +93,26 @@ public String generateDropDatabaseSql(String databaseName, boolean cascade) { @Override public JdbcSchema load(String databaseName) throws NoSuchSchemaException { try (final Connection connection = this.dataSource.getConnection()) { - String query = "SELECT * FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ?"; - try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { - preparedStatement.setString(1, databaseName); + ResultSet resultSet = getDatabase(connection, databaseName); - // Execute the query - try (ResultSet resultSet = preparedStatement.executeQuery()) { - if (!resultSet.next()) { - throw new NoSuchSchemaException( - "Database %s could not be found in information_schema.SCHEMATA", databaseName); - } - String schemaName = resultSet.getString("SCHEMA_NAME"); - // Mysql currently only supports these two attributes - String characterSetName = resultSet.getString("DEFAULT_CHARACTER_SET_NAME"); - String collationName = resultSet.getString("DEFAULT_COLLATION_NAME"); - Map properties = new HashMap<>(); - properties.put("CHARACTER SET", characterSetName); - properties.put("COLLATE", collationName); - - JdbcSchema.Builder builder = - JdbcSchema.builder() - .withName(schemaName) - .withProperties(properties) - .withAuditInfo(AuditInfo.EMPTY); - - return builder.build(); + boolean found = false; + while (resultSet.next()) { + if (Objects.equals(resultSet.getString(1), databaseName)) { + found = true; + break; } } + + if (!found) { + throw new NoSuchSchemaException("Database %s could not be found", databaseName); + } + + return JdbcSchema.builder() + .withName(databaseName) + .withProperties(ImmutableMap.of()) + .withAuditInfo(AuditInfo.EMPTY) + .build(); + } catch (final SQLException se) { throw this.exceptionMapper.toGravitinoException(se); } From b563e9a4e6fa6ffd6d06dcd18d91b1608bf809c3 Mon Sep 17 00:00:00 2001 From: yuqi Date: Thu, 21 Mar 2024 20:03:05 +0800 Subject: [PATCH 2/7] Fix comments again. --- .../operation/JdbcDatabaseOperations.java | 6 +- .../operation/SqliteDatabaseOperations.java | 6 ++ .../operation/DorisDatabaseOperations.java | 8 +++ .../operation/MysqlDatabaseOperations.java | 11 +++- .../operation/PostgreSqlSchemaOperations.java | 59 ++++++++++++++----- 5 files changed, 69 insertions(+), 21 deletions(-) diff --git a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java index b7edcb0d259..0eacb510765 100644 --- a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java +++ b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java @@ -77,11 +77,7 @@ public List listDatabases() { } } - protected ResultSet getDatabase(Connection connection, String databaseName) throws SQLException { - final DatabaseMetaData metaData = connection.getMetaData(); - // It's indeed the catalog name, not the schema name. - return metaData.getCatalogs(); - } + protected abstract ResultSet getSchema(Connection connection, String schemaName) throws SQLException; /** * @param databaseName The name of the database. diff --git a/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/operation/SqliteDatabaseOperations.java b/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/operation/SqliteDatabaseOperations.java index b8fd9ad9f06..4e9915f4ca0 100644 --- a/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/operation/SqliteDatabaseOperations.java +++ b/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/operation/SqliteDatabaseOperations.java @@ -15,6 +15,7 @@ import java.io.File; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arrays; import java.util.List; @@ -91,4 +92,9 @@ public String generateCreateDatabaseSql( public String generateDropDatabaseSql(String databaseName, boolean cascade) { return null; } + + @Override + protected ResultSet getSchema(Connection connection, String schemaName) throws SQLException { + throw new UnsupportedOperationException("Not supported for SQLite"); + } } diff --git a/catalogs/catalog-jdbc-doris/src/main/java/com/datastrato/gravitino/catalog/doris/operation/DorisDatabaseOperations.java b/catalogs/catalog-jdbc-doris/src/main/java/com/datastrato/gravitino/catalog/doris/operation/DorisDatabaseOperations.java index 82d4b1cac09..10bfc84243a 100644 --- a/catalogs/catalog-jdbc-doris/src/main/java/com/datastrato/gravitino/catalog/doris/operation/DorisDatabaseOperations.java +++ b/catalogs/catalog-jdbc-doris/src/main/java/com/datastrato/gravitino/catalog/doris/operation/DorisDatabaseOperations.java @@ -7,6 +7,9 @@ import com.datastrato.gravitino.catalog.jdbc.JdbcSchema; import com.datastrato.gravitino.catalog.jdbc.operation.JdbcDatabaseOperations; import com.datastrato.gravitino.exceptions.NoSuchSchemaException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.Map; /** Database operations for Doris. */ @@ -28,4 +31,9 @@ protected String generateCreateDatabaseSql( protected String generateDropDatabaseSql(String databaseName, boolean cascade) { throw new UnsupportedOperationException(); } + + @Override + protected ResultSet getSchema(Connection connection, String schemaName) throws SQLException { + throw new UnsupportedOperationException(); + } } diff --git a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java index 6fc72f8634c..67bf5f2f468 100644 --- a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java +++ b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java @@ -11,6 +11,7 @@ import com.datastrato.gravitino.meta.AuditInfo; import com.google.common.collect.ImmutableMap; import java.sql.Connection; +import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -91,9 +92,17 @@ public String generateDropDatabaseSql(String databaseName, boolean cascade) { } @Override + protected ResultSet getSchema(Connection connection, String databaseName) throws SQLException { + final DatabaseMetaData metaData = connection.getMetaData(); + // It'd indeed need to call getCatalogs() to get the schema not `getSchemas()` for MySQL. + return metaData.getCatalogs(); + } + + + @Override public JdbcSchema load(String databaseName) throws NoSuchSchemaException { try (final Connection connection = this.dataSource.getConnection()) { - ResultSet resultSet = getDatabase(connection, databaseName); + ResultSet resultSet = getSchema(connection, databaseName); boolean found = false; while (resultSet.next()) { diff --git a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java index b0a9e106a6a..c9394103069 100644 --- a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java +++ b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java @@ -11,6 +11,7 @@ import com.datastrato.gravitino.exceptions.NoSuchSchemaException; import com.datastrato.gravitino.meta.AuditInfo; import java.sql.Connection; +import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -20,6 +21,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Set; import javax.sql.DataSource; import org.apache.commons.collections4.MapUtils; @@ -50,25 +52,45 @@ public void initialize( @Override public JdbcSchema load(String schema) throws NoSuchSchemaException { try (Connection connection = getConnection()) { - String sql = - "SELECT schema_name FROM information_schema.schemata WHERE schema_name = ? AND catalog_name = ?"; - try (PreparedStatement statement = connection.prepareStatement(sql)) { - statement.setString(1, schema); - statement.setString(2, database); - try (ResultSet resultSet = statement.executeQuery()) { - if (!resultSet.next()) { - throw new NoSuchSchemaException("No such schema: %s", schema); - } - String schemaName = resultSet.getString(1); - String comment = getSchemaComment(schema, connection); - return JdbcSchema.builder() - .withName(schemaName) +// String sql = +// "SELECT schema_name FROM information_schema.schemata WHERE schema_name = ? AND catalog_name = ?"; +// try (PreparedStatement statement = connection.prepareStatement(sql)) { +// statement.setString(1, schema); +// statement.setString(2, database); +// try (ResultSet resultSet = statement.executeQuery()) { +// if (!resultSet.next()) { +// throw new NoSuchSchemaException("No such schema: %s", schema); +// } +// String schemaName = resultSet.getString(1); +// String comment = getSchemaComment(schema, connection); +// return JdbcSchema.builder() +// .withName(schemaName) +// .withComment(comment) +// .withAuditInfo(AuditInfo.EMPTY) +// .withProperties(Collections.emptyMap()) +// .build(); +// } + ResultSet resultSet = getSchema(connection, schema); + + boolean found = false; + while (resultSet.next()) { + if (Objects.equals(resultSet.getString(1), schema)) { + found = true; + break; + } + } + + if (!found) { + throw new NoSuchSchemaException("No such schema: %s", schema); + } + + String comment = getSchemaComment(schema, connection); + return JdbcSchema.builder() + .withName(schema) .withComment(comment) .withAuditInfo(AuditInfo.EMPTY) .withProperties(Collections.emptyMap()) .build(); - } - } } catch (SQLException e) { throw exceptionMapper.toGravitinoException(e); } @@ -116,6 +138,13 @@ public String generateCreateDatabaseSql( return sqlBuilder.toString(); } + + @Override + protected ResultSet getSchema(Connection connection, String databaseName) throws SQLException { + final DatabaseMetaData metaData = connection.getMetaData(); + return metaData.getSchemas(database, databaseName); + } + @Override public String generateDropDatabaseSql(String schema, boolean cascade) { StringBuilder sqlBuilder = new StringBuilder(String.format("DROP SCHEMA %s", schema)); From 5b7a2181491fd8968fc2b41a4996cd8c6eb2002b Mon Sep 17 00:00:00 2001 From: yuqi Date: Thu, 21 Mar 2024 20:11:46 +0800 Subject: [PATCH 3/7] Fix comments again. --- .../operation/JdbcDatabaseOperations.java | 3 +- .../operation/PostgreSqlSchemaOperations.java | 29 ++++--------------- 2 files changed, 7 insertions(+), 25 deletions(-) diff --git a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java index 0eacb510765..19683e2459c 100644 --- a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java +++ b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java @@ -77,7 +77,8 @@ public List listDatabases() { } } - protected abstract ResultSet getSchema(Connection connection, String schemaName) throws SQLException; + protected abstract ResultSet getSchema(Connection connection, String schemaName) + throws SQLException; /** * @param databaseName The name of the database. diff --git a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java index c9394103069..ca640e5c6e5 100644 --- a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java +++ b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java @@ -52,24 +52,6 @@ public void initialize( @Override public JdbcSchema load(String schema) throws NoSuchSchemaException { try (Connection connection = getConnection()) { -// String sql = -// "SELECT schema_name FROM information_schema.schemata WHERE schema_name = ? AND catalog_name = ?"; -// try (PreparedStatement statement = connection.prepareStatement(sql)) { -// statement.setString(1, schema); -// statement.setString(2, database); -// try (ResultSet resultSet = statement.executeQuery()) { -// if (!resultSet.next()) { -// throw new NoSuchSchemaException("No such schema: %s", schema); -// } -// String schemaName = resultSet.getString(1); -// String comment = getSchemaComment(schema, connection); -// return JdbcSchema.builder() -// .withName(schemaName) -// .withComment(comment) -// .withAuditInfo(AuditInfo.EMPTY) -// .withProperties(Collections.emptyMap()) -// .build(); -// } ResultSet resultSet = getSchema(connection, schema); boolean found = false; @@ -86,11 +68,11 @@ public JdbcSchema load(String schema) throws NoSuchSchemaException { String comment = getSchemaComment(schema, connection); return JdbcSchema.builder() - .withName(schema) - .withComment(comment) - .withAuditInfo(AuditInfo.EMPTY) - .withProperties(Collections.emptyMap()) - .build(); + .withName(schema) + .withComment(comment) + .withAuditInfo(AuditInfo.EMPTY) + .withProperties(Collections.emptyMap()) + .build(); } catch (SQLException e) { throw exceptionMapper.toGravitinoException(e); } @@ -138,7 +120,6 @@ public String generateCreateDatabaseSql( return sqlBuilder.toString(); } - @Override protected ResultSet getSchema(Connection connection, String databaseName) throws SQLException { final DatabaseMetaData metaData = connection.getMetaData(); From adf781b13b49b3e4d01af124c375514acdbf24d3 Mon Sep 17 00:00:00 2001 From: yuqi Date: Thu, 21 Mar 2024 20:21:23 +0800 Subject: [PATCH 4/7] Fix --- .../catalog/mysql/operation/MysqlDatabaseOperations.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java index 67bf5f2f468..7136c93f946 100644 --- a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java +++ b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java @@ -98,8 +98,7 @@ protected ResultSet getSchema(Connection connection, String databaseName) throws return metaData.getCatalogs(); } - - @Override + @Override public JdbcSchema load(String databaseName) throws NoSuchSchemaException { try (final Connection connection = this.dataSource.getConnection()) { ResultSet resultSet = getSchema(connection, databaseName); From 8d827a3f2fb0cefdb79d29febc26197df382803e Mon Sep 17 00:00:00 2001 From: yuqi Date: Thu, 21 Mar 2024 20:36:04 +0800 Subject: [PATCH 5/7] Fix again --- .../operation/JdbcDatabaseOperations.java | 9 +++++++++ .../operation/PostgreSqlSchemaOperations.java | 19 +++++++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java index 19683e2459c..511fce53c96 100644 --- a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java +++ b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java @@ -77,6 +77,15 @@ public List listDatabases() { } } + /** + * Load the schema with the given name. + * + * @param connection The connection to the database. + * @param schemaName The name of the schema. + * @return The result set of the schema. Some database may not support fuzzy query and the result + * set may be empty or is not the schema with the given name. + * @throws SQLException + */ protected abstract ResultSet getSchema(Connection connection, String schemaName) throws SQLException; diff --git a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java index ca640e5c6e5..4ee2ce01ff1 100644 --- a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java +++ b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java @@ -82,16 +82,11 @@ public JdbcSchema load(String schema) throws NoSuchSchemaException { public List listDatabases() { List result = new ArrayList<>(); try (Connection connection = getConnection()) { - try (PreparedStatement statement = - connection.prepareStatement( - "SELECT schema_name FROM information_schema.schemata WHERE catalog_name = ?")) { - statement.setString(1, database); - ResultSet resultSet = statement.executeQuery(); - while (resultSet.next()) { - String databaseName = resultSet.getString(1); - if (!isSystemDatabase(databaseName)) { - result.add(databaseName); - } + ResultSet resultSet = getSchema(connection, null); + while (resultSet.next()) { + String schemaName = resultSet.getString(1); + if (!isSystemDatabase(schemaName)) { + result.add(resultSet.getString(1)); } } } catch (final SQLException se) { @@ -121,9 +116,9 @@ public String generateCreateDatabaseSql( } @Override - protected ResultSet getSchema(Connection connection, String databaseName) throws SQLException { + protected ResultSet getSchema(Connection connection, String schemaName) throws SQLException { final DatabaseMetaData metaData = connection.getMetaData(); - return metaData.getSchemas(database, databaseName); + return metaData.getSchemas(database, schemaName); } @Override From 787917be518fbb8040567e7d3bb83a332f797a25 Mon Sep 17 00:00:00 2001 From: yuqi Date: Mon, 25 Mar 2024 15:51:22 +0800 Subject: [PATCH 6/7] Fix --- .../operation/JdbcDatabaseOperations.java | 12 ----- .../operation/SqliteDatabaseOperations.java | 6 --- .../operation/DorisDatabaseOperations.java | 8 --- .../operation/MysqlDatabaseOperations.java | 50 +++++-------------- .../operation/PostgreSqlSchemaOperations.java | 11 +++- 5 files changed, 22 insertions(+), 65 deletions(-) diff --git a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java index 511fce53c96..6c0be7351c3 100644 --- a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java +++ b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java @@ -77,18 +77,6 @@ public List listDatabases() { } } - /** - * Load the schema with the given name. - * - * @param connection The connection to the database. - * @param schemaName The name of the schema. - * @return The result set of the schema. Some database may not support fuzzy query and the result - * set may be empty or is not the schema with the given name. - * @throws SQLException - */ - protected abstract ResultSet getSchema(Connection connection, String schemaName) - throws SQLException; - /** * @param databaseName The name of the database. * @param comment The comment of the database. diff --git a/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/operation/SqliteDatabaseOperations.java b/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/operation/SqliteDatabaseOperations.java index 4e9915f4ca0..b8fd9ad9f06 100644 --- a/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/operation/SqliteDatabaseOperations.java +++ b/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/operation/SqliteDatabaseOperations.java @@ -15,7 +15,6 @@ import java.io.File; import java.sql.Connection; import java.sql.DriverManager; -import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arrays; import java.util.List; @@ -92,9 +91,4 @@ public String generateCreateDatabaseSql( public String generateDropDatabaseSql(String databaseName, boolean cascade) { return null; } - - @Override - protected ResultSet getSchema(Connection connection, String schemaName) throws SQLException { - throw new UnsupportedOperationException("Not supported for SQLite"); - } } diff --git a/catalogs/catalog-jdbc-doris/src/main/java/com/datastrato/gravitino/catalog/doris/operation/DorisDatabaseOperations.java b/catalogs/catalog-jdbc-doris/src/main/java/com/datastrato/gravitino/catalog/doris/operation/DorisDatabaseOperations.java index 10bfc84243a..82d4b1cac09 100644 --- a/catalogs/catalog-jdbc-doris/src/main/java/com/datastrato/gravitino/catalog/doris/operation/DorisDatabaseOperations.java +++ b/catalogs/catalog-jdbc-doris/src/main/java/com/datastrato/gravitino/catalog/doris/operation/DorisDatabaseOperations.java @@ -7,9 +7,6 @@ import com.datastrato.gravitino.catalog.jdbc.JdbcSchema; import com.datastrato.gravitino.catalog.jdbc.operation.JdbcDatabaseOperations; import com.datastrato.gravitino.exceptions.NoSuchSchemaException; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.Map; /** Database operations for Doris. */ @@ -31,9 +28,4 @@ protected String generateCreateDatabaseSql( protected String generateDropDatabaseSql(String databaseName, boolean cascade) { throw new UnsupportedOperationException(); } - - @Override - protected ResultSet getSchema(Connection connection, String schemaName) throws SQLException { - throw new UnsupportedOperationException(); - } } diff --git a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java index 7136c93f946..9971b9ee490 100644 --- a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java +++ b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java @@ -11,15 +11,14 @@ import com.datastrato.gravitino.meta.AuditInfo; import com.google.common.collect.ImmutableMap; import java.sql.Connection; -import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Objects; import java.util.Set; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; @@ -54,11 +53,6 @@ public String generateCreateDatabaseSql( if (MapUtils.isNotEmpty(properties)) { // TODO #804 Properties will be unified in the future. throw new UnsupportedOperationException("Properties are not supported yet"); - // sqlBuilder.append("\n"); - // sqlBuilder.append( - // properties.entrySet().stream() - // .map(entry -> entry.getKey() + " " + entry.getValue()) - // .collect(Collectors.joining("\n"))); } String result = sqlBuilder.toString(); LOG.info("Generated create database:{} sql: {}", databaseName, result); @@ -91,39 +85,21 @@ public String generateDropDatabaseSql(String databaseName, boolean cascade) { return dropDatabaseSql; } - @Override - protected ResultSet getSchema(Connection connection, String databaseName) throws SQLException { - final DatabaseMetaData metaData = connection.getMetaData(); - // It'd indeed need to call getCatalogs() to get the schema not `getSchemas()` for MySQL. - return metaData.getCatalogs(); - } - @Override public JdbcSchema load(String databaseName) throws NoSuchSchemaException { - try (final Connection connection = this.dataSource.getConnection()) { - ResultSet resultSet = getSchema(connection, databaseName); + List allDatabases = listDatabases(); + String dbName = + allDatabases.stream() + .filter(db -> db.equals(databaseName)) + .findFirst() + .orElseThrow( + () -> new NoSuchSchemaException("Database %s could not be found", databaseName)); - boolean found = false; - while (resultSet.next()) { - if (Objects.equals(resultSet.getString(1), databaseName)) { - found = true; - break; - } - } - - if (!found) { - throw new NoSuchSchemaException("Database %s could not be found", databaseName); - } - - return JdbcSchema.builder() - .withName(databaseName) - .withProperties(ImmutableMap.of()) - .withAuditInfo(AuditInfo.EMPTY) - .build(); - - } catch (final SQLException se) { - throw this.exceptionMapper.toGravitinoException(se); - } + return JdbcSchema.builder() + .withName(dbName) + .withProperties(ImmutableMap.of()) + .withAuditInfo(AuditInfo.EMPTY) + .build(); } @Override diff --git a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java index 4ee2ce01ff1..a26de1d2d59 100644 --- a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java +++ b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java @@ -115,8 +115,15 @@ public String generateCreateDatabaseSql( return sqlBuilder.toString(); } - @Override - protected ResultSet getSchema(Connection connection, String schemaName) throws SQLException { + /** + * Get the schema with the given name. + * + *

Database in PG corresponds to Catalog in JDBC. Schema in PG corresponds to Schema in JDBC. + * + * @param connection the connection to the database + * @param schemaName the name of the schema + */ + private ResultSet getSchema(Connection connection, String schemaName) throws SQLException { final DatabaseMetaData metaData = connection.getMetaData(); return metaData.getSchemas(database, schemaName); } From 329ed8d058f510d9d5e267e271bb0450ba370d3b Mon Sep 17 00:00:00 2001 From: yuqi Date: Mon, 8 Apr 2024 17:26:08 +0800 Subject: [PATCH 7/7] Fix --- .../postgresql/operation/PostgreSqlSchemaOperations.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java index a26de1d2d59..978121434b7 100644 --- a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java +++ b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java @@ -118,6 +118,9 @@ public String generateCreateDatabaseSql( /** * Get the schema with the given name. * + *

Note: This method will return a result set that may contain multiple rows as the schemaName + * in `getSchemas` is a pattern. The result set will contain all schemas that match the pattern. + * *

Database in PG corresponds to Catalog in JDBC. Schema in PG corresponds to Schema in JDBC. * * @param connection the connection to the database