Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/sql/alter_sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func alterSequenceImpl(
//
// The code below handles the second case.

if opts.Increment < 0 && (oldIncrement != seqDesc.SequenceOpts.Increment || oldMinValue != seqDesc.SequenceOpts.MinValue) {
if opts.Increment < 0 && (oldIncrement != seqDesc.SequenceOpts.Increment || seqDesc.SequenceOpts.MinValue < oldMinValue) {
// Only get the sequence value from KV if it's needed.
sequenceVal, err := getSequenceValue()
if err != nil {
Expand Down Expand Up @@ -209,13 +209,13 @@ func alterSequenceImpl(
}
}
if restartVal != nil {
// Using RESTART on a sequence should always cause the operation to run
// in the current transaction. This is achieved by treating the sequence
// as if it were just created.
if err := params.p.createdSequences.addCreatedSequence(seqDesc.ID); err != nil {
return err
var err error
if opts.Increment != 0 {
err = setSequenceVal(*restartVal - opts.Increment)
} else {
err = setSequenceVal(*restartVal - oldIncrement)
}
if err := params.p.SetSequenceValueByID(params.ctx, uint32(seqDesc.ID), *restartVal, false); err != nil {
if err != nil {
return err
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
gojson "encoding/json"
"fmt"
"sort"
"strings"
"time"

"github.com/cockroachdb/cockroach/pkg/build"
Expand Down Expand Up @@ -1267,7 +1268,7 @@ func applyColumnMutation(
return err
}

// Alter referenced sequence for identity with sepcified option.
// Alter referenced sequence for identity with specified option.
// Does not override existing values if not specified.
if err := alterSequenceImpl(params, seqDesc, t.SeqOptions, t); err != nil {
return err
Expand All @@ -1288,7 +1289,7 @@ func applyColumnMutation(
if opts.Virtual {
optsNode = append(optsNode, tree.SequenceOption{Name: tree.SeqOptVirtual})
}
s := tree.Serialize(&optsNode)
s := strings.TrimSpace(tree.Serialize(&optsNode))
col.ColumnDesc().GeneratedAsIdentitySequenceOption = &s

case *tree.AlterTableDropIdentity:
Expand Down
18 changes: 18 additions & 0 deletions pkg/sql/catalog/schemaexpr/sequence_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,21 @@ func AssignSequenceOptions(
}
return nil
}

// DefaultSequenceOptions is a helper that returns the default sequence options.
// It panics on error.
func DefaultSequenceOptions() descpb.TableDescriptor_SequenceOpts {
defaultOpts := descpb.TableDescriptor_SequenceOpts{
Increment: 1,
}
if err := AssignSequenceOptions(&defaultOpts,
nil, /* optsNode */
64,
true, /* setDefaults */
nil, /* existingTypes */
); err != nil {
panic(err)
}

return defaultOpts
}
72 changes: 70 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -4401,7 +4401,7 @@ statement ok
INSERT INTO t_alter_identity DEFAULT VALUES;

statement ok
ALTER TABLE t_alter_identity ALTER COLUMN b SET MAXVALUE 40 RESTART WITH 20 SET CACHE 5 SET INCREMENT BY 2
ALTER TABLE t_alter_identity ALTER COLUMN b SET MAXVALUE 40 RESTART WITH 20 SET CACHE 5 SET INCREMENT BY 2;

statement ok
INSERT INTO t_alter_identity DEFAULT VALUES;
Expand All @@ -4415,8 +4415,76 @@ SELECT b from t_alter_identity ORDER BY b;
1
5
9
18
20
22

# TODO(21564): Enable this test when `ALTER COLUMN` on options updates the value of a sequence
# subtest issue-52552
#
# statement ok
# CREATE TABLE test_52552_asc (c1 int GENERATED ALWAYS AS IDENTITY);
# ALTER TABLE test_52552_asc ALTER COLUMN c1 SET INCREMENT 3 SET MINVALUE 1 SET MAXVALUE 12;
# ALTER TABLE test_52552_asc ALTER COLUMN c1 SET INCREMENT 8 SET MINVALUE 1 SET MAXVALUE 12;
# INSERT INTO test_52552_asc DEFAULT VALUES;
#
# query I
# SELECT c1 FROM test_52552_asc
# ----
# 8
#
# statement error pq: reached maximum value of sequence "test_52552_asc_c1_seq" \(12\)
# INSERT INTO test_52552_asc DEFAULT VALUES;
#
# statement error pq: reached maximum value of sequence "test_52552_asc_c1_seq" \(12\)
# INSERT INTO test_52552_asc DEFAULT VALUES;
#
# statement ok
# ALTER TABLE test_52552_asc ALTER COLUMN c1 SET NO MAXVALUE;
# INSERT INTO test_52552_asc DEFAULT VALUES;
#
# query I nosort
# SELECT c1 FROM test_52552_asc
# ----
# 8
# 32
#
# statement ok
# CREATE TABLE test_52552_desc (c1 int GENERATED ALWAYS AS IDENTITY);
# ALTER TABLE test_52552_desc ALTER COLUMN c1 SET INCREMENT -5 SET MINVALUE 1 SET MAXVALUE 12;
# ALTER TABLE test_52552_desc ALTER COLUMN c1 SET INCREMENT -8 SET MINVALUE 1 SET MAXVALUE 12;
# INSERT INTO test_52552_desc DEFAULT VALUES;
#
# query I
# SELECT c1 FROM test_52552_desc
# ----
# 8
#
# statement error pq: reached minimum value of sequence "test_52552_desc_seq" \(1\)
# INSERT INTO test_52552_desc DEFAULT VALUES;
#
# statement error pq: reached minimum value of sequence "test_52552_desc_seq" \(1\)
# INSERT INTO test_52552_desc DEFAULT VALUES;
#
# statement ok
# ALTER TABLE test_52552_desc ALTER COLUMN c1 SET NO MINVALUE;
# INSERT INTO test_52552_desc DEFAULT VALUES;
#
# query I nosort
# SELECT c1 FROM test_52552_desc
# ----
# -4
#
# statement ok
# CREATE TABLE test_52552_start INCREMENT BY 3 MINVALUE 1 MAXVALUE 100 START 50;
# ALTER SEQUENCE test_52552_start INCREMENT BY 8;
# INSERT INTO test_52552_desc DEFAULT VALUES;
#
# query I
# SELECT nextval('test_52552_start');
# ----
# 50
#
# subtest end

statement ok
CREATE TABLE t_identity_drop (a int GENERATED ALWAYS AS IDENTITY (START WITH 10), b int GENERATED BY DEFAULT AS IDENTITY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
"alter_table_alter_column_add_identity.go",
"alter_table_alter_column_drop_not_null.go",
"alter_table_alter_column_drop_stored.go",
"alter_table_alter_column_identity.go",
"alter_table_alter_column_set_default.go",
"alter_table_alter_column_set_identity.go",
"alter_table_alter_column_set_not_null.go",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var supportedAlterTableStatements = map[reflect.Type]supportedAlterTableCommand{
reflect.TypeOf((*tree.AlterTableRenameConstraint)(nil)): {fn: alterTableRenameConstraint, on: true, checks: isV261Active},
reflect.TypeOf((*tree.AlterTableSetIdentity)(nil)): {fn: alterTableSetIdentity, on: true, checks: isV261Active},
reflect.TypeOf((*tree.AlterTableAddIdentity)(nil)): {fn: alterTableAddIdentity, on: true, checks: isV261Active},
reflect.TypeOf((*tree.AlterTableIdentity)(nil)): {fn: alterTableAlterColumnIdentity, on: true, checks: isV261Active},
}

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func alterTableAddColumn(
seqOptions = *ptr
}
// Versions from 26.1 GeneratedAsIdentity will have a separate element for
// GeneratedAsIdentity. Older versios store it in the column element.
// GeneratedAsIdentity. Older versions store it in the column element.
if spec.colType.ElementCreationMetadata.In_26_1OrLater {
spec.generatedAsID = &scpb.ColumnGeneratedAsIdentity{
TableID: tbl.TableID,
Expand Down
Loading
Loading