You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by fo...@apache.org on 2023/02/02 16:45:42 UTC

[iceberg] branch master updated: API: Fix Transform backward compatibility in PartitionSpec (#6653)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new efe7ea6fd0 API: Fix Transform backward compatibility in PartitionSpec (#6653)
efe7ea6fd0 is described below

commit efe7ea6fd0e5634de1df899af132b87429537b97
Author: Denys Kuzmenko <de...@gmail.com>
AuthorDate: Thu Feb 2 18:45:34 2023 +0200

    API: Fix Transform backward compatibility in PartitionSpec (#6653)
---
 .../apache/iceberg/BaseUpdatePartitionSpec.java    |  7 ++++
 .../iceberg/TestTableUpdatePartitionSpec.java      | 40 ++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java b/core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java
index 11c74a045e..02794da2e4 100644
--- a/core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java
+++ b/core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java
@@ -38,6 +38,7 @@ import org.apache.iceberg.transforms.PartitionSpecVisitor;
 import org.apache.iceberg.transforms.Transform;
 import org.apache.iceberg.transforms.Transforms;
 import org.apache.iceberg.transforms.UnknownTransform;
+import org.apache.iceberg.types.Type;
 import org.apache.iceberg.util.Pair;
 
 class BaseUpdatePartitionSpec implements UpdatePartitionSpec {
@@ -338,6 +339,12 @@ class BaseUpdatePartitionSpec implements UpdatePartitionSpec {
     int sourceId = boundTerm.ref().fieldId();
     Transform<?, ?> transform = toTransform(boundTerm);
 
+    Type fieldType = schema.findType(sourceId);
+    if (fieldType != null) {
+      transform = Transforms.fromString(fieldType, transform.toString());
+    } else {
+      transform = Transforms.fromString(transform.toString());
+    }
     return Pair.of(sourceId, transform);
   }
 
diff --git a/core/src/test/java/org/apache/iceberg/TestTableUpdatePartitionSpec.java b/core/src/test/java/org/apache/iceberg/TestTableUpdatePartitionSpec.java
index f770cd2792..f3bfdf669e 100644
--- a/core/src/test/java/org/apache/iceberg/TestTableUpdatePartitionSpec.java
+++ b/core/src/test/java/org/apache/iceberg/TestTableUpdatePartitionSpec.java
@@ -20,8 +20,10 @@ package org.apache.iceberg;
 
 import static org.apache.iceberg.expressions.Expressions.bucket;
 import static org.apache.iceberg.expressions.Expressions.truncate;
+import static org.apache.iceberg.expressions.Expressions.year;
 
 import org.apache.iceberg.transforms.Transforms;
+import org.apache.iceberg.types.Types;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -187,6 +189,44 @@ public class TestTableUpdatePartitionSpec extends TableTestBase {
     Assert.assertEquals(1001, table.spec().lastAssignedFieldId());
   }
 
+  @Test
+  public void testRemoveAndAddYearField() {
+    table.updateSchema().addColumn("year_field", Types.DateType.get()).commit();
+    table.updateSpec().addField(year("year_field")).commit();
+
+    PartitionSpec evolvedSpec =
+        PartitionSpec.builderFor(table.schema())
+            .withSpecId(1)
+            .bucket("data", 16)
+            .year("year_field")
+            .build();
+
+    Assert.assertEquals("should match evolved spec", evolvedSpec, table.spec());
+    Assert.assertEquals(1001, table.spec().lastAssignedFieldId());
+
+    table.updateSpec().removeField("year_field_year").addField(year("year_field")).commit();
+
+    V1Assert.assertEquals(
+        "Should soft delete id and data buckets",
+        PartitionSpec.builderFor(table.schema())
+            .withSpecId(1)
+            .bucket("data", 16)
+            .year("year_field")
+            .build(),
+        table.spec());
+
+    V2Assert.assertEquals(
+        "Should remove and then add a year field",
+        PartitionSpec.builderFor(table.schema())
+            .withSpecId(1)
+            .bucket("data", 16)
+            .add(3, 1001, "year_field_year", Transforms.year())
+            .build(),
+        table.spec());
+
+    Assert.assertEquals(1001, table.spec().lastAssignedFieldId());
+  }
+
   @Test
   public void testAddAndRemoveField() {
     table.updateSpec().addField(bucket("data", 6)).removeField("data_bucket").commit();