Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2401] improvement(spark-connector): support alter namespace operation #2407

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,37 @@ void testCreateAndLoadSchema() {
testDatabaseName = "t_create2";
sql(
String.format(
"CREATE DATABASE %s COMMENT 'comment' LOCATION '/user'\n"
"CREATE DATABASE %s COMMENT 'comment' LOCATION '/tmp/test_%s'\n"
SteNicholas marked this conversation as resolved.
Show resolved Hide resolved
+ " WITH DBPROPERTIES (ID=001);",
testDatabaseName));
testDatabaseName, testDatabaseName));
databaseMeta = getDatabaseMetadata(testDatabaseName);
String comment = databaseMeta.get("Comment");
Assertions.assertEquals("comment", comment);
Assertions.assertEquals("datastrato", databaseMeta.get("Owner"));
// underlying catalog may change /user to file:/user
SteNicholas marked this conversation as resolved.
Show resolved Hide resolved
Assertions.assertTrue(databaseMeta.get("Location").contains("/user"));
Assertions.assertTrue(databaseMeta.get("Location").contains("/tmp/test_" + testDatabaseName));
properties = databaseMeta.get("Properties");
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"));

// Hive metastore doesn't support alter database location, therefore this test method
// doesn't verify ALTER DATABASE database_name SET LOCATION 'new_location'.

Assertions.assertThrowsExactly(
NoSuchNamespaceException.class,
() -> sql("ALTER DATABASE notExists SET DBPROPERTIES ('ID'='001')"));
}

@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,7 @@
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.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 +177,28 @@ 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 {
throw new UnsupportedOperationException(
String.format(
"Unsupported namespace change %s", change.getClass().getName()));
}
})
.toArray(SchemaChange[]::new);
try {
gravitinoCatalogClient
.asSchemas()
.alterSchema(NameIdentifier.of(metalakeName, catalogName, namespace[0]), schemaChanges);
} catch (NoSuchSchemaException e) {
throw new NoSuchNamespaceException(namespace);
}
}

@Override
Expand Down
Loading