ShivamSharma43 opened a new pull request, #2626: URL: https://github.com/apache/iceberg-go/pull/2626
`UpdateSchema.AddColumn` accepted a column name that already exists. `Apply` returned a schema with two columns of the same name, and `Commit` failed with `invalid schema: error encountered during schema visitor: invalid schema: multiple fields for name name: 2 and 3` (see the issue for the reproduction). In case-insensitive mode, adding `NAME` next to `name` (or adding `x` and then `X`) committed a schema whose column names only differ in case. ### Cause The duplicate check in `addColumn`, and its copy in `unionAddColumn`, only failed when the existing column was found *and* a pending update in the same struct had that name. With no pending update the add went through. A name that another column is renamed to in the same update was never checked, because the loop over the updates only ran when the name already existed in the schema. ### Fix Both paths now call a new `checkNameAvailable`, which rejects the name when: - an existing column keeps that name (`cannot add column, name already exists: name`), - another column in the same struct is renamed to it (`field already exists: years`), or - an earlier add in the same update used it (`field already exists in adds: X`). Names are compared with the update's case sensitivity, like `findField` already does. This matches Java's [`SchemaUpdate.internalAddColumn`](https://github.com/apache/iceberg/blob/52fc764127402ade8170b1b53ddd95fde13cb75c/core/src/main/java/org/apache/iceberg/SchemaUpdate.java#L141-L156), which uses `caseInsensitiveFindField` in case-insensitive mode, and PyIceberg's [`add_column`](https://github.com/apache/iceberg-python/blob/639a3bf505b6932945bba3f661395255bac872c2/pyiceberg/table/update/schema.py#L214-L221). The name of a column deleted in the same update can still be reused, as in Java and PyIceberg. The existing code also lets a new column take the name of a column renamed to something else in the same update (the comment said "support add field with the same name as deleted field and renamed field"). Java rejects that case, but the result is a valid schema, so I kept it working and added a test for it. ### Testing - New `TestAddColumnNameAlreadyExists` runs each rejected case through both `Apply` and `Commit`: an existing top-level column, an existing nested column, a different-case name in case-insensitive mode, a column with only a doc update, a name another column is renamed to (through `AddColumn` and `UnionByNameWith`), and two adds that differ only in case. All of them fail on main. The doc-update case already failed there, with `field already exists: name`; the other six were accepted. It also checks that deleting or renaming away a column and then adding its name still works. - `go vet ./table/` and `golangci-lint run ./table/` (v2.14.0) are clean. `go test ./table/...` passes except for tests that fail the same way on unmodified main on my Windows machine (path handling): `TestEmptyStringIdentityPartitionScans`, `TestScanImportedSignedZeroBounds`, `TestDeleteImportedSignedZerosWithIn`, `TestDeleteNormallyWrittenSignedZeroWithIn`, `TestScanLegacyEmptyStringPartitionDeleteBounds`, `TestScanWithIDLessNameMappingFields`, two `TestRewriteDataFiles_PartialProgress*` tests, `TestWriteMapType` and `TestGetReferencedFiles_OverwriteThenExpireExcludesTombstones`. Closes #2625 This PR was prepared with AI assistance (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- 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]
