Skip to content

Commit

Permalink
Oracle purges dropped temp tables
Browse files Browse the repository at this point in the history
  • Loading branch information
mwd410 authored and losipiuk committed May 16, 2023
1 parent 5b8e06a commit f1a3447
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ public void finishInsertTable(ConnectorSession session, JdbcOutputTableHandle ha

// We conditionally create more than the one table, so keep a list of the tables that need to be dropped.
Closer closer = Closer.create();
closer.register(() -> dropTable(session, temporaryTable));
closer.register(() -> dropTable(session, temporaryTable, true));

try (Connection connection = getConnection(session, handle)) {
verify(connection.getAutoCommit());
Expand All @@ -906,7 +906,7 @@ public void finishInsertTable(ConnectorSession session, JdbcOutputTableHandle ha

if (handle.getPageSinkIdColumnName().isPresent()) {
RemoteTableName pageSinkTable = constructPageSinkIdsTable(session, connection, handle, pageSinkIds);
closer.register(() -> dropTable(session, pageSinkTable));
closer.register(() -> dropTable(session, pageSinkTable, true));

insertSql += format(" WHERE EXISTS (SELECT 1 FROM %s page_sink_table WHERE page_sink_table.%s = temp_table.%s)",
quoted(pageSinkTable),
Expand Down Expand Up @@ -1034,10 +1034,10 @@ public void setColumnType(ConnectorSession session, JdbcTableHandle handle, Jdbc
public void dropTable(ConnectorSession session, JdbcTableHandle handle)
{
verify(handle.getAuthorization().isEmpty(), "Unexpected authorization is required for table: %s".formatted(handle));
dropTable(session, handle.asPlainTable().getRemoteTableName());
dropTable(session, handle.asPlainTable().getRemoteTableName(), false);
}

protected void dropTable(ConnectorSession session, RemoteTableName remoteTableName)
protected void dropTable(ConnectorSession session, RemoteTableName remoteTableName, boolean temporaryTable)
{
String sql = "DROP TABLE " + quoted(remoteTableName);
execute(session, sql);
Expand All @@ -1047,10 +1047,12 @@ protected void dropTable(ConnectorSession session, RemoteTableName remoteTableNa
public void rollbackCreateTable(ConnectorSession session, JdbcOutputTableHandle handle)
{
if (handle.getTemporaryTableName().isPresent()) {
dropTable(session, new JdbcTableHandle(
new SchemaTableName(handle.getSchemaName(), handle.getTemporaryTableName().get()),
new RemoteTableName(Optional.ofNullable(handle.getCatalogName()), Optional.ofNullable(handle.getSchemaName()), handle.getTemporaryTableName().get()),
Optional.empty()));
dropTable(session,
new RemoteTableName(
Optional.ofNullable(handle.getCatalogName()),
Optional.ofNullable(handle.getSchemaName()),
handle.getTemporaryTableName().get()),
true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,20 @@ public void dropSchema(ConnectorSession session, String schemaName)
throw new TrinoException(NOT_SUPPORTED, "This connector does not support dropping schemas");
}

@Override
protected void dropTable(ConnectorSession session, RemoteTableName remoteTableName, boolean temporaryTable)
{
String sql = "DROP TABLE " + quoted(remoteTableName);
// Oracle puts dropped tables into a recycling bin, which keeps them accessible for a period of time.
// PURGE will bypass the bin and completely delete the table immediately.
// We should only PURGE the table if it is a temporary table that trino created,
// as purging all dropped tables may be unexpected behavior for our clients.
if (temporaryTable) {
sql += " PURGE";
}
execute(session, sql);
}

@Override
public void renameSchema(ConnectorSession session, String schemaName, String newSchemaName)
{
Expand Down

0 comments on commit f1a3447

Please sign in to comment.