World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#1442] fix(jdbc-catalog): Ensure the JDBC catalogs implement the correct semantics of dropTable and purgeTable (#1705)
FANNGb13486cee970
diff --git a/api/src/main/java/com/datastrato/gravitino/rel/TableCatalog.java b/api/src/main/java/com/datastrato/gravitino/rel/TableCatalog.javaindex fdaece6632..16a673f18f 100644--- a/api/src/main/java/com/datastrato/gravitino/rel/TableCatalog.java+++ b/api/src/main/java/com/datastrato/gravitino/rel/TableCatalog.java@@ -261,7 +261,9 @@ public interface TableCatalog {throws NoSuchTableException, IllegalArgumentException;/**- * Drop a table from the catalog.+ * Removes both the metadata and the directory associated with the table from the file system if+ * the table is not an external table. In case of an external table, only the associated metadata+ * is removed.** @param ident A table identifier.* @return True if the table was dropped, false if the table did not exist.@@ -269,7 +271,10 @@ public interface TableCatalog {boolean dropTable(NameIdentifier ident);/**- * Drop a table from the catalog and completely remove its data.+ * Drop a table from the catalog and completely remove its data. Removes both the metadata and the+ * directory associated with the table completely and skipping trash. If the table is an external+ * table or the catalogs don't support purge table, {@link UnsupportedOperationException} is+ * thrown.** <p>If the catalog supports to purge a table, this method should be overridden. The default* implementation throws an {@link UnsupportedOperationException}.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 616a65fec9..2f5c73ab55 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@@ -236,13 +236,13 @@ public class MysqlTableOperations extends JdbcTableOperations {@Overrideprotected String generateDropTableSql(String tableName) {- throw new UnsupportedOperationException(- "MySQL does not support drop operation in Gravitino, please use purge operation");+ return "DROP TABLE " + tableName;}@Overrideprotected String generatePurgeTableSql(String tableName) {- return "DROP TABLE " + tableName;+ throw new UnsupportedOperationException(+ "MySQL does not support purge table in Gravitino, please use drop table");}@Overridediff --git a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.javaindex db8c406b33..9cc9f976b7 100644--- a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java+++ b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java@@ -327,13 +327,13 @@ public class PostgreSqlTableOperations extends JdbcTableOperations {@Overrideprotected String generateDropTableSql(String tableName) {- throw new UnsupportedOperationException(- "PostgreSQL does not support drop operation in Gravitino, please use purge operation");+ return "DROP TABLE " + tableName;}@Overrideprotected String generatePurgeTableSql(String tableName) {- return "DROP TABLE " + tableName;+ throw new UnsupportedOperationException(+ "PostgreSQL does not support purge table in Gravitino, please use drop table");}@Overridediff --git a/docs/manage-metadata-using-gravitino.md b/docs/manage-metadata-using-gravitino.mdindex 69b76ce05e..0e1245e6e7 100644--- a/docs/manage-metadata-using-gravitino.md+++ b/docs/manage-metadata-using-gravitino.md@@ -865,8 +865,12 @@ tableCatalog.purgeTable(NameIdentifier.of("metalake", "catalog", "schema", "tabl</TabItem></Tabs>-There are two ways to drop a table: `dropTable` and `purgeTable`, the difference between them is that `purgeTable` will remove data of the table, while `dropTable` only removes the metadata of the table. Some engines such as-Apache Hive support both, `dropTable` will only remove the metadata of a table and the data in HDFS can be reused later through the format of the external table.+There are two ways to remove a table: `dropTable` and `purgeTable`:++* `dropTable` removes both the metadata and the directory associated with the table from the file system if the table is not an external table. In case of an external table, only the associated metadata is removed.+* `purgeTable` completely removes both the metadata and the directory associated with the table and skipping trash, if the table is an external table or the catalogs don't support purge table, `UnsupportedOperationException` is thrown.++Hive catalog and lakehouse-iceberg catalog supports `purgeTable` while jdbc-mysql and jdbc-postgresql catalog doesn't support.### List all tables under a schemadiff --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 7db465d388..9b4aabeeea 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@@ -127,7 +127,7 @@ public class CatalogMysqlIT extends AbstractIT {NameIdentifier[] nameIdentifiers =catalog.asTableCatalog().listTables(Namespace.of(metalakeName, catalogName, schemaName));for (NameIdentifier nameIdentifier : nameIdentifiers) {- catalog.asTableCatalog().purgeTable(nameIdentifier);+ catalog.asTableCatalog().dropTable(nameIdentifier);}catalog.asSchemas().dropSchema(NameIdentifier.of(metalakeName, catalogName, schemaName), false);}diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/TestMysqlTableOperations.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/TestMysqlTableOperations.javaindex 402e912d2a..d4e26f2969 100644--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/TestMysqlTableOperations.java+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/TestMysqlTableOperations.java@@ -154,9 +154,9 @@ public class TestMysqlTableOperations extends TestMysqlAbstractIT {TABLE_OPERATIONS.alterTable(TEST_DB_NAME, newName, TableChange.deleteColumn(new String[] {newColumn.name()}, true));- Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.purge(TEST_DB_NAME, newName));+ Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.drop(TEST_DB_NAME, newName));Assertions.assertThrows(- NoSuchTableException.class, () -> TABLE_OPERATIONS.purge(TEST_DB_NAME, newName));+ NoSuchTableException.class, () -> TABLE_OPERATIONS.drop(TEST_DB_NAME, newName));}@Testdiff --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 fc25464260..95603d0969 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@@ -124,7 +124,7 @@ public class CatalogPostgreSqlIT extends AbstractIT {NameIdentifier[] nameIdentifiers =catalog.asTableCatalog().listTables(Namespace.of(metalakeName, catalogName, schemaName));for (NameIdentifier nameIdentifier : nameIdentifiers) {- catalog.asTableCatalog().purgeTable(nameIdentifier);+ catalog.asTableCatalog().dropTable(nameIdentifier);}catalog.asSchemas().dropSchema(NameIdentifier.of(metalakeName, catalogName, schemaName), false);}diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/TestPostgreSqlTableOperations.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/TestPostgreSqlTableOperations.javaindex c953a4c30c..a98e727d36 100644--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/TestPostgreSqlTableOperations.java+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/TestPostgreSqlTableOperations.java@@ -203,9 +203,9 @@ public class TestPostgreSqlTableOperations extends TestPostgreSqlAbstractIT {TEST_DB_NAME,newName,TableChange.deleteColumn(new String[] {newColumn.name()}, true)));- Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.purge(TEST_DB_NAME, newName));+ Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.drop(TEST_DB_NAME, newName));Assertions.assertThrows(- NoSuchTableException.class, () -> TABLE_OPERATIONS.purge(TEST_DB_NAME, newName));+ NoSuchTableException.class, () -> TABLE_OPERATIONS.drop(TEST_DB_NAME, newName));}@Test@@ -389,7 +389,7 @@ public class TestPostgreSqlTableOperations extends TestPostgreSqlAbstractIT {NoSuchTableException.class, () -> postgreSqlTableOperations.load("other_schema", table_2));Assertions.assertThrows(NoSuchTableException.class, () -> TABLE_OPERATIONS.load("other_schema", table_2));- postgreSqlTableOperations.purge(TEST_DB_NAME, table_1);+ postgreSqlTableOperations.drop(TEST_DB_NAME, table_1);Assertions.assertThrows(NoSuchTableException.class,diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/trino/TrinoQueryITBase.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/trino/TrinoQueryITBase.javaindex 95e9353ff7..6c1e19cbd3 100644--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/trino/TrinoQueryITBase.java+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/trino/TrinoQueryITBase.java@@ -196,21 +196,17 @@ public class TrinoQueryITBase {Namespace.ofTable(metalakeName, catalogName, schema.name()))).forEach(table -> {- try {- tableCatalog.purgeTable(- NameIdentifier.ofTable(- metalakeName, catalogName, schema.name(), table.name()));- } catch (UnsupportedOperationException e) {- tableCatalog.dropTable(- NameIdentifier.ofTable(- metalakeName, catalogName, schema.name(), table.name()));- LOG.info(- "Drop table \"{}.{}\".{}.{}",- metalakeName,- catalogName,- schema.name(),- table.name());- } catch (Exception e) {+ boolean dropped =+ tableCatalog.dropTable(+ NameIdentifier.ofTable(+ metalakeName, catalogName, schema.name(), table.name()));+ LOG.info(+ "Drop table \"{}.{}\".{}.{}",+ metalakeName,+ catalogName,+ schema.name(),+ table.name());+ if (!dropped) {LOG.error("Failed to drop table {}", table);}});diff --git a/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/GravitinoErrorCode.java b/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/GravitinoErrorCode.javaindex 6257e10c71..f8669d70d0 100644--- a/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/GravitinoErrorCode.java+++ b/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/GravitinoErrorCode.java@@ -33,6 +33,7 @@ public enum GravitinoErrorCode implements ErrorCodeSupplier {GRAVITINO_MISSING_REQUIRED_PROPERTY(19, EXTERNAL),GRAVITINO_CATALOG_ALREADY_EXISTS(20, EXTERNAL),GRAVITINO_METALAKE_ALREADY_EXISTS(21, EXTERNAL),+ GRAVITINO_OPERATION_FAILED(22, EXTERNAL),;private final ErrorCode errorCode;diff --git a/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/catalog/CatalogConnectorMetadata.java b/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/catalog/CatalogConnectorMetadata.javaindex 846f3c2ed4..9592b4462b 100644--- a/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/catalog/CatalogConnectorMetadata.java+++ b/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/catalog/CatalogConnectorMetadata.java@@ -6,6 +6,7 @@ package com.datastrato.gravitino.trino.connector.catalog;import static com.datastrato.gravitino.trino.connector.GravitinoErrorCode.GRAVITINO_CATALOG_NOT_EXISTS;import static com.datastrato.gravitino.trino.connector.GravitinoErrorCode.GRAVITINO_ILLEGAL_ARGUMENT;+import static com.datastrato.gravitino.trino.connector.GravitinoErrorCode.GRAVITINO_OPERATION_FAILED;import static com.datastrato.gravitino.trino.connector.GravitinoErrorCode.GRAVITINO_SCHEMA_ALREADY_EXISTS;import static com.datastrato.gravitino.trino.connector.GravitinoErrorCode.GRAVITINO_SCHEMA_NOT_EMPTY;import static com.datastrato.gravitino.trino.connector.GravitinoErrorCode.GRAVITINO_SCHEMA_NOT_EXISTS;@@ -166,20 +167,12 @@ public class CatalogConnectorMetadata {}public void dropTable(SchemaTableName tableName) {- try {- tableCatalog.purgeTable(- NameIdentifier.ofTable(- metalake.name(), catalogName, tableName.getSchemaName(), tableName.getTableName()));- } catch (UnsupportedOperationException e) {- LOG.warn("Purge table is not supported", e);- boolean dropped =- tableCatalog.dropTable(- NameIdentifier.ofTable(- metalake.name(),- catalogName,- tableName.getSchemaName(),- tableName.getTableName()));- if (!dropped) throw new TrinoException(GRAVITINO_TABLE_NOT_EXISTS, "Table does not exist");+ boolean dropped =+ tableCatalog.dropTable(+ NameIdentifier.ofTable(+ metalake.name(), catalogName, tableName.getSchemaName(), tableName.getTableName()));+ if (!dropped) {+ throw new TrinoException(GRAVITINO_OPERATION_FAILED, "Failed to drop table " + tableName);}}