-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathcreate_sequence.go
204 lines (177 loc) · 6.04 KB
/
create_sequence.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
196
197
198
199
200
201
202
203
204
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql
import (
"context"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
)
type createSequenceNode struct {
n *tree.CreateSequence
dbDesc *sqlbase.DatabaseDescriptor
}
func (p *planner) CreateSequence(ctx context.Context, n *tree.CreateSequence) (planNode, error) {
dbDesc, err := p.ResolveUncachedDatabase(ctx, &n.Name)
if err != nil {
return nil, err
}
if err := p.CheckPrivilege(ctx, dbDesc, privilege.CREATE); err != nil {
return nil, err
}
return &createSequenceNode{
n: n,
dbDesc: dbDesc,
}, nil
}
func (n *createSequenceNode) startExec(params runParams) error {
// TODO(arul): Allow temporary sequences once temp tables work for regular tables.
if n.n.Temporary {
return unimplemented.NewWithIssuef(5807,
"temporary sequences are unsupported")
}
exists, _, err := sqlbase.LookupPublicTableID(params.ctx, params.p.txn, n.dbDesc.ID, n.n.Name.Table())
if err == nil && exists {
if n.n.IfNotExists {
// If the sequence exists but the user specified IF NOT EXISTS, return
// without doing anything.
return nil
}
return sqlbase.NewRelationAlreadyExistsError(n.n.Name.Table())
} else if err != nil {
return err
}
return doCreateSequence(params, n.n.String(), n.dbDesc, &n.n.Name, n.n.Options)
}
// doCreateSequence performs the creation of a sequence in KV. The
// context argument is a string to use in the event log.
func doCreateSequence(
params runParams,
context string,
dbDesc *DatabaseDescriptor,
name *ObjectName,
opts tree.SequenceOptions,
) error {
// All the intermediate schema update stages of a DDL statement will
// want to see their previous writes. Disable step-wise execution
// for that phase.
if err := params.p.Txn().DisableStepping(); err != nil {
return err
}
id, err := GenerateUniqueDescID(params.ctx, params.p.ExecCfg().DB)
if err != nil {
return err
}
// Inherit permissions from the database descriptor.
privs := dbDesc.GetPrivileges()
desc, err := MakeSequenceTableDesc(name.Table(), opts,
dbDesc.ID, id, params.creationTimeForNewTableDescriptor(), privs, ¶ms)
if err != nil {
return err
}
// makeSequenceTableDesc already validates the table. No call to
// desc.ValidateTable() needed here.
key := sqlbase.MakePublicTableNameKey(params.ctx, params.ExecCfg().Settings,
dbDesc.ID, name.Table()).Key()
if err = params.p.createDescriptorWithID(params.ctx, key, id, &desc, params.EvalContext().Settings); err != nil {
return err
}
// Initialize the sequence value.
seqValueKey := keys.MakeSequenceKey(uint32(id))
b := &client.Batch{}
b.Inc(seqValueKey, desc.SequenceOpts.Start-desc.SequenceOpts.Increment)
if err := params.p.txn.Run(params.ctx, b); err != nil {
return err
}
if err := desc.Validate(params.ctx, params.p.txn); err != nil {
return err
}
// The event logging wants to operate in step-wise execution. Mark a
// sequence point now that the descriptor exists.
if err := params.p.Txn().Step(); err != nil {
return err
}
// Log Create Sequence event. This is an auditable log event and is
// recorded in the same transaction as the table descriptor update.
return MakeEventLogger(params.extendedEvalCtx.ExecCfg).InsertEventRecord(
params.ctx,
params.p.txn,
EventLogCreateSequence,
int32(desc.ID),
int32(params.extendedEvalCtx.NodeID),
struct {
SequenceName string
Statement string
User string
}{name.FQString(), context, params.SessionData().User},
)
}
func (*createSequenceNode) Next(runParams) (bool, error) { return false, nil }
func (*createSequenceNode) Values() tree.Datums { return tree.Datums{} }
func (*createSequenceNode) Close(context.Context) {}
const (
sequenceColumnID = 1
sequenceColumnName = "value"
)
// MakeSequenceTableDesc creates a sequence descriptor.
func MakeSequenceTableDesc(
sequenceName string,
sequenceOptions tree.SequenceOptions,
parentID sqlbase.ID,
id sqlbase.ID,
creationTime hlc.Timestamp,
privileges *sqlbase.PrivilegeDescriptor,
params *runParams,
) (sqlbase.MutableTableDescriptor, error) {
desc := InitTableDescriptor(id, parentID, keys.PublicSchemaID, sequenceName, creationTime,
privileges, false /* temporary */)
// Mimic a table with one column, "value".
desc.Columns = []sqlbase.ColumnDescriptor{
{
ID: 1,
Name: sequenceColumnName,
Type: *types.Int,
},
}
desc.PrimaryIndex = sqlbase.IndexDescriptor{
ID: keys.SequenceIndexID,
Name: sqlbase.PrimaryKeyIndexName,
ColumnIDs: []sqlbase.ColumnID{sqlbase.ColumnID(1)},
ColumnNames: []string{sequenceColumnName},
ColumnDirections: []sqlbase.IndexDescriptor_Direction{sqlbase.IndexDescriptor_ASC},
}
desc.Families = []sqlbase.ColumnFamilyDescriptor{
{
ID: keys.SequenceColumnFamilyID,
ColumnIDs: []sqlbase.ColumnID{1},
ColumnNames: []string{sequenceColumnName},
Name: "primary",
DefaultColumnID: sequenceColumnID,
},
}
// Fill in options, starting with defaults then overriding.
opts := &sqlbase.TableDescriptor_SequenceOpts{
Increment: 1,
}
err := assignSequenceOptions(opts, sequenceOptions, true /* setDefaults */, params, id)
if err != nil {
return desc, err
}
desc.SequenceOpts = opts
// A sequence doesn't have dependencies and thus can be made public
// immediately.
desc.State = sqlbase.TableDescriptor_PUBLIC
return desc, desc.ValidateTable()
}