Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -261,9 +261,9 @@ public void createSchema(String schemaName)
schemaEmulation.createSchema(client, schemaName);
}

public void dropSchema(String schemaName)
public void dropSchema(String schemaName, boolean cascade)
{
schemaEmulation.dropSchema(client, schemaName);
schemaEmulation.dropSchema(client, schemaName, cascade);
}

public void dropTable(SchemaTableName schemaTableName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ public void createSchema(ConnectorSession session, String schemaName, Map<String
}

@Override
public void dropSchema(ConnectorSession session, String schemaName)
public void dropSchema(ConnectorSession session, String schemaName, boolean cascade)
{
clientSession.dropSchema(schemaName);
clientSession.dropSchema(schemaName, cascade);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void createSchema(KuduClientWrapper client, String schemaName)
}

@Override
public void dropSchema(KuduClientWrapper client, String schemaName)
public void dropSchema(KuduClientWrapper client, String schemaName, boolean cascade)
{
if (DEFAULT_SCHEMA.equals(schemaName)) {
throw new TrinoException(GENERIC_USER_ERROR, "Deleting default schema not allowed.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface SchemaEmulation
{
void createSchema(KuduClientWrapper client, String schemaName);

void dropSchema(KuduClientWrapper client, String schemaName);
void dropSchema(KuduClientWrapper client, String schemaName, boolean cascade);

boolean existsSchema(KuduClientWrapper client, String schemaName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static io.trino.plugin.kudu.KuduClientSession.DEFAULT_SCHEMA;
import static io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR;
import static io.trino.spi.StandardErrorCode.GENERIC_USER_ERROR;
import static io.trino.spi.StandardErrorCode.SCHEMA_NOT_EMPTY;

public class SchemaEmulationByTableNameConvention
implements SchemaEmulation
Expand Down Expand Up @@ -81,14 +82,18 @@ public boolean existsSchema(KuduClientWrapper client, String schemaName)
}

@Override
public void dropSchema(KuduClientWrapper client, String schemaName)
public void dropSchema(KuduClientWrapper client, String schemaName, boolean cascade)
{
if (DEFAULT_SCHEMA.equals(schemaName)) {
throw new TrinoException(GENERIC_USER_ERROR, "Deleting default schema not allowed.");
}
try (KuduOperationApplier operationApplier = KuduOperationApplier.fromKuduClientWrapper(client)) {
String prefix = getPrefixForTablesOfSchema(schemaName);
for (String name : client.getTablesList(prefix).getTablesList()) {
List<String> tables = client.getTablesList(prefix).getTablesList();
if (!cascade && !tables.isEmpty()) {
throw new TrinoException(SCHEMA_NOT_EMPTY, "Cannot drop non-empty schema '%s'".formatted(schemaName));
}
for (String name : tables) {
client.deleteTable(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.trino.testing.BaseConnectorSmokeTest;
import io.trino.testing.QueryRunner;
import io.trino.testing.TestingConnectorBehavior;
import org.testng.SkipException;
import org.testng.annotations.Test;

import java.util.Optional;
Expand All @@ -26,6 +27,7 @@
import static io.trino.plugin.kudu.TestingKuduServer.EARLIEST_TAG;
import static io.trino.testing.TestingNames.randomNameSuffix;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public abstract class BaseKuduConnectorSmokeTest
extends BaseConnectorSmokeTest
Expand Down Expand Up @@ -162,4 +164,29 @@ public void testCreateTableWithTableComment()

assertUpdate("DROP TABLE " + tableName);
}

@Test
public void testDropSchemaCascade()
{
String schemaName = "test_drop_schema_cascade_" + randomNameSuffix();
String tableName = "test_table" + randomNameSuffix();
try {
if (getKuduSchemaEmulationPrefix().isEmpty()) {
assertThatThrownBy(() -> assertUpdate("CREATE SCHEMA " + schemaName))
.hasMessageContaining("Creating schema in Kudu connector not allowed if schema emulation is disabled.");
throw new SkipException("Cannot test when schema emulation is disabled");
}
assertUpdate("CREATE SCHEMA " + schemaName);
assertUpdate("CREATE TABLE " + schemaName + "." + tableName + " AS SELECT 1 a", 1);

assertThat(computeActual("SHOW SCHEMAS").getOnlyColumnAsSet()).contains(schemaName);

assertUpdate("DROP SCHEMA " + schemaName + " CASCADE");
assertThat(computeActual("SHOW SCHEMAS").getOnlyColumnAsSet()).doesNotContain(schemaName);
}
finally {
assertUpdate("DROP TABLE IF EXISTS " + schemaName + "." + tableName);
assertUpdate("DROP SCHEMA IF EXISTS " + schemaName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
return false;

case SUPPORTS_RENAME_SCHEMA:
case SUPPORTS_DROP_SCHEMA_CASCADE:
return false;

case SUPPORTS_CREATE_TABLE_WITH_COLUMN_COMMENT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ public void createSchema(ConnectorSession session, String schemaName, Map<String
}

@Override
public void dropSchema(ConnectorSession session, String schemaName)
public void dropSchema(ConnectorSession session, String schemaName, boolean cascade)
{
mongoSession.dropSchema(schemaName);
mongoSession.dropSchema(schemaName, cascade);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import static io.trino.plugin.mongodb.ObjectIdType.OBJECT_ID;
import static io.trino.plugin.mongodb.ptf.Query.parseFilter;
import static io.trino.spi.HostAddress.fromParts;
import static io.trino.spi.StandardErrorCode.SCHEMA_NOT_EMPTY;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.spi.type.Chars.padSpaces;
Expand Down Expand Up @@ -212,9 +213,20 @@ public void createSchema(String schemaName)
client.getDatabase(schemaName).createCollection(schemaCollection);
}

public void dropSchema(String schemaName)
public void dropSchema(String schemaName, boolean cascade)
{
client.getDatabase(toRemoteSchemaName(schemaName)).drop();
MongoDatabase database = client.getDatabase(toRemoteSchemaName(schemaName));
if (!cascade) {
try (MongoCursor<String> collections = database.listCollectionNames().cursor()) {
while (collections.hasNext()) {
if (collections.next().equals(schemaCollection)) {
continue;
}
throw new TrinoException(SCHEMA_NOT_EMPTY, "Cannot drop non-empty schema '%s'".formatted(schemaName));
}
}
}
database.drop();
}

public Set<String> getAllTables(String schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
return false;

case SUPPORTS_RENAME_SCHEMA:
case SUPPORTS_DROP_SCHEMA_CASCADE:
return false;

case SUPPORTS_ADD_FIELD:
Expand Down