8000 sql/schemachanger: implement `ALTER TABLE ... RENAME COLUMN ... TO` · Issue #148340 · cockroachdb/cockroach · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
sql/schemachanger: implement ALTER TABLE ... RENAME COLUMN ... TO #148340
Open
@rafiss

Description

@rafiss

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.

Jira issue: CRDB-51552

Epic CRDB-31465

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-schema-changer-implRelated to the implementation of the new schema changerC-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)T-sql-foundationsSQL Foundations Team (formerly SQL Schema + SQL Sessions)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions