mattfaltyn commented on code in PR #1988:
URL: https://github.com/apache/iceberg-go/pull/1988#discussion_r3972675333


##########
table/metadata.go:
##########
@@ -2053,24 +2058,27 @@ func assignMissingPartitionFieldIDsFromMetadata(b 
[]byte, metadata map[string]js
                        return nil, err
                }
                field["field-id"] = rawFieldID
+               normalizedLastPartitionID = max(normalizedLastPartitionID, 
lastAssignedID)
        }
 
-       if usesSpecList {
-               rawSpecs, err := json.Marshal(specs)
-               if err != nil {
-                       return nil, err
-               }
-               metadata["partition-specs"] = rawSpecs
-       } else {
-               rawFields, err := json.Marshal(specs[0].Fields)
-               if err != nil {
-                       return nil, err
+       if len(missingFields) > 0 {
+               if usesSpecList {
+                       rawSpecs, err := json.Marshal(specs)
+                       if err != nil {
+                               return nil, err
+                       }
+                       metadata["partition-specs"] = rawSpecs
+               } else {
+                       rawFields, err := json.Marshal(specs[0].Fields)
+                       if err != nil {
+                               return nil, err
+                       }
+                       metadata["partition-spec"] = rawFields
                }
-               metadata["partition-spec"] = rawFields
        }
 
        if lastPartitionIDSet {
-               rawLastPartitionID, err := json.Marshal(lastAssignedID)
+               rawLastPartitionID, err := 
json.Marshal(normalizedLastPartitionID)

Review Comment:
   > move the max-field-id scan to the write side
   
   Thanks—implemented in c9bae41 with one shared history-aware allocation floor.



##########
table/metadata_preflight_test.go:
##########
@@ -85,6 +86,88 @@ func TestParseMetadataBytesAssignsMissingPartitionFieldIDs(t 
*testing.T) {
        }
 }
 
+func TestParseMetadataBytesNormalizesStaleLastPartitionID(t *testing.T) {
+       data := strings.Replace(ExampleTableMetadataV2,
+               `"last-partition-id": 1000`, `"last-partition-id": 999`, 1)
+
+       parsed, err := ParseMetadataBytes([]byte(data))
+       require.NoError(t, err)
+       require.NotNil(t, parsed.LastPartitionSpecID())
+       assert.Equal(t, 1000, *parsed.LastPartitionSpecID())
+
+       update := NewUpdateSpec(New(nil, parsed, "", nil, 
nil).NewTransaction(), false).
+               AddField("x", iceberg.BucketTransform{NumBuckets: 16}, 
"x_bucket")
+       _, _, err = update.BuildUpdates()

Review Comment:
   > assert that update requirement equals 999
   
   Great catch—covered, and the new field still receives 1001.



##########
table/metadata.go:
##########
@@ -2053,24 +2058,27 @@ func assignMissingPartitionFieldIDsFromMetadata(b 
[]byte, metadata map[string]js
                        return nil, err
                }
                field["field-id"] = rawFieldID
+               normalizedLastPartitionID = max(normalizedLastPartitionID, 
lastAssignedID)

Review Comment:
   > this max is always lastAssignedID
   
   Thanks—removed along with the parser-side normalization path.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to