-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathserial.go
195 lines (165 loc) · 6.75 KB
/
serial.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package sql
import (
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/sql/coltypes"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
// uniqueRowIDExpr is used as default expression when
// SessionNormalizationMode is SerialUsesRowID.
var uniqueRowIDExpr = &tree.FuncExpr{Func: tree.WrapFunction("unique_rowid")}
// realSequenceOpts (nil) is used when SessionNormalizationMode is
// SerialUsesSQLSequences.
var realSequenceOpts tree.SequenceOptions
// virtualSequenceOpts is used when SessionNormalizationMode is
// SerialUsesVirtualSequences.
var virtualSequenceOpts = tree.SequenceOptions{
tree.SequenceOption{Name: tree.SeqOptVirtual},
}
// processSerialInColumnDef analyzes a column definition and determines
// whether to use a sequence if the requested type is SERIAL-like.
// If a sequence must be created, it returns an ObjectName to use
// to create the new sequence and the DatabaseDescriptor of the
// parent database where it should be created.
// The ColumnTableDef is not mutated in-place; instead a new one is returned.
func (p *planner) processSerialInColumnDef(
ctx context.Context, d *tree.ColumnTableDef, tableName *ObjectName,
) (*tree.ColumnTableDef, *DatabaseDescriptor, *ObjectName, tree.SequenceOptions, error) {
t, ok := d.Type.(*coltypes.TSerial)
if !ok {
// Column is not SERIAL: nothing to do.
return d, nil, nil, nil, nil
}
if err := assertValidSerialColumnDef(d, tableName); err != nil {
return nil, nil, nil, nil, err
}
newSpec := *d
// Make the column non-nullable in all cases. PostgreSQL requires
// this.
newSpec.Nullable.Nullability = tree.NotNull
serialNormalizationMode := p.SessionData().SerialNormalizationMode
// Find the integer type that corresponds to the specification.
switch serialNormalizationMode {
case sessiondata.SerialUsesRowID, sessiondata.SerialUsesVirtualSequences:
// If unique_rowid() or virtual sequences are requested, we have
// no choice but to use the full-width integer type, no matter
// which serial size was requested, otherwise the values will not fit.
newSpec.Type = coltypes.Int8
case sessiondata.SerialUsesSQLSequences:
// With real sequences we can use exactly the requested type.
newSpec.Type = t.TInt
}
if serialNormalizationMode == sessiondata.SerialUsesRowID {
// We're not constructing a sequence for this SERIAL column.
// Use the "old school" CockroachDB default.
newSpec.DefaultExpr.Expr = uniqueRowIDExpr
return &newSpec, nil, nil, nil, nil
}
log.VEventf(ctx, 2, "creating sequence for new column %q of %q", d, tableName)
// We want a sequence; for this we need to generate a new sequence name.
// The constraint on the name is that an object of this name must not exist already.
seqName := tree.NewUnqualifiedTableName(
tree.Name(tableName.Table() + "_" + string(d.Name) + "_seq"))
// The first step in the search is to prepare the seqName to fill in
// the catalog/schema parent. This is what ResolveUncachedDatabase does.
//
// Here and below we skip the cache because name resolution using
// the cache does not work (well) if the txn retries and the
// descriptor was written already in an early txn attempt.
dbDesc, err := p.ResolveUncachedDatabase(ctx, seqName)
if err != nil {
return nil, nil, nil, nil, err
}
// Now skip over all names that are already taken.
nameBase := seqName.TableName
for i := 0; ; i++ {
if i > 0 {
seqName.TableName = tree.Name(fmt.Sprintf("%s%d", nameBase, i))
}
res, err := p.ResolveUncachedTableDescriptor(ctx, seqName, false /*required*/, anyDescType)
if err != nil {
return nil, nil, nil, nil, err
}
if res == nil {
break
}
}
defaultExpr := &tree.FuncExpr{
Func: tree.WrapFunction("nextval"),
Exprs: tree.Exprs{tree.NewStrVal(seqName.Table())},
}
seqType := ""
seqOpts := realSequenceOpts
if serialNormalizationMode == sessiondata.SerialUsesVirtualSequences {
seqType = "virtual "
seqOpts = virtualSequenceOpts
}
log.VEventf(ctx, 2, "new column %q of %q will have %ssequence name %q and default %q",
d, tableName, seqType, seqName, defaultExpr)
newSpec.DefaultExpr.Expr = defaultExpr
return &newSpec, dbDesc, seqName, seqOpts, nil
}
// SimplifySerialInColumnDefWithRowID analyzes a column definition and
// simplifies any use of SERIAL as if SerialNormalizationMode was set
// to SerialUsesRowID. No sequence needs to be created.
//
// This is currently used by bulk I/O import statements which do not
// (yet?) support customization of the SERIAL behavior.
func SimplifySerialInColumnDefWithRowID(
ctx context.Context, d *tree.ColumnTableDef, tableName *ObjectName,
) error {
if _, ok := d.Type.(*coltypes.TSerial); !ok {
// Column is not SERIAL: nothing to do.
return nil
}
if err := assertValidSerialColumnDef(d, tableName); err != nil {
return err
}
// Make the column non-nullable in all cases. PostgreSQL requires
// this.
d.Nullable.Nullability = tree.NotNull
// We're not constructing a sequence for this SERIAL column.
// Use the "old school" CockroachDB default.
d.Type = coltypes.Int8
d.DefaultExpr.Expr = uniqueRowIDExpr
return nil
}
func assertValidSerialColumnDef(d *tree.ColumnTableDef, tableName *ObjectName) error {
if d.HasDefaultExpr() {
// SERIAL implies a new default expression, we can't have one to
// start with. This is the error produced by pg in such case.
return pgerror.NewErrorf(pgerror.CodeSyntaxError,
"multiple default values specified for column %q of table %q",
tree.ErrString(&d.Name), tree.ErrString(tableName))
}
if d.Nullable.Nullability == tree.Null {
// SERIAL implies a non-NULL column, we can't accept a nullability
// spec. This is the error produced by pg in such case.
return pgerror.NewErrorf(pgerror.CodeSyntaxError,
"conflicting NULL/NOT NULL declarations for column %q of table %q",
tree.ErrString(&d.Name), tree.ErrString(tableName))
}
if d.Computed.Expr != nil {
// SERIAL cannot be a computed column.
return pgerror.NewErrorf(pgerror.CodeSyntaxError,
"SERIAL column %q of table %q cannot be computed",
tree.ErrString(&d.Name), tree.ErrString(tableName))
}
return nil
}