-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathcreate_sequence.go
More file actions
204 lines (199 loc) · 6.37 KB
/
Copy pathcreate_sequence.go
File metadata and controls
204 lines (199 loc) · 6.37 KB
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 2023 Dolthub, Inc.
//
// 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 ast
import (
"math"
"github.com/cockroachdb/errors"
vitess "github.com/dolthub/vitess/go/vt/sqlparser"
"github.com/dolthub/doltgresql/core/id"
"github.com/dolthub/doltgresql/core/sequences"
"github.com/dolthub/doltgresql/postgres/parser/sem/tree"
pgnodes "github.com/dolthub/doltgresql/server/node"
pgtypes "github.com/dolthub/doltgresql/server/types"
)
// nodeCreateSequence handles *tree.CreateSequence nodes.
func nodeCreateSequence(ctx *Context, node *tree.CreateSequence) (vitess.Statement, error) {
if node == nil {
return nil, nil
}
if node.Persistence.IsTemporary() {
return nil, errors.Errorf("temporary sequences are not yet supported")
}
if node.Persistence.IsUnlogged() {
return nil, errors.Errorf("unlogged sequences are not yet supported")
}
name, err := nodeTableName(ctx, &node.Name)
if err != nil {
return nil, err
}
if len(name.DbQualifier.String()) > 0 {
return nil, errors.Errorf("CREATE SEQUENCE for the non-current database is not yet supported")
}
// Read all options and check whether they've been set (if not, we'll use the defaults)
minValueLimit := int64(math.MinInt64)
maxValueLimit := int64(math.MaxInt64)
increment := int64(1)
var minValue int64
var maxValue int64
var start int64
var dataType *pgtypes.DoltgresType
var ownerTableName string
var ownerColumnName string
minValueSet := false
maxValueSet := false
incrementSet := false
startSet := false
cycle := false
fromAlter := false
for _, option := range node.Options {
switch option.Name {
case tree.SeqOptAs:
if !dataType.IsEmptyType() {
return nil, errors.Errorf("conflicting or redundant options")
}
_, dataType, err = nodeResolvableTypeReference(ctx, option.AsType)
if err != nil {
return nil, err
}
switch dataType.ID {
case pgtypes.Int16.ID:
minValueLimit = int64(math.MinInt16)
maxValueLimit = int64(math.MaxInt16)
case pgtypes.Int32.ID:
minValueLimit = int64(math.MinInt32)
maxValueLimit = int64(math.MaxInt32)
case pgtypes.Int64.ID:
minValueLimit = int64(math.MinInt64)
maxValueLimit = int64(math.MaxInt64)
default:
return nil, errors.Errorf("sequence type must be smallint, integer, or bigint")
}
case tree.SeqOptCycle:
cycle = true
case tree.SeqOptNoCycle:
cycle = false
case tree.SeqOptOwnedBy:
expr, err := nodeExpr(ctx, option.ColumnItemVal)
if err != nil {
return nil, err
}
colName, ok := expr.(*vitess.ColName)
if !ok {
return nil, errors.New("expected sequence owner to be a table and column name")
}
if colName.Qualifier.SchemaQualifier.String() != name.SchemaQualifier.String() {
return nil, errors.New("CREATE SEQUENCE must use the same schema for the sequence and owned table")
}
if len(colName.Qualifier.DbQualifier.String()) > 0 {
return nil, errors.New("database specification is not yet supported for sequences")
}
ownerTableName = colName.Qualifier.Name.String()
ownerColumnName = colName.Name.String()
case tree.SeqOptCache:
// TODO: implement caching
if *option.IntVal != 1 {
return nil, errors.Errorf("sequence caching for values other than 1 are not yet supported")
}
case tree.SeqOptIncrement:
increment = *option.IntVal
if incrementSet {
return nil, errors.Errorf("conflicting or redundant options")
}
if increment == 0 {
return nil, errors.Errorf("INCREMENT must not be zero")
}
incrementSet = true
case tree.SeqOptMinValue:
if option.IntVal != nil {
minValue = *option.IntVal
if minValueSet {
return nil, errors.Errorf("conflicting or redundant options")
}
minValueSet = true
}
case tree.SeqOptMaxValue:
if option.IntVal != nil {
maxValue = *option.IntVal
if maxValueSet {
return nil, errors.Errorf("conflicting or redundant options")
}
maxValueSet = true
}
case tree.SeqOptStart:
start = *option.IntVal
if startSet {
return nil, errors.Errorf("conflicting or redundant options")
}
startSet = true
case tree.SeqOptViaAlterTable:
fromAlter = true
default:
return nil, errors.Errorf("unknown CREATE SEQUENCE option")
}
}
// Determine what all values should be based on what was set and what is inferred, as well as perform
// validation for options that make sense
if minValueSet {
if minValue < minValueLimit || minValue > maxValueLimit {
return nil, errors.Errorf("MINVALUE (%d) is out of range for sequence data type %s", minValue, dataType.String())
}
} else if increment > 0 {
minValue = 1
} else {
minValue = minValueLimit
}
if maxValueSet {
if maxValue < minValueLimit || maxValue > maxValueLimit {
return nil, errors.Errorf("MAXVALUE (%d) is out of range for sequence data type %s", maxValue, dataType.String())
}
} else if increment > 0 {
maxValue = maxValueLimit
} else {
maxValue = -1
}
if startSet {
if start < minValue {
return nil, errors.Errorf("START value (%d) cannot be less than MINVALUE (%d))", start, minValue)
}
if start > maxValue {
return nil, errors.Errorf("START value (%d) cannot be greater than MAXVALUE (%d)", start, maxValue)
}
} else if increment > 0 {
start = minValue
} else {
start = maxValue
}
if dataType.IsEmptyType() {
dataType = pgtypes.Int64
}
// Returns the stored procedure call with all options
return vitess.InjectedStatement{
Statement: pgnodes.NewCreateSequence(node.IfNotExists, name.SchemaQualifier.String(), fromAlter, &sequences.Sequence{
Id: id.NewSequence("", name.Name.String()),
DataTypeID: dataType.ID,
Persistence: sequences.Persistence_Permanent,
Start: start,
Current: start,
Increment: increment,
Minimum: minValue,
Maximum: maxValue,
Cache: 1,
Cycle: cycle,
IsAtEnd: false,
OwnerTable: id.NewTable("", ownerTableName),
OwnerColumn: ownerColumnName,
}),
Children: nil,
}, nil
}