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


##########
internal/provider/table_schema_test.go:
##########
@@ -186,6 +186,327 @@ func TestValidateSchemaEvolution(t *testing.T) {
        }
 }
 
+func TestValidateFieldIDParents(t *testing.T) {
+       child := iceberg.NestedField{ID: 3, Name: "child", Type: 
iceberg.PrimitiveTypes.Int64, Required: false}
+
+       twoStructs := func(leftFields, rightFields []iceberg.NestedField) 
*iceberg.Schema {
+               return iceberg.NewSchema(0,
+                       iceberg.NestedField{ID: 1, Name: "left", Type: 
&iceberg.StructType{FieldList: leftFields}, Required: false},
+                       iceberg.NestedField{ID: 2, Name: "right", Type: 
&iceberg.StructType{FieldList: rightFields}, Required: false},
+               )
+       }
+
+       twoLists := func(leftElemID, rightElemID int) *iceberg.Schema {
+               return iceberg.NewSchema(0,
+                       iceberg.NestedField{ID: 1, Name: "left", Type: 
&iceberg.ListType{
+                               ElementID: leftElemID, Element: 
iceberg.PrimitiveTypes.Int64, ElementRequired: false,
+                       }, Required: false},
+                       iceberg.NestedField{ID: 2, Name: "right", Type: 
&iceberg.ListType{
+                               ElementID: rightElemID, Element: 
iceberg.PrimitiveTypes.String, ElementRequired: false,
+                       }, Required: false},
+               )
+       }
+
+       twoMaps := func(leftKeyID, leftValueID, rightKeyID, rightValueID int) 
*iceberg.Schema {
+               return iceberg.NewSchema(0,
+                       iceberg.NestedField{ID: 1, Name: "left", Type: 
&iceberg.MapType{
+                               KeyID: leftKeyID, KeyType: 
iceberg.PrimitiveTypes.String,
+                               ValueID: leftValueID, ValueType: 
iceberg.PrimitiveTypes.Int64, ValueRequired: false,
+                       }, Required: false},
+                       iceberg.NestedField{ID: 2, Name: "right", Type: 
&iceberg.MapType{
+                               KeyID: rightKeyID, KeyType: 
iceberg.PrimitiveTypes.String,
+                               ValueID: rightValueID, ValueType: 
iceberg.PrimitiveTypes.Int32, ValueRequired: false,
+                       }, Required: false},
+               )
+       }
+
+       // list<map<string, struct<item: long>>> under left and right branches.
+       deepListMapStruct := func(leftItemID, rightItemID int) *iceberg.Schema {
+               branch := func(listID, elemID, keyID, valueID, itemID int, name 
string) iceberg.NestedField {
+                       return iceberg.NestedField{ID: listID, Name: name, 
Type: &iceberg.ListType{
+                               ElementID: elemID,
+                               Element: &iceberg.MapType{
+                                       KeyID: keyID, KeyType: 
iceberg.PrimitiveTypes.String,
+                                       ValueID: valueID,
+                                       ValueType: 
&iceberg.StructType{FieldList: []iceberg.NestedField{
+                                               {ID: itemID, Name: "item", 
Type: iceberg.PrimitiveTypes.Int64, Required: false},
+                                       }},
+                                       ValueRequired: false,
+                               },
+                               ElementRequired: false,
+                       }, Required: false}
+               }
+
+               return iceberg.NewSchema(0,
+                       branch(1, 3, 4, 5, leftItemID, "left"),
+                       branch(2, 7, 8, 9, rightItemID, "right"),
+               )
+       }
+
+       tests := []struct {
+               name    string
+               current *iceberg.Schema
+               planned *iceberg.Schema
+               wantErr string
+       }{
+               {
+                       name:    "struct child stays under same parent",
+                       current: twoStructs([]iceberg.NestedField{child}, nil),
+                       planned: twoStructs([]iceberg.NestedField{child}, nil),
+               },
+               {
+                       name:    "struct child renamed under same parent",
+                       current: twoStructs([]iceberg.NestedField{child}, nil),
+                       planned: twoStructs([]iceberg.NestedField{{ID: 3, Name: 
"renamed", Type: iceberg.PrimitiveTypes.Int64, Required: false}}, nil),
+               },
+               {
+                       name:    "struct child dropped and new id added under 
other parent",
+                       current: twoStructs([]iceberg.NestedField{child}, nil),
+                       planned: twoStructs(nil, []iceberg.NestedField{{ID: 4, 
Name: "child", Type: iceberg.PrimitiveTypes.Int64, Required: false}}),
+               },
+               {
+                       name:    "struct child moved between parents",
+                       current: twoStructs([]iceberg.NestedField{child}, nil),
+                       planned: twoStructs(nil, []iceberg.NestedField{child}),
+                       wantErr: `field "child" (id 3): cannot move from parent 
id 1 ("left") to parent id 2 ("right")`,
+               },
+               {
+                       name:    "list element moved between parents",
+                       current: twoLists(3, 4),
+                       planned: twoLists(5, 3),
+                       wantErr: `field "element" (id 3): cannot move from 
parent id 1 ("left") to parent id 2 ("right")`,
+               },
+               {
+                       name:    "map key moved between parents",
+                       current: twoMaps(3, 4, 5, 6),
+                       planned: twoMaps(7, 4, 3, 6),
+                       wantErr: `field "key" (id 3): cannot move from parent 
id 1 ("left") to parent id 2 ("right")`,
+               },
+               {
+                       name:    "map value moved between parents",
+                       current: twoMaps(3, 4, 5, 6),
+                       planned: twoMaps(3, 7, 5, 4),
+                       wantErr: `field "value" (id 4): cannot move from parent 
id 1 ("left") to parent id 2 ("right")`,
+               },
+               {
+                       name:    "map key and value stay under same map parent",
+                       current: twoMaps(3, 4, 5, 6),
+                       planned: twoMaps(3, 4, 5, 6),
+               },
+               {
+                       name: "nested fields reordered under same map-value 
struct parent",
+                       current: iceberg.NewSchema(0,
+                               iceberg.NestedField{ID: 1, Name: "m", Type: 
&iceberg.MapType{
+                                       KeyID: 2, KeyType: 
iceberg.PrimitiveTypes.String,
+                                       ValueID: 3, ValueType: 
&iceberg.StructType{FieldList: []iceberg.NestedField{
+                                               {ID: 4, Name: "a", Type: 
iceberg.PrimitiveTypes.Int64, Required: false},
+                                               {ID: 5, Name: "b", Type: 
iceberg.PrimitiveTypes.String, Required: false},
+                                       }}, ValueRequired: false,
+                               }, Required: false},
+                       ),
+                       planned: iceberg.NewSchema(0,
+                               iceberg.NestedField{ID: 1, Name: "m", Type: 
&iceberg.MapType{
+                                       KeyID: 2, KeyType: 
iceberg.PrimitiveTypes.String,
+                                       ValueID: 3, ValueType: 
&iceberg.StructType{FieldList: []iceberg.NestedField{
+                                               {ID: 5, Name: "b", Type: 
iceberg.PrimitiveTypes.String, Required: false},
+                                               {ID: 4, Name: "a", Type: 
iceberg.PrimitiveTypes.Int64, Required: false},
+                                       }}, ValueRequired: false,
+                               }, Required: false},
+                       ),
+               },
+               {
+                       name: "root field moved into a struct",
+                       current: iceberg.NewSchema(0,
+                               iceberg.NestedField{ID: 1, Name: "wrapper", 
Type: &iceberg.StructType{}, Required: false},
+                               iceberg.NestedField{ID: 3, Name: "data", Type: 
iceberg.PrimitiveTypes.String, Required: false},
+                       ),
+                       planned: iceberg.NewSchema(0,
+                               iceberg.NestedField{ID: 1, Name: "wrapper", 
Type: &iceberg.StructType{FieldList: []iceberg.NestedField{
+                                       {ID: 3, Name: "data", Type: 
iceberg.PrimitiveTypes.String, Required: false},
+                               }}, Required: false},
+                       ),
+                       wantErr: `field "data" (id 3): cannot move from parent 
table root to parent id 1 ("wrapper")`,
+               },
+               {
+                       name: "nested field moved to table root",
+                       current: iceberg.NewSchema(0,
+                               iceberg.NestedField{ID: 1, Name: "wrapper", 
Type: &iceberg.StructType{FieldList: []iceberg.NestedField{
+                                       {ID: 3, Name: "data", Type: 
iceberg.PrimitiveTypes.String, Required: false},
+                               }}, Required: false},
+                       ),
+                       planned: iceberg.NewSchema(0,
+                               iceberg.NestedField{ID: 1, Name: "wrapper", 
Type: &iceberg.StructType{}, Required: false},
+                               iceberg.NestedField{ID: 3, Name: "data", Type: 
iceberg.PrimitiveTypes.String, Required: false},
+                       ),
+                       wantErr: `field "data" (id 3): cannot move from parent 
id 1 ("wrapper") to parent table root`,
+               },
+               {
+                       name: "rename swap of parents that also moves children",
+                       // Swapping a/b by renaming and reparenting yields the 
same parent
+                       // name on both sides; the message must still show 
distinct ids.
+                       current: iceberg.NewSchema(0,
+                               iceberg.NestedField{ID: 1, Name: "a", Type: 
&iceberg.StructType{FieldList: []iceberg.NestedField{
+                                       {ID: 3, Name: "x", Type: 
iceberg.PrimitiveTypes.Int64, Required: false},
+                               }}, Required: false},
+                               iceberg.NestedField{ID: 2, Name: "b", Type: 
&iceberg.StructType{FieldList: []iceberg.NestedField{
+                                       {ID: 4, Name: "x", Type: 
iceberg.PrimitiveTypes.Int64, Required: false},
+                               }}, Required: false},
+                       ),
+                       planned: iceberg.NewSchema(0,
+                               iceberg.NestedField{ID: 2, Name: "a", Type: 
&iceberg.StructType{FieldList: []iceberg.NestedField{
+                                       {ID: 3, Name: "x", Type: 
iceberg.PrimitiveTypes.Int64, Required: false},
+                               }}, Required: false},
+                               iceberg.NestedField{ID: 1, Name: "b", Type: 
&iceberg.StructType{FieldList: []iceberg.NestedField{
+                                       {ID: 4, Name: "x", Type: 
iceberg.PrimitiveTypes.Int64, Required: false},
+                               }}, Required: false},
+                       ),
+                       wantErr: `field "x" (id 3): cannot move from parent id 
1 ("a") to parent id 2 ("a")`,
+               },
+               {
+                       name: "deep nested list-of-map-of-struct child stays",

Review Comment:
   gofmt (fails the Go CI jobs):
   ```suggestion
                        name:    "deep nested list-of-map-of-struct child 
stays",
   ```



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