World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#2037] fix(mysql-catalog): Fix MySQL/PG wild card and case sensitive issues in table name (#2044)
Qi Yued4b8146efe4
diff --git a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcTableOperations.java b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcTableOperations.javaindex eb9d2cab80..d7b98fc951 100644--- a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcTableOperations.java+++ b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/operation/JdbcTableOperations.java@@ -33,6 +33,7 @@ import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.Map;+import java.util.Objects;import java.util.Set;import java.util.stream.Collectors;import javax.sql.DataSource;@@ -117,23 +118,44 @@ public abstract class JdbcTableOperations implements TableOperation {@Overridepublic JdbcTable load(String databaseName, String tableName) throws NoSuchTableException {+ // We should handle case sensitivity and wild card issue in some catalog tables, take a MySQL+ // table for example.+ // 1. MySQL will get table 'a_b' and 'A_B' when we query 'a_b' in a case-insensitive charset+ // like utf8mb4.+ // 2. MySQL treats 'a_b' as a wildcard, matching any table name that begins with 'a', followed+ // by any character, and ending with 'b'.try (Connection connection = getConnection(databaseName)) {// 1.Get table informationResultSet table = getTable(connection, databaseName, tableName);- if (!table.next() || !tableName.equals(table.getString("TABLE_NAME"))) {+ // The result of tables may be more than one due to the reason above, so we need to check the+ // result+ JdbcTable.Builder jdbcTableBuilder = new JdbcTable.Builder();+ boolean found = false;+ // Handle case-sensitive issues.+ while (table.next() && !found) {+ if (Objects.equals(table.getString("TABLE_NAME"), tableName)) {+ jdbcTableBuilder = getBasicJdbcTableInfo(table);+ found = true;+ }+ }++ if (!found) {throw new NoSuchTableException(String.format("Table %s does not exist in %s.", tableName, databaseName));}- JdbcTable.Builder jdbcTableBuilder = getBasicJdbcTableInfo(table);// 2.Get column informationList<JdbcColumn> jdbcColumns = new ArrayList<>();+ // Get columns are wildcard sensitive, so we need to check the result.ResultSet columns = getColumns(connection, databaseName, tableName);while (columns.next()) {- JdbcColumn.Builder columnBuilder = getBasicJdbcColumnInfo(columns);- boolean autoIncrement = getAutoIncrementInfo(columns);- columnBuilder.withAutoIncrement(autoIncrement);- jdbcColumns.add(columnBuilder.build());+ // TODO(yunqing): check schema and catalog also+ if (Objects.equals(columns.getString("TABLE_NAME"), tableName)) {+ JdbcColumn.Builder columnBuilder = getBasicJdbcColumnInfo(columns);+ boolean autoIncrement = getAutoIncrementInfo(columns);+ columnBuilder.withAutoIncrement(autoIncrement);+ jdbcColumns.add(columnBuilder.build());+ }}jdbcTableBuilder.withColumns(jdbcColumns.toArray(new JdbcColumn[0]));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.javaindex c1ecc86733..e6bb757b41 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@@ -32,6 +32,7 @@ import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;+import java.util.Objects;import java.util.Optional;import java.util.stream.Collectors;import org.apache.commons.collections4.CollectionUtils;@@ -209,27 +210,30 @@ public class MysqlTableOperations extends JdbcTableOperations {@Overrideprotected Map<String, String> getTableProperties(Connection connection, String tableName)throws SQLException {+ // MySQL in CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci is case-insensitive, when the name is+ // hello, the result can be 'HELLO', 'Hello', 'hello' and so on.try (PreparedStatement statement = connection.prepareStatement("SHOW TABLE STATUS LIKE ?")) {statement.setString(1, tableName);try (ResultSet resultSet = statement.executeQuery()) {- if (!resultSet.next() || !tableName.equals(resultSet.getString("NAME"))) {- throw new NoSuchTableException(- String.format("Table %s does not exist in %s.", tableName, connection.getCatalog()));+ while (resultSet.next()) {+ String name = resultSet.getString("NAME");+ if (Objects.equals(name, tableName)) {+ return Collections.unmodifiableMap(+ new HashMap<String, String>() {+ {+ put(COMMENT, resultSet.getString(COMMENT));+ put(MYSQL_ENGINE_KEY, resultSet.getString(MYSQL_ENGINE_KEY));+ String autoIncrement = resultSet.getString(MYSQL_AUTO_INCREMENT_OFFSET_KEY);+ if (StringUtils.isNotEmpty(autoIncrement)) {+ put(MYSQL_AUTO_INCREMENT_OFFSET_KEY, autoIncrement);+ }+ }+ });+ }}- return Collections.unmodifiableMap(- new HashMap<String, String>() {- {- // It is not possible to get all column assembly returns here, because in version- // 5.7, some columns exist in metadata but get will report an error that they do not- // exist, such as: TABLE_NAME- put(COMMENT, resultSet.getString(COMMENT));- put(MYSQL_ENGINE_KEY, resultSet.getString(MYSQL_ENGINE_KEY));- String autoIncrement = resultSet.getString(MYSQL_AUTO_INCREMENT_OFFSET_KEY);- if (StringUtils.isNotEmpty(autoIncrement)) {- put(MYSQL_AUTO_INCREMENT_OFFSET_KEY, autoIncrement);- }- }- });++ throw new NoSuchTableException(+ String.format("Table %s does not exist in %s.", tableName, connection.getCatalog()));}}}diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.javaindex 6f810bec09..acf23d30c1 100644--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java@@ -51,6 +51,7 @@ import java.nio.file.Paths;import java.util.Arrays;import java.util.Collections;import java.util.Map;+import java.util.Objects;import java.util.Set;import java.util.stream.Collectors;import org.apache.commons.lang.math.RandomUtils;@@ -1072,4 +1073,139 @@ public class CatalogMysqlIT extends AbstractIT {Assertions.assertDoesNotThrow(() -> tableCatalog.dropTable(tableIdentifier));}++ @Test+ void testMySQLSpecialTableName() {+ // Test create many indexes with name success.+ Map<String, String> properties = createProperties();+ TableCatalog tableCatalog = catalog.asTableCatalog();++ String tableName = "t112";+ Column col1 = Column.of(tableName, Types.LongType.get(), "id", false, false, null);+ Column[] columns = {col1};++ NameIdentifier tableIdentifier =+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);+ tableCatalog.createTable(+ tableIdentifier,+ columns,+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0]);++ tableName = "t212";+ col1 = Column.of(tableName, Types.LongType.get(), "id", false, false, null);+ columns = new Column[] {col1};+ tableIdentifier = NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);+ tableCatalog.createTable(+ tableIdentifier,+ columns,+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0]);++ tableName = "t_12";+ col1 = Column.of(tableName, Types.LongType.get(), "id", false, false, null);+ columns = new Column[] {col1};+ tableIdentifier = NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);+ tableCatalog.createTable(+ tableIdentifier,+ columns,+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0]);++ tableName = "_1__";+ col1 = Column.of(tableName, Types.LongType.get(), "id", false, false, null);+ columns = new Column[] {col1};+ tableIdentifier = NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);+ tableCatalog.createTable(+ tableIdentifier,+ columns,+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0]);++ Table t1 =+ tableCatalog.loadTable(NameIdentifier.of(metalakeName, catalogName, schemaName, "t112"));+ Arrays.stream(t1.columns()).anyMatch(c -> Objects.equals(c.name(), "t112"));++ Table t2 =+ tableCatalog.loadTable(NameIdentifier.of(metalakeName, catalogName, schemaName, "t212"));+ Arrays.stream(t2.columns()).anyMatch(c -> Objects.equals(c.name(), "t212"));++ Table t3 =+ tableCatalog.loadTable(NameIdentifier.of(metalakeName, catalogName, schemaName, "t_12"));+ Arrays.stream(t3.columns()).anyMatch(c -> Objects.equals(c.name(), "t_12"));++ Table t4 =+ tableCatalog.loadTable(NameIdentifier.of(metalakeName, catalogName, schemaName, "_1__"));+ Arrays.stream(t4.columns()).anyMatch(c -> Objects.equals(c.name(), "_1__"));+ }++ @Test+ void testMySQLTableNameCaseSensitive() {+ Column col1 = Column.of("col_1", Types.LongType.get(), "id", false, false, null);+ Column col2 = Column.of("col_2", Types.ByteType.get(), "yes", false, false, null);+ Column col3 = Column.of("col_3", Types.DateType.get(), "comment", false, false, null);+ Column col4 = Column.of("col_4", Types.VarCharType.of(255), "code", false, false, null);+ Column col5 = Column.of("col_5", Types.VarCharType.of(255), "config", false, false, null);+ Column[] newColumns = new Column[] {col1, col2, col3, col4, col5};++ Index[] indexes =+ new Index[] {+ Indexes.createMysqlPrimaryKey(new String[][] {{"col_1"}, {"col_2"}}),+ Indexes.unique("u1_key", new String[][] {{"col_2"}, {"col_3"}})+ };++ NameIdentifier tableIdentifier =+ NameIdentifier.of(metalakeName, catalogName, schemaName, "tableName");+ Map<String, String> properties = createProperties();+ TableCatalog tableCatalog = catalog.asTableCatalog();+ Table createdTable =+ tableCatalog.createTable(+ tableIdentifier,+ newColumns,+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0],+ indexes);+ assertionsTableInfo(+ "tableName", table_comment, Arrays.asList(newColumns), properties, indexes, createdTable);+ Table table = tableCatalog.loadTable(tableIdentifier);+ assertionsTableInfo(+ "tableName", table_comment, Arrays.asList(newColumns), properties, indexes, table);++ // Test create table with same name but different case+ NameIdentifier tableIdentifier2 =+ NameIdentifier.of(metalakeName, catalogName, schemaName, "TABLENAME");++ Table tableAgain =+ Assertions.assertDoesNotThrow(+ () ->+ tableCatalog.createTable(+ tableIdentifier2,+ newColumns,+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0],+ indexes));+ Assertions.assertEquals("TABLENAME", tableAgain.name());++ table = tableCatalog.loadTable(tableIdentifier2);+ assertionsTableInfo(+ "TABLENAME", table_comment, Arrays.asList(newColumns), properties, indexes, table);+ }}diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.javaindex 1a2190c6c4..5d4e54d38c 100644--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java@@ -51,6 +51,7 @@ import java.nio.file.Paths;import java.util.Arrays;import java.util.Collections;import java.util.Map;+import java.util.Objects;import java.util.Optional;import java.util.Set;import java.util.stream.Collectors;@@ -902,4 +903,139 @@ public class CatalogPostgreSqlIT extends AbstractIT {}}}++ @Test+ void testPGSpecialTableName() {+ // Test create many indexes with name success.+ Map<String, String> properties = createProperties();+ TableCatalog tableCatalog = catalog.asTableCatalog();++ String tableName = "t112";+ Column col1 = Column.of(tableName, Types.LongType.get(), "id", false, false, null);+ Column[] columns = {col1};++ NameIdentifier tableIdentifier =+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);+ tableCatalog.createTable(+ tableIdentifier,+ columns,+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0]);++ tableName = "t212";+ col1 = Column.of(tableName, Types.LongType.get(), "id", false, false, null);+ columns = new Column[] {col1};+ tableIdentifier = NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);+ tableCatalog.createTable(+ tableIdentifier,+ columns,+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0]);++ tableName = "t_12";+ col1 = Column.of(tableName, Types.LongType.get(), "id", false, false, null);+ columns = new Column[] {col1};+ tableIdentifier = NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);+ tableCatalog.createTable(+ tableIdentifier,+ columns,+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0]);++ tableName = "_1__";+ col1 = Column.of(tableName, Types.LongType.get(), "id", false, false, null);+ columns = new Column[] {col1};+ tableIdentifier = NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);+ tableCatalog.createTable(+ tableIdentifier,+ columns,+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0]);++ Table t1 =+ tableCatalog.loadTable(NameIdentifier.of(metalakeName, catalogName, schemaName, "t112"));+ Arrays.stream(t1.columns()).anyMatch(c -> Objects.equals(c.name(), "t112"));++ Table t2 =+ tableCatalog.loadTable(NameIdentifier.of(metalakeName, catalogName, schemaName, "t212"));+ Arrays.stream(t2.columns()).anyMatch(c -> Objects.equals(c.name(), "t212"));++ Table t3 =+ tableCatalog.loadTable(NameIdentifier.of(metalakeName, catalogName, schemaName, "t_12"));+ Arrays.stream(t3.columns()).anyMatch(c -> Objects.equals(c.name(), "t_12"));++ Table t4 =+ tableCatalog.loadTable(NameIdentifier.of(metalakeName, catalogName, schemaName, "_1__"));+ Arrays.stream(t4.columns()).anyMatch(c -> Objects.equals(c.name(), "_1__"));+ }++ @Test+ void testPGTableNameCaseSensitive() {+ Column col1 = Column.of("col_1", Types.LongType.get(), "id", false, false, null);+ Column col2 = Column.of("col_2", Types.IntegerType.get(), "yes", false, false, null);+ Column col3 = Column.of("col_3", Types.DateType.get(), "comment", false, false, null);+ Column col4 = Column.of("col_4", Types.VarCharType.of(255), "code", false, false, null);+ Column col5 = Column.of("col_5", Types.VarCharType.of(255), "config", false, false, null);+ Column[] newColumns = new Column[] {col1, col2, col3, col4, col5};++ Index[] indexes = new Index[0];+ NameIdentifier tableIdentifier =+ NameIdentifier.of(metalakeName, catalogName, schemaName, "tablename");+ Map<String, String> properties = createProperties();+ TableCatalog tableCatalog = catalog.asTableCatalog();+ Table createdTable =+ tableCatalog.createTable(+ tableIdentifier,+ newColumns,+ "low case table name",+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0],+ indexes);+ assertionsTableInfo(+ "tablename",+ "low case table name",+ Arrays.asList(newColumns),+ properties,+ indexes,+ createdTable);+ Table table = tableCatalog.loadTable(tableIdentifier);+ assertionsTableInfo(+ "tablename", "low case table name", Arrays.asList(newColumns), properties, indexes, table);++ // Test create table with same name but different case+ NameIdentifier tableIdentifier2 =+ NameIdentifier.of(metalakeName, catalogName, schemaName, "TABLENAME");++ Column[] upperTableColumns = new Column[] {col1, col4, col5};+ Table tableAgain =+ Assertions.assertDoesNotThrow(+ () ->+ tableCatalog.createTable(+ tableIdentifier2,+ upperTableColumns,+ "upper case table name",+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0],+ indexes));+ Assertions.assertEquals("TABLENAME", tableAgain.name());++ table = tableCatalog.loadTable(tableIdentifier2);+ Assertions.assertEquals("TABLENAME", table.name());+ }}diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/trino/TrinoConnectorIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/trino/TrinoConnectorIT.javaindex 2c8af6ca51..7c8a1c50bf 100644--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/trino/TrinoConnectorIT.java+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/trino/TrinoConnectorIT.java@@ -1333,6 +1333,64 @@ public class TrinoConnectorIT extends AbstractIT {data = containerSuite.getTrinoContainer().executeQuerySQL(sql).get(0).get(0);Assertions.assertTrue(data.contains("age integer NOT NULL"));Assertions.assertTrue(data.contains("address varchar(20) NOT NULL"));++ // Test special characters in table name+ String tableName1 = "t112";+ sql =+ String.format(+ "create table \"%s.%s\".%s.%s (id int, t1name varchar)",+ metalakeName, catalogName, schemaName, tableName1);+ containerSuite.getTrinoContainer().executeUpdateSQL(sql);++ String tableName2 = "t212";+ sql =+ String.format(+ "create table \"%s.%s\".%s.%s (id int, t2name varchar)",+ metalakeName, catalogName, schemaName, tableName2);+ containerSuite.getTrinoContainer().executeUpdateSQL(sql);++ String tableName3 = "t_12";+ sql =+ String.format(+ "create table \"%s.%s\".%s.%s (id int, t3name varchar)",+ metalakeName, catalogName, schemaName, tableName3);++ containerSuite.getTrinoContainer().executeUpdateSQL(sql);++ String tableName4 = "_1__";+ sql =+ String.format(+ "create table \"%s.%s\".%s.%s (id int, t4name varchar)",+ metalakeName, catalogName, schemaName, tableName4);+ containerSuite.getTrinoContainer().executeUpdateSQL(sql);++ // Get table tableName1+ sql =+ String.format(+ "show create table \"%s.%s\".%s.%s", metalakeName, catalogName, schemaName, tableName1);+ data = containerSuite.getTrinoContainer().executeQuerySQL(sql).get(0).get(0);+ data.contains("t1name varchar");++ // Get table tableName2+ sql =+ String.format(+ "show create table \"%s.%s\".%s.%s", metalakeName, catalogName, schemaName, tableName2);+ data = containerSuite.getTrinoContainer().executeQuerySQL(sql).get(0).get(0);+ data.contains("t2name varchar");++ // Get table tableName3+ sql =+ String.format(+ "show create table \"%s.%s\".%s.%s", metalakeName, catalogName, schemaName, tableName3);+ data = containerSuite.getTrinoContainer().executeQuerySQL(sql).get(0).get(0);+ data.contains("t3name varchar");++ // Get table tableName4+ sql =+ String.format(+ "show create table \"%s.%s\".%s.%s", metalakeName, catalogName, schemaName, tableName4);+ data = containerSuite.getTrinoContainer().executeQuerySQL(sql).get(0).get(0);+ data.contains("t4name varchar");}@Test