forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inserts.go
308 lines (270 loc) · 9.15 KB
/
inserts.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2020-2021 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 analyzer
import (
"strings"
"github.com/gabereiser/go-mysql-server/sql/transform"
"github.com/gabereiser/go-mysql-server/sql/types"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/expression"
"github.com/gabereiser/go-mysql-server/sql/plan"
)
func setInsertColumns(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {
// We capture all INSERTs along the tree, such as those inside of block statements.
return transform.Node(n, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) {
ii, ok := n.(*plan.InsertInto)
if !ok {
return n, transform.SameTree, nil
}
if !ii.Destination.Resolved() {
return n, transform.SameTree, nil
}
schema := ii.Destination.Schema()
// If no column names were specified in the query, go ahead and fill
// them all in now that the destination is resolved.
// TODO: setting the plan field directly is not great
if len(ii.ColumnNames) == 0 {
colNames := make([]string, len(schema))
for i, col := range schema {
colNames[i] = col.Name
}
ii.ColumnNames = colNames
}
return ii, transform.NewTree, nil
})
}
func resolveInsertRows(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {
if _, ok := n.(*plan.TriggerExecutor); ok {
return n, transform.SameTree, nil
} else if _, ok := n.(*plan.CreateProcedure); ok {
return n, transform.SameTree, nil
}
// We capture all INSERTs along the tree, such as those inside of block statements.
return transform.Node(n, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) {
insert, ok := n.(*plan.InsertInto)
if !ok {
return n, transform.SameTree, nil
}
table := getResolvedTable(insert.Destination)
insertable, err := plan.GetInsertable(table)
if err != nil {
return nil, transform.SameTree, err
}
if insert.IsReplace {
var ok bool
_, ok = insertable.(sql.ReplaceableTable)
if !ok {
return nil, transform.SameTree, plan.ErrReplaceIntoNotSupported.New()
}
}
if len(insert.OnDupExprs) > 0 {
var ok bool
_, ok = insertable.(sql.UpdatableTable)
if !ok {
return nil, transform.SameTree, plan.ErrOnDuplicateKeyUpdateNotSupported.New()
}
}
source := insert.Source
// TriggerExecutor has already been analyzed
if _, ok := insert.Source.(*plan.TriggerExecutor); !ok {
// Analyze the source of the insert independently
source, _, err = a.analyzeWithSelector(ctx, insert.Source, scope, SelectAllBatches, newInsertSourceSelector(sel))
if err != nil {
return nil, transform.SameTree, err
}
source = StripPassthroughNodes(source)
}
dstSchema := insertable.Schema()
// normalize the column name
columnNames := make([]string, len(insert.ColumnNames))
for i, name := range insert.ColumnNames {
columnNames[i] = strings.ToLower(name)
}
// If no columns are given and value tuples are not all empty, use the full schema
if len(columnNames) == 0 && existsNonZeroValueCount(source) {
columnNames = make([]string, len(dstSchema))
for i, f := range dstSchema {
columnNames[i] = f.Name
}
} else {
err = validateColumns(columnNames, dstSchema)
if err != nil {
return nil, transform.SameTree, err
}
}
err = validateValueCount(columnNames, source)
if err != nil {
return nil, transform.SameTree, err
}
// The schema of the destination node and the underlying table differ subtly in terms of defaults
project, err := wrapRowSource(ctx, source, insertable, insert.Destination.Schema(), columnNames)
if err != nil {
return nil, transform.SameTree, err
}
return insert.WithSource(project), transform.NewTree, nil
})
}
// resolvePreparedInsert applies post-optimization
// rules to Insert.Source for prepared statements.
func resolvePreparedInsert(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {
return transform.Node(n, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) {
ins, ok := n.(*plan.InsertInto)
if !ok {
return n, transform.SameTree, nil
}
// TriggerExecutor has already been analyzed
if _, ok := ins.Source.(*plan.TriggerExecutor); ok {
return n, transform.SameTree, nil
}
source, _, err := a.analyzeWithSelector(ctx, ins.Source, scope, SelectAllBatches, postPrepareInsertSourceRuleSelector)
if err != nil {
return nil, transform.SameTree, err
}
source = StripPassthroughNodes(source)
return ins.WithSource(source), transform.NewTree, nil
})
}
// Ensures that the number of elements in each Value tuple is empty
func existsNonZeroValueCount(values sql.Node) bool {
switch node := values.(type) {
case *plan.Values:
for _, exprTuple := range node.ExpressionTuples {
if len(exprTuple) != 0 {
return true
}
}
default:
return true
}
return false
}
// wrapRowSource wraps the original row source in a projection so that its schema matches the full schema of the
// underlying table, in the same order.
func wrapRowSource(ctx *sql.Context, insertSource sql.Node, destTbl sql.Table, schema sql.Schema, columnNames []string) (sql.Node, error) {
projExprs := make([]sql.Expression, len(schema))
for i, f := range schema {
found := false
for j, col := range columnNames {
if strings.EqualFold(f.Name, col) {
projExprs[i] = expression.NewGetField(j, f.Type, f.Name, f.Nullable)
found = true
break
}
}
if !found {
if !f.Nullable && f.Default == nil && !f.AutoIncrement {
return nil, sql.ErrInsertIntoNonNullableDefaultNullColumn.New(f.Name)
}
projExprs[i] = f.Default
}
if f.AutoIncrement {
ai, err := expression.NewAutoIncrement(ctx, destTbl, projExprs[i])
if err != nil {
return nil, err
}
projExprs[i] = ai
}
}
err := validateRowSource(insertSource, projExprs)
if err != nil {
return nil, err
}
return plan.NewProject(projExprs, insertSource), nil
}
func validateColumns(columnNames []string, dstSchema sql.Schema) error {
dstColNames := make(map[string]struct{})
for _, dstCol := range dstSchema {
dstColNames[strings.ToLower(dstCol.Name)] = struct{}{}
}
usedNames := make(map[string]struct{})
for _, columnName := range columnNames {
if _, exists := dstColNames[columnName]; !exists {
return plan.ErrInsertIntoNonexistentColumn.New(columnName)
}
if _, exists := usedNames[columnName]; !exists {
usedNames[columnName] = struct{}{}
} else {
return plan.ErrInsertIntoDuplicateColumn.New(columnName)
}
}
return nil
}
func validateValueCount(columnNames []string, values sql.Node) error {
if exchange, ok := values.(*plan.Exchange); ok {
values = exchange.Child
}
switch node := values.(type) {
case *plan.Values:
for _, exprTuple := range node.ExpressionTuples {
if len(exprTuple) != len(columnNames) {
return plan.ErrInsertIntoMismatchValueCount.New()
}
}
case *plan.LoadData:
dataColLen := len(node.ColumnNames)
if dataColLen == 0 {
dataColLen = len(node.Schema())
}
if len(columnNames) != dataColLen {
return plan.ErrInsertIntoMismatchValueCount.New()
}
default:
// Parser assures us that this will be some form of SelectStatement, so no need to type check it
if len(columnNames) != len(values.Schema()) {
return plan.ErrInsertIntoMismatchValueCount.New()
}
}
return nil
}
func assertCompatibleSchemas(projExprs []sql.Expression, schema sql.Schema) error {
for _, expr := range projExprs {
switch e := expr.(type) {
case *expression.Literal,
*expression.AutoIncrement,
*sql.ColumnDefaultValue:
continue
case *expression.GetField:
otherCol := schema[e.Index()]
// special case: null field type, will get checked at execution time
if otherCol.Type == types.Null {
continue
}
exprType := expr.Type()
_, err := exprType.Convert(otherCol.Type.Zero())
if err != nil {
// The zero value will fail when passing string values to ENUM, so we specially handle this case
if _, ok := exprType.(sql.EnumType); ok && types.IsText(otherCol.Type) {
continue
}
return plan.ErrInsertIntoIncompatibleTypes.New(otherCol.Type.String(), expr.Type().String())
}
default:
return plan.ErrInsertIntoUnsupportedValues.New(expr)
}
}
return nil
}
func validateRowSource(values sql.Node, projExprs []sql.Expression) error {
if exchange, ok := values.(*plan.Exchange); ok {
values = exchange.Child
}
switch n := values.(type) {
case *plan.Values, *plan.LoadData:
// already verified
return nil
default:
// Parser assures us that this will be some form of SelectStatement, so no need to type check it
return assertCompatibleSchemas(projExprs, n.Schema())
}
}