Skip to content

Commit 9948f59

Browse files
annrpomrafiss
authored andcommitted
schemachanger: implement RENAME COLUMN in the declarative schema changer
Release note: None
1 parent e3770b8 commit 9948f59

24 files changed

+620
-17
lines changed

pkg/ccl/schemachangerccl/sctestbackupccl/backup_base_generated_test.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sql/alter_table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ func (n *alterTableNode) startExec(params runParams) error {
804804
tableDesc.GetRowLevelTTL().HasDurationExpr() {
805805
return pgerror.Newf(
806806
pgcode.InvalidTableDefinition,
807-
`cannot rename column %s while ttl_expire_after is set`,
807+
`cannot alter column %s while ttl_expire_after is set`,
808808
columnName,
809809
)
810810
}

pkg/sql/logictest/testdata/logic_test/rename_column

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,34 @@ query II
199199
SELECT j, k FROM foo;
200200
----
201201
1 2
202+
203+
# Test that mixed-case column names are handled correctly for rename operations.
204+
statement ok
205+
CREATE TABLE mixed_case_table (
206+
"CamelCase" INT PRIMARY KEY,
207+
"snake_case" TEXT,
208+
"UPPERCASE" DECIMAL
209+
);
210+
211+
statement error column "UPPERCASE" of relation "mixed_case_table" already exists
212+
ALTER TABLE mixed_case_table RENAME COLUMN "CamelCase" TO "UPPERCASE";
213+
214+
statement ok
215+
ALTER TABLE mixed_case_table RENAME COLUMN "CamelCase" TO "CamelCase";
216+
217+
statement ok
218+
ALTER TABLE mixed_case_table RENAME COLUMN "CamelCase" TO "NewCamelCase";
219+
220+
statement ok
221+
ALTER TABLE mixed_case_table RENAME COLUMN "snake_case" TO "SnakeCase";
222+
223+
statement ok
224+
ALTER TABLE mixed_case_table RENAME COLUMN "UPPERCASE" TO "decimal_value";
225+
226+
query T colnames
227+
SELECT column_name FROM [SHOW COLUMNS FROM mixed_case_table] ORDER BY column_name;
228+
----
229+
column_name
230+
NewCamelCase
231+
SnakeCase
232+
decimal_value

pkg/sql/logictest/testdata/logic_test/row_level_ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ subtest alter_column_crdb_internal_expiration_rename
227227
statement ok
228228
CREATE TABLE alter_column_crdb_internal_expiration_rename() WITH (ttl_expire_after='10 minutes')
229229

230-
statement error cannot rename column crdb_internal_expiration while ttl_expire_after is set
230+
statement error cannot alter column crdb_internal_expiration while ttl_expire_after is set
231231
ALTER TABLE alter_column_crdb_internal_expiration_rename RENAME COLUMN crdb_internal_expiration TO crdb_internal_expiration_2
232232

233233
subtest end

pkg/sql/logictest/testdata/logic_test/system_columns

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ ALTER TABLE tab3 SET (schema_locked=true)
253253

254254
subtest alter_commands
255255

256-
statement error pq: cannot rename system column "crdb_internal_mvcc_timestamp"
256+
statement error pq: cannot alter system column "crdb_internal_mvcc_timestamp"
257257
ALTER TABLE tab3 RENAME crdb_internal_mvcc_timestamp TO blah;
258258

259259
statement error pq: cannot alter system column "crdb_internal_mvcc_timestamp"

pkg/sql/rename_column.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (p *planner) findColumnToRename(
105105
if col.IsSystemColumn() {
106106
return nil, pgerror.Newf(
107107
pgcode.FeatureNotSupported,
108-
"cannot rename system column %q", col.ColName(),
108+
"cannot alter system column %q", col.ColName(),
109109
)
110110
}
111111
for _, tableRef := range tableDesc.DependedOnBy {

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ go_library(
1515
"alter_table_alter_primary_key.go",
1616
"alter_table_drop_column.go",
1717
"alter_table_drop_constraint.go",
18+
"alter_table_rename_column.go",
1819
"alter_table_set_rls_mode.go",
1920
"alter_table_validate_constraint.go",
2021
"comment_on.go",

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ var supportedAlterTableStatements = map[reflect.Type]supportedAlterTableCommand{
3838
reflect.TypeOf((*tree.AlterTableDropConstraint)(nil)): {fn: alterTableDropConstraint, on: true, checks: nil},
3939
reflect.TypeOf((*tree.AlterTableValidateConstraint)(nil)): {fn: alterTableValidateConstraint, on: true, checks: nil},
4040
reflect.TypeOf((*tree.AlterTableSetDefault)(nil)): {fn: alterTableSetDefault, on: true, checks: nil},
41-
reflect.TypeOf((*tree.AlterTableSetOnUpdate)(nil)): {fn: alterTableSetOnUpdate, on: true, checks: isV254Active},
4241
reflect.TypeOf((*tree.AlterTableAlterColumnType)(nil)): {fn: alterTableAlterColumnType, on: true, checks: nil},
4342
reflect.TypeOf((*tree.AlterTableSetRLSMode)(nil)): {fn: alterTableSetRLSMode, on: true, checks: isV252Active},
4443
reflect.TypeOf((*tree.AlterTableDropNotNull)(nil)): {fn: alterTableDropNotNull, on: true, checks: isV253Active},
44+
reflect.TypeOf((*tree.AlterTableSetOnUpdate)(nil)): {fn: alterTableSetOnUpdate, on: true, checks: isV254Active},
45+
reflect.TypeOf((*tree.AlterTableRenameColumn)(nil)): {fn: alterTableRenameColumn, on: true, checks: isV254Active},
4546
}
4647

4748
func init() {

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_column_type.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func alterTableAlterColumnType(
9393
case *scpb.PolicyUsingExpr, *scpb.PolicyWithCheckExpr:
9494
panic(sqlerrors.NewAlterDependsOnPolicyExprError(op, objType, t.Column.String()))
9595
}
96-
})
96+
}, false /* allowPartialIdxPredicateRef */)
9797

9898
var err error
9999
newColType.Type, err = schemachange.ValidateAlterColumnTypeChecks(
@@ -309,7 +309,7 @@ func handleGeneralColumnConversion(
309309
case *scpb.SecondaryIndex:
310310
panic(sqlerrors.NewAlterColumnTypeColInIndexNotSupportedErr())
311311
}
312-
})
312+
}, false /* allowPartialIdxPredicateRef */)
313313

314314
// This code path should never be reached for virtual columns, as their values
315315
// are always computed dynamically on access and are never stored on disk.
@@ -505,7 +505,7 @@ func maybeWriteNoticeForFKColTypeMismatch(b BuildCtx, col *scpb.Column, colType
505505
case *scpb.ForeignKeyConstraintUnvalidated:
506506
writeNoticeHelper(e.ColumnIDs, e.ReferencedColumnIDs, e.ReferencedTableID)
507507
}
508-
})
508+
}, false /* allowPartialIdxPredicateRef */)
509509
}
510510

511511
func getComputeExpressionForBackfill(

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_primary_key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ func checkIfColumnCanBeDropped(b BuildCtx, columnToDrop *scpb.Column) bool {
11491149
canBeDropped = false
11501150
}
11511151
}
1152-
})
1152+
}, false /* allowPartialIdxPredicateRef */)
11531153
return canBeDropped
11541154
}
11551155

0 commit comments

Comments
 (0)