Skip to content

Commit 6c9b548

Browse files
Add support for drop branch for Iceberg Table
1 parent bb0d754 commit 6c9b548

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

presto-docs/src/main/sphinx/connector/iceberg.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,8 @@ Alter table operations are supported in the Iceberg connector::
14041404

14051405
ALTER TABLE iceberg.web.page_views DROP COLUMN location;
14061406

1407+
ALTER TABLE iceberg.web.page_views DROP BRANCH 'branch1';
1408+
14071409
To add a new column as a partition column, identify the transform functions for the column.
14081410
The table is partitioned by the transformed value of the column::
14091411

presto-iceberg/src/main/java/com/facebook/presto/iceberg/IcebergAbstractMetadata.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
import static com.facebook.presto.iceberg.optimizer.IcebergPlanOptimizer.getEnforcedColumns;
184184
import static com.facebook.presto.iceberg.util.StatisticsUtil.calculateBaseTableStatistics;
185185
import static com.facebook.presto.iceberg.util.StatisticsUtil.calculateStatisticsConsideringLayout;
186+
import static com.facebook.presto.spi.StandardErrorCode.NOT_FOUND;
186187
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
187188
import static com.facebook.presto.spi.statistics.TableStatisticType.ROW_COUNT;
188189
import static com.google.common.base.Strings.isNullOrEmpty;
@@ -843,6 +844,22 @@ public void rollback()
843844
// TODO: cleanup open transaction
844845
}
845846

847+
@Override
848+
public void dropBranch(ConnectorSession session, ConnectorTableHandle tableHandle, String branchName, boolean branchExists)
849+
{
850+
IcebergTableHandle icebergTableHandle = (IcebergTableHandle) tableHandle;
851+
verify(icebergTableHandle.getIcebergTableName().getTableType() == DATA, "only the data table can have branch dropped");
852+
Table icebergTable = getIcebergTable(session, icebergTableHandle.getSchemaTableName());
853+
if (icebergTable.refs().containsKey(branchName) && icebergTable.refs().get(branchName).isBranch()) {
854+
icebergTable.manageSnapshots().removeBranch(branchName).commit();
855+
}
856+
else {
857+
if (!branchExists) {
858+
throw new PrestoException(NOT_FOUND, format("Branch %s doesn't exist in table %s", branchName, icebergTableHandle.getSchemaTableName().getTableName()));
859+
}
860+
}
861+
}
862+
846863
@Override
847864
public void addColumn(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnMetadata column)
848865
{

presto-iceberg/src/test/java/com/facebook/presto/iceberg/IcebergDistributedTestBase.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,6 +1817,33 @@ public void testDecimal(boolean decimalVectorReaderEnabled)
18171817
}
18181818
}
18191819

1820+
@Test
1821+
public void testDropBranch()
1822+
{
1823+
assertUpdate("CREATE TABLE test_table_branch (id1 BIGINT, id2 BIGINT)");
1824+
assertUpdate("INSERT INTO test_table_branch VALUES (0, 00), (1, 10)", 2);
1825+
1826+
Table icebergTable = loadTable("test_table_branch");
1827+
icebergTable.manageSnapshots().createBranch("testBranch1").commit();
1828+
1829+
assertUpdate("INSERT INTO test_table_branch VALUES (2, 30), (3, 30)", 2);
1830+
icebergTable.manageSnapshots().createBranch("testBranch2").commit();
1831+
assertUpdate("INSERT INTO test_table_branch VALUES (4, 40), (5, 50)", 2);
1832+
assertEquals(icebergTable.refs().size(), 3);
1833+
1834+
assertQuery("SELECT count(*) FROM test_table_branch FOR SYSTEM_VERSION AS OF 'testBranch1'", "VALUES 2");
1835+
assertQuery("SELECT count(*) FROM test_table_branch FOR SYSTEM_VERSION AS OF 'testBranch2'", "VALUES 4");
1836+
assertQuery("SELECT count(*) FROM test_table_branch FOR SYSTEM_VERSION AS OF 'main'", "VALUES 6");
1837+
1838+
assertQuerySucceeds("ALTER TABLE test_table_branch DROP BRANCH 'testBranch1'");
1839+
icebergTable = loadTable("test_table_branch");
1840+
assertEquals(icebergTable.refs().size(), 2);
1841+
assertQueryFails("ALTER TABLE test_table_branch DROP BRANCH 'testBranchNotExist'", "Branch testBranchNotExist doesn't exist in table test_table_branch");
1842+
assertQuerySucceeds("ALTER TABLE test_table_branch DROP BRANCH IF EXISTS 'testBranch2'");
1843+
assertQuerySucceeds("ALTER TABLE test_table_branch DROP BRANCH IF EXISTS 'testBranchNotExist'");
1844+
assertQuerySucceeds("DROP TABLE test_table_branch");
1845+
}
1846+
18201847
@Test
18211848
public void testRefsTable()
18221849
{

0 commit comments

Comments
 (0)