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 truncate statement in Iceberg #22340

Merged
merged 1 commit into from
Jun 11, 2024
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 @@ -306,6 +306,7 @@
import static org.apache.iceberg.TableProperties.DELETE_ISOLATION_LEVEL_DEFAULT;
import static org.apache.iceberg.TableProperties.FORMAT_VERSION;
import static org.apache.iceberg.TableProperties.WRITE_LOCATION_PROVIDER_IMPL;
import static org.apache.iceberg.expressions.Expressions.alwaysTrue;
import static org.apache.iceberg.types.TypeUtil.indexParents;
import static org.apache.iceberg.util.LocationUtil.stripTrailingSlash;
import static org.apache.iceberg.util.SnapshotUtil.schemaFor;
Expand Down Expand Up @@ -2551,6 +2552,16 @@ public OptionalLong executeDelete(ConnectorSession session, ConnectorTableHandle
return OptionalLong.of(deletedRecords - removedPositionDeletes - removedEqualityDeletes);
}

@Override
public void truncateTable(ConnectorSession session, ConnectorTableHandle tableHandle)
{
IcebergTableHandle table = checkValidTableHandle(tableHandle);
Table icebergTable = catalog.loadTable(session, table.getSchemaTableName());
DeleteFiles deleteFiles = icebergTable.newDelete()
.deleteFromRowFilter(alwaysTrue());
ebyhr marked this conversation as resolved.
Show resolved Hide resolved
commit(deleteFiles, session);
}

public void rollback()
{
// TODO: cleanup open transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public void initFileSystem()
protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
{
return switch (connectorBehavior) {
case SUPPORTS_TOPN_PUSHDOWN,
SUPPORTS_TRUNCATE -> false;
case SUPPORTS_TOPN_PUSHDOWN -> false;
default -> super.hasBehavior(connectorBehavior);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
SUPPORTS_REPORTING_WRITTEN_BYTES -> true;
case SUPPORTS_ADD_COLUMN_NOT_NULL_CONSTRAINT,
SUPPORTS_RENAME_MATERIALIZED_VIEW_ACROSS_SCHEMAS,
SUPPORTS_TOPN_PUSHDOWN,
SUPPORTS_TRUNCATE -> false;
SUPPORTS_TOPN_PUSHDOWN -> false;
default -> super.hasBehavior(connectorBehavior);
};
}
Expand Down Expand Up @@ -6227,6 +6226,15 @@ public void testEmptyDelete()
assertUpdate("DROP TABLE " + tableName);
}

@Test
public void testEmptyFilesTruncate()
{
try (TestTable table = new TestTable(getQueryRunner()::execute, "test_empty_files_truncate_", "AS SELECT 1 AS id")) {
assertUpdate("TRUNCATE TABLE " + table.getName());
assertQueryReturnsEmptyResult("SELECT * FROM \"" + table.getName() + "$files\"");
}
}

@Test
public void testModifyingOldSnapshotIsNotPossible()
{
Expand Down Expand Up @@ -7298,7 +7306,7 @@ public void testCorruptedTableLocation()
assertQueryFails("UPDATE " + tableName + " SET country = 'AUSTRIA'", "Metadata not found in metadata location for table " + schemaTableName);
assertQueryFails("DELETE FROM " + tableName, "Metadata not found in metadata location for table " + schemaTableName);
assertQueryFails("MERGE INTO " + tableName + " USING (SELECT 1 a) input ON true WHEN MATCHED THEN DELETE", "Metadata not found in metadata location for table " + schemaTableName);
assertQueryFails("TRUNCATE TABLE " + tableName, "This connector does not support truncating tables");
assertQueryFails("TRUNCATE TABLE " + tableName, "Metadata not found in metadata location for table " + schemaTableName);
assertQueryFails("COMMENT ON TABLE " + tableName + " IS NULL", "Metadata not found in metadata location for table " + schemaTableName);
assertQueryFails("COMMENT ON COLUMN " + tableName + ".foo IS NULL", "Metadata not found in metadata location for table " + schemaTableName);
assertQueryFails("CALL iceberg.system.rollback_to_snapshot(CURRENT_SCHEMA, '" + tableName + "', 8954597067493422955)", "Metadata not found in metadata location for table " + schemaTableName);
Expand Down
Loading