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

Add support for DROP SCHEMA CASCADE in JDBC connectors #18305

Merged
merged 1 commit into from
Aug 10, 2023
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 @@ -1159,23 +1159,27 @@ protected void createSchema(ConnectorSession session, Connection connection, Str
}

@Override
public void dropSchema(ConnectorSession session, String schemaName)
public void dropSchema(ConnectorSession session, String schemaName, boolean cascade)
{
ConnectorIdentity identity = session.getIdentity();
try (Connection connection = connectionFactory.openConnection(session)) {
verify(connection.getAutoCommit());
schemaName = identifierMapping.toRemoteSchemaName(getRemoteIdentifiers(connection), identity, schemaName);
dropSchema(session, connection, schemaName);
dropSchema(session, connection, schemaName, cascade);
}
catch (SQLException e) {
throw new TrinoException(JDBC_ERROR, e);
}
}

protected void dropSchema(ConnectorSession session, Connection connection, String remoteSchemaName)
protected void dropSchema(ConnectorSession session, Connection connection, String remoteSchemaName, boolean cascade)
throws SQLException
{
execute(session, connection, "DROP SCHEMA " + quoted(remoteSchemaName));
String dropSchema = "DROP SCHEMA " + quoted(remoteSchemaName);
if (cascade) {
dropSchema += " CASCADE";
}
execute(session, connection, dropSchema);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,9 @@ public void createSchema(ConnectorSession session, String schemaName)
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -984,9 +984,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)
{
jdbcClient.dropSchema(session, schemaName);
jdbcClient.dropSchema(session, schemaName, cascade);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,9 @@ public void createSchema(ConnectorSession session, String schemaName)
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ default TableStatistics getTableStatistics(ConnectorSession session, JdbcTableHa

void createSchema(ConnectorSession session, String schemaName);

void dropSchema(ConnectorSession session, String schemaName);
void dropSchema(ConnectorSession session, String schemaName, boolean cascade);

void renameSchema(ConnectorSession session, String schemaName, String newSchemaName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,9 @@ public void createSchema(ConnectorSession session, String schemaName)
}

@Override
public void dropSchema(ConnectorSession session, String schemaName)
public void dropSchema(ConnectorSession session, String schemaName, boolean cascade)
{
stats.getDropSchema().wrap(() -> delegate().dropSchema(session, schemaName));
stats.getDropSchema().wrap(() -> delegate().dropSchema(session, schemaName, cascade));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ public void afterClass()
protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
{
switch (connectorBehavior) {
case SUPPORTS_DROP_SCHEMA_CASCADE:
return false;

case SUPPORTS_UPDATE: // not supported by any JDBC connector
case SUPPORTS_MERGE: // not supported by any JDBC connector
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void testSchemaNamesCached()
.afterRunning(() -> {
assertThat(cachingJdbcClient.getSchemaNames(SESSION)).contains(phantomSchema);
});
jdbcClient.dropSchema(SESSION, phantomSchema);
jdbcClient.dropSchema(SESSION, phantomSchema, false);

assertThat(jdbcClient.getSchemaNames(SESSION)).doesNotContain(phantomSchema);
assertSchemaNamesCache(cachingJdbcClient)
Expand Down Expand Up @@ -853,7 +853,7 @@ public void testConcurrentSchemaCreateAndDrop()
assertThat(cachingJdbcClient.getSchemaNames(session)).doesNotContain(schemaName);
cachingJdbcClient.createSchema(session, schemaName);
assertThat(cachingJdbcClient.getSchemaNames(session)).contains(schemaName);
cachingJdbcClient.dropSchema(session, schemaName);
cachingJdbcClient.dropSchema(session, schemaName, false);
assertThat(cachingJdbcClient.getSchemaNames(session)).doesNotContain(schemaName);
return null;
}));
Expand Down Expand Up @@ -1026,7 +1026,7 @@ public void testSpecificSchemaAndTableCaches()

jdbcClient.dropTable(SESSION, first);
jdbcClient.dropTable(SESSION, second);
jdbcClient.dropSchema(SESSION, secondSchema);
jdbcClient.dropSchema(SESSION, secondSchema, false);
}

private JdbcTableHandle getAnyTable(String schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void testCreateSchema()
String schemaName = "test schema";
jdbcClient.createSchema(session, schemaName);
assertThat(jdbcClient.getSchemaNames(session)).contains(schemaName);
jdbcClient.dropSchema(session, schemaName);
jdbcClient.dropSchema(session, schemaName, false);
assertThat(jdbcClient.getSchemaNames(session)).doesNotContain(schemaName);
}

Expand All @@ -154,7 +154,7 @@ public void testRenameTable()
jdbcClient.createTable(session, tableMetadata);
jdbcClient.renameTable(session, jdbcClient.getTableHandle(session, oldTable).get(), newTable);
jdbcClient.dropTable(session, jdbcClient.getTableHandle(session, newTable).get());
jdbcClient.dropSchema(session, schemaName);
jdbcClient.dropSchema(session, schemaName, false);
assertThat(jdbcClient.getTableNames(session, Optional.empty()))
.doesNotContain(oldTable)
.doesNotContain(newTable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
import static io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR;
import static io.trino.spi.StandardErrorCode.INVALID_TABLE_PROPERTY;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.StandardErrorCode.SCHEMA_NOT_EMPTY;
import static io.trino.spi.connector.ConnectorMetadata.MODIFYING_ROWS_MESSAGE;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.BooleanType.BOOLEAN;
Expand Down Expand Up @@ -406,9 +407,17 @@ protected void createSchema(ConnectorSession session, Connection connection, Str
}

@Override
protected void dropSchema(ConnectorSession session, Connection connection, String remoteSchemaName)
protected void dropSchema(ConnectorSession session, Connection connection, String remoteSchemaName, boolean cascade)
throws SQLException
{
// ClickHouse always deletes all tables inside the database https://clickhouse.com/docs/en/sql-reference/statements/drop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to prevent this if cascade was not specified? (and in MySQL, MariaDB and Singlestore too).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean throwing an exception when !cascade? It means users always need to specify cascade option even when the target database is empty, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I was thinking of would require a list operation - and even then there can be races.
I don't know a good solution. My main concern is that if someone does a DROP SCHEMA (without CASCADE) they may not expect the operation to actually CASCADE to all objects in the schema.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the test and manually tested without the same check in DropSchemaTask.

if (!cascade) {
try (ResultSet tables = getTables(connection, Optional.of(remoteSchemaName), Optional.empty())) {
if (tables.next()) {
throw new TrinoException(SCHEMA_NOT_EMPTY, "Cannot drop non-empty schema '%s'".formatted(remoteSchemaName));
}
}
}
execute(session, connection, "DROP DATABASE " + quoted(remoteSchemaName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ public void createSchema(ConnectorSession session, String schemaName)
}

@Override
public void dropSchema(ConnectorSession session, String schemaName)
public void dropSchema(ConnectorSession session, String schemaName, boolean cascade)
{
throw new TrinoException(NOT_SUPPORTED, "This connector does not support dropping schemas");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ public void createSchema(ConnectorSession session, String schemaName)
}

@Override
public void dropSchema(ConnectorSession session, String schemaName)
public void dropSchema(ConnectorSession session, String schemaName, boolean cascade)
{
throw new TrinoException(NOT_SUPPORTED, "This connector does not support dropping schemas");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
import static io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling;
import static io.trino.plugin.jdbc.UnsupportedTypeHandling.CONVERT_TO_VARCHAR;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
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.DateType.DATE;
Expand Down Expand Up @@ -238,6 +239,21 @@ public void renameSchema(ConnectorSession session, String schemaName, String new
throw new TrinoException(NOT_SUPPORTED, "This connector does not support renaming schemas");
}

@Override
protected void dropSchema(ConnectorSession session, Connection connection, String remoteSchemaName, boolean cascade)
throws SQLException
{
// MariaDB always deletes all tables inside the database https://mariadb.com/kb/en/drop-database/
if (!cascade) {
try (ResultSet tables = getTables(connection, Optional.of(remoteSchemaName), Optional.empty())) {
if (tables.next()) {
throw new TrinoException(SCHEMA_NOT_EMPTY, "Cannot drop non-empty schema '%s'".formatted(remoteSchemaName));
}
}
}
execute(session, connection, "DROP SCHEMA " + quoted(remoteSchemaName));
}

@Override
public ResultSet getTables(Connection connection, Optional<String> schemaName, Optional<String> tableName)
throws SQLException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
import static io.trino.plugin.jdbc.UnsupportedTypeHandling.CONVERT_TO_VARCHAR;
import static io.trino.spi.StandardErrorCode.ALREADY_EXISTS;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
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.CharType.createCharType;
Expand Down Expand Up @@ -355,6 +356,21 @@ protected boolean filterSchema(String schemaName)
return super.filterSchema(schemaName);
}

@Override
protected void dropSchema(ConnectorSession session, Connection connection, String remoteSchemaName, boolean cascade)
throws SQLException
{
// MySQL always deletes all tables inside the database https://dev.mysql.com/doc/refman/8.0/en/drop-database.html
if (!cascade) {
try (ResultSet tables = getTables(connection, Optional.of(remoteSchemaName), Optional.empty())) {
if (tables.next()) {
throw new TrinoException(SCHEMA_NOT_EMPTY, "Cannot drop non-empty schema '%s'".formatted(remoteSchemaName));
}
}
}
execute(session, connection, "DROP SCHEMA " + quoted(remoteSchemaName));
}

@Override
public void abortReadConnection(Connection connection, ResultSet resultSet)
throws SQLException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public void createSchema(ConnectorSession session, String schemaName)
}

@Override
public void dropSchema(ConnectorSession session, String schemaName)
public void dropSchema(ConnectorSession session, String schemaName, boolean cascade)
{
throw new TrinoException(NOT_SUPPORTED, "This connector does not support dropping schemas");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,12 @@ 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)
{
if (cascade) {
// Phoenix doesn't support CASCADE option https://phoenix.apache.org/language/index.html#drop_schema
throw new TrinoException(NOT_SUPPORTED, "This connector does not support dropping schemas with CASCADE option");
}
if (DEFAULT_SCHEMA.equalsIgnoreCase(schemaName)) {
throw new TrinoException(NOT_SUPPORTED, "Can't drop 'default' schema which maps to Phoenix empty schema");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
return true;

case SUPPORTS_RENAME_SCHEMA:
case SUPPORTS_DROP_SCHEMA_CASCADE:
return false;

case SUPPORTS_CREATE_TABLE_WITH_TABLE_COMMENT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,17 @@ public PostgreSqlClient(
.build());
}

@Override
protected void dropSchema(ConnectorSession session, Connection connection, String remoteSchemaName, boolean cascade)
throws SQLException
{
if (cascade) {
// Dropping schema with cascade option may lead to other metadata listing operations. Disable until finding the solution.
throw new TrinoException(NOT_SUPPORTED, "This connector does not support dropping schemas with CASCADE option");
}
execute(session, connection, "DROP SCHEMA " + quoted(remoteSchemaName));
}

@Override
public void createTable(ConnectorSession session, ConnectorTableMetadata tableMetadata)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_JOIN_PUSHDOWN_WITH_FULL_JOIN:
return false;

case SUPPORTS_DROP_SCHEMA_CASCADE:
return false;

case SUPPORTS_CREATE_TABLE_WITH_COLUMN_COMMENT:
case SUPPORTS_RENAME_TABLE_ACROSS_SCHEMAS:
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,17 @@ public Connection getConnection(ConnectorSession session, JdbcSplit split, JdbcT
return connection;
}

@Override
protected void dropSchema(ConnectorSession session, Connection connection, String remoteSchemaName, boolean cascade)
throws SQLException
{
if (cascade) {
// Dropping schema with cascade option may lead to other metadata listing operations. Disable until finding the solution.
throw new TrinoException(NOT_SUPPORTED, "This connector does not support dropping schemas with CASCADE option");
}
execute(session, connection, "DROP SCHEMA " + quoted(remoteSchemaName));
}

@Override
protected List<String> createTableSqls(RemoteTableName remoteTableName, List<String> columns, ConnectorTableMetadata tableMetadata)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ protected QueryRunner createQueryRunner()
protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
{
switch (connectorBehavior) {
case SUPPORTS_DROP_SCHEMA_CASCADE:
return false;

case SUPPORTS_COMMENT_ON_COLUMN:
return true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
import static io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling;
import static io.trino.plugin.jdbc.UnsupportedTypeHandling.CONVERT_TO_VARCHAR;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
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.DateType.DATE;
Expand Down Expand Up @@ -220,6 +221,22 @@ protected boolean filterSchema(String schemaName)
return super.filterSchema(schemaName);
}

@Override
protected void dropSchema(ConnectorSession session, Connection connection, String remoteSchemaName, boolean cascade)
throws SQLException
{
// SingleStore always deletes all tables inside the database though
// the behavior isn't documented in https://docs.singlestore.com/cloud/reference/sql-reference/data-definition-language-ddl/drop-database/
if (!cascade) {
try (ResultSet tables = getTables(connection, Optional.of(remoteSchemaName), Optional.empty())) {
if (tables.next()) {
throw new TrinoException(SCHEMA_NOT_EMPTY, "Cannot drop non-empty schema '%s'".formatted(remoteSchemaName));
}
}
}
execute(session, connection, "DROP SCHEMA " + quoted(remoteSchemaName));
}

@Override
public Optional<String> getTableComment(ResultSet resultSet)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,17 @@ public SqlServerClient(
.build());
}

@Override
protected void dropSchema(ConnectorSession session, Connection connection, String remoteSchemaName, boolean cascade)
throws SQLException
{
if (cascade) {
// SQL Server doesn't support CASCADE option https://learn.microsoft.com/en-us/sql/t-sql/statements/drop-schema-transact-sql
throw new TrinoException(NOT_SUPPORTED, "This connector does not support dropping schemas with CASCADE option");
}
execute(session, connection, "DROP SCHEMA " + quoted(remoteSchemaName));
}

@Override
public JdbcOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
return false;

case SUPPORTS_RENAME_SCHEMA:
case SUPPORTS_DROP_SCHEMA_CASCADE:
return false;

case SUPPORTS_CREATE_TABLE_WITH_TABLE_COMMENT:
Expand Down