This is an automated email from the ASF dual-hosted git repository.
laskoviymishka pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-go.git
The following commit(s) were added to refs/heads/main by this push:
new c861b995b fix(table): reject case-only duplicate names when renaming a
column (#2630)
c861b995b is described below
commit c861b995b678853aaefc097cabe05892478bccfc
Author: Shivam Sharma <[email protected]>
AuthorDate: Wed Nov 25 13:37:25 2026 +0530
fix(table): reject case-only duplicate names when renaming a column (#2630)
---
table/update_schema.go | 50 +++++++-------
table/update_schema_test.go | 156 ++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 174 insertions(+), 32 deletions(-)
diff --git a/table/update_schema.go b/table/update_schema.go
index da2add9ea..528069183 100644
--- a/table/update_schema.go
+++ b/table/update_schema.go
@@ -299,30 +299,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's addColumn rejects the renamed-away case; we allow it because the
+// resulting schema is valid.
+func (u *UpdateSchema) nameTaken(parentID int, name, fullName string, selfID
int) bool {
for _, add := range u.adds[parentID] {
if u.sameName(add.Name, name) {
- return fmt.Errorf("field already exists in adds: %s",
fullName)
+ return true
}
}
- if field, ok := u.findField(fullName); ok && !u.isDeleted(field.ID) {
+ if field, ok := u.findField(fullName); ok && field.ID != selfID &&
!u.isDeleted(field.ID) {
if upd, updated := u.updates[parentID][field.ID]; !updated ||
u.sameName(upd.Name, name) {
- return fmt.Errorf("cannot add column, name already
exists: %s", fullName)
+ return true
}
}
// Another column in the same struct may be renamed to this name.
for _, upd := range u.updates[parentID] {
- if u.sameName(upd.Name, name) {
- return fmt.Errorf("field already exists: %s", fullName)
+ if upd.ID != selfID && u.sameName(upd.Name, name) {
+ return true
}
}
+ return false
+}
+
+// checkNameAvailable returns an error if a new column cannot be called name.
+func (u *UpdateSchema) checkNameAvailable(parentID int, name, fullName string)
error {
+ if u.nameTaken(parentID, name, fullName, TableRootID) {
+ return fmt.Errorf("%w: cannot add column, name already exists:
%s", iceberg.ErrInvalidSchema, fullName)
+ }
+
return nil
}
@@ -505,22 +517,8 @@ func (u *UpdateSchema) updateColumn(path []string, update
ColumnUpdate) error {
}
newFullName :=
strings.Join(append(slices.Clone(path[:len(path)-1]), update.Name.Val), ".")
- if existingField, ok := u.findField(newFullName); ok {
- if !u.isDeleted(existingField.ID) {
- return fmt.Errorf("field already exists: %s",
newFullName)
- }
- }
-
- for _, add := range u.adds[parentID] {
- if add.Name == update.Name.Val {
- return fmt.Errorf("cannot rename field to added
field: %s", newFullName)
- }
- }
-
- for _, upd := range u.updates[parentID] {
- if upd.Name == update.Name.Val && upd.ID != field.ID {
- return fmt.Errorf("cannot rename field to
renamed field: %s", newFullName)
- }
+ if u.nameTaken(parentID, update.Name.Val, newFullName,
field.ID) {
+ return fmt.Errorf("%w: cannot rename column, name
already exists: %s", iceberg.ErrInvalidSchema, newFullName)
}
if _, ok := u.identifierFieldNames[fullName]; ok {
diff --git a/table/update_schema_test.go b/table/update_schema_test.go
index e9df6094a..99f14e56c 100644
--- a/table/update_schema_test.go
+++ b/table/update_schema_test.go
@@ -391,7 +391,7 @@ func TestAddColumnNameAlreadyExists(t *testing.T) {
return u.RenameColumn([]string{"age"}, "years").
AddColumn([]string{"years"}, str, "",
false, nil)
},
- wantErr: "field already exists: years",
+ wantErr: "cannot add column, name already exists:
years",
},
{
name: "name another column is renamed to, by
union",
@@ -402,7 +402,16 @@ func TestAddColumnNameAlreadyExists(t *testing.T) {
iceberg.NestedField{ID: 1,
Name: "years", Type: str},
))
},
- wantErr: "field already exists: years",
+ wantErr: "cannot add column, name already exists:
years",
+ },
+ {
+ name: "name a nested column is renamed to",
+ caseSensitive: true,
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.RenameColumn([]string{"address",
"city"}, "town").
+ AddColumn([]string{"address", "town"},
str, "", false, nil)
+ },
+ wantErr: "cannot add column, name already exists:
address.town",
},
{
name: "column added with different case in
case-insensitive mode",
@@ -411,7 +420,7 @@ func TestAddColumnNameAlreadyExists(t *testing.T) {
return u.AddColumn([]string{"x"}, str, "",
false, nil).
AddColumn([]string{"X"}, str, "",
false, nil)
},
- wantErr: "field already exists in adds: X",
+ wantErr: "cannot add column, name already exists: X",
},
}
@@ -420,9 +429,11 @@ func TestAddColumnNameAlreadyExists(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)
})
}
@@ -472,6 +483,139 @@ 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)
+ })
+ }
+
+ allowed := []struct {
+ name string
+ caseSensitive bool
+ build func(*UpdateSchema) *UpdateSchema
+ wantIDs map[string]int
+ }{
+ {
+ name: "same column with different case in
case-insensitive mode",
+ caseSensitive: false,
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.RenameColumn([]string{"name"}, "Name")
+ },
+ wantIDs: map[string]int{"Name": 2},
+ },
+ {
+ name: "same column with different case in
case-sensitive mode",
+ caseSensitive: true,
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.RenameColumn([]string{"name"}, "Name")
+ },
+ wantIDs: map[string]int{"Name": 2},
+ },
+ {
+ name: "name of a column renamed away",
+ caseSensitive: true,
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.RenameColumn([]string{"name"},
"full_name").
+ RenameColumn([]string{"age"}, "name")
+ },
+ wantIDs: map[string]int{"full_name": 2, "name": 3},
+ },
+ }
+
+ for _, tt := range allowed {
+ t.Run(tt.name, func(t *testing.T) {
+ txn := New([]string{"id"}, testMetadata, "", nil,
nil).NewTransaction()
+
+ applied, err := tt.build(NewUpdateSchema(txn,
tt.caseSensitive, false)).Apply()
+ require.NoError(t, err)
+
+ require.NoError(t, tt.build(NewUpdateSchema(txn,
tt.caseSensitive, false)).Commit())
+
+ for _, s := range []*iceberg.Schema{applied,
txn.meta.CurrentSchema()} {
+ for name, id := range tt.wantIDs {
+ f, ok := s.FindFieldByName(name)
+ require.True(t, ok, name)
+ assert.Equal(t, id, f.ID)
+ }
+ }
+ })
+ }
+
+ // The first rename of a swap asks for a name that is still in use.
+ t.Run("swapped names", func(t *testing.T) {
+ txn := New([]string{"id"}, testMetadata, "", nil,
nil).NewTransaction()
+
+ _, err := NewUpdateSchema(txn, true, false).
+ RenameColumn([]string{"name"}, "age").
+ RenameColumn([]string{"age"}, "name").
+ Apply()
+ require.ErrorIs(t, err, iceberg.ErrInvalidSchema)
+ require.ErrorContains(t, err, "cannot rename column, name
already exists: age")
+ })
+}
+
func TestApplyChanges(t *testing.T) {
t.Run("test apply changes on schema", func(t *testing.T) {
deletes := map[int]struct{}{
@@ -863,8 +1007,8 @@ func TestRenameColumn(t *testing.T) {
txn := table.NewTransaction()
_, err := NewUpdateSchema(txn, true,
true).RenameColumn([]string{"name"}, "age").Apply()
- assert.Error(t, err)
- assert.Contains(t, err.Error(), "field already exists")
+ assert.ErrorIs(t, err, iceberg.ErrInvalidSchema)
+ assert.Contains(t, err.Error(), "cannot rename column, name
already exists: age")
})
}
@@ -2689,7 +2833,7 @@ func TestUnionByNameCannotExtendStructAddedByPriorOp(t
*testing.T) {
), "", false, nil).
UnionByNameWith(extend).
Apply()
- require.ErrorContains(t, err, "field already exists in adds: addr")
+ require.ErrorContains(t, err, "cannot add column, name already exists:
addr")
}
func TestUnionByNameMirroredSchemas(t *testing.T) {