Skip to content

Commit 7d2ec5e

Browse files
committed
Added AlterTableTest using InMemoryCatalog.
1 parent 47c8172 commit 7d2ec5e

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright (c) 2025 Altinity Inc and/or its affiliates. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*/
10+
package com.altinity.ice.cli.internal.cmd;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
14+
15+
import java.util.Arrays;
16+
import java.util.List;
17+
import org.apache.iceberg.PartitionSpec;
18+
import org.apache.iceberg.Schema;
19+
import org.apache.iceberg.Table;
20+
import org.apache.iceberg.catalog.TableIdentifier;
21+
import org.apache.iceberg.inmemory.InMemoryCatalog;
22+
import org.apache.iceberg.types.Types;
23+
import org.testng.annotations.BeforeMethod;
24+
import org.testng.annotations.Test;
25+
26+
public class AlterTableTest {
27+
28+
private InMemoryCatalog catalog;
29+
private TableIdentifier tableId;
30+
private Schema schema;
31+
32+
@BeforeMethod
33+
public void setUp() {
34+
catalog = new InMemoryCatalog();
35+
catalog.initialize("test-catalog", java.util.Map.of());
36+
tableId = TableIdentifier.of("test", "table1");
37+
38+
// create namespace.
39+
catalog.createNamespace(org.apache.iceberg.catalog.Namespace.of("test"));
40+
schema =
41+
new Schema(
42+
Types.NestedField.required(1, "id", Types.LongType.get()),
43+
Types.NestedField.required(2, "name", Types.StringType.get()),
44+
Types.NestedField.required(3, "timestamp_col", Types.TimestampType.withZone()),
45+
Types.NestedField.required(4, "date_col", Types.DateType.get()));
46+
}
47+
48+
@Test
49+
public void testDropPartitionField() throws Exception {
50+
PartitionSpec partitionSpec =
51+
PartitionSpec.builderFor(schema).identity("name").year("timestamp_col").build();
52+
53+
Table table = catalog.buildTable(tableId, schema).withPartitionSpec(partitionSpec).create();
54+
55+
assertThat(table.spec().fields()).hasSize(2);
56+
assertThat(table.spec().fields().get(0).name()).isEqualTo("name");
57+
assertThat(table.spec().fields().get(1).name()).isEqualTo("timestamp_col_year");
58+
59+
List<AlterTable.Update> updates = Arrays.asList(new AlterTable.DropPartitionField("name"));
60+
61+
AlterTable.run(catalog, tableId, updates);
62+
63+
table = catalog.loadTable(tableId);
64+
assertThat(table.spec().fields()).hasSize(1);
65+
assertThat(table.spec().fields().get(0).name()).isEqualTo("timestamp_col_year");
66+
}
67+
68+
@Test
69+
public void testDropPartitionFieldByTransformName() throws Exception {
70+
PartitionSpec partitionSpec =
71+
PartitionSpec.builderFor(schema).identity("name").year("timestamp_col").build();
72+
73+
Table table = catalog.buildTable(tableId, schema).withPartitionSpec(partitionSpec).create();
74+
75+
assertThat(table.spec().fields()).hasSize(2);
76+
77+
List<AlterTable.Update> updates =
78+
Arrays.asList(new AlterTable.DropPartitionField("timestamp_col_year"));
79+
80+
AlterTable.run(catalog, tableId, updates);
81+
82+
table = catalog.loadTable(tableId);
83+
assertThat(table.spec().fields()).hasSize(1);
84+
assertThat(table.spec().fields().get(0).name()).isEqualTo("name");
85+
}
86+
87+
@Test
88+
public void testDropNonExistentPartitionField() throws Exception {
89+
PartitionSpec partitionSpec = PartitionSpec.builderFor(schema).identity("name").build();
90+
91+
catalog.buildTable(tableId, schema).withPartitionSpec(partitionSpec).create();
92+
93+
List<AlterTable.Update> updates =
94+
Arrays.asList(new AlterTable.DropPartitionField("non_existent_field"));
95+
96+
assertThatThrownBy(() -> AlterTable.run(catalog, tableId, updates))
97+
.isInstanceOf(IllegalArgumentException.class);
98+
}
99+
100+
@Test
101+
public void testDropAllPartitionFields() throws Exception {
102+
PartitionSpec partitionSpec =
103+
PartitionSpec.builderFor(schema).identity("name").year("timestamp_col").build();
104+
105+
Table table = catalog.buildTable(tableId, schema).withPartitionSpec(partitionSpec).create();
106+
107+
assertThat(table.spec().fields()).hasSize(2);
108+
109+
List<AlterTable.Update> updates =
110+
Arrays.asList(
111+
new AlterTable.DropPartitionField("name"),
112+
new AlterTable.DropPartitionField("timestamp_col_year"));
113+
114+
AlterTable.run(catalog, tableId, updates);
115+
116+
table = catalog.loadTable(tableId);
117+
assertThat(table.spec().fields()).isEmpty();
118+
}
119+
}

0 commit comments

Comments
 (0)