Open
Description
This issue tracks the implementation of the ALTER TABLE [IF EXISTS] ... RENAME COLUMN old_name TO new_name
statement in the declarative schema changer.
The declarative schema changer already has all the infrastructure needed, so this should be a straightforward implementation:
- ColumnName elements handle column name management
- SetColumnName operation handles execution
- Dependency tracking and rollback work automatically
- Cross-references in indexes, constraints, etc. are handled by existing logic
High-level steps:
- Update supportedAlterTableStatements map in pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table.go
- Create a builder function in pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_rename_column.go
// High level implementation sketch.
func alterTableRenameColumn(
b BuildCtx, tn *tree.TableName, tbl *scpb.Table, n *tree.AlterTable, t *tree.AlterTableRenameColumn,
) {
// 1. Resolve the column by current name
colElts := b.ResolveColumn(tbl.TableID, t.Column, ResolveParams{
RequiredPrivilege: privilege.CREATE,
})
_, _, col := scpb.FindColumn(colElts)
_, _, currentColName := scpb.FindColumnName(colElts)
// 2. Validate column exists
if col == nil {
panic(pgerror.Newf(pgcode.UndefinedColumn, "column %q does not exist", t.Column))
}
// 3. Check for name conflicts
checkColumnNameConflict(b, tbl.TableID, t.NewName)
// 4. Mark old column name as ABSENT
b.Drop(currentColName)
// 5. Add new column name targeting PUBLIC
b.Add(&scpb.ColumnName{
TableID: tbl.TableID,
ColumnID: col.ColumnID,
Name: string(t.NewName),
})
}
- Add tests in pkg/sql/schemachanger/testdata/end_to_end/alter_table_rename_column (new file)
- Create data-driven tests covering:
- Basic column rename functionality
- Name collision detection
- Dependencies (indexes, constraints referencing column)
- Also verify that existing logic tests in pkg/sql/logictest pass.
- Create data-driven tests covering:
Jira issue: CRDB-51552
Epic CRDB-31465