advancedxy commented on code in PR #10755:
URL: https://github.com/apache/iceberg/pull/10755#discussion_r1696945706


##########
core/src/test/java/org/apache/iceberg/TestRemoveUnusedSpecs.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.types.Types;
+import org.junit.jupiter.api.TestTemplate;
+
+public class TestRemoveUnusedSpecs extends TestBase {
+
+  @TestTemplate
+  public void testRemoveAllButCurrent() {
+    table
+        .updateSchema()
+        .addColumn("ts", Types.TimestampType.withoutZone())
+        .addColumn("category", Types.StringType.get())
+        .commit();
+    table.updateSpec().addField("id").commit();
+    table.updateSpec().addField("ts").commit();
+    table.updateSpec().addField("category").commit();
+    table.updateSpec().addField("data").commit();
+    assertThat(table.specs().size()).as("Added specs should be 
present").isEqualTo(5);
+
+    PartitionSpec currentSpec = table.spec();
+    table.removeUnusedSpecs().commit();
+
+    assertThat(table.specs().size()).as("All but current spec should be 
removed").isEqualTo(1);
+    assertThat(table.spec()).as("Current spec shall not 
change").isEqualTo(currentSpec);
+  }
+
+  @TestTemplate
+  public void testDontRemoveInUseSpecs() {
+    table
+        .updateSchema()
+        .addColumn("ts", Types.LongType.get())
+        .addColumn("category", Types.StringType.get())
+        .commit();
+
+    table.updateSpec().addField("id").commit(); // 1
+    table.newAppend().appendFile(newDataFile("data_bucket=0/id=5")).commit();
+
+    table.updateSpec().addField("ts").commit(); // 2
+
+    table.updateSpec().addField("category").commit(); // 3
+    if (formatVersion == 1) {
+      
table.newAppend().appendFile(newDataFile("data_bucket=0/id=5/ts=100/category=fo")).commit();
+    } else {
+      table
+          .newRowDelta()
+          .addDeletes(newDeleteFile(table.spec().specId(), 
"data_bucket=0/id=5/ts=100/category=fo"))
+          .commit();
+    }
+
+    table.updateSpec().addField("data").commit(); // 4
+    assertThat(table.specs()).size().as("Added specs should be 
present").isEqualTo(5);
+
+    PartitionSpec currentSpec = table.spec();
+    table.removeUnusedSpecs().commit();
+    assertThat(table.specs().keySet()).as("Unused specs are 
removed").containsExactly(1, 3, 4);
+    assertThat(table.spec()).as("Current spec shall not 
change").isEqualTo(currentSpec);
+  }
+
+  @TestTemplate
+  public void testRemoveUnpartitionedSpec() {
+    // clean it first to reset to un-partitioned
+    cleanupTables();
+    this.table = create(SCHEMA, PartitionSpec.unpartitioned());
+    DataFile file =
+        DataFiles.builder(table.spec())
+            .withPath("/path/to/data-0.parquet")
+            .withFileSizeInBytes(10)
+            .withRecordCount(100)
+            .build();
+    table.newAppend().appendFile(file).commit();
+    // add a bucket partition
+    table.updateSpec().addField("data_bucket", Expressions.bucket("data", 
16)).commit();
+
+    // removeUnusedPartitionSpec shall not remove the un-partitioned spec
+    table.removeUnusedSpecs().commit();
+    assertThat(table.specs().keySet())
+        .as("Un-partitioned spec is still used")
+        .containsExactly(0, 1);
+
+    table.newDelete().deleteFile(file).commit();
+    DataFile bucketFile =
+        DataFiles.builder(table.spec())
+            .withPath("/path/to/data-0.parquet")
+            .withFileSizeInBytes(10)
+            .withRecordCount(100)
+            .withPartitionPath("data_bucket=0")
+            .build();
+    table.newAppend().appendFile(bucketFile).commit();
+
+    
table.expireSnapshots().expireOlderThan(System.currentTimeMillis()).commit();
+    // un-partitioned spec can be removed.
+    table.removeUnusedSpecs().commit();
+    assertThat(table.specs().keySet()).as("Un-partitioned spec is 
removed").containsExactly(1);
+
+    // bucket spec can reset to un-partitioned
+    table.updateSpec().removeField("data_bucket").commit();
+    assertThat(table.spec().isUnpartitioned()).as("Should equal to 
un-partitioned").isTrue();
+    if (formatVersion == 1) {
+      assertThat(table.spec().fields().size()).as("Should have one void 
transform").isEqualTo(1);
+      assertThat(table.spec().specId())
+          .as("un-partitioned is evolved to use a new SpecId")
+          .isEqualTo(2);
+    } else {
+      assertThat(table.spec().fields().size()).as("Should have no 
fields").isEqualTo(0);
+      assertThat(table.spec().specId())
+          .as("un-partitioned is evolved to use a new SpecId")
+          .isEqualTo(2);
+    }

Review Comment:
   Fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to