From 1c0e5744811ad3ae8ed79d71c8abe875800f8425 Mon Sep 17 00:00:00 2001 From: Marig_Weizhi Date: Tue, 10 Dec 2024 14:05:11 +0800 Subject: [PATCH] Implement dropNamespace --- .../amoro/spark/SparkUnifiedCatalogBase.java | 4 ++-- .../amoro/spark/SparkUnifiedCatalog.java | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/amoro-format-mixed/amoro-mixed-spark/amoro-mixed-spark-3-common/src/main/java/org/apache/amoro/spark/SparkUnifiedCatalogBase.java b/amoro-format-mixed/amoro-mixed-spark/amoro-mixed-spark-3-common/src/main/java/org/apache/amoro/spark/SparkUnifiedCatalogBase.java index aed47f5b8a..1535dc7c33 100644 --- a/amoro-format-mixed/amoro-mixed-spark/amoro-mixed-spark-3-common/src/main/java/org/apache/amoro/spark/SparkUnifiedCatalogBase.java +++ b/amoro-format-mixed/amoro-mixed-spark/amoro-mixed-spark-3-common/src/main/java/org/apache/amoro/spark/SparkUnifiedCatalogBase.java @@ -76,7 +76,7 @@ public class SparkUnifiedCatalogBase implements TableCatalog, SupportsNamespaces TableFormat.MIXED_ICEBERG, "org.apache.amoro.spark.MixedFormatSparkCatalog", TableFormat.PAIMON, "org.apache.paimon.spark.SparkCatalog"); - private UnifiedCatalog unifiedCatalog; + protected UnifiedCatalog unifiedCatalog; private String name; private final Map tableFormats = Maps.newConcurrentMap(); private final Map tableCatalogs = Maps.newConcurrentMap(); @@ -128,7 +128,7 @@ public String name() { return name; } - private String namespaceToDatabase(String[] namespace) { + protected String namespaceToDatabase(String[] namespace) { Preconditions.checkArgument(namespace.length == 1, "only support namespace with 1 level."); return namespace[0]; } diff --git a/amoro-format-mixed/amoro-mixed-spark/v3.3/amoro-mixed-spark-3.3/src/main/java/org/apache/amoro/spark/SparkUnifiedCatalog.java b/amoro-format-mixed/amoro-mixed-spark/v3.3/amoro-mixed-spark-3.3/src/main/java/org/apache/amoro/spark/SparkUnifiedCatalog.java index 09fbfeb5a0..2822232fde 100644 --- a/amoro-format-mixed/amoro-mixed-spark/v3.3/amoro-mixed-spark-3.3/src/main/java/org/apache/amoro/spark/SparkUnifiedCatalog.java +++ b/amoro-format-mixed/amoro-mixed-spark/v3.3/amoro-mixed-spark-3.3/src/main/java/org/apache/amoro/spark/SparkUnifiedCatalog.java @@ -19,6 +19,7 @@ package org.apache.amoro.spark; import org.apache.amoro.TableFormat; +import org.apache.amoro.TableIDWithFormat; import org.apache.spark.sql.catalyst.analysis.NoSuchFunctionException; import org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException; import org.apache.spark.sql.catalyst.analysis.NoSuchTableException; @@ -31,6 +32,8 @@ import org.apache.spark.sql.connector.catalog.functions.UnboundFunction; import org.apache.spark.sql.connector.iceberg.catalog.ProcedureCatalog; +import java.util.List; + public class SparkUnifiedCatalog extends SparkUnifiedCatalogBase implements TableCatalog, SupportsNamespaces, ProcedureCatalog, FunctionCatalog { @@ -86,7 +89,20 @@ public UnboundFunction loadFunction(Identifier ident) throws NoSuchFunctionExcep @Override public boolean dropNamespace(String[] namespace, boolean cascade) throws NoSuchNamespaceException, NonEmptyNamespaceException { - return false; + String database = namespaceToDatabase(namespace); + if (!unifiedCatalog.databaseExists(database)) { + throw new NoSuchNamespaceException(namespace); + } + List tables = unifiedCatalog.listTables(database); + if (!tables.isEmpty() && !cascade) { + throw new NonEmptyNamespaceException(namespace); + } + + for (TableIDWithFormat id : tables) { + unifiedCatalog.dropTable(database, id.getIdentifier().getTableName(), true); + } + unifiedCatalog.dropDatabase(database); + return !unifiedCatalog.databaseExists(database); } /**