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


##########
core/src/main/java/org/apache/iceberg/BaseRemoveUnusedSpecs.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.apache.iceberg.TableProperties.COMMIT_MAX_RETRY_WAIT_MS;
+import static 
org.apache.iceberg.TableProperties.COMMIT_MAX_RETRY_WAIT_MS_DEFAULT;
+import static org.apache.iceberg.TableProperties.COMMIT_MIN_RETRY_WAIT_MS;
+import static 
org.apache.iceberg.TableProperties.COMMIT_MIN_RETRY_WAIT_MS_DEFAULT;
+import static org.apache.iceberg.TableProperties.COMMIT_NUM_RETRIES;
+import static org.apache.iceberg.TableProperties.COMMIT_NUM_RETRIES_DEFAULT;
+import static org.apache.iceberg.TableProperties.COMMIT_TOTAL_RETRY_TIME_MS;
+import static 
org.apache.iceberg.TableProperties.COMMIT_TOTAL_RETRY_TIME_MS_DEFAULT;
+
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.util.Tasks;
+
+public class BaseRemoveUnusedSpecs implements RemoveUnusedSpecs {

Review Comment:
   fixed.



##########
core/src/main/java/org/apache/iceberg/TableMetadata.java:
##########
@@ -1102,6 +1121,22 @@ public Builder setDefaultPartitionSpec(int specId) {
       return this;
     }
 
+    private Builder removePartitionSpec(PartitionSpec spec) {
+      Preconditions.checkArgument(

Review Comment:
   Addressed.



##########
core/src/main/java/org/apache/iceberg/TableMetadata.java:
##########
@@ -1102,6 +1121,22 @@ public Builder setDefaultPartitionSpec(int specId) {
       return this;
     }
 
+    private Builder removePartitionSpec(PartitionSpec spec) {
+      Preconditions.checkArgument(
+          changes.isEmpty(), "Cannot remove partition spec with other metadata 
update");
+      Preconditions.checkArgument(
+          spec.specId() != defaultSpecId, "Cannot remove default partition 
spec");
+      PartitionSpec toBeRemoved = specsById.remove(spec.specId());

Review Comment:
   I added a new test to demonstrate how RemoveUnusedSpec will affect 
unpartitioned spec. 
   
   I think it works as expected, so there's no need to special handle default 
unpartitioned spec.



##########
core/src/main/java/org/apache/iceberg/TableMetadata.java:
##########
@@ -597,6 +597,24 @@ public TableMetadata replaceProperties(Map<String, String> 
rawProperties) {
         .build();
   }
 
+  /**
+   * Prune the unused partition specs from the table metadata.
+   *
+   * <p>Note: it's not safe for external client to call this directly, it's 
usually called by the

Review Comment:
   I'd like to highlight this note to reduce potential misusage as it's 
possible for users to put their own code in `org.apache.iceberg` package to 
bypass Java's access control.



##########
core/src/test/java/org/apache/iceberg/TestRemoveUnusedSpecs.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.relocated.com.google.common.collect.ImmutableSet;
+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

Review Comment:
   nice catch, fixed in bc09991b718809cc73447917e0e3580fb0ed151c



##########
core/src/main/java/org/apache/iceberg/BaseMetadataTable.java:
##########
@@ -192,6 +192,11 @@ public List<StatisticsFile> statisticsFiles() {
     return ImmutableList.of();
   }
 
+  @Override

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