laskoviymishka commented on code in PR #2626:
URL: https://github.com/apache/iceberg-go/pull/2626#discussion_r4596238191
##########
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.
Review Comment:
Java's `internalAddColumn` rejects a new column that takes the name of one
renamed away in the same update
([SchemaUpdate.java#L152-L156](https://github.com/apache/iceberg/blob/52fc764127402ade8170b1b53ddd95fde13cb75c/core/src/main/java/org/apache/iceberg/SchemaUpdate.java#L152-L156)).
`main` allowed it too, and keeping it is reasonable. Could the comment say so,
so the next reader doesn't have to re-check Java?
```suggestion
// its name, so a new column may take it. Java rejects the renamed-away case;
// we allow it because the resulting schema is valid.
```
##########
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 {
Review Comment:
Could the rename branch of `updateColumn` use `sameName` too? It still
compares with `==`
([L512-L522](https://github.com/apache/iceberg-go/blob/53ae3e6d91db7f973e1ef393f970198bbfd18ca5/table/update_schema.go#L512-L522)),
so in case-insensitive mode `AddColumn("x")` then `RenameColumn("age", "X")`
passes `Apply` and `Commit` with both `X` and `x` in the schema, and two
renames to `y`/`Y` do the same. `main` has the same gap, but it's the case-only
duplicate this PR fixes for `AddColumn`.
##########
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",
Review Comment:
Could we add a rejected case inside `address`, e.g. rename `address.city` to
`town` and then add `address.town`? Every rename-then-add case here is at the
root, so the `parentID`-keyed `adds`/`updates` lookups in `checkNameAvailable`
are only exercised for the root struct.
##########
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)
Review Comment:
Should these share one message?
[`checkNameAvailable`](https://github.com/apache/iceberg-go/blob/53ae3e6d91db7f973e1ef393f970198bbfd18ca5/table/update_schema.go#L304-L325)
returns `field already exists in adds`, `cannot add column, name already
exists` and `field already exists` for the same condition, and none is a
sentinel, so callers can only match on strings. Java's `internalAddColumn` uses
`Cannot add column, name already exists: %s`.
--
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]