Skip to content

Commit

Permalink
Support Spark DROP TABLE command (unitycatalog#295)
Browse files Browse the repository at this point in the history
**Description of changes**

<!-- Please state what you've changed and how it might affect the users.
-->

Support Spark DROP TABLE command
  • Loading branch information
amaliujia authored Jul 29, 2024
1 parent 3feba9d commit 9e6ac0e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class UCSingleCatalog extends TableCatalog {
}
override def alterTable(ident: Identifier, changes: TableChange*): Table = ???

override def dropTable(ident: Identifier): Boolean = ???
override def dropTable(ident: Identifier): Boolean = deltaCatalog.dropTable(ident)

override def renameTable(oldIdent: Identifier, newIdent: Identifier): Unit = ???
}
Expand Down Expand Up @@ -93,9 +93,8 @@ private class UCProxy extends TableCatalog {
}

override def listTables(namespace: Array[String]): Array[Identifier] = {
if (namespace.length > 1) {
throw new ApiException("Nested namespaces are not supported: " + namespace.mkString("."))
}
checkUnsupportedNestedNamespace(namespace)

val catalogName = this.name
val schemaName = namespace.head
val maxResults = 0
Expand Down Expand Up @@ -174,8 +173,18 @@ private class UCProxy extends TableCatalog {

override def alterTable(ident: Identifier, changes: TableChange*): Table = ???

override def dropTable(ident: Identifier): Boolean = ???
override def dropTable(ident: Identifier): Boolean = {
checkUnsupportedNestedNamespace(ident.namespace())
val ret =
tablesApi.deleteTable(Seq(this.name, ident.namespace()(0), ident.name()).mkString("."))
if (ret == 200) true else false
}

override def renameTable(oldIdent: Identifier, newIdent: Identifier): Unit = ???

private def checkUnsupportedNestedNamespace(namespace: Array[String]): Unit = {
if (namespace.length > 1) {
throw new ApiException("Nested namespaces are not supported: " + namespace.mkString("."))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ public void testShowTables() throws ApiException, IOException {
session.stop();
}

@Test
public void testDropTable() throws ApiException, IOException {
createCommonResources();
SparkSession session = createSparkSessionWithCatalogs(SPARK_CATALOG);
setupExternalParquetTable(PARQUET_TABLE, new ArrayList<>(0));
String fullName = String.join(".", SPARK_CATALOG, SCHEMA_NAME, PARQUET_TABLE);
assertTrue(session.catalog().tableExists(fullName));
session.sql("DROP TABLE " + fullName).collect();
assertFalse(session.catalog().tableExists(fullName));
AnalysisException exception =
assertThrows(AnalysisException.class, () -> session.sql("DROP TABLE a.b.c.d").collect());
session.stop();
}

private String generateTableLocation(String catalogName, String tableName) throws IOException {
return new File(new File(dataDir, catalogName), tableName).getCanonicalPath();
}
Expand Down

0 comments on commit 9e6ac0e

Please sign in to comment.