forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert.go
583 lines (513 loc) · 15.1 KB
/
insert.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// 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 plan
import (
"fmt"
"io"
"strings"
"gopkg.in/src-d/go-errors.v1"
"github.com/Sndav/go-mysql-server/sql"
"github.com/Sndav/go-mysql-server/sql/expression"
"github.com/Sndav/go-mysql-server/sql/expression/function"
)
// ErrInsertIntoNotSupported is thrown when a table doesn't support inserts
var ErrInsertIntoNotSupported = errors.NewKind("table doesn't support INSERT INTO")
var ErrReplaceIntoNotSupported = errors.NewKind("table doesn't support REPLACE INTO")
var ErrOnDuplicateKeyUpdateNotSupported = errors.NewKind("table doesn't support ON DUPLICATE KEY UPDATE")
var ErrAutoIncrementNotSupported = errors.NewKind("table doesn't support AUTO_INCREMENT")
var ErrInsertIntoMismatchValueCount = errors.NewKind("number of values does not match number of columns provided")
var ErrInsertIntoUnsupportedValues = errors.NewKind("%T is unsupported for inserts")
var ErrInsertIntoDuplicateColumn = errors.NewKind("duplicate column name %v")
var ErrInsertIntoNonexistentColumn = errors.NewKind("invalid column name %v")
var ErrInsertIntoIncompatibleTypes = errors.NewKind("cannot convert type %s to %s")
var ErrInsertIgnore = errors.NewKind("This row was ignored") // Used for making sure the row accumulator is correct
// cc: https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sql-mode-strict
// The INSERT IGNORE syntax applies to these ignorable errors
// ER_BAD_NULL_ERROR - yes
// ER_DUP_ENTRY - yes
// ER_DUP_ENTRY_WITH_KEY_NAME - Yes
// ER_DUP_KEY - kinda
// ER_NO_PARTITION_FOR_GIVEN_VALUE - yes
// ER_NO_PARTITION_FOR_GIVEN_VALUE_SILENT - No
// ER_NO_REFERENCED_ROW_2 - Yes
// ER_ROW_DOES_NOT_MATCH_GIVEN_PARTITION_SET - No
// ER_ROW_IS_REFERENCED_2 - Yes
// ER_SUBQUERY_NO_1_ROW - yes
// ER_VIEW_CHECK_FAILED - No
var IgnorableErrors = []*errors.Kind{sql.ErrInsertIntoNonNullableProvidedNull,
sql.ErrPrimaryKeyViolation,
sql.ErrPartitionNotFound,
sql.ErrExpectedSingleRow,
sql.ErrForeignKeyChildViolation,
sql.ErrForeignKeyParentViolation,
sql.ErrDuplicateEntry,
sql.ErrUniqueKeyViolation}
// InsertInto is a node describing the insertion into some table.
type InsertInto struct {
db sql.Database
Destination sql.Node
Source sql.Node
ColumnNames []string
IsReplace bool
OnDupExprs []sql.Expression
Checks sql.CheckConstraints
Ignore bool
}
var _ sql.Databaser = (*InsertInto)(nil)
var _ sql.Node = (*InsertInto)(nil)
// NewInsertInto creates an InsertInto node.
func NewInsertInto(db sql.Database, dst, src sql.Node, isReplace bool, cols []string, onDupExprs []sql.Expression, ignore bool) *InsertInto {
return &InsertInto{
db: db,
Destination: dst,
Source: src,
ColumnNames: cols,
IsReplace: isReplace,
OnDupExprs: onDupExprs,
Ignore: ignore,
}
}
// Schema implements the sql.Node interface.
// Insert nodes return rows that are inserted. Replaces return a concatenation of the deleted row and the inserted row.
// If no row was deleted, the value of those columns is nil.
func (ii *InsertInto) Schema() sql.Schema {
if ii.IsReplace {
return append(ii.Destination.Schema(), ii.Destination.Schema()...)
}
return ii.Destination.Schema()
}
func (ii *InsertInto) Children() []sql.Node {
return []sql.Node{ii.Destination}
}
func (ii *InsertInto) Database() sql.Database {
return ii.db
}
func (ii *InsertInto) WithDatabase(database sql.Database) (sql.Node, error) {
nc := *ii
nc.db = database
return &nc, nil
}
type insertIter struct {
schema sql.Schema
inserter sql.RowInserter
replacer sql.RowReplacer
updater sql.RowUpdater
rowSource sql.RowIter
lastInsertIdUpdated bool
ctx *sql.Context
insertExprs []sql.Expression
updateExprs []sql.Expression
checks sql.CheckConstraints
tableNode sql.Node
closed bool
ignore bool
}
func GetInsertable(node sql.Node) (sql.InsertableTable, error) {
switch node := node.(type) {
case *Exchange:
return GetInsertable(node.Child)
case sql.InsertableTable:
return node, nil
case *ResolvedTable:
return getInsertableTable(node.Table)
case sql.TableWrapper:
return getInsertableTable(node.Underlying())
case *prependNode:
return GetInsertable(node.Child)
default:
return nil, ErrInsertIntoNotSupported.New()
}
}
func getInsertableTable(t sql.Table) (sql.InsertableTable, error) {
switch t := t.(type) {
case sql.InsertableTable:
return t, nil
case sql.TableWrapper:
return getInsertableTable(t.Underlying())
default:
return nil, ErrInsertIntoNotSupported.New()
}
}
func newInsertIter(
ctx *sql.Context,
table sql.Node,
values sql.Node,
isReplace bool,
onDupUpdateExpr []sql.Expression,
checks sql.CheckConstraints,
row sql.Row,
ignore bool,
) (sql.RowIter, error) {
dstSchema := table.Schema()
insertable, err := GetInsertable(table)
if err != nil {
return nil, err
}
var inserter sql.RowInserter
var replacer sql.RowReplacer
var updater sql.RowUpdater
// These type casts have already been asserted in the analyzer
if isReplace {
replacer = insertable.(sql.ReplaceableTable).Replacer(ctx)
} else {
inserter = insertable.Inserter(ctx)
if len(onDupUpdateExpr) > 0 {
updater = insertable.(sql.UpdatableTable).Updater(ctx)
}
}
rowIter, err := values.RowIter(ctx, row)
if err != nil {
return nil, err
}
insertExpressions := getInsertExpressions(values)
insertIter := &insertIter{
schema: dstSchema,
tableNode: table,
inserter: inserter,
replacer: replacer,
updater: updater,
rowSource: rowIter,
updateExprs: onDupUpdateExpr,
insertExprs: insertExpressions,
checks: checks,
ctx: ctx,
ignore: ignore,
}
if replacer != nil {
return NewTableEditorIter(ctx, replacer, insertIter), nil
} else {
return NewTableEditorIter(ctx, inserter, insertIter), nil
}
}
func getInsertExpressions(values sql.Node) []sql.Expression {
var exprs []sql.Expression
Inspect(values, func(node sql.Node) bool {
switch node := node.(type) {
case *Project:
exprs = node.Projections
return false
}
return true
})
return exprs
}
func (i *insertIter) Next() (returnRow sql.Row, returnErr error) {
row, err := i.rowSource.Next()
if err == io.EOF {
return nil, err
}
if err != nil {
return i.ignoreOrClose(err)
}
// Prune the row down to the size of the schema. It can be larger in the case of running with an outer scope, in which
// case the additional scope variables are prepended to the row.
if len(row) > len(i.schema) {
row = row[len(row)-len(i.schema):]
}
err = i.validateNullability(i.schema, row)
if err != nil {
return i.ignoreOrClose(err)
}
// apply check constraints
for _, check := range i.checks {
if !check.Enforced {
continue
}
res, err := sql.EvaluateCondition(i.ctx, check.Expr, row)
if err != nil {
return nil, i.warnOnIgnorableError(err)
}
if sql.IsFalse(res) {
return nil, sql.ErrCheckConstraintViolated.New(check.Name)
}
}
// Do any necessary type conversions to the target schema
for i, col := range i.schema {
if row[i] != nil {
row[i], err = col.Type.Convert(row[i])
if err != nil {
return nil, err
}
}
}
if i.replacer != nil {
toReturn := make(sql.Row, len(row)*2)
for i := 0; i < len(row); i++ {
toReturn[i+len(row)] = row[i]
}
// May have multiple duplicate pk & unique errors due to multiple indexes
//TODO: how does this interact with triggers?
for {
if err := i.replacer.Insert(i.ctx, row); err != nil {
if !sql.ErrPrimaryKeyViolation.Is(err) && !sql.ErrUniqueKeyViolation.Is(err) {
_ = i.rowSource.Close(i.ctx)
return nil, err
}
ue := err.(*errors.Error).Cause().(sql.UniqueKeyError)
if err = i.replacer.Delete(i.ctx, ue.Existing); err != nil {
_ = i.rowSource.Close(i.ctx)
return nil, err
}
// the row had to be deleted, write the values into the toReturn row
for i := 0; i < len(ue.Existing); i++ {
toReturn[i] = ue.Existing[i]
}
} else {
break
}
}
return toReturn, nil
} else {
if err := i.inserter.Insert(i.ctx, row); err != nil {
if (!sql.ErrPrimaryKeyViolation.Is(err) && !sql.ErrUniqueKeyViolation.Is(err) && !sql.ErrDuplicateEntry.Is(err)) || len(i.updateExprs) == 0 {
return i.ignoreOrClose(err)
}
ue := err.(*errors.Error).Cause().(sql.UniqueKeyError)
return i.handleOnDuplicateKeyUpdate(row, ue.Existing)
}
}
i.updateLastInsertId(i.ctx, row)
return row, nil
}
func (i *insertIter) handleOnDuplicateKeyUpdate(row, rowToUpdate sql.Row) (returnRow sql.Row, returnErr error) {
err := i.resolveValues(i.ctx, row)
if err != nil {
return nil, err
}
newRow, err := applyUpdateExpressions(i.ctx, i.updateExprs, rowToUpdate)
if err != nil {
return nil, err
}
err = i.updater.Update(i.ctx, rowToUpdate, newRow)
if err != nil {
return nil, err
}
// In the case that we attempted an update, return a concatenated [old,new] row just like update.
return rowToUpdate.Append(newRow), nil
}
// resolveValues resolves all VALUES functions.
func (i *insertIter) resolveValues(ctx *sql.Context, insertRow sql.Row) error {
for _, updateExpr := range i.updateExprs {
var err error
sql.Inspect(updateExpr, func(expr sql.Expression) bool {
valuesExpr, ok := expr.(*function.Values)
if !ok {
return true
}
getField, ok := valuesExpr.Child.(*expression.GetField)
if !ok {
err = fmt.Errorf("VALUES functions may only contain column names")
return false
}
valuesExpr.Value = insertRow[getField.Index()]
return false
})
if err != nil {
return err
}
}
return nil
}
func (i *insertIter) Close(ctx *sql.Context) error {
if !i.closed {
i.closed = true
if i.inserter != nil {
if err := i.inserter.Close(ctx); err != nil {
return err
}
}
if i.replacer != nil {
if err := i.replacer.Close(ctx); err != nil {
return err
}
}
if i.updater != nil {
if err := i.updater.Close(ctx); err != nil {
return err
}
}
if i.rowSource != nil {
if err := i.rowSource.Close(ctx); err != nil {
return err
}
}
}
return nil
}
func (i *insertIter) updateLastInsertId(ctx *sql.Context, row sql.Row) {
if i.lastInsertIdUpdated {
return
}
var autoIncVal int64
var found bool
for i, expr := range i.insertExprs {
if _, ok := expr.(*expression.AutoIncrement); ok {
autoIncVal = toInt64(row[i])
found = true
break
}
}
if found {
ctx.SetLastQueryInfo(sql.LastInsertId, autoIncVal)
i.lastInsertIdUpdated = true
}
}
func (i *insertIter) ignoreOrClose(err error) (sql.Row, error) {
if i.ignore {
return nil, i.warnOnIgnorableError(err)
} else {
_ = i.rowSource.Close(i.ctx)
return nil, err
}
}
func (i *insertIter) warnOnIgnorableError(err error) error {
if !i.ignore {
return err
}
// Check that this error is a part of the list of Ignorable Errors and create the relevant warning
for _, ie := range IgnorableErrors {
if ie.Is(err) {
sqlerr, _ := sql.CastSQLError(err)
// Add a warning instead
i.ctx.Session.Warn(&sql.Warning{
Level: "Note",
Code: sqlerr.Num,
Message: err.Error(),
})
// In this case the default value gets updated so return nil
if sql.ErrInsertIntoNonNullableDefaultNullColumn.Is(err) {
return nil
}
// Return the InsertIgnore err to ensure our accumulator doesn't count this row.
return ErrInsertIgnore.New()
}
}
return err
}
func toInt64(x interface{}) int64 {
switch x := x.(type) {
case int:
return int64(x)
case uint:
return int64(x)
case int8:
return int64(x)
case uint8:
return int64(x)
case int16:
return int64(x)
case uint16:
return int64(x)
case int32:
return int64(x)
case uint32:
return int64(x)
case int64:
return x
case uint64:
return int64(x)
case float32:
return int64(x)
case float64:
return int64(x)
default:
panic(fmt.Sprintf("Expected a numeric auto increment value, but got %T", x))
}
}
// RowIter implements the Node interface.
func (ii *InsertInto) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
return newInsertIter(ctx, ii.Destination, ii.Source, ii.IsReplace, ii.OnDupExprs, ii.Checks, row, ii.Ignore)
}
// WithChildren implements the Node interface.
func (ii *InsertInto) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(ii, len(children), 1)
}
np := *ii
np.Destination = children[0]
return &np, nil
}
// WithSource sets the source node for this insert, which is analyzed separately
func (ii *InsertInto) WithSource(src sql.Node) sql.Node {
np := *ii
np.Source = src
return &np
}
func (ii InsertInto) String() string {
pr := sql.NewTreePrinter()
if ii.IsReplace {
_ = pr.WriteNode("Replace(%s)", strings.Join(ii.ColumnNames, ", "))
} else {
_ = pr.WriteNode("Insert(%s)", strings.Join(ii.ColumnNames, ", "))
}
_ = pr.WriteChildren(ii.Destination.String(), ii.Source.String())
return pr.String()
}
func (ii InsertInto) DebugString() string {
pr := sql.NewTreePrinter()
var columnNames []string
if ii.IsReplace {
_ = pr.WriteNode("Replace(%s)", strings.Join(columnNames, ", "))
} else {
_ = pr.WriteNode("Insert(%s)", strings.Join(columnNames, ", "))
}
_ = pr.WriteChildren(sql.DebugString(ii.Destination), sql.DebugString(ii.Source))
return pr.String()
}
func (i *insertIter) validateNullability(dstSchema sql.Schema, row sql.Row) error {
for count, col := range dstSchema {
if !col.Nullable && row[count] == nil {
// In the case of an IGNORE we set the nil value to a default and add a warning
if i.ignore {
row[count] = col.Type.Zero()
_ = i.warnOnIgnorableError(sql.ErrInsertIntoNonNullableProvidedNull.New(col.Name)) // will always return nil
} else {
return sql.ErrInsertIntoNonNullableProvidedNull.New(col.Name)
}
}
}
return nil
}
func (ii *InsertInto) Expressions() []sql.Expression {
return append(ii.OnDupExprs, ii.Checks.ToExpressions()...)
}
func (ii InsertInto) WithExpressions(newExprs ...sql.Expression) (sql.Node, error) {
if len(newExprs) != len(ii.OnDupExprs)+len(ii.Checks) {
return nil, sql.ErrInvalidChildrenNumber.New(ii, len(newExprs), len(ii.OnDupExprs)+len(ii.Checks))
}
ii.OnDupExprs = newExprs[:len(ii.OnDupExprs)]
var err error
ii.Checks, err = ii.Checks.FromExpressions(newExprs[len(ii.OnDupExprs):])
if err != nil {
return nil, err
}
return &ii, nil
}
// Resolved implements the Resolvable interface.
func (ii *InsertInto) Resolved() bool {
if !ii.Destination.Resolved() || !ii.Source.Resolved() {
return false
}
for _, updateExpr := range ii.OnDupExprs {
if !updateExpr.Resolved() {
return false
}
}
for _, checkExpr := range ii.Checks {
if !checkExpr.Expr.Resolved() {
return false
}
}
return true
}