Skip to content

Commit

Permalink
Improve error message when trying to drop partitioned columns in Iceberg
Browse files Browse the repository at this point in the history
  • Loading branch information
krvikash authored and ebyhr committed Jan 17, 2023
1 parent dda6432 commit 46ee6c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
import org.apache.iceberg.expressions.Expressions;
import org.apache.iceberg.expressions.Term;
import org.apache.iceberg.io.CloseableIterable;
import org.apache.iceberg.transforms.Transforms;
import org.apache.iceberg.types.Type;
import org.apache.iceberg.types.Types;
import org.apache.iceberg.types.Types.IntegerType;
Expand Down Expand Up @@ -1529,6 +1530,11 @@ public void dropColumn(ConnectorSession session, ConnectorTableHandle tableHandl
{
IcebergColumnHandle handle = (IcebergColumnHandle) column;
Table icebergTable = catalog.loadTable(session, ((IcebergTableHandle) tableHandle).getSchemaTableName());
boolean isPartitionColumn = icebergTable.spec().fields().stream()
.anyMatch(field -> field.sourceId() == handle.getId() && !isVoidTransform(field));
if (isPartitionColumn) {
throw new TrinoException(NOT_SUPPORTED, "Cannot drop partition field: " + handle.getName());
}
try {
icebergTable.updateSchema()
.deleteColumn(handle.getName())
Expand All @@ -1539,6 +1545,11 @@ public void dropColumn(ConnectorSession session, ConnectorTableHandle tableHandl
}
}

private static boolean isVoidTransform(PartitionField field)
{
return field.transform().equals(Transforms.alwaysNull());
}

@Override
public void renameColumn(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle source, String target)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,17 @@ public void testSchemaEvolution()
dropTable("test_schema_evolution_drop_middle");
}

@Test
public void testDropPartitionColumn()
{
String tableName = "test_drop_partition_column_" + randomNameSuffix();
assertUpdate("CREATE TABLE " + tableName + " (id INTEGER, name VARCHAR, age INTEGER) WITH (partitioning = ARRAY['id', 'truncate(name, 5)', 'void(age)'])");
assertQueryFails("ALTER TABLE " + tableName + " DROP COLUMN id", "Cannot drop partition field: id");
assertQueryFails("ALTER TABLE " + tableName + " DROP COLUMN name", "Cannot drop partition field: name");
assertUpdate("ALTER TABLE " + tableName + " DROP COLUMN age");
dropTable(tableName);
}

@Test
public void testShowStatsAfterAddColumn()
{
Expand Down

0 comments on commit 46ee6c9

Please sign in to comment.