zeroshade commented on code in PR #140:
URL: 
https://github.com/apache/terraform-provider-iceberg/pull/140#discussion_r4620249895


##########
internal/provider/table_schema.go:
##########
@@ -77,9 +87,63 @@ func (s *icebergTableSchema) UnmarshalJSON(b []byte) error {
        s.ID = types.Int64Value(raw.ID)
        s.Fields = raw.Fields
 
+       ids := raw.IdentifierFieldIDs
+       if len(ids) == 0 {
+               // Represent "no identifiers" as an empty list so 
create/read/import
+               // round-trip consistently (null means "omitted from config").
+               s.IdentifierFieldIDs = types.ListValueMust(types.Int64Type, 
[]attr.Value{})
+       } else {
+               elems := make([]attr.Value, len(ids))
+               for i, id := range ids {
+                       elems[i] = types.Int64Value(id)
+               }
+               s.IdentifierFieldIDs = types.ListValueMust(types.Int64Type, 
elems)
+       }
+
        return nil
 }
 
+// int64ListElements returns the int64 elements of a Terraform list, or nil 
when
+// the list is null/unknown (including an unset optional attribute).
+func int64ListElements(list types.List) ([]int64, error) {
+       if list.IsNull() || list.IsUnknown() {
+               return nil, nil
+       }
+       elems := list.Elements()
+       out := make([]int64, 0, len(elems))
+       for _, elem := range elems {
+               v, ok := elem.(types.Int64)
+               if !ok {
+                       return nil, fmt.Errorf("expected int64 list element, 
got %T", elem)
+               }
+               if v.IsNull() || v.IsUnknown() {
+                       continue
+               }
+               out = append(out, v.ValueInt64())
+       }
+
+       return out, nil
+}
+
+// preserveIdentifierFieldIDs copies identifier field IDs from the current 
table
+// schema when the plan omits them (null/unknown). An explicit empty list 
clears
+// identifiers; a non-empty plan list is left unchanged.
+func (s *icebergTableSchema) preserveIdentifierFieldIDs(current 
*iceberg.Schema) {

Review Comment:
   Nothing validates the identifier ids before commit. Per the spec each 
identifier field must exist in the schema, be required, be primitive, not be 
float/double, and not be nested under an optional struct/list/map. In 
particular this carries the table's current ids forward even when the plan 
drops that field or makes it optional, so the commit either fails with an 
opaque catalog error or a lenient catalog stores invalid metadata. Could we 
reject these with a clear diagnostic, alongside the existing `validateFieldIDs` 
check?



##########
internal/provider/resource_table.go:
##########
@@ -480,13 +484,14 @@ func (r *icebergTableResource) calculateSchemaUpdates(ctx 
context.Context, plan,
                return nil
        }
 
-       // Normalize by comparing the JSON of the fields list only.
-       // This ignores the top-level schema-id and any other schema-level 
metadata
-       // while ensuring every field change (name, type, id, etc.) is detected.
+       // Compare fields and identifier-field-ids. Ignore top-level schema-id.
        planFieldsJson, _ := json.Marshal(planIceberg.Fields())
        stateFieldsJson, _ := json.Marshal(stateIceberg.Fields())
+       planIdentJson, _ := json.Marshal(planIceberg.IdentifierFieldIDs)

Review Comment:
   Comparing marshalled JSON is order-sensitive, so `[1,2]` vs `[2,1]` would 
produce a new schema. Switching the attribute to a set (and comparing as a set) 
avoids that.



##########
internal/provider/table_schema.go:
##########
@@ -77,9 +87,63 @@ func (s *icebergTableSchema) UnmarshalJSON(b []byte) error {
        s.ID = types.Int64Value(raw.ID)
        s.Fields = raw.Fields
 
+       ids := raw.IdentifierFieldIDs
+       if len(ids) == 0 {
+               // Represent "no identifiers" as an empty list so 
create/read/import
+               // round-trip consistently (null means "omitted from config").
+               s.IdentifierFieldIDs = types.ListValueMust(types.Int64Type, 
[]attr.Value{})
+       } else {
+               elems := make([]attr.Value, len(ids))
+               for i, id := range ids {
+                       elems[i] = types.Int64Value(id)
+               }
+               s.IdentifierFieldIDs = types.ListValueMust(types.Int64Type, 
elems)
+       }
+
        return nil
 }
 
+// int64ListElements returns the int64 elements of a Terraform list, or nil 
when
+// the list is null/unknown (including an unset optional attribute).
+func int64ListElements(list types.List) ([]int64, error) {

Review Comment:
   Minor: silently skipping null/unknown elements could drop a user's 
identifier. At apply time these should be resolved, so an error seems safer 
than `continue`.



-- 
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