|
| 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 | +} |
0 commit comments