Skip to content

Commit

Permalink
sql: fix ALTER SEQUENCE ... AS ... defaults
Browse files Browse the repository at this point in the history
Previously, ALTER SEQUENCE ... AS ... can change MINVALUE and MAXVALUE to
the new type value. This is now preserved and errors if values are out
of range.

Release note: None
  • Loading branch information
otan committed Nov 17, 2021
1 parent ae846cb commit a411384
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 51 deletions.
28 changes: 24 additions & 4 deletions pkg/sql/alter_sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
"github.com/cockroachdb/errors"
)

type alterSequenceNode struct {
Expand Down Expand Up @@ -66,10 +68,28 @@ func (n *alterSequenceNode) startExec(params runParams) error {
oldMinValue := desc.SequenceOpts.MinValue
oldMaxValue := desc.SequenceOpts.MaxValue

err := assignSequenceOptions(
desc.SequenceOpts, n.n.Options, false /* setDefaults */, &params, desc.GetID(), desc.ParentID,
)
if err != nil {
existingType := types.Int
if desc.GetSequenceOpts().AsIntegerType != "" {
switch desc.GetSequenceOpts().AsIntegerType {
case types.Int2.SQLString():
existingType = types.Int2
case types.Int4.SQLString():
existingType = types.Int4
case types.Int.SQLString():
// Already set.
default:
return errors.AssertionFailedf("sequence has unexpected type %s", desc.GetSequenceOpts().AsIntegerType)
}
}
if err := assignSequenceOptions(
desc.SequenceOpts,
n.n.Options,
false, /* setDefaults */
&params,
desc.GetID(),
desc.ParentID,
existingType,
); err != nil {
return err
}
opts := desc.SequenceOpts
Expand Down
11 changes: 9 additions & 2 deletions pkg/sql/create_sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,15 @@ func NewSequenceTableDesc(
opts := &descpb.TableDescriptor_SequenceOpts{
Increment: 1,
}
err := assignSequenceOptions(opts, sequenceOptions, true /* setDefaults */, params, id, parentID)
if err != nil {
if err := assignSequenceOptions(
opts,
sequenceOptions,
true, /* setDefaults */
params,
id,
parentID,
nil, /* existingType */
); err != nil {
return nil, err
}
desc.SequenceOpts = opts
Expand Down
154 changes: 154 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_sequence
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,157 @@ query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE foo]
----
CREATE SEQUENCE public.foo MINVALUE 1 MAXVALUE 9223372036854775807 INCREMENT 5 START 1

user root

# Alter between sequences, check it is ok.
statement ok
CREATE SEQUENCE seq_as AS int2

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_as]
----
CREATE SEQUENCE public.seq_as AS INT2 MINVALUE 1 MAXVALUE 32767 INCREMENT 1 START 1

statement ok
ALTER SEQUENCE seq_as AS int4

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_as]
----
CREATE SEQUENCE public.seq_as AS INT4 MINVALUE 1 MAXVALUE 2147483647 INCREMENT 1 START 1

statement ok
ALTER SEQUENCE seq_as AS int8

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_as]
----
CREATE SEQUENCE public.seq_as AS INT8 MINVALUE 1 MAXVALUE 9223372036854775807 INCREMENT 1 START 1

statement ok
ALTER SEQUENCE seq_as AS int4

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_as]
----
CREATE SEQUENCE public.seq_as AS INT4 MINVALUE 1 MAXVALUE 2147483647 INCREMENT 1 START 1

statement ok
ALTER SEQUENCE seq_as AS int2

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_as]
----
CREATE SEQUENCE public.seq_as AS INT2 MINVALUE 1 MAXVALUE 32767 INCREMENT 1 START 1

# Test ALTER SEQUENCE AS when downgrading sizes behaves appropriately if MINVALUE/MAXVALUE/START is set.

statement ok
CREATE SEQUENCE seq_int4_max_high AS int4 MAXVALUE 99999

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_int4_max_high]
----
CREATE SEQUENCE public.seq_int4_max_high AS INT4 MINVALUE 1 MAXVALUE 99999 INCREMENT 1 START 1

statement error MAXVALUE \(99999\) must be less than \(32767\) for type INT2
ALTER SEQUENCE seq_int4_max_high AS int2

statement ok
ALTER SEQUENCE seq_int4_max_high AS int8

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_int4_max_high]
----
CREATE SEQUENCE public.seq_int4_max_high AS INT8 MINVALUE 1 MAXVALUE 99999 INCREMENT 1 START 1

statement ok
CREATE SEQUENCE seq_int4_min_high AS int4 MINVALUE 99999

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_int4_min_high]
----
CREATE SEQUENCE public.seq_int4_min_high AS INT4 MINVALUE 99999 MAXVALUE 2147483647 INCREMENT 1 START 99999

statement error MINVALUE \(99999\) must be less than \(32767\) for type INT2
ALTER SEQUENCE seq_int4_min_high AS int2

statement ok
ALTER SEQUENCE seq_int4_min_high AS int8

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_int4_min_high]
----
CREATE SEQUENCE public.seq_int4_min_high AS INT8 MINVALUE 99999 MAXVALUE 9223372036854775807 INCREMENT 1 START 99999

statement ok
CREATE SEQUENCE seq_int4_start_high AS int4 START 99999

statement error START value \(99999\) cannot be greater than MAXVALUE \(32767\)
ALTER SEQUENCE seq_int4_start_high AS int2

statement ok
ALTER SEQUENCE seq_int4_start_high AS int8

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_int4_start_high]
----
CREATE SEQUENCE public.seq_int4_start_high AS INT8 MINVALUE 1 MAXVALUE 9223372036854775807 INCREMENT 1 START 99999

statement ok
CREATE SEQUENCE seq_int4_min_low AS int4 MINVALUE -99999

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_int4_min_low]
----
CREATE SEQUENCE public.seq_int4_min_low AS INT4 MINVALUE -99999 MAXVALUE 2147483647 INCREMENT 1 START -99999

statement error MINVALUE \(-99999\) must be greater than \(-32768\) for type INT2
ALTER SEQUENCE seq_int4_min_low AS int2

statement ok
ALTER SEQUENCE seq_int4_min_low AS int8

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_int4_min_low]
----
CREATE SEQUENCE public.seq_int4_min_low AS INT8 MINVALUE -99999 MAXVALUE 9223372036854775807 INCREMENT 1 START -99999

statement ok
CREATE SEQUENCE seq_int4_max_high_desc AS int4 MAXVALUE 99999 INCREMENT -1

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_int4_max_high_desc]
----
CREATE SEQUENCE public.seq_int4_max_high_desc AS INT4 MINVALUE -2147483648 MAXVALUE 99999 INCREMENT -1 START 99999

statement error MAXVALUE \(99999\) must be less than \(32767\) for type INT2
ALTER SEQUENCE seq_int4_max_high_desc AS int2

statement ok
ALTER SEQUENCE seq_int4_max_high_desc AS int8

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_int4_max_high_desc]
----
CREATE SEQUENCE public.seq_int4_max_high_desc AS INT8 MINVALUE -9223372036854775808 MAXVALUE 99999 INCREMENT -1 START 99999

statement ok
CREATE SEQUENCE seq_int4_min_high_desc AS int4 MINVALUE -99999 INCREMENT -1

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_int4_min_high_desc]
----
CREATE SEQUENCE public.seq_int4_min_high_desc AS INT4 MINVALUE -99999 MAXVALUE -1 INCREMENT -1 START -1

statement error MINVALUE \(-99999\) must be greater than \(-32768\) for type INT2
ALTER SEQUENCE seq_int4_min_high_desc AS int2

statement ok
ALTER SEQUENCE seq_int4_min_high_desc AS int8

query T
SELECT create_statement FROM [SHOW CREATE SEQUENCE seq_int4_min_high_desc]
----
CREATE SEQUENCE public.seq_int4_min_high_desc AS INT8 MINVALUE -99999 MAXVALUE -1 INCREMENT -1 START -1
Loading

0 comments on commit a411384

Please sign in to comment.