laskoviymishka commented on code in PR #2630:
URL: https://github.com/apache/iceberg-go/pull/2630#discussion_r4597497347
##########
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:
Could the two allowed cases also go through `Commit()`, like the `rejected`
loop above? Both commit on the head commit, and after the second one the
committed schema has `name` at ID 3, so asserting it here would pin the
end-to-end behavior this PR is about.
##########
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:
Could we add a swap (`name→age`, then `age→name`) and a case-sensitive
`name→Name`? These two allowed cases are the only tests that reach the new
`field.ID != selfID` / `upd.ID != selfID` exclusions in `nameTaken`, so a test
for each would pin whichever result we want there.
##########
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:
Should this name `addColumn`? Java's `renameColumn` runs no name check at
call time
([SchemaUpdate.java#L205-L227](https://github.com/apache/iceberg/blob/f69ed7b7d500261cc4ac30bd7c392eeae3cabd6e/core/src/main/java/org/apache/iceberg/SchemaUpdate.java#L205-L227));
only `addColumn` rejects a renamed-away name
([L140-L157](https://github.com/apache/iceberg/blob/f69ed7b7d500261cc4ac30bd7c392eeae3cabd6e/core/src/main/java/org/apache/iceberg/SchemaUpdate.java#L140-L157)).
On the rename path we're stricter than Java, not looser. Something like
"Java's `addColumn` rejects the renamed-away case" would keep the comment
accurate.
--
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]