This is an automated email from the ASF dual-hosted git repository.

russellspitzer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/main by this push:
     new d9aacd24cc Core, API: UpdatePartitionSpec: Added ability to create a 
new Partition Spec but not set it as the Default
d9aacd24cc is described below

commit d9aacd24cc9d730d6416a93d31dd5cde8cbd260a
Author: Shani Elharrar <[email protected]>
AuthorDate: Mon Aug 5 05:45:46 2024 +0300

    Core, API: UpdatePartitionSpec: Added ability to create a new Partition 
Spec but not set it as the Default
---
 .../java/org/apache/iceberg/UpdatePartitionSpec.java  | 11 +++++++++++
 .../org/apache/iceberg/BaseUpdatePartitionSpec.java   | 16 +++++++++++++++-
 .../main/java/org/apache/iceberg/TableMetadata.java   |  4 ++++
 .../apache/iceberg/TestTableUpdatePartitionSpec.java  | 19 +++++++++++++++++++
 4 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/api/src/main/java/org/apache/iceberg/UpdatePartitionSpec.java 
b/api/src/main/java/org/apache/iceberg/UpdatePartitionSpec.java
index f48d590af1..eeb596d42d 100644
--- a/api/src/main/java/org/apache/iceberg/UpdatePartitionSpec.java
+++ b/api/src/main/java/org/apache/iceberg/UpdatePartitionSpec.java
@@ -122,4 +122,15 @@ public interface UpdatePartitionSpec extends 
PendingUpdate<PartitionSpec> {
    *     change conflicts with other additions, removals, or renames.
    */
   UpdatePartitionSpec renameField(String name, String newName);
+
+  /**
+   * Sets that the new partition spec will be NOT set as the default partition 
spec for the table,
+   * the default behavior is to do so.
+   *
+   * @return this for method chaining
+   */
+  default UpdatePartitionSpec addNonDefaultSpec() {
+    throw new UnsupportedOperationException(
+        this.getClass().getName() + " doesn't implement addNonDefaultSpec()");
+  };
 }
diff --git a/core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java 
b/core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java
index 2e1c919917..c69f6f3844 100644
--- a/core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java
+++ b/core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java
@@ -59,11 +59,13 @@ class BaseUpdatePartitionSpec implements 
UpdatePartitionSpec {
   private final Map<String, String> renames = Maps.newHashMap();
 
   private boolean caseSensitive;
+  private boolean setAsDefault;
   private int lastAssignedPartitionId;
 
   BaseUpdatePartitionSpec(TableOperations ops) {
     this.ops = ops;
     this.caseSensitive = true;
+    this.setAsDefault = true;
     this.base = ops.current();
     this.formatVersion = base.formatVersion();
     this.spec = base.spec();
@@ -95,6 +97,7 @@ class BaseUpdatePartitionSpec implements UpdatePartitionSpec {
     this.base = null;
     this.formatVersion = formatVersion;
     this.caseSensitive = true;
+    this.setAsDefault = true;
     this.spec = spec;
     this.schema = spec.schema();
     this.nameToField = indexSpecByName(spec);
@@ -146,6 +149,12 @@ class BaseUpdatePartitionSpec implements 
UpdatePartitionSpec {
     return this;
   }
 
+  @Override
+  public UpdatePartitionSpec addNonDefaultSpec() {
+    this.setAsDefault = false;
+    return this;
+  }
+
   @Override
   public BaseUpdatePartitionSpec addField(String sourceName) {
     return addField(Expressions.ref(sourceName));
@@ -327,7 +336,12 @@ class BaseUpdatePartitionSpec implements 
UpdatePartitionSpec {
 
   @Override
   public void commit() {
-    TableMetadata update = base.updatePartitionSpec(apply());
+    TableMetadata update;
+    if (setAsDefault) {
+      update = base.updatePartitionSpec(apply());
+    } else {
+      update = base.addPartitionSpec(apply());
+    }
     ops.commit(base, update);
   }
 
diff --git a/core/src/main/java/org/apache/iceberg/TableMetadata.java 
b/core/src/main/java/org/apache/iceberg/TableMetadata.java
index bd1c8a1a03..923db6bbd6 100644
--- a/core/src/main/java/org/apache/iceberg/TableMetadata.java
+++ b/core/src/main/java/org/apache/iceberg/TableMetadata.java
@@ -564,6 +564,10 @@ public class TableMetadata implements Serializable {
     return new Builder(this).setDefaultPartitionSpec(newPartitionSpec).build();
   }
 
+  public TableMetadata addPartitionSpec(PartitionSpec newPartitionSpec) {
+    return new Builder(this).addPartitionSpec(newPartitionSpec).build();
+  }
+
   public TableMetadata replaceSortOrder(SortOrder newOrder) {
     return new Builder(this).setDefaultSortOrder(newOrder).build();
   }
diff --git 
a/core/src/test/java/org/apache/iceberg/TestTableUpdatePartitionSpec.java 
b/core/src/test/java/org/apache/iceberg/TestTableUpdatePartitionSpec.java
index 482514c400..f327ef7529 100644
--- a/core/src/test/java/org/apache/iceberg/TestTableUpdatePartitionSpec.java
+++ b/core/src/test/java/org/apache/iceberg/TestTableUpdatePartitionSpec.java
@@ -287,4 +287,23 @@ public class TestTableUpdatePartitionSpec extends TestBase 
{
     assertThat(table.spec().lastAssignedFieldId()).isEqualTo(1001);
     
assertThat(table.ops().current().lastAssignedPartitionId()).isEqualTo(1001);
   }
+
+  @TestTemplate
+  public void testCommitUpdatedSpecWithoutSettingNewDefault() {
+    PartitionSpec originalSpec = table.spec();
+    table.updateSpec().addField("id").addNonDefaultSpec().commit();
+
+    assertThat(table.spec())
+        .as("Should not set the default spec for the table")
+        .isSameAs(originalSpec);
+
+    assertThat(table.specs().get(1))
+        .as("The new spec created for the table")
+        .isEqualTo(
+            PartitionSpec.builderFor(table.schema())
+                .withSpecId(1)
+                .bucket("data", 16)
+                .identity("id")
+                .build());
+  }
 }

Reply via email to