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 21d6ec03d fix(table): reject AddColumn when the column name already
exists (#2626)
21d6ec03d is described below
commit 21d6ec03dbdbb35b13da1525c8f8ab7675c6d7d2
Author: Shivam Sharma <[email protected]>
AuthorDate: Tue Nov 17 14:00:42 2026 +0530
fix(table): reject AddColumn when the column name already exists (#2626)
UpdateSchema.AddColumn only failed for an existing name when a pending
update in the same struct had that name. Otherwise it added a second
column with the same name: Apply returned a schema with duplicate names,
Commit failed with a visitor error, and in case-insensitive mode a
column that only differs in case was committed. A name that another
column is renamed to in the same update was not checked at all.
AddColumn and UnionByNameWith now reject a name that an existing column
keeps, that another column is renamed to, or that an earlier add used,
comparing names with the update's case sensitivity. As in Java's
SchemaUpdate and PyIceberg's UpdateSchema, a deleted column's name can
still be reused. A column renamed to another name also gives up its
name, as before.
Signed-off-by: Shivam Sharma
<[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
table/update_schema.go | 70 ++++++++++++++----------
table/update_schema_test.go | 130 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 170 insertions(+), 30 deletions(-)
diff --git a/table/update_schema.go b/table/update_schema.go
index 3de6f1b09..633c5ae14 100644
--- a/table/update_schema.go
+++ b/table/update_schema.go
@@ -297,6 +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 {
+ for _, add := range u.adds[parentID] {
+ if u.sameName(add.Name, name) {
+ return fmt.Errorf("field already exists in adds: %s",
fullName)
+ }
+ }
+
+ if field, ok := u.findField(fullName); ok && !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)
+ }
+ }
+
+ // 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)
+ }
+ }
+
+ return nil
+}
+
+// sameName compares two column names using the case sensitivity of the update.
+func (u *UpdateSchema) sameName(a, b string) bool {
+ if u.caseSensitive {
+ return a == b
+ }
+
+ return strings.EqualFold(a, b)
+}
+
func (u *UpdateSchema) AddColumn(path []string, fieldType iceberg.Type, doc
string, required bool, defaultValue iceberg.Literal) *UpdateSchema {
u.ops = append(u.ops, func() error {
return u.addColumn(path, fieldType, doc, required, defaultValue)
@@ -355,21 +391,8 @@ func (u *UpdateSchema) addColumn(path []string, fieldType
iceberg.Type, doc stri
}
name := path[len(path)-1]
- for _, add := range u.adds[parentID] {
- if add.Name == name {
- return fmt.Errorf("field already exists in adds: %s",
fullName)
- }
- }
-
- // support add field with the same name as deleted field and renamed
field
- if field, ok := u.findField(fullName); ok {
- if !u.isDeleted(field.ID) {
- for _, upd := range u.updates[parentID] {
- if upd.Name == name {
- return fmt.Errorf("field already
exists: %s", fullName)
- }
- }
- }
+ if err := u.checkNameAvailable(parentID, name, fullName); err != nil {
+ return err
}
field := iceberg.NestedField{
@@ -819,21 +842,8 @@ func (u *UpdateSchema) unionAddColumn(path []string,
newField iceberg.NestedFiel
}
name := path[len(path)-1]
- for _, add := range u.adds[parentID] {
- if add.Name == name {
- return fmt.Errorf("field already exists in adds: %s",
fullName)
- }
- }
-
- // guard against colliding with a pending rename
- if field, ok := u.findField(fullName); ok {
- if !u.isDeleted(field.ID) {
- for _, upd := range u.updates[parentID] {
- if upd.Name == name {
- return fmt.Errorf("field already
exists: %s", fullName)
- }
- }
- }
+ if err := u.checkNameAvailable(parentID, name, fullName); err != nil {
+ return err
}
if err := validateFieldDefaults(newField); err != nil {
diff --git a/table/update_schema_test.go b/table/update_schema_test.go
index c16430548..7e3d4165d 100644
--- a/table/update_schema_test.go
+++ b/table/update_schema_test.go
@@ -335,6 +335,136 @@ func TestAddColumn(t *testing.T) {
})
}
+func TestAddColumnNameAlreadyExists(t *testing.T) {
+ str := iceberg.PrimitiveTypes.String
+
+ rejected := []struct {
+ name string
+ caseSensitive bool
+ build func(*UpdateSchema) *UpdateSchema
+ wantErr string
+ }{
+ {
+ name: "top-level column",
+ caseSensitive: true,
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.AddColumn([]string{"name"}, str, "",
false, nil)
+ },
+ wantErr: "cannot add column, name already exists: name",
+ },
+ {
+ name: "nested column",
+ caseSensitive: true,
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.AddColumn([]string{"address", "city"},
str, "", false, nil)
+ },
+ wantErr: "cannot add column, name already exists:
address.city",
+ },
+ {
+ name: "column with different case in
case-insensitive mode",
+ caseSensitive: false,
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.AddColumn([]string{"NAME"}, str, "",
false, nil)
+ },
+ wantErr: "cannot add column, name already exists: NAME",
+ },
+ {
+ name: "column with only a doc update",
+ caseSensitive: true,
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.UpdateColumn([]string{"name"},
ColumnUpdate{Doc: iceberg.Optional[string]{Valid: true, Val: "doc"}}).
+ AddColumn([]string{"name"}, str, "",
false, nil)
+ },
+ wantErr: "cannot add column, name already exists: name",
+ },
+ {
+ name: "name another column is renamed to",
+ caseSensitive: true,
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.RenameColumn([]string{"age"}, "years").
+ AddColumn([]string{"years"}, str, "",
false, nil)
+ },
+ wantErr: "field already exists: years",
+ },
+ {
+ name: "name another column is renamed to, by
union",
+ caseSensitive: true,
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.RenameColumn([]string{"age"}, "years").
+ UnionByNameWith(iceberg.NewSchema(0,
+ iceberg.NestedField{ID: 1,
Name: "years", Type: str},
+ ))
+ },
+ wantErr: "field already exists: years",
+ },
+ {
+ name: "column added with different case in
case-insensitive mode",
+ caseSensitive: false,
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.AddColumn([]string{"x"}, str, "",
false, nil).
+ AddColumn([]string{"X"}, str, "",
false, nil)
+ },
+ wantErr: "field already exists in adds: X",
+ },
+ }
+
+ 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.ErrorContains(t, err, tt.wantErr)
+
+ err = tt.build(NewUpdateSchema(txn, tt.caseSensitive,
false)).Commit()
+ require.ErrorContains(t, err, tt.wantErr)
+ })
+ }
+
+ allowed := []struct {
+ name string
+ build func(*UpdateSchema) *UpdateSchema
+ checkID func(t *testing.T, s *iceberg.Schema)
+ }{
+ {
+ name: "name of a deleted column",
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.DeleteColumn([]string{"name"}).
+ AddColumn([]string{"name"}, str, "",
false, nil)
+ },
+ checkID: func(t *testing.T, s *iceberg.Schema) {
+ f, ok := s.FindFieldByName("name")
+ require.True(t, ok)
+ assert.Equal(t, 12, f.ID)
+ },
+ },
+ {
+ name: "name of a column renamed away",
+ build: func(u *UpdateSchema) *UpdateSchema {
+ return u.RenameColumn([]string{"name"},
"full_name").
+ AddColumn([]string{"name"}, str, "",
false, nil)
+ },
+ checkID: func(t *testing.T, s *iceberg.Schema) {
+ f, ok := s.FindFieldByName("name")
+ require.True(t, ok)
+ assert.Equal(t, 12, f.ID)
+ f, ok = s.FindFieldByName("full_name")
+ require.True(t, ok)
+ assert.Equal(t, 2, f.ID)
+ },
+ },
+ }
+
+ for _, tt := range allowed {
+ t.Run(tt.name, func(t *testing.T) {
+ txn := New([]string{"id"}, testMetadata, "", nil,
nil).NewTransaction()
+
+ s, err := tt.build(NewUpdateSchema(txn, true,
false)).Apply()
+ require.NoError(t, err)
+ tt.checkID(t, s)
+ })
+ }
+}
+
func TestApplyChanges(t *testing.T) {
t.Run("test apply changes on schema", func(t *testing.T) {
deletes := map[int]struct{}{