forked from dolthub/go-mysql-server
/
insert.go
801 lines (698 loc) · 21.7 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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
// 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"
"github.com/dolthub/vitess/go/vt/proto/query"
"gopkg.in/src-d/go-errors.v1"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/expression"
"github.com/gabereiser/go-mysql-server/sql/expression/function"
"github.com/gabereiser/go-mysql-server/sql/transform"
"github.com/gabereiser/go-mysql-server/sql/types"
)
// 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")
// 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,
sql.ErrCheckConstraintViolated,
}
// InsertInto is the top level node for INSERT INTO statements. It has a source for rows and a destination to insert
// them into.
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)
var _ sql.Expressioner = (*InsertInto)(nil)
var _ DisjointedChildrenNode = (*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,
}
}
func (ii *InsertInto) Dispose() {
disposeNode(ii.Source)
}
// 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 {
// The source node is analyzed completely independently, so we don't include it in children
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
}
// InsertDestination is a wrapper for a table to be used with InsertInto.Destination that allows the schema to be
// overridden. This is useful when the table in question has late-resolving column defaults.
type InsertDestination struct {
UnaryNode
Sch sql.Schema
}
var _ sql.Expressioner = (*InsertDestination)(nil)
func NewInsertDestination(schema sql.Schema, node sql.Node) *InsertDestination {
return &InsertDestination{
UnaryNode: UnaryNode{Child: node},
Sch: schema,
}
}
func (id *InsertDestination) Expressions() []sql.Expression {
return transform.WrappedColumnDefaults(id.Sch)
}
func (id InsertDestination) WithExpressions(exprs ...sql.Expression) (sql.Node, error) {
if len(exprs) != len(id.Sch) {
return nil, sql.ErrInvalidChildrenNumber.New(id, len(exprs), len(id.Sch))
}
id.Sch = transform.SchemaWithDefaults(id.Sch, exprs)
return &id, nil
}
func (id *InsertDestination) String() string {
return id.UnaryNode.Child.String()
}
func (id *InsertDestination) DebugString() string {
pr := sql.NewTreePrinter()
pr.WriteNode("InsertDestination")
var children []string
for _, col := range id.Sch {
children = append(children, sql.DebugString(col.Default))
}
children = append(children, sql.DebugString(id.Child))
pr.WriteChildren(children...)
return pr.String()
}
func (id *InsertDestination) Schema() sql.Schema {
return id.Sch
}
func (id *InsertDestination) Resolved() bool {
if !id.UnaryNode.Resolved() {
return false
}
for _, col := range id.Sch {
if !col.Default.Resolved() {
return false
}
}
return true
}
func (id *InsertDestination) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
return id.UnaryNode.Child.RowIter(ctx, row)
}
func (id InsertDestination) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(id, len(children), 1)
}
id.UnaryNode.Child = children[0]
return &id, nil
}
// CheckPrivileges implements the interface sql.Node.
func (id *InsertDestination) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
return id.Child.CheckPrivileges(ctx, opChecker)
}
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 *InsertDestination:
return GetInsertable(node.Child)
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,
dest sql.Node,
values sql.Node,
isReplace bool,
onDupUpdateExpr []sql.Expression,
checks sql.CheckConstraints,
row sql.Row,
ignore bool,
) (sql.RowIter, error) {
// This schema may vary from the table itself, particularly in terms of column defaults
dstSchema := dest.Schema()
insertable, err := GetInsertable(dest)
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: dest,
inserter: inserter,
replacer: replacer,
updater: updater,
rowSource: rowIter,
updateExprs: onDupUpdateExpr,
insertExprs: insertExpressions,
checks: checks,
ctx: ctx,
ignore: ignore,
}
var ed sql.EditOpenerCloser
if replacer != nil {
ed = replacer
} else {
ed = inserter
}
if ignore {
return NewCheckpointingTableEditorIter(insertIter, ed), nil
} else {
return NewTableEditorIter(insertIter, ed), nil
}
}
func getInsertExpressions(values sql.Node) []sql.Expression {
var exprs []sql.Expression
transform.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(ctx *sql.Context) (returnRow sql.Row, returnErr error) {
row, err := i.rowSource.Next(ctx)
if err == io.EOF {
return nil, err
}
if err != nil {
return nil, i.ignoreOrClose(ctx, row, 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(ctx, i.schema, row)
if err != nil {
return nil, i.ignoreOrClose(ctx, row, err)
}
err = i.evaluateChecks(ctx, row)
if err != nil {
return nil, i.ignoreOrClose(ctx, row, err)
}
origRow := make(sql.Row, len(row))
copy(origRow, row)
// Do any necessary type conversions to the target schema
for idx, col := range i.schema {
if row[idx] != nil {
converted, cErr := col.Type.Convert(row[idx]) // allows for better error handling
if cErr != nil {
// Ignore individual column errors when INSERT IGNORE, UPDATE IGNORE, etc. is specified.
// For JSON column types, always throw an error. MySQL throws the following error even when
// IGNORE is specified:
// ERROR 3140 (22032): Invalid JSON text: "Invalid value." at position 0 in value for column
// 'table.column'.
if i.ignore && col.Type.Type() != query.Type_JSON {
row = convertDataAndWarn(ctx, i.schema, row, idx, cErr)
continue
} else {
// Fill in error with information
if types.ErrLengthBeyondLimit.Is(cErr) {
cErr = types.ErrLengthBeyondLimit.New(row[idx], col.Name)
} else if sql.ErrNotMatchingSRID.Is(cErr) {
cErr = sql.ErrNotMatchingSRIDWithColName.New(col.Name, cErr)
}
return nil, sql.NewWrappedInsertError(origRow, cErr)
}
}
row[idx] = converted
}
}
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(ctx, row); err != nil {
if !sql.ErrPrimaryKeyViolation.Is(err) && !sql.ErrUniqueKeyViolation.Is(err) {
i.rowSource.Close(ctx)
i.rowSource = nil
return nil, sql.NewWrappedInsertError(row, err)
}
ue := err.(*errors.Error).Cause().(sql.UniqueKeyError)
if err = i.replacer.Delete(ctx, ue.Existing); err != nil {
i.rowSource.Close(ctx)
i.rowSource = nil
return nil, sql.NewWrappedInsertError(row, 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(ctx, row); err != nil {
if (!sql.ErrPrimaryKeyViolation.Is(err) && !sql.ErrUniqueKeyViolation.Is(err) && !sql.ErrDuplicateEntry.Is(err)) || len(i.updateExprs) == 0 {
return nil, i.ignoreOrClose(ctx, row, err)
}
ue := err.(*errors.Error).Cause().(sql.UniqueKeyError)
return i.handleOnDuplicateKeyUpdate(ctx, row, ue.Existing)
}
}
i.updateLastInsertId(ctx, row)
return row, nil
}
func (i *insertIter) handleOnDuplicateKeyUpdate(ctx *sql.Context, row, rowToUpdate sql.Row) (returnRow sql.Row, returnErr error) {
err := i.resolveValues(ctx, row)
if err != nil {
return nil, err
}
newRow := rowToUpdate
for _, updateExpr := range i.updateExprs {
val, err := updateExpr.Eval(i.ctx, newRow)
if err != nil {
if i.ignore {
idx, ok := getFieldIndexFromUpdateExpr(updateExpr)
if !ok {
return nil, err
}
val = convertDataAndWarn(ctx, i.schema, row, idx, err)
} else {
return nil, err
}
}
newRow = val.(sql.Row)
}
// Should revaluate the check conditions.
err = i.evaluateChecks(ctx, newRow)
if err != nil {
return nil, i.ignoreOrClose(ctx, newRow, err)
}
err = i.updater.Update(ctx, rowToUpdate, newRow)
if err != nil {
return nil, i.ignoreOrClose(ctx, newRow, err)
}
// In the case that we attempted an update, return a concatenated [old,new] row just like update.
return rowToUpdate.Append(newRow), nil
}
func getFieldIndexFromUpdateExpr(updateExpr sql.Expression) (int, bool) {
setField, ok := updateExpr.(*expression.SetField)
if !ok {
return 0, false
}
getField, ok := setField.Left.(*expression.GetField)
if !ok {
return 0, false
}
return getField.Index(), true
}
// 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
var rsErr, iErr, rErr, uErr error
if i.rowSource != nil {
rsErr = i.rowSource.Close(ctx)
}
if i.inserter != nil {
iErr = i.inserter.Close(ctx)
}
if i.replacer != nil {
rErr = i.replacer.Close(ctx)
}
if i.updater != nil {
uErr = i.updater.Close(ctx)
}
if rsErr != nil {
return rsErr
}
if iErr != nil {
return iErr
}
if rErr != nil {
return rErr
}
if uErr != nil {
return uErr
}
}
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(ctx *sql.Context, row sql.Row, err error) error {
if !i.ignore {
return sql.NewWrappedInsertError(row, err)
}
return warnOnIgnorableError(ctx, row, err)
}
// convertDataAndWarn modifies a row with data conversion issues in INSERT/UPDATE IGNORE calls
// Per MySQL docs "Rows set to values that would cause data conversion errors are set to the closest valid values instead"
// cc. https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sql-mode-strict
func convertDataAndWarn(ctx *sql.Context, tableSchema sql.Schema, row sql.Row, columnIdx int, err error) sql.Row {
if types.ErrLengthBeyondLimit.Is(err) {
maxLength := tableSchema[columnIdx].Type.(sql.StringType).MaxCharacterLength()
row[columnIdx] = row[columnIdx].(string)[:maxLength] // truncate string
} else {
row[columnIdx] = tableSchema[columnIdx].Type.Zero()
}
sqlerr := sql.CastSQLError(err)
// Add a warning instead
ctx.Session.Warn(&sql.Warning{
Level: "Note",
Code: sqlerr.Num,
Message: err.Error(),
})
return row
}
func warnOnIgnorableError(ctx *sql.Context, row sql.Row, err error) error {
// 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
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 sql.NewIgnorableError(row)
}
}
return err
}
func (i *insertIter) evaluateChecks(ctx *sql.Context, row sql.Row) error {
for _, check := range i.checks {
if !check.Enforced {
continue
}
res, err := sql.EvaluateCondition(ctx, check.Expr, row)
if err != nil {
return err
}
if sql.IsFalse(res) {
return sql.ErrCheckConstraintViolated.New(check.Name)
}
}
return nil
}
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
}
// CheckPrivileges implements the interface sql.Node.
func (ii *InsertInto) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
if ii.IsReplace {
return opChecker.UserHasPrivileges(ctx,
sql.NewPrivilegedOperation(ii.db.Name(), getTableName(ii.Destination), "", sql.PrivilegeType_Insert, sql.PrivilegeType_Delete))
} else {
return opChecker.UserHasPrivileges(ctx,
sql.NewPrivilegedOperation(ii.db.Name(), getTableName(ii.Destination), "", sql.PrivilegeType_Insert))
}
}
// DisjointedChildren implements the interface DisjointedChildrenNode.
func (ii *InsertInto) DisjointedChildren() [][]sql.Node {
return [][]sql.Node{
{ii.Destination},
{ii.Source},
}
}
// WithDisjointedChildren implements the interface DisjointedChildrenNode.
func (ii *InsertInto) WithDisjointedChildren(children [][]sql.Node) (sql.Node, error) {
if len(children) != 2 || len(children[0]) != 1 || len(children[1]) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(ii, len(children), 2)
}
np := *ii
np.Destination = children[0][0]
np.Source = children[1][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()
if ii.IsReplace {
_ = pr.WriteNode("Replace(%s)", strings.Join(ii.ColumnNames, ", "))
} else {
_ = pr.WriteNode("Insert(%s)", strings.Join(ii.ColumnNames, ", "))
}
_ = pr.WriteChildren(sql.DebugString(ii.Destination), sql.DebugString(ii.Source))
return pr.String()
}
func (i *insertIter) validateNullability(ctx *sql.Context, 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()
_ = warnOnIgnorableError(ctx, row, 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
}