From 99b9661d0aa9139a1e1fc025c8a6542b69dd2ca9 Mon Sep 17 00:00:00 2001 From: Clearvive <143773256+Clearvive@users.noreply.github.com> Date: Thu, 25 Jan 2024 16:56:03 +0800 Subject: [PATCH] [#1701] fix(mysql): Compatible with lower versions of MySQL driver, unable to obtain table comments. (#1702) ### What changes were proposed in this pull request? Compatible with lower versions of MySQL driver, unable to obtain table comments. ### Why are the changes needed? Fix: #1701 ### Does this PR introduce _any_ user-facing change? NO ### How was this patch tested? UT/IT --------- Co-authored-by: Clearvive --- .../mysql/operation/MysqlTableOperations.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlTableOperations.java b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlTableOperations.java index bae9f3744cb..e1fcd293cf0 100644 --- a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlTableOperations.java +++ b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlTableOperations.java @@ -18,6 +18,7 @@ import com.datastrato.gravitino.rel.expressions.transforms.Transform; import java.sql.Connection; import java.sql.DatabaseMetaData; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -49,6 +50,11 @@ public JdbcTable load(String databaseName, String tableName) throws NoSuchTableE String.format("Table %s does not exist in %s.", tableName, databaseName)); } String comment = table.getString("REMARKS"); + if (StringUtils.isEmpty(comment)) { + // In Mysql version 5.7, the comment field value cannot be obtained in the driver API. + LOG.warn("Not found comment in mysql driver api. Will try to get comment from sql"); + comment = loadCommentFromSql(connection, tableName); + } // 2.Get column information ResultSet columns = metaData.getColumns(databaseName, null, tableName, null); @@ -84,6 +90,18 @@ public JdbcTable load(String databaseName, String tableName) throws NoSuchTableE } } + private String loadCommentFromSql(Connection connection, String tableName) throws SQLException { + try (PreparedStatement statement = connection.prepareStatement("SHOW TABLE STATUS LIKE ?")) { + statement.setString(1, tableName); + try (ResultSet resultSet = statement.executeQuery()) { + if (!resultSet.next()) { + return null; + } + return resultSet.getString("COMMENT"); + } + } + } + @Override public List listTables(String databaseName) throws NoSuchSchemaException { try (Connection connection = getConnection(databaseName)) {