ShivamSharma43 commented on code in PR #2630:
URL: https://github.com/apache/iceberg-go/pull/2630#discussion_r4657806440


##########
table/update_schema.go:
##########
@@ -297,30 +297,42 @@ func (u *UpdateSchema) findParentID(fieldID int) int {
        return parentID
 }
 
-// checkNameAvailable returns an error if the struct with the given parent ID
-// already has a column called name, or will have one after this update. A
-// column that is deleted, or renamed to another name, in this update gives up
-// its name, so a new column may take it.
-func (u *UpdateSchema) checkNameAvailable(parentID int, name, fullName string) 
error {
+// nameTaken reports whether the struct with the given parent ID already has a
+// column called name, or will have one after this update. selfID is the column
+// being renamed, which may keep its own name with different case, or
+// TableRootID when adding a column. A column that is deleted, or renamed to
+// another name, in this update gives up its name, so another column may take
+// it. Java rejects the renamed-away case; we allow it because the resulting
+// schema is valid.

Review Comment:
   Yes, reworded in a13db0f to name Java's `addColumn`.



##########
table/update_schema_test.go:
##########
@@ -465,6 +476,101 @@ func TestAddColumnNameAlreadyExists(t *testing.T) {
        }
 }
 
+func TestRenameColumnNameAlreadyExists(t *testing.T) {
+       str := iceberg.PrimitiveTypes.String
+
+       rejected := []struct {
+               name          string
+               caseSensitive bool
+               build         func(*UpdateSchema) *UpdateSchema
+               wantErr       string
+       }{
+               {
+                       name:          "existing column",
+                       caseSensitive: true,
+                       build: func(u *UpdateSchema) *UpdateSchema {
+                               return u.RenameColumn([]string{"name"}, "age")
+                       },
+                       wantErr: "cannot rename column, name already exists: 
age",
+               },
+               {
+                       name:          "existing column with different case in 
case-insensitive mode",
+                       caseSensitive: false,
+                       build: func(u *UpdateSchema) *UpdateSchema {
+                               return u.RenameColumn([]string{"name"}, "AGE")
+                       },
+                       wantErr: "cannot rename column, name already exists: 
AGE",
+               },
+               {
+                       name:          "added column with different case in 
case-insensitive mode",
+                       caseSensitive: false,
+                       build: func(u *UpdateSchema) *UpdateSchema {
+                               return u.AddColumn([]string{"x"}, str, "", 
false, nil).
+                                       RenameColumn([]string{"age"}, "X")
+                       },
+                       wantErr: "cannot rename column, name already exists: X",
+               },
+               {
+                       name:          "renamed column with different case in 
case-insensitive mode",
+                       caseSensitive: false,
+                       build: func(u *UpdateSchema) *UpdateSchema {
+                               return u.RenameColumn([]string{"name"}, "y").
+                                       RenameColumn([]string{"age"}, "Y")
+                       },
+                       wantErr: "cannot rename column, name already exists: Y",
+               },
+               {
+                       name:          "nested added column with different case 
in case-insensitive mode",
+                       caseSensitive: false,
+                       build: func(u *UpdateSchema) *UpdateSchema {
+                               return u.AddColumn([]string{"address", "town"}, 
str, "", false, nil).
+                                       RenameColumn([]string{"address", 
"city"}, "Town")
+                       },
+                       wantErr: "cannot rename column, name already exists: 
address.Town",
+               },
+       }
+
+       for _, tt := range rejected {
+               t.Run(tt.name, func(t *testing.T) {
+                       txn := New([]string{"id"}, testMetadata, "", nil, 
nil).NewTransaction()
+
+                       _, err := tt.build(NewUpdateSchema(txn, 
tt.caseSensitive, false)).Apply()
+                       require.ErrorIs(t, err, iceberg.ErrInvalidSchema)
+                       require.ErrorContains(t, err, tt.wantErr)
+
+                       err = tt.build(NewUpdateSchema(txn, tt.caseSensitive, 
false)).Commit()
+                       require.ErrorIs(t, err, iceberg.ErrInvalidSchema)
+                       require.ErrorContains(t, err, tt.wantErr)
+               })
+       }
+
+       t.Run("same column with different case in case-insensitive mode", 
func(t *testing.T) {
+               txn := New([]string{"id"}, testMetadata, "", nil, 
nil).NewTransaction()
+
+               s, err := NewUpdateSchema(txn, false, 
false).RenameColumn([]string{"name"}, "Name").Apply()

Review Comment:
   Done in a13db0f: the allowed cases run through both `Apply()` and 
`Commit()`, and the column IDs are checked on the applied and the committed 
schema (`name` is ID 3).



##########
table/update_schema_test.go:
##########
@@ -465,6 +476,101 @@ func TestAddColumnNameAlreadyExists(t *testing.T) {
        }
 }
 
+func TestRenameColumnNameAlreadyExists(t *testing.T) {
+       str := iceberg.PrimitiveTypes.String
+
+       rejected := []struct {
+               name          string
+               caseSensitive bool
+               build         func(*UpdateSchema) *UpdateSchema
+               wantErr       string
+       }{
+               {
+                       name:          "existing column",
+                       caseSensitive: true,
+                       build: func(u *UpdateSchema) *UpdateSchema {
+                               return u.RenameColumn([]string{"name"}, "age")
+                       },
+                       wantErr: "cannot rename column, name already exists: 
age",
+               },
+               {
+                       name:          "existing column with different case in 
case-insensitive mode",
+                       caseSensitive: false,
+                       build: func(u *UpdateSchema) *UpdateSchema {
+                               return u.RenameColumn([]string{"name"}, "AGE")
+                       },
+                       wantErr: "cannot rename column, name already exists: 
AGE",
+               },
+               {
+                       name:          "added column with different case in 
case-insensitive mode",
+                       caseSensitive: false,
+                       build: func(u *UpdateSchema) *UpdateSchema {
+                               return u.AddColumn([]string{"x"}, str, "", 
false, nil).
+                                       RenameColumn([]string{"age"}, "X")
+                       },
+                       wantErr: "cannot rename column, name already exists: X",
+               },
+               {
+                       name:          "renamed column with different case in 
case-insensitive mode",
+                       caseSensitive: false,
+                       build: func(u *UpdateSchema) *UpdateSchema {
+                               return u.RenameColumn([]string{"name"}, "y").
+                                       RenameColumn([]string{"age"}, "Y")
+                       },
+                       wantErr: "cannot rename column, name already exists: Y",
+               },
+               {
+                       name:          "nested added column with different case 
in case-insensitive mode",
+                       caseSensitive: false,
+                       build: func(u *UpdateSchema) *UpdateSchema {
+                               return u.AddColumn([]string{"address", "town"}, 
str, "", false, nil).
+                                       RenameColumn([]string{"address", 
"city"}, "Town")
+                       },
+                       wantErr: "cannot rename column, name already exists: 
address.Town",
+               },
+       }
+
+       for _, tt := range rejected {
+               t.Run(tt.name, func(t *testing.T) {
+                       txn := New([]string{"id"}, testMetadata, "", nil, 
nil).NewTransaction()
+
+                       _, err := tt.build(NewUpdateSchema(txn, 
tt.caseSensitive, false)).Apply()
+                       require.ErrorIs(t, err, iceberg.ErrInvalidSchema)
+                       require.ErrorContains(t, err, tt.wantErr)
+
+                       err = tt.build(NewUpdateSchema(txn, tt.caseSensitive, 
false)).Commit()
+                       require.ErrorIs(t, err, iceberg.ErrInvalidSchema)
+                       require.ErrorContains(t, err, tt.wantErr)
+               })
+       }
+
+       t.Run("same column with different case in case-insensitive mode", 
func(t *testing.T) {
+               txn := New([]string{"id"}, testMetadata, "", nil, 
nil).NewTransaction()
+
+               s, err := NewUpdateSchema(txn, false, 
false).RenameColumn([]string{"name"}, "Name").Apply()
+               require.NoError(t, err)
+               f, ok := s.FindFieldByName("Name")
+               require.True(t, ok)
+               assert.Equal(t, 2, f.ID)
+       })
+
+       t.Run("name of a column renamed away", func(t *testing.T) {

Review Comment:
   Added both in a13db0f. Case-sensitive `name→Name` is allowed and keeps ID 2. 
The swap is rejected at its first rename, because `age` is still in use when 
`name→age` is requested.



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