Skip to content

Commit

Permalink
[apache#2401] improvement(spark-connector): support alter namespace o…
Browse files Browse the repository at this point in the history
…peration
  • Loading branch information
SteNicholas committed Mar 4, 2024
1 parent 32661a1 commit 46e5fac
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ void testCreateAndLoadSchema() {
Assertions.assertEquals("((ID,001))", properties);
}

@Test
void testAlterSchema() {
String testDatabaseName = "t_alter";
sql("create database " + testDatabaseName);
Assertions.assertTrue(
StringUtils.isBlank(getDatabaseMetadata(testDatabaseName).get("Properties")));

sql(String.format("alter database %s set dbproperties ('ID'='001')", testDatabaseName));
Assertions.assertEquals("((ID,001))", getDatabaseMetadata(testDatabaseName).get("Properties"));

sql(String.format("alter database %s set location '/user'", testDatabaseName));
Assertions.assertTrue(getDatabaseMetadata(testDatabaseName).get("Location").contains("/user"));

Assertions.assertThrowsExactly(
NoSuchNamespaceException.class,
() -> sql("alter database notExists set dbproperties ('ID'='001')"));
Assertions.assertThrowsExactly(
NoSuchNamespaceException.class, () -> sql("alter database notExists set location '/user'"));
}

@Test
void testDropSchema() {
String testDatabaseName = "t_drop";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.datastrato.gravitino.exceptions.NonEmptySchemaException;
import com.datastrato.gravitino.exceptions.SchemaAlreadyExistsException;
import com.datastrato.gravitino.rel.Schema;
import com.datastrato.gravitino.rel.SchemaChange;
import com.datastrato.gravitino.spark.GravitinoSparkConfig;
import com.google.common.base.Preconditions;
import java.util.Arrays;
Expand All @@ -30,6 +31,8 @@
import org.apache.spark.sql.connector.catalog.Column;
import org.apache.spark.sql.connector.catalog.Identifier;
import org.apache.spark.sql.connector.catalog.NamespaceChange;
import org.apache.spark.sql.connector.catalog.NamespaceChange.RemoveProperty;
import org.apache.spark.sql.connector.catalog.NamespaceChange.SetProperty;
import org.apache.spark.sql.connector.catalog.SupportsNamespaces;
import org.apache.spark.sql.connector.catalog.Table;
import org.apache.spark.sql.connector.catalog.TableCatalog;
Expand Down Expand Up @@ -175,7 +178,29 @@ public void createNamespace(String[] namespace, Map<String, String> metadata)
@Override
public void alterNamespace(String[] namespace, NamespaceChange... changes)
throws NoSuchNamespaceException {
throw new NotSupportedException("Doesn't support altering namespace");
validateNamespace(namespace);
SchemaChange[] schemaChanges =
Arrays.stream(changes)
.map(
change -> {
if (change instanceof SetProperty) {
SetProperty setProperty = ((SetProperty) change);
return SchemaChange.setProperty(setProperty.property(), setProperty.value());
} else if (change instanceof RemoveProperty) {
RemoveProperty removeProperty = ((RemoveProperty) change);
return SchemaChange.removeProperty(removeProperty.property());
} else {
throw new UnsupportedOperationException("Unsupported namespace change");
}
})
.toArray(SchemaChange[]::new);
try {
gravitinoCatalogClient
.asSchemas()
.alterSchema(NameIdentifier.of(metalakeName, catalogName, namespace[0]), schemaChanges);
} catch (NoSuchSchemaException e) {
throw new NoSuchNamespaceException(namespace);
}
}

@Override
Expand Down

0 comments on commit 46e5fac

Please sign in to comment.