Skip to content

Commit d17f0de

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 d17f0de

File tree

34 files changed

+570
-199
lines changed

34 files changed

+570
-199
lines changed

pkg/ccl/logictestccl/tests/3node-tenant/generated_test.go

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

pkg/ccl/logictestccl/tests/local-read-committed/generated_test.go

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

pkg/ccl/logictestccl/tests/local-repeatable-read/generated_test.go

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

pkg/sql/alter_sequence.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func alterSequenceImpl(
148148
//
149149
// The code below handles the second case.
150150

151-
if opts.Increment < 0 && (oldIncrement != seqDesc.SequenceOpts.Increment || oldMinValue != seqDesc.SequenceOpts.MinValue) {
151+
if opts.Increment < 0 && (oldIncrement != seqDesc.SequenceOpts.Increment || seqDesc.SequenceOpts.MinValue < oldMinValue) {
152152
// Only get the sequence value from KV if it's needed.
153153
sequenceVal, err := getSequenceValue()
154154
if err != nil {

pkg/sql/catalog/schemaexpr/sequence_options.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,21 @@ func AssignSequenceOptions(
274274
}
275275
return nil
276276
}
277+
278+
// DefaultSequenceOptions is a helper that returns the default sequence options.
279+
// It panics on error.
280+
func DefaultSequenceOptions() descpb.TableDescriptor_SequenceOpts {
281+
defaultOpts := descpb.TableDescriptor_SequenceOpts{
282+
Increment: 1,
283+
}
284+
if err := AssignSequenceOptions(&defaultOpts,
285+
nil, /* optsNode */
286+
64,
287+
true, /* setDefaults */
288+
nil, /* existingTypes */
289+
); err != nil {
290+
panic(err)
291+
}
292+
293+
return defaultOpts
294+
}

pkg/sql/logictest/testdata/logic_test/alter_table

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4362,9 +4362,6 @@ RESET create_table_with_schema_locked
43624362

43634363
subtest end
43644364

4365-
statement ok
4366-
SET create_table_with_schema_locked=false
4367-
43684365
statement ok
43694366
CREATE TABLE t_alter_identity (id SERIAL PRIMARY KEY, a int GENERATED ALWAYS AS IDENTITY, b int GENERATED ALWAYS AS IDENTITY, z int);
43704367

@@ -4377,7 +4374,7 @@ ALTER TABLE t_alter_identity ALTER COLUMN b SET MAXVALUE 10 SET START WITH 11;
43774374
query error pq: START value \(5\) cannot be less than MINVALUE \(10\)
43784375
ALTER TABLE t_alter_identity ALTER COLUMN b SET MINVALUE 10 SET START WITH 5;
43794376

4380-
query error pq: column "z" of relation "t_alter_identity" is not an identity column
4377+
query error pq: cannot alter identity of a non-sequence column "z"
43814378
ALTER TABLE t_alter_identity ALTER COLUMN z SET START WITH 5;
43824379

43834380
# Verify sequence options are implemented on the identity
@@ -4401,7 +4398,7 @@ statement ok
44014398
INSERT INTO t_alter_identity DEFAULT VALUES;
44024399

44034400
statement ok
4404-
ALTER TABLE t_alter_identity ALTER COLUMN b SET MAXVALUE 40 RESTART WITH 20 SET CACHE 5 SET INCREMENT BY 2
4401+
ALTER TABLE t_alter_identity ALTER COLUMN b SET MAXVALUE 40 RESTART WITH 20 SET CACHE 5 SET INCREMENT BY 2;
44054402

44064403
statement ok
44074404
INSERT INTO t_alter_identity DEFAULT VALUES;
@@ -4415,8 +4412,83 @@ SELECT b from t_alter_identity ORDER BY b;
44154412
1
44164413
5
44174414
9
4418-
18
44194415
20
4416+
22
4417+
4418+
subtest issue-52552
4419+
4420+
statement ok
4421+
CREATE TABLE test_52552_asc (c1 int GENERATED ALWAYS AS IDENTITY);
4422+
ALTER TABLE test_52552_asc ALTER COLUMN c1 SET INCREMENT 3 SET MINVALUE 1 SET MAXVALUE 12;
4423+
ALTER TABLE test_52552_asc ALTER COLUMN c1 SET INCREMENT 8 SET MINVALUE 1 SET MAXVALUE 12;
4424+
INSERT INTO test_52552_asc DEFAULT VALUES;
4425+
4426+
query I
4427+
SELECT c1 FROM test_52552_asc
4428+
----
4429+
8
4430+
4431+
statement error pq: reached maximum value of sequence "test_52552_asc_c1_seq" \(12\)
4432+
INSERT INTO test_52552_asc DEFAULT VALUES;
4433+
4434+
statement error pq: reached maximum value of sequence "test_52552_asc_c1_seq" \(12\)
4435+
INSERT INTO test_52552_asc DEFAULT VALUES;
4436+
4437+
statement ok
4438+
ALTER TABLE test_52552_asc ALTER COLUMN c1 SET NO MAXVALUE;
4439+
INSERT INTO test_52552_asc DEFAULT VALUES;
4440+
4441+
query I nosort
4442+
SELECT c1 FROM test_52552_asc
4443+
----
4444+
8
4445+
32
4446+
4447+
4448+
statement ok
4449+
CREATE TABLE test_52552_desc (c1 int GENERATED ALWAYS AS IDENTITY);
4450+
ALTER TABLE test_52552_desc ALTER COLUMN c1 SET INCREMENT -5 SET MINVALUE 1 SET MAXVALUE 12;
4451+
ALTER TABLE test_52552_desc ALTER COLUMN c1 SET INCREMENT -8 SET MINVALUE 1 SET MAXVALUE 12;
4452+
INSERT INTO test_52552_desc DEFAULT VALUES;
4453+
4454+
query I
4455+
SELECT c1 FROM test_52552_desc
4456+
----
4457+
8
4458+
4459+
statement error pq: reached minimum value of sequence "test_52552_desc_seq" \(1\)
4460+
INSERT INTO test_52552_desc DEFAULT VALUES;
4461+
4462+
statement error pq: reached minimum value of sequence "test_52552_desc_seq" \(1\)
4463+
INSERT INTO test_52552_desc DEFAULT VALUES;
4464+
4465+
statement ok
4466+
ALTER TABLE test_52552_desc ALTER COLUMN c1 SET NO MINVALUE;
4467+
INSERT INTO test_52552_desc DEFAULT VALUES;
4468+
4469+
4470+
query I nosort
4471+
SELECT c1 FROM test_52552_desc
4472+
----
4473+
-4
4474+
4475+
4476+
#
4477+
# statement ok
4478+
# CREATE SEQUENCE test_52552_start INCREMENT BY 3 MINVALUE 1 MAXVALUE 100 START 50;
4479+
#
4480+
# statement ok
4481+
# ALTER SEQUENCE test_52552_start INCREMENT BY 8;
4482+
#
4483+
# query I
4484+
# SELECT nextval('test_52552_start');
4485+
# ----
4486+
# 50
4487+
4488+
subtest end
4489+
4490+
statement ok
4491+
SET create_table_with_schema_locked=false
44204492

44214493
statement ok
44224494
CREATE TABLE t_identity_drop (a int GENERATED ALWAYS AS IDENTITY (START WITH 10), b int GENERATED BY DEFAULT AS IDENTITY);

pkg/sql/logictest/tests/fakedist-disk/generated_test.go

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

pkg/sql/logictest/tests/fakedist-vec-off/generated_test.go

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

pkg/sql/logictest/tests/fakedist/generated_test.go

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

pkg/sql/logictest/tests/local-legacy-schema-changer/generated_test.go

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

0 commit comments

Comments
 (0)