Skip to content

Commit f9aa939

Browse files
committed
sql: add column identity sequence options to schema changer
TODO: Fixes: #142914 Epic: CRDB-31283 Release note (sql change): The `ALTER COLUMN ...` sequence identity commands are run by the declarative schema changer.
1 parent 3def712 commit f9aa939

File tree

9 files changed

+134
-6
lines changed

9 files changed

+134
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ go_library(
99
"alter_table_add_constraint.go",
1010
"alter_table_alter_column_drop_not_null.go",
1111
"alter_table_alter_column_drop_stored.go",
12+
"alter_table_alter_column_identity.go",
1213
"alter_table_alter_column_set_default.go",
1314
"alter_table_alter_column_set_not_null.go",
1415
"alter_table_alter_column_set_on_update.go",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var supportedAlterTableStatements = map[reflect.Type]supportedAlterTableCommand{
4545
reflect.TypeOf((*tree.AlterTableRenameColumn)(nil)): {fn: alterTableRenameColumn, on: true, checks: isV254Active},
4646
reflect.TypeOf((*tree.AlterTableDropStored)(nil)): {fn: alterTableDropStored, on: true, checks: isV261Active},
4747
reflect.TypeOf((*tree.AlterTableRenameConstraint)(nil)): {fn: alterTableRenameConstraint, on: true, checks: isV261Active},
48+
reflect.TypeOf((*tree.AlterTableIdentity)(nil)): {fn: alterTableAlterColumnIdentity, on: true, checks: isV261Active},
4849
}
4950

5051
func init() {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
"fmt"
10+
"reflect"
11+
12+
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
13+
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr"
14+
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
15+
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
16+
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb"
17+
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
18+
)
19+
20+
func alterTableAlterColumnIdentity(
21+
b BuildCtx, tn *tree.TableName, tbl *scpb.Table, stmt tree.Statement, t *tree.AlterTableIdentity,
22+
) {
23+
alterColumnPreChecks(b, tn, tbl, t.Column)
24+
25+
columnID := getColumnIDFromColumnName(b, tbl.TableID, t.Column, true /* required */)
26+
// Block alters on system columns.
27+
panicIfSystemColumn(mustRetrieveColumnElem(b, tbl.TableID, columnID), t.Column)
28+
29+
sequenceOwner := b.QueryByID(tbl.TableID).FilterSequenceOwner().Filter(func(_ scpb.Status, _ scpb.TargetStatus, e *scpb.SequenceOwner) bool {
30+
return e.TableID == tbl.TableID && e.ColumnID == columnID
31+
}).MustGetZeroOrOneElement()
32+
33+
if sequenceOwner == nil {
34+
panic(pgerror.Newf(
35+
pgcode.FeatureNotSupported,
36+
"cannot alter identity of a non-sequence column %q", tree.ErrString(&t.Column)))
37+
}
38+
39+
newOpts := descpb.TableDescriptor_SequenceOpts{
40+
Increment: 1,
41+
}
42+
if err := schemaexpr.AssignSequenceOptions(&newOpts,
43+
t.SeqOptions,
44+
64,
45+
true,
46+
nil,
47+
); err != nil {
48+
panic(err)
49+
}
50+
51+
defaultOpts := descpb.TableDescriptor_SequenceOpts{
52+
Increment: 1,
53+
}
54+
if err := schemaexpr.AssignSequenceOptions(&defaultOpts,
55+
nil,
56+
64,
57+
true,
58+
nil,
59+
); err != nil {
60+
panic(err)
61+
}
62+
63+
updateSequenceOption := func(key string, defaultValue, value interface{}) {
64+
newSeqOption := scpb.SequenceOption{
65+
SequenceID: sequenceOwner.SequenceID,
66+
Key: key,
67+
Value: fmt.Sprintf("%v", value),
68+
}
69+
70+
oldSeqOption := b.QueryByID(sequenceOwner.SequenceID).FilterSequenceOption().Filter(func(current scpb.Status, target scpb.TargetStatus, e *scpb.SequenceOption) bool {
71+
return e.Key == key
72+
}).MustGetZeroOrOneElement()
73+
if oldSeqOption != nil {
74+
// Skip a noop update.
75+
if oldSeqOption.Value == newSeqOption.Value {
76+
return
77+
}
78+
b.Drop(oldSeqOption)
79+
}
80+
81+
// Skip setting to default values.
82+
if reflect.DeepEqual(defaultValue, value) {
83+
return
84+
}
85+
86+
b.Add(&newSeqOption)
87+
}
88+
89+
updateSequenceOption(tree.SeqOptIncrement, defaultOpts.Increment, newOpts.Increment)
90+
// updateSequenceOption(tree.SeqOptMinValue, defaultOpts.MinValue, newOpts.MinValue)
91+
// updateSequenceOption(tree.SeqOptMaxValue, defaultOpts.MaxValue, newOpts.MaxValue)
92+
// updateSequenceOption(tree.SeqOptStart, defaultOpts.Start, newOpts.Start)
93+
// updateSequenceOption(tree.SeqOptVirtual, defaultOpts.Virtual, newOpts.Virtual)
94+
// updateSequenceOption(tree.SeqOptCacheSession, defaultOpts.SessionCacheSize, newOpts.SessionCacheSize)
95+
// updateSequenceOption(tree.SeqOptCacheNode, defaultOpts.NodeCacheSize, newOpts.NodeCacheSize)
96+
// updateSequenceOption(tree.SeqOptAs, defaultOpts.AsIntegerType, newOpts.AsIntegerType)
97+
}

pkg/sql/schemachanger/scexec/scmutationexec/sequence.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ func (i *immediateVisitor) SetSequenceOption(ctx context.Context, op scop.SetSeq
8686
return sequenceOptionMeta[op.Key].SetFunc(op.Value)
8787
}
8888

89+
// TODO: implement
90+
func (i *immediateVisitor) UnsetSequenceOption(
91+
ctx context.Context, op scop.UnsetSequenceOption,
92+
) error {
93+
panic("unimplemented")
94+
}
95+
8996
func (i *immediateVisitor) InitSequence(ctx context.Context, op scop.InitSequence) error {
9097
sc, err := i.checkOutTable(ctx, op.SequenceID)
9198
if err != nil {

pkg/sql/schemachanger/scop/immediate_mutation.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,12 @@ type SetSequenceOption struct {
10921092
Value string
10931093
}
10941094

1095+
type UnsetSequenceOption struct {
1096+
immediateMutationOp
1097+
SequenceID descpb.ID
1098+
Key string
1099+
}
1100+
10951101
type InitSequence struct {
10961102
immediateMutationOp
10971103
SequenceID descpb.ID

pkg/sql/schemachanger/scop/immediate_mutation_visitor_generated.go

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

pkg/sql/schemachanger/scpb/elements.proto

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ message SequenceOption {
458458
string value = 3;
459459
}
460460

461-
462461
message SequenceOwner {
463462
uint32 sequence_id = 1 [(gogoproto.customname) = "SequenceID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"];
464463
uint32 table_id = 2 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"];

pkg/sql/schemachanger/scplan/internal/opgen/opgen_sequence_option.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,16 @@ func init() {
3232
}),
3333
),
3434
),
35+
// toAbsent(
36+
// scpb.Status_PUBLIC,
37+
// to(scpb.Status_ABSENT,
38+
// emit(func(this *scpb.SequenceOption) *scop.UnsetSequenceOption {
39+
// return &scop.UnsetSequenceOption{
40+
// SequenceID: this.SequenceID,
41+
// Key: this.Key,
42+
// }
43+
// }),
44+
// ),
45+
// ),
3546
)
3647
}

pkg/sql/sem/tree/alter_table.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ type AlterTableDropStored struct {
527527
Column Name
528528
}
529529

530-
// GetColumn implemnets the ColumnMutationCmd interface.
530+
// GetColumn implements the ColumnMutationCmd interface.
531531
func (node *AlterTableDropStored) GetColumn() Name {
532532
return node.Column
533533
}
@@ -778,7 +778,7 @@ type AlterTableAddIdentity struct {
778778
Qualification ColumnQualification
779779
}
780780

781-
// GetColumn implemnets the ColumnMutationCmd interface.
781+
// GetColumn implements the ColumnMutationCmd interface.
782782
func (node *AlterTableAddIdentity) GetColumn() Name {
783783
return node.Column
784784
}
@@ -814,7 +814,7 @@ type AlterTableSetIdentity struct {
814814
GeneratedAsIdentityType GeneratedIdentityType
815815
}
816816

817-
// GetColumn implemnets the ColumnMutationCmd interface.
817+
// GetColumn implements the ColumnMutationCmd interface.
818818
func (node *AlterTableSetIdentity) GetColumn() Name {
819819
return node.Column
820820
}
@@ -843,7 +843,7 @@ type AlterTableIdentity struct {
843843
SeqOptions SequenceOptions
844844
}
845845

846-
// GetColumn implemnets the ColumnMutationCmd interface.
846+
// GetColumn implements the ColumnMutationCmd interface.
847847
func (node *AlterTableIdentity) GetColumn() Name {
848848
return node.Column
849849
}
@@ -873,7 +873,7 @@ type AlterTableDropIdentity struct {
873873
IfExists bool
874874
}
875875

876-
// GetColumn implemnets the ColumnMutationCmd interface.
876+
// GetColumn implements the ColumnMutationCmd interface.
877877
func (node *AlterTableDropIdentity) GetColumn() Name {
878878
return node.Column
879879
}

0 commit comments

Comments
 (0)