Skip to content

Commit b1c79a5

Browse files
committed
schemachanger: implement ALTER COLUMN [SET | DROP] ON UPDATE in declarative
This adds support for setting and dropping ON UPDATE expressions to the declarative schema changer. No new elements were needed, so this was pretty straightforward. Release note: None
1 parent 2f72cf4 commit b1c79a5

23 files changed

+854
-5
lines changed

pkg/ccl/schemachangerccl/sctestbackupccl/backup_base_generated_test.go

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

pkg/sql/catalog/rewrite/rewrite.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,11 @@ func rewriteSchemaChangerState(
10101010
// just drop the target.
10111011
removeElementAtCurrentIdx()
10121012
continue
1013+
case *scpb.ColumnOnUpdateExpression:
1014+
// IF there is any dependency missing for column ON UPDATE expression,
1015+
// we just drop the target.
1016+
removeElementAtCurrentIdx()
1017+
continue
10131018
case *scpb.SequenceOwner:
10141019
// If a sequence owner is missing the sequence, then the sequence
10151020
// was already dropped and this element can be safely removed.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ go_library(
1010
"alter_table_alter_column_drop_not_null.go",
1111
"alter_table_alter_column_set_default.go",
1212
"alter_table_alter_column_set_not_null.go",
13+
"alter_table_alter_column_set_on_update.go",
1314
"alter_table_alter_column_type.go",
1415
"alter_table_alter_primary_key.go",
1516
"alter_table_drop_column.go",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ 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},
4142
reflect.TypeOf((*tree.AlterTableAlterColumnType)(nil)): {fn: alterTableAlterColumnType, on: true, checks: nil},
4243
reflect.TypeOf((*tree.AlterTableSetRLSMode)(nil)): {fn: alterTableSetRLSMode, on: true, checks: isV252Active},
4344
reflect.TypeOf((*tree.AlterTableDropNotNull)(nil)): {fn: alterTableDropNotNull, on: true, checks: isV253Active},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func panicIfInvalidNonComputedColumnExpr(
8080
}
8181

8282
colType := mustRetrieveColumnTypeElem(b, tbl.TableID, col.ColumnID)
83-
typedNewExpr, _, err := sanitizeColumnExpression(context.Background(), b.SemaCtx(), newExpr, colType, schemaChange)
83+
typedNewExpr, _, err := sanitizeColumnExpression(b, b.SemaCtx(), newExpr, colType, schemaChange)
8484
if err != nil {
8585
panic(err)
8686
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2025 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the CockroachDB Software License
4+
// included in the /LICENSE file.
5+
6+
package scbuildstmt
7+
8+
import (
9+
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb"
10+
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
11+
)
12+
13+
func alterTableSetOnUpdate(
14+
b BuildCtx,
15+
tn *tree.TableName,
16+
tbl *scpb.Table,
17+
stmt tree.Statement,
18+
t *tree.AlterTableSetOnUpdate,
19+
) {
20+
alterColumnPreChecks(b, tn, tbl, t.Column)
21+
colID := getColumnIDFromColumnName(b, tbl.TableID, t.Column, true /* required */)
22+
col := mustRetrieveColumnElem(b, tbl.TableID, colID)
23+
oldOnUpdateExpr := retrieveColumnOnUpdateExpressionElem(b, tbl.TableID, colID)
24+
colType := mustRetrieveColumnTypeElem(b, tbl.TableID, colID)
25+
26+
// Block alters on system columns.
27+
panicIfSystemColumn(col, t.Column.String())
28+
29+
// Block disallowed operations on computed columns.
30+
panicIfComputedColumn(b, tn.ObjectName, colType, t.Column.String(), t.Expr)
31+
32+
// For DROP ON UPDATE.
33+
if t.Expr == nil {
34+
if oldOnUpdateExpr != nil {
35+
b.Drop(oldOnUpdateExpr)
36+
}
37+
return
38+
}
39+
40+
// If our target column already has an ON UPDATE expression, we want to drop it first.
41+
if oldOnUpdateExpr != nil {
42+
b.Drop(oldOnUpdateExpr)
43+
}
44+
typedExpr := panicIfInvalidNonComputedColumnExpr(b, tbl, tn.ToUnresolvedObjectName(), col, t.Column.String(), t.Expr, tree.ColumnOnUpdateExpr)
45+
46+
b.Add(&scpb.ColumnOnUpdateExpression{
47+
TableID: tbl.TableID,
48+
ColumnID: colID,
49+
Expression: *b.WrapExpression(tbl.TableID, typedExpr),
50+
})
51+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,7 @@ var isV252Active = func(_ tree.NodeFormatter, _ sessiondatapb.NewSchemaChangerMo
221221
var isV253Active = func(_ tree.NodeFormatter, _ sessiondatapb.NewSchemaChangerMode, activeVersion clusterversion.ClusterVersion) bool {
222222
return activeVersion.IsActive(clusterversion.V25_3)
223223
}
224+
225+
var isV254Active = func(_ tree.NodeFormatter, _ sessiondatapb.NewSchemaChangerMode, activeVersion clusterversion.ClusterVersion) bool {
226+
return activeVersion.IsActive(clusterversion.V25_4)
227+
}

0 commit comments

Comments
 (0)