|
| 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 | + "strconv" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" |
| 19 | +) |
| 20 | + |
| 21 | +func alterTableAlterColumnIdentity( |
| 22 | + b BuildCtx, tn *tree.TableName, tbl *scpb.Table, stmt tree.Statement, t *tree.AlterTableIdentity, |
| 23 | +) { |
| 24 | + alterColumnPreChecks(b, tn, tbl, t.Column) |
| 25 | + |
| 26 | + columnID := getColumnIDFromColumnName(b, tbl.TableID, t.Column, true /* required */) |
| 27 | + // Block alters on system columns. |
| 28 | + column := mustRetrieveColumnElem(b, tbl.TableID, columnID) |
| 29 | + panicIfSystemColumn(column, t.Column) |
| 30 | + |
| 31 | + sequenceOwner := b.QueryByID(tbl.TableID).FilterSequenceOwner().Filter(func(_ scpb.Status, _ scpb.TargetStatus, e *scpb.SequenceOwner) bool { |
| 32 | + return e.TableID == tbl.TableID && e.ColumnID == columnID |
| 33 | + }).MustGetZeroOrOneElement() |
| 34 | + if sequenceOwner == nil { |
| 35 | + panic(pgerror.Newf( |
| 36 | + pgcode.FeatureNotSupported, |
| 37 | + "cannot alter identity of a non-sequence column %q", tree.ErrString(&t.Column))) |
| 38 | + } |
| 39 | + |
| 40 | + // Get the current sequence options from elements and defaults. |
| 41 | + currentOpts := schemaexpr.DefaultSequenceOptions() |
| 42 | + b.QueryByID(sequenceOwner.SequenceID).FilterSequenceOption().ForEach(func(_ scpb.Status, _ scpb.TargetStatus, e *scpb.SequenceOption) { |
| 43 | + var err error |
| 44 | + switch name := e.Key; name { |
| 45 | + case tree.SeqOptAs: |
| 46 | + currentOpts.AsIntegerType = e.Value |
| 47 | + case tree.SeqOptCacheNode: |
| 48 | + currentOpts.NodeCacheSize, err = strconv.ParseInt(e.Value, 10, 64) |
| 49 | + if err != nil { |
| 50 | + panic(pgerror.Wrapf(err, pgcode.Internal, "invalid sequence option value for %q", name)) |
| 51 | + } |
| 52 | + case tree.SeqOptCacheSession: |
| 53 | + currentOpts.SessionCacheSize, err = strconv.ParseInt(e.Value, 10, 64) |
| 54 | + if err != nil { |
| 55 | + panic(pgerror.Wrapf(err, pgcode.Internal, "invalid sequence option value for %q", name)) |
| 56 | + } |
| 57 | + case tree.SeqOptIncrement: |
| 58 | + currentOpts.Increment, err = strconv.ParseInt(e.Value, 10, 64) |
| 59 | + if err != nil { |
| 60 | + panic(pgerror.Wrapf(err, pgcode.Internal, "invalid sequence option value for %q", name)) |
| 61 | + } |
| 62 | + case tree.SeqOptMinValue: |
| 63 | + currentOpts.MinValue, err = strconv.ParseInt(e.Value, 10, 64) |
| 64 | + if err != nil { |
| 65 | + panic(pgerror.Wrapf(err, pgcode.Internal, "invalid sequence option value for %q", name)) |
| 66 | + } |
| 67 | + case tree.SeqOptMaxValue: |
| 68 | + currentOpts.MaxValue, err = strconv.ParseInt(e.Value, 10, 64) |
| 69 | + if err != nil { |
| 70 | + panic(pgerror.Wrapf(err, pgcode.Internal, "invalid sequence option value for %q", name)) |
| 71 | + } |
| 72 | + case tree.SeqOptStart: |
| 73 | + currentOpts.Start, err = strconv.ParseInt(e.Value, 10, 64) |
| 74 | + if err != nil { |
| 75 | + panic(pgerror.Wrapf(err, pgcode.Internal, "invalid sequence option value for %q", name)) |
| 76 | + } |
| 77 | + case tree.SeqOptVirtual: |
| 78 | + currentOpts.Virtual, err = strconv.ParseBool(e.Value) |
| 79 | + if err != nil { |
| 80 | + panic(pgerror.Wrapf(err, pgcode.Internal, "invalid sequence option value for %q", name)) |
| 81 | + } |
| 82 | + default: |
| 83 | + panic(pgerror.Newf(pgcode.Internal, "unexpected sequence option %q", name)) |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + // And the final state for the sequence options. |
| 88 | + updatedOpts := currentOpts |
| 89 | + if err := schemaexpr.AssignSequenceOptions(&updatedOpts, |
| 90 | + t.SeqOptions, |
| 91 | + 64, |
| 92 | + false, /* setDefaults */ |
| 93 | + nil, /* existingTypes */ |
| 94 | + ); err != nil { |
| 95 | + panic(pgerror.Wrap( |
| 96 | + err, |
| 97 | + pgcode.FeatureNotSupported, |
| 98 | + "", /* message */ |
| 99 | + )) |
| 100 | + } |
| 101 | + |
| 102 | + defaultOpts := schemaexpr.DefaultSequenceOptions() |
| 103 | + |
| 104 | + updateElement := func(key string, defaultValue, value interface{}) { |
| 105 | + newSeqOption := scpb.SequenceOption{ |
| 106 | + SequenceID: sequenceOwner.SequenceID, |
| 107 | + Key: key, |
| 108 | + Value: fmt.Sprintf("%v", value), |
| 109 | + } |
| 110 | + |
| 111 | + oldSeqOption := b.QueryByID(sequenceOwner.SequenceID).FilterSequenceOption().Filter(func(_ scpb.Status, _ scpb.TargetStatus, e *scpb.SequenceOption) bool { |
| 112 | + return e.Key == key |
| 113 | + }).MustGetZeroOrOneElement() |
| 114 | + if oldSeqOption != nil { |
| 115 | + // Skip a noop update. |
| 116 | + if oldSeqOption.Value == newSeqOption.Value { |
| 117 | + return |
| 118 | + } |
| 119 | + b.Drop(oldSeqOption) |
| 120 | + } |
| 121 | + |
| 122 | + // Skip setting to default values. |
| 123 | + if reflect.DeepEqual(defaultValue, value) { |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + b.Add(&newSeqOption) |
| 128 | + } |
| 129 | + |
| 130 | + var updateSequenceValue bool |
| 131 | + var restartWith int64 |
| 132 | + var useRestartWith bool |
| 133 | + for _, opt := range t.SeqOptions { |
| 134 | + switch name := opt.Name; name { |
| 135 | + case tree.SeqOptAs: |
| 136 | + updateElement(name, defaultOpts.AsIntegerType, updatedOpts.AsIntegerType) |
| 137 | + case tree.SeqOptCacheNode: |
| 138 | + updateElement(name, defaultOpts.NodeCacheSize, updatedOpts.NodeCacheSize) |
| 139 | + case tree.SeqOptCacheSession: |
| 140 | + updateElement(name, defaultOpts.SessionCacheSize, updatedOpts.SessionCacheSize) |
| 141 | + case tree.SeqOptIncrement: |
| 142 | + updateElement(name, defaultOpts.Increment, updatedOpts.Increment) |
| 143 | + updateSequenceValue = true |
| 144 | + case tree.SeqOptMinValue: |
| 145 | + updateElement(name, defaultOpts.MinValue, updatedOpts.MinValue) |
| 146 | + updateSequenceValue = true |
| 147 | + case tree.SeqOptMaxValue: |
| 148 | + updateElement(name, defaultOpts.MaxValue, updatedOpts.MaxValue) |
| 149 | + updateSequenceValue = true |
| 150 | + case tree.SeqOptStart: |
| 151 | + updateElement(name, defaultOpts.Start, updatedOpts.Start) |
| 152 | + updateSequenceValue = true |
| 153 | + case tree.SeqOptVirtual: |
| 154 | + updateElement(name, defaultOpts.Virtual, updatedOpts.Virtual) |
| 155 | + case tree.SeqOptRestart: |
| 156 | + useRestartWith = true |
| 157 | + if opt.IntVal != nil { |
| 158 | + restartWith = *opt.IntVal |
| 159 | + } else { |
| 160 | + restartWith = updatedOpts.Start |
| 161 | + } |
| 162 | + default: |
| 163 | + panic(fmt.Sprintf("unexpected sequence option: %q", name)) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if useRestartWith { |
| 168 | + restartSeqOption := scpb.SequenceOption{ |
| 169 | + SequenceID: sequenceOwner.SequenceID, |
| 170 | + Key: tree.SeqOptRestart, |
| 171 | + Value: fmt.Sprintf("%v", restartWith-updatedOpts.Increment), |
| 172 | + } |
| 173 | + b.AddTransient(&restartSeqOption) |
| 174 | + } else if updateSequenceValue { |
| 175 | + // TODO(21564): Update the sequence value when changes in options expose implementation. |
| 176 | + } |
| 177 | + |
| 178 | + var seqOptions tree.SequenceOptions |
| 179 | + if updatedOpts.SessionCacheSize != defaultOpts.SessionCacheSize { |
| 180 | + seqOptions = append(seqOptions, tree.SequenceOption{Name: tree.SeqOptCacheSession, IntVal: &updatedOpts.SessionCacheSize}) |
| 181 | + } |
| 182 | + if updatedOpts.NodeCacheSize != defaultOpts.NodeCacheSize { |
| 183 | + seqOptions = append(seqOptions, tree.SequenceOption{Name: tree.SeqOptCacheNode, IntVal: &updatedOpts.NodeCacheSize}) |
| 184 | + } |
| 185 | + if updatedOpts.MinValue != defaultOpts.MinValue { |
| 186 | + seqOptions = append(seqOptions, tree.SequenceOption{Name: tree.SeqOptMinValue, IntVal: &updatedOpts.MinValue}) |
| 187 | + } |
| 188 | + if updatedOpts.MaxValue != defaultOpts.MaxValue { |
| 189 | + seqOptions = append(seqOptions, tree.SequenceOption{Name: tree.SeqOptMaxValue, IntVal: &updatedOpts.MaxValue}) |
| 190 | + } |
| 191 | + if updatedOpts.Increment != defaultOpts.Increment { |
| 192 | + seqOptions = append(seqOptions, tree.SequenceOption{Name: tree.SeqOptIncrement, IntVal: &updatedOpts.Increment}) |
| 193 | + } |
| 194 | + if updatedOpts.Start != defaultOpts.Start { |
| 195 | + seqOptions = append(seqOptions, tree.SequenceOption{Name: tree.SeqOptStart, IntVal: &updatedOpts.Start}) |
| 196 | + } |
| 197 | + if updatedOpts.Virtual != defaultOpts.Virtual { |
| 198 | + seqOptions = append(seqOptions, tree.SequenceOption{Name: tree.SeqOptVirtual}) |
| 199 | + } |
| 200 | + seqOptionsString := strings.TrimSpace(tree.Serialize(&seqOptions)) |
| 201 | + |
| 202 | + columnType := b.QueryByID(tbl.TableID).FilterColumnType().Filter(func(_ scpb.Status, _ scpb.TargetStatus, e *scpb.ColumnType) bool { |
| 203 | + return e.ColumnID == sequenceOwner.ColumnID |
| 204 | + }).MustGetOneElement() |
| 205 | + |
| 206 | + if columnType.ElementCreationMetadata.In_26_1OrLater { |
| 207 | + oldColumnGeneratedAsIdentity := b.QueryByID(tbl.TableID).FilterColumnGeneratedAsIdentity().Filter(func(_ scpb.Status, _ scpb.TargetStatus, e *scpb.ColumnGeneratedAsIdentity) bool { |
| 208 | + return e.ColumnID == sequenceOwner.ColumnID |
| 209 | + }).MustGetOneElement() |
| 210 | + |
| 211 | + if oldColumnGeneratedAsIdentity.SequenceOption != seqOptionsString { |
| 212 | + b.Drop(oldColumnGeneratedAsIdentity) |
| 213 | + newColumnGeneratedAsIdentity := *oldColumnGeneratedAsIdentity |
| 214 | + newColumnGeneratedAsIdentity.SequenceOption = seqOptionsString |
| 215 | + b.Add(&newColumnGeneratedAsIdentity) |
| 216 | + } |
| 217 | + } else { |
| 218 | + if column.GeneratedAsIdentitySequenceOption != seqOptionsString { |
| 219 | + b.Drop(column) |
| 220 | + newColumn := *column |
| 221 | + newColumn.GeneratedAsIdentitySequenceOption = seqOptionsString |
| 222 | + b.Add(&newColumn) |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments