Skip to content

Commit

Permalink
sql: make sequence integer bound consistent with default_int_size
Browse files Browse the repository at this point in the history
Previously, the default bound of sequence are always
`math.MaxInt64` or `math.MinInt64` (depending on the sequence's order). This
is inconsistent with the cluster setting `default_int_size`. This commit is
to fix it.

fixes #84554

Release note (bug fix): make sequence integer bound consistent with the
cluster setting `default_int_size`.
  • Loading branch information
ZhouXing19 committed Aug 3, 2022
1 parent e52f732 commit 45198c9
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 18 deletions.
22 changes: 13 additions & 9 deletions pkg/sql/catalog/schemaexpr/sequence_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
// descriptor to a descpb.TableDescriptor_SequenceOpts.
// Note that this function is used to acquire the sequence option for the
// information schema table, so it doesn't parse for the sequence owner info.
func ParseSequenceOpts(s string) (*descpb.TableDescriptor_SequenceOpts, error) {
func ParseSequenceOpts(
s string, defaultIntSize int32,
) (*descpb.TableDescriptor_SequenceOpts, error) {
stmt, err := parser.ParseOne("CREATE SEQUENCE fake_seq " + s)
if err != nil {
return nil, errors.Wrap(err, "cannot parse sequence option")
Expand All @@ -42,6 +44,7 @@ func ParseSequenceOpts(s string) (*descpb.TableDescriptor_SequenceOpts, error) {
if err := AssignSequenceOptions(
opts,
createSeqNode.Options,
defaultIntSize,
true, /* setDefaults */
nil, /* existingType */
); err != nil {
Expand Down Expand Up @@ -125,13 +128,14 @@ func setSequenceIntegerBounds(
func AssignSequenceOptions(
opts *descpb.TableDescriptor_SequenceOpts,
optsNode tree.SequenceOptions,
defaultIntSize int32,
setDefaults bool,
existingType *types.T,
) error {
wasAscending := opts.Increment > 0

// Set the default integer type of a sequence.
var integerType = types.Int
integerType := parser.NakedIntTypeFromDefaultIntSize(defaultIntSize)
// All other defaults are dependent on the value of increment
// and the AS integerType. (i.e. whether the sequence is ascending
// or descending, bigint vs. smallint)
Expand All @@ -148,25 +152,25 @@ func AssignSequenceOptions(
}
isAscending := opts.Increment > 0

lowerIntBound, upperIntBound, err := getSequenceIntegerBounds(integerType)
if err != nil {
return err
}

// Set increment-dependent defaults.
if setDefaults {
if isAscending {
opts.MinValue = 1
opts.MaxValue = math.MaxInt64
opts.MaxValue = upperIntBound
opts.Start = opts.MinValue
} else {
opts.MinValue = math.MinInt64
opts.MinValue = lowerIntBound
opts.MaxValue = -1
opts.Start = opts.MaxValue
}
opts.CacheSize = 1
}

lowerIntBound, upperIntBound, err := getSequenceIntegerBounds(integerType)
if err != nil {
return err
}

// Set default MINVALUE and MAXVALUE if AS option value for integer type is specified.
if opts.AsIntegerType != "" {
// We change MINVALUE and MAXVALUE if it is the originally set to the default during ALTER.
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/table_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ type Column interface {
// If the column is not an identity column, return nil for both sequence option
// and the error.
// Note it doesn't return the sequence owner info.
GetGeneratedAsIdentitySequenceOption() (*descpb.TableDescriptor_SequenceOpts, error)
GetGeneratedAsIdentitySequenceOption(defaultIntSize int32) (*descpb.TableDescriptor_SequenceOpts, error)
}

// ConstraintToUpdate is an interface around a constraint mutation.
Expand Down
9 changes: 4 additions & 5 deletions pkg/sql/catalog/tabledesc/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,13 @@ func (w column) GetGeneratedAsIdentitySequenceOptionStr() string {
// If the column is not an identity column, return nil for both sequence option
// and the error.
// Note it doesn't return the sequence owner info.
func (w column) GetGeneratedAsIdentitySequenceOption() (
*descpb.TableDescriptor_SequenceOpts,
error,
) {
func (w column) GetGeneratedAsIdentitySequenceOption(
defaultIntSize int32,
) (*descpb.TableDescriptor_SequenceOpts, error) {
if !w.HasGeneratedAsIdentitySequenceOption() {
return nil, nil
}
seqOpts, err := schemaexpr.ParseSequenceOpts(*w.desc.GeneratedAsIdentitySequenceOption)
seqOpts, err := schemaexpr.ParseSequenceOpts(*w.desc.GeneratedAsIdentitySequenceOption, defaultIntSize)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ https://www.postgresql.org/docs/9.5/infoschema-columns.html`,
identityIncrement := tree.DNull
identityMax := tree.DNull
identityMin := tree.DNull
generatedAsIdentitySeqOpt, err := column.GetGeneratedAsIdentitySequenceOption()
generatedAsIdentitySeqOpt, err := column.GetGeneratedAsIdentitySequenceOption(p.SessionData().DefaultIntSize)
if err != nil {
return err
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -5020,3 +5020,38 @@ query TTTTT
select identity_start, identity_increment, identity_maximum, identity_minimum, identity_cycle from information_schema.columns where table_name = 't' and column_name='id27';
----
6 3 12 6 NULL


subtest get_seq_opt_with_default_int_size

statement ok
DROP TABLE IF EXISTS t

statement ok
SET default_int_size=4

statement ok
CREATE TABLE t (
id1 INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10)
)

query TTTTT
select identity_start, identity_increment, identity_maximum, identity_minimum, identity_cycle from information_schema.columns where table_name = 't' and column_name='id1';
----
10 1 2147483647 1 NULL

statement ok
DROP TABLE IF EXISTS t

statement ok
SET default_int_size=8

statement ok
CREATE TABLE t (
id1 INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10)
)

query TTTTT
select identity_start, identity_increment, identity_maximum, identity_minimum, identity_cycle from information_schema.columns where table_name = 't' and column_name='id1';
----
10 1 9223372036854775807 1 NULL
22 changes: 20 additions & 2 deletions pkg/sql/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,28 @@ func assignSequenceOptions(
if err := checkDupSeqOption(optsNode); err != nil {
return err
}
if err := schemaexpr.AssignSequenceOptions(opts, optsNode, setDefaults, existingType); err != nil {

defaultIntSize := int32(64)
if p != nil && p.SessionData() != nil {
defaultIntSize = p.SessionData().DefaultIntSize
}
if err := schemaexpr.AssignSequenceOptions(
opts,
optsNode,
defaultIntSize,
setDefaults,
existingType,
); err != nil {
return pgerror.WithCandidateCode(err, pgcode.InvalidParameterValue)
}
if err := assignSequenceOwner(ctx, p, opts, optsNode, sequenceID, sequenceParentID); err != nil {
if err := assignSequenceOwner(
ctx,
p,
opts,
optsNode,
sequenceID,
sequenceParentID,
); err != nil {
return pgerror.WithCandidateCode(err, pgcode.InvalidParameterValue)
}
return nil
Expand Down

0 comments on commit 45198c9

Please sign in to comment.