forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_test.go
1378 lines (1222 loc) · 54.1 KB
/
write_test.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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package executor_test
import (
"errors"
"fmt"
"sync/atomic"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/testkit"
)
func (s *testSuite) TestInsert(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
testSQL := `drop table if exists insert_test;create table insert_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1);`
tk.MustExec(testSQL)
testSQL = `insert insert_test (c1) values (1),(2),(NULL);`
tk.MustExec(testSQL)
errInsertSelectSQL := `insert insert_test (c1) values ();`
tk.MustExec("begin")
_, err := tk.Exec(errInsertSelectSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
errInsertSelectSQL = `insert insert_test (c1, c2) values (1,2),(1);`
tk.MustExec("begin")
_, err = tk.Exec(errInsertSelectSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
errInsertSelectSQL = `insert insert_test (xxx) values (3);`
tk.MustExec("begin")
_, err = tk.Exec(errInsertSelectSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
errInsertSelectSQL = `insert insert_test_xxx (c1) values ();`
tk.MustExec("begin")
_, err = tk.Exec(errInsertSelectSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
insertSetSQL := `insert insert_test set c1 = 3;`
tk.MustExec(insertSetSQL)
errInsertSelectSQL = `insert insert_test set c1 = 4, c1 = 5;`
tk.MustExec("begin")
_, err = tk.Exec(errInsertSelectSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
errInsertSelectSQL = `insert insert_test set xxx = 6;`
tk.MustExec("begin")
_, err = tk.Exec(errInsertSelectSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
insertSelectSQL := `create table insert_test_1 (id int, c1 int);`
tk.MustExec(insertSelectSQL)
insertSelectSQL = `insert insert_test_1 select id, c1 from insert_test;`
tk.MustExec(insertSelectSQL)
insertSelectSQL = `create table insert_test_2 (id int, c1 int);`
tk.MustExec(insertSelectSQL)
insertSelectSQL = `insert insert_test_1 select id, c1 from insert_test union select id * 10, c1 * 10 from insert_test;`
tk.MustExec(insertSelectSQL)
errInsertSelectSQL = `insert insert_test_1 select c1 from insert_test;`
tk.MustExec("begin")
_, err = tk.Exec(errInsertSelectSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
// Updating column is PK handle.
// Make sure the record is "1, 1, nil, 1".
r := tk.MustQuery("select * from insert_test where id = 1;")
rowStr := fmt.Sprintf("%v %v %v %v", "1", "1", nil, "1")
r.Check(testkit.Rows(rowStr))
insertSQL := `insert into insert_test (id, c3) values (1, 2) on duplicate key update id=values(id), c2=10;`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_test where id = 1;")
rowStr = fmt.Sprintf("%v %v %v %v", "1", "1", "10", "1")
r.Check(testkit.Rows(rowStr))
insertSQL = `insert into insert_test (id, c2) values (1, 1) on duplicate key update insert_test.c2=10;`
tk.MustExec(insertSQL)
_, err = tk.Exec(`insert into insert_test (id, c2) values(1, 1) on duplicate key update t.c2 = 10`)
c.Assert(err, NotNil)
// for on duplicate key
insertSQL = `INSERT INTO insert_test (id, c3) VALUES (1, 2) ON DUPLICATE KEY UPDATE c3=values(c3)+c3+3;`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_test where id = 1;")
rowStr = fmt.Sprintf("%v %v %v %v", "1", "1", "10", "6")
r.Check(testkit.Rows(rowStr))
tk.MustExec("create table insert_err (id int, c1 varchar(8))")
_, err = tk.Exec("insert insert_err values (1, 'abcdabcdabcd')")
c.Assert(types.ErrDataTooLong.Equal(err), IsTrue)
_, err = tk.Exec("insert insert_err values (1, '你好,世界')")
c.Assert(err, IsNil)
tk.MustExec("create table TEST1 (ID INT NOT NULL, VALUE INT DEFAULT NULL, PRIMARY KEY (ID))")
_, err = tk.Exec("INSERT INTO TEST1(id,value) VALUE(3,3) on DUPLICATE KEY UPDATE VALUE=4")
c.Assert(err, IsNil)
tk.MustExec("create table t (id int)")
tk.MustExec("insert into t values(1)")
tk.MustExec("update t t1 set id = (select count(*) + 1 from t t2 where t1.id = t2.id)")
r = tk.MustQuery("select * from t;")
r.Check(testkit.Rows("2"))
// issue 3235
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(c decimal(5, 5))")
_, err = tk.Exec("insert into t value(0)")
c.Assert(err, IsNil)
_, err = tk.Exec("insert into t value(1)")
c.Assert(types.ErrOverflow.Equal(err), IsTrue)
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(c binary(255))")
_, err = tk.Exec("insert into t value(1)")
c.Assert(err, IsNil)
r = tk.MustQuery("select length(c) from t;")
r.Check(testkit.Rows("255"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(c varbinary(255))")
_, err = tk.Exec("insert into t value(1)")
c.Assert(err, IsNil)
r = tk.MustQuery("select length(c) from t;")
r.Check(testkit.Rows("1"))
// issue 3509
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(c int)")
tk.MustExec("set @origin_time_zone = @@time_zone")
tk.MustExec("set @@time_zone = '+08:00'")
_, err = tk.Exec("insert into t value(Unix_timestamp('2002-10-27 01:00'))")
c.Assert(err, IsNil)
r = tk.MustQuery("select * from t;")
r.Check(testkit.Rows("1035651600"))
tk.MustExec("set @@time_zone = @origin_time_zone")
// issue 3832
tk.MustExec("create table t1 (b char(0));")
_, err = tk.Exec(`insert into t1 values ("");`)
c.Assert(err, IsNil)
// issue 3895
tk = testkit.NewTestKit(c, s.store)
tk.MustExec("USE test;")
tk.MustExec("DROP TABLE IF EXISTS t;")
tk.MustExec("CREATE TABLE t(a DECIMAL(4,2));")
tk.MustExec("INSERT INTO t VALUES (1.000001);")
r = tk.MustQuery("SHOW WARNINGS;")
r.Check(testkit.Rows("Warning 1265 Data Truncated"))
tk.MustExec("INSERT INTO t VALUES (1.000000);")
r = tk.MustQuery("SHOW WARNINGS;")
r.Check(testkit.Rows())
// issue 4653
tk.MustExec("DROP TABLE IF EXISTS t;")
tk.MustExec("CREATE TABLE t(a datetime);")
_, err = tk.Exec("INSERT INTO t VALUES('2017-00-00')")
c.Assert(err, NotNil)
tk.MustExec("set sql_mode = ''")
tk.MustExec("INSERT INTO t VALUES('2017-00-00')")
r = tk.MustQuery("SELECT * FROM t;")
r.Check(testkit.Rows("2017-00-00 00:00:00"))
tk.MustExec("set sql_mode = 'strict_all_tables';")
r = tk.MustQuery("SELECT * FROM t;")
r.Check(testkit.Rows("2017-00-00 00:00:00"))
// test auto_increment with unsigned.
tk.MustExec("drop table if exists test")
tk.MustExec("CREATE TABLE test(id int(10) UNSIGNED NOT NULL AUTO_INCREMENT, p int(10) UNSIGNED NOT NULL, PRIMARY KEY(p), KEY(id))")
tk.MustExec("insert into test(p) value(1)")
tk.MustQuery("select * from test").Check(testkit.Rows("1 1"))
tk.MustQuery("select * from test use index (id) where id = 1").Check(testkit.Rows("1 1"))
tk.MustExec("insert into test values(NULL, 2)")
tk.MustQuery("select * from test use index (id) where id = 2").Check(testkit.Rows("2 2"))
tk.MustExec("insert into test values(2, 3)")
tk.MustQuery("select * from test use index (id) where id = 2").Check(testkit.Rows("2 2", "2 3"))
}
func (s *testSuite) TestInsertAutoInc(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
createSQL := `drop table if exists insert_autoinc_test; create table insert_autoinc_test (id int primary key auto_increment, c1 int);`
tk.MustExec(createSQL)
insertSQL := `insert into insert_autoinc_test(c1) values (1), (2)`
tk.MustExec(insertSQL)
tk.MustExec("begin")
r := tk.MustQuery("select * from insert_autoinc_test;")
rowStr1 := fmt.Sprintf("%v %v", "1", "1")
rowStr2 := fmt.Sprintf("%v %v", "2", "2")
r.Check(testkit.Rows(rowStr1, rowStr2))
tk.MustExec("commit")
tk.MustExec("begin")
insertSQL = `insert into insert_autoinc_test(id, c1) values (5,5)`
tk.MustExec(insertSQL)
insertSQL = `insert into insert_autoinc_test(c1) values (6)`
tk.MustExec(insertSQL)
tk.MustExec("commit")
tk.MustExec("begin")
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr3 := fmt.Sprintf("%v %v", "5", "5")
rowStr4 := fmt.Sprintf("%v %v", "6", "6")
r.Check(testkit.Rows(rowStr1, rowStr2, rowStr3, rowStr4))
tk.MustExec("commit")
tk.MustExec("begin")
insertSQL = `insert into insert_autoinc_test(id, c1) values (3,3)`
tk.MustExec(insertSQL)
tk.MustExec("commit")
tk.MustExec("begin")
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr5 := fmt.Sprintf("%v %v", "3", "3")
r.Check(testkit.Rows(rowStr1, rowStr2, rowStr5, rowStr3, rowStr4))
tk.MustExec("commit")
tk.MustExec("begin")
insertSQL = `insert into insert_autoinc_test(c1) values (7)`
tk.MustExec(insertSQL)
tk.MustExec("commit")
tk.MustExec("begin")
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr6 := fmt.Sprintf("%v %v", "7", "7")
r.Check(testkit.Rows(rowStr1, rowStr2, rowStr5, rowStr3, rowStr4, rowStr6))
tk.MustExec("commit")
// issue-962
createSQL = `drop table if exists insert_autoinc_test; create table insert_autoinc_test (id int primary key auto_increment, c1 int);`
tk.MustExec(createSQL)
insertSQL = `insert into insert_autoinc_test(id, c1) values (0.3, 1)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr1 = fmt.Sprintf("%v %v", "1", "1")
r.Check(testkit.Rows(rowStr1))
insertSQL = `insert into insert_autoinc_test(id, c1) values (-0.3, 2)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr2 = fmt.Sprintf("%v %v", "2", "2")
r.Check(testkit.Rows(rowStr1, rowStr2))
insertSQL = `insert into insert_autoinc_test(id, c1) values (-3.3, 3)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr3 = fmt.Sprintf("%v %v", "-3", "3")
r.Check(testkit.Rows(rowStr3, rowStr1, rowStr2))
insertSQL = `insert into insert_autoinc_test(id, c1) values (4.3, 4)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr4 = fmt.Sprintf("%v %v", "4", "4")
r.Check(testkit.Rows(rowStr3, rowStr1, rowStr2, rowStr4))
insertSQL = `insert into insert_autoinc_test(c1) values (5)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr5 = fmt.Sprintf("%v %v", "5", "5")
r.Check(testkit.Rows(rowStr3, rowStr1, rowStr2, rowStr4, rowStr5))
insertSQL = `insert into insert_autoinc_test(id, c1) values (null, 6)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr6 = fmt.Sprintf("%v %v", "6", "6")
r.Check(testkit.Rows(rowStr3, rowStr1, rowStr2, rowStr4, rowStr5, rowStr6))
// SQL_MODE=NO_AUTO_VALUE_ON_ZERO
createSQL = `drop table if exists insert_autoinc_test; create table insert_autoinc_test (id int primary key auto_increment, c1 int);`
tk.MustExec(createSQL)
insertSQL = `insert into insert_autoinc_test(id, c1) values (5, 1)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr1 = fmt.Sprintf("%v %v", "5", "1")
r.Check(testkit.Rows(rowStr1))
insertSQL = `insert into insert_autoinc_test(id, c1) values (0, 2)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr2 = fmt.Sprintf("%v %v", "6", "2")
r.Check(testkit.Rows(rowStr1, rowStr2))
insertSQL = `insert into insert_autoinc_test(id, c1) values (0, 3)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr3 = fmt.Sprintf("%v %v", "7", "3")
r.Check(testkit.Rows(rowStr1, rowStr2, rowStr3))
tk.MustExec("set SQL_MODE=NO_AUTO_VALUE_ON_ZERO")
insertSQL = `insert into insert_autoinc_test(id, c1) values (0, 4)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr4 = fmt.Sprintf("%v %v", "0", "4")
r.Check(testkit.Rows(rowStr4, rowStr1, rowStr2, rowStr3))
insertSQL = `insert into insert_autoinc_test(id, c1) values (0, 5)`
_, err := tk.Exec(insertSQL)
// ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'
c.Assert(err, NotNil)
insertSQL = `insert into insert_autoinc_test(c1) values (6)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr5 = fmt.Sprintf("%v %v", "8", "6")
r.Check(testkit.Rows(rowStr4, rowStr1, rowStr2, rowStr3, rowStr5))
insertSQL = `insert into insert_autoinc_test(id, c1) values (null, 7)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr6 = fmt.Sprintf("%v %v", "9", "7")
r.Check(testkit.Rows(rowStr4, rowStr1, rowStr2, rowStr3, rowStr5, rowStr6))
tk.MustExec("set SQL_MODE='';")
insertSQL = `insert into insert_autoinc_test(id, c1) values (0, 8)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr7 := fmt.Sprintf("%v %v", "10", "8")
r.Check(testkit.Rows(rowStr4, rowStr1, rowStr2, rowStr3, rowStr5, rowStr6, rowStr7))
insertSQL = `insert into insert_autoinc_test(id, c1) values (null, 9)`
tk.MustExec(insertSQL)
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr8 := fmt.Sprintf("%v %v", "11", "9")
r.Check(testkit.Rows(rowStr4, rowStr1, rowStr2, rowStr3, rowStr5, rowStr6, rowStr7, rowStr8))
}
func (s *testSuite) TestInsertIgnore(c *C) {
var cfg kv.InjectionConfig
tk := testkit.NewTestKit(c, kv.NewInjectedStore(s.store, &cfg))
tk.MustExec("use test")
testSQL := `drop table if exists t;
create table t (id int PRIMARY KEY AUTO_INCREMENT, c1 int unique key);`
tk.MustExec(testSQL)
testSQL = `insert into t values (1, 2);`
tk.MustExec(testSQL)
r := tk.MustQuery("select * from t;")
rowStr := fmt.Sprintf("%v %v", "1", "2")
r.Check(testkit.Rows(rowStr))
tk.MustExec("insert ignore into t values (1, 3), (2, 3)")
r = tk.MustQuery("select * from t;")
rowStr1 := fmt.Sprintf("%v %v", "2", "3")
r.Check(testkit.Rows(rowStr, rowStr1))
tk.MustExec("insert ignore into t values (3, 4), (3, 4)")
r = tk.MustQuery("select * from t;")
rowStr2 := fmt.Sprintf("%v %v", "3", "4")
r.Check(testkit.Rows(rowStr, rowStr1, rowStr2))
tk.MustExec("begin")
tk.MustExec("insert ignore into t values (4, 4), (4, 5), (4, 6)")
r = tk.MustQuery("select * from t;")
rowStr3 := fmt.Sprintf("%v %v", "4", "5")
r.Check(testkit.Rows(rowStr, rowStr1, rowStr2, rowStr3))
tk.MustExec("commit")
cfg.SetGetError(errors.New("foo"))
_, err := tk.Exec("insert ignore into t values (1, 3)")
c.Assert(err, NotNil)
cfg.SetGetError(nil)
// for issue 4268
testSQL = `drop table if exists t;
create table t (a bigint);`
tk.MustExec(testSQL)
testSQL = "insert ignore into t select '1a';"
_, err = tk.Exec(testSQL)
c.Assert(err, IsNil)
r = tk.MustQuery("SHOW WARNINGS")
r.Check(testkit.Rows("Warning 1265 Data Truncated"))
testSQL = "insert ignore into t values ('1a')"
_, err = tk.Exec(testSQL)
c.Assert(err, IsNil)
r = tk.MustQuery("SHOW WARNINGS")
r.Check(testkit.Rows("Warning 1265 Data Truncated"))
// for duplicates with warning
testSQL = `drop table if exists t;
create table t(a int primary key, b int);`
tk.MustExec(testSQL)
testSQL = "insert ignore into t values (1,1);"
tk.MustExec(testSQL)
_, err = tk.Exec(testSQL)
c.Assert(err, IsNil)
r = tk.MustQuery("SHOW WARNINGS")
r.Check(testkit.Rows("Warning 1062 Duplicate entry '1' for key 'PRIMARY'"))
}
func (s *testSuite) TestReplace(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
testSQL := `drop table if exists replace_test;
create table replace_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1);`
tk.MustExec(testSQL)
testSQL = `replace replace_test (c1) values (1),(2),(NULL);`
tk.MustExec(testSQL)
errReplaceSQL := `replace replace_test (c1) values ();`
tk.MustExec("begin")
_, err := tk.Exec(errReplaceSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
errReplaceSQL = `replace replace_test (c1, c2) values (1,2),(1);`
tk.MustExec("begin")
_, err = tk.Exec(errReplaceSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
errReplaceSQL = `replace replace_test (xxx) values (3);`
tk.MustExec("begin")
_, err = tk.Exec(errReplaceSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
errReplaceSQL = `replace replace_test_xxx (c1) values ();`
tk.MustExec("begin")
_, err = tk.Exec(errReplaceSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
replaceSetSQL := `replace replace_test set c1 = 3;`
tk.MustExec(replaceSetSQL)
errReplaceSetSQL := `replace replace_test set c1 = 4, c1 = 5;`
tk.MustExec("begin")
_, err = tk.Exec(errReplaceSetSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
errReplaceSetSQL = `replace replace_test set xxx = 6;`
tk.MustExec("begin")
_, err = tk.Exec(errReplaceSetSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
replaceSelectSQL := `create table replace_test_1 (id int, c1 int);`
tk.MustExec(replaceSelectSQL)
replaceSelectSQL = `replace replace_test_1 select id, c1 from replace_test;`
tk.MustExec(replaceSelectSQL)
replaceSelectSQL = `create table replace_test_2 (id int, c1 int);`
tk.MustExec(replaceSelectSQL)
replaceSelectSQL = `replace replace_test_1 select id, c1 from replace_test union select id * 10, c1 * 10 from replace_test;`
tk.MustExec(replaceSelectSQL)
errReplaceSelectSQL := `replace replace_test_1 select c1 from replace_test;`
tk.MustExec("begin")
_, err = tk.Exec(errReplaceSelectSQL)
c.Assert(err, NotNil)
tk.MustExec("rollback")
replaceUniqueIndexSQL := `create table replace_test_3 (c1 int, c2 int, UNIQUE INDEX (c2));`
tk.MustExec(replaceUniqueIndexSQL)
replaceUniqueIndexSQL = `replace into replace_test_3 set c2=1;`
tk.MustExec(replaceUniqueIndexSQL)
replaceUniqueIndexSQL = `replace into replace_test_3 set c2=1;`
tk.MustExec(replaceUniqueIndexSQL)
c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(1))
replaceUniqueIndexSQL = `replace into replace_test_3 set c1=1, c2=1;`
tk.MustExec(replaceUniqueIndexSQL)
c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(2))
replaceUniqueIndexSQL = `replace into replace_test_3 set c2=NULL;`
tk.MustExec(replaceUniqueIndexSQL)
replaceUniqueIndexSQL = `replace into replace_test_3 set c2=NULL;`
tk.MustExec(replaceUniqueIndexSQL)
c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(1))
replaceUniqueIndexSQL = `create table replace_test_4 (c1 int, c2 int, c3 int, UNIQUE INDEX (c1, c2));`
tk.MustExec(replaceUniqueIndexSQL)
replaceUniqueIndexSQL = `replace into replace_test_4 set c2=NULL;`
tk.MustExec(replaceUniqueIndexSQL)
replaceUniqueIndexSQL = `replace into replace_test_4 set c2=NULL;`
tk.MustExec(replaceUniqueIndexSQL)
c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(1))
replacePrimaryKeySQL := `create table replace_test_5 (c1 int, c2 int, c3 int, PRIMARY KEY (c1, c2));`
tk.MustExec(replacePrimaryKeySQL)
replacePrimaryKeySQL = `replace into replace_test_5 set c1=1, c2=2;`
tk.MustExec(replacePrimaryKeySQL)
replacePrimaryKeySQL = `replace into replace_test_5 set c1=1, c2=2;`
tk.MustExec(replacePrimaryKeySQL)
c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(1))
// For Issue989
issue989SQL := `CREATE TABLE tIssue989 (a int, b int, PRIMARY KEY(a), UNIQUE KEY(b));`
tk.MustExec(issue989SQL)
issue989SQL = `insert into tIssue989 (a, b) values (1, 2);`
tk.MustExec(issue989SQL)
issue989SQL = `replace into tIssue989(a, b) values (111, 2);`
tk.MustExec(issue989SQL)
r := tk.MustQuery("select * from tIssue989;")
r.Check(testkit.Rows("111 2"))
// For Issue1012
issue1012SQL := `CREATE TABLE tIssue1012 (a int, b int, PRIMARY KEY(a), UNIQUE KEY(b));`
tk.MustExec(issue1012SQL)
issue1012SQL = `insert into tIssue1012 (a, b) values (1, 2);`
tk.MustExec(issue1012SQL)
issue1012SQL = `insert into tIssue1012 (a, b) values (2, 1);`
tk.MustExec(issue1012SQL)
issue1012SQL = `replace into tIssue1012(a, b) values (1, 1);`
tk.MustExec(issue1012SQL)
c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(3))
r = tk.MustQuery("select * from tIssue1012;")
r.Check(testkit.Rows("1 1"))
}
func (s *testSuite) TestUpdate(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
s.fillData(tk, "update_test")
updateStr := `UPDATE update_test SET name = "abc" where id > 0;`
tk.MustExec(updateStr)
tk.CheckExecResult(2, 0)
// select data
tk.MustExec("begin")
r := tk.MustQuery(`SELECT * from update_test limit 2;`)
r.Check(testkit.Rows("1 abc", "2 abc"))
tk.MustExec("commit")
tk.MustExec(`UPDATE update_test SET name = "foo"`)
tk.CheckExecResult(2, 0)
// table option is auto-increment
tk.MustExec("begin")
tk.MustExec("drop table if exists update_test;")
tk.MustExec("commit")
tk.MustExec("begin")
tk.MustExec("create table update_test(id int not null auto_increment, name varchar(255), primary key(id))")
tk.MustExec("insert into update_test(name) values ('aa')")
tk.MustExec("update update_test set id = 8 where name = 'aa'")
tk.MustExec("insert into update_test(name) values ('bb')")
tk.MustExec("commit")
tk.MustExec("begin")
r = tk.MustQuery("select * from update_test;")
r.Check(testkit.Rows("8 aa", "9 bb"))
tk.MustExec("commit")
tk.MustExec("begin")
tk.MustExec("drop table if exists update_test;")
tk.MustExec("commit")
tk.MustExec("begin")
tk.MustExec("create table update_test(id int not null auto_increment, name varchar(255), index(id))")
tk.MustExec("insert into update_test(name) values ('aa')")
_, err := tk.Exec("update update_test set id = null where name = 'aa'")
c.Assert(err, NotNil)
c.Assert(err.Error(), DeepEquals, "Column 'id' cannot be null")
tk.MustExec("drop table update_test")
tk.MustExec("create table update_test(id int)")
tk.MustExec("begin")
tk.MustExec("insert into update_test(id) values (1)")
tk.MustExec("update update_test set id = 2 where id = 1 limit 1")
r = tk.MustQuery("select * from update_test;")
r.Check(testkit.Rows("2"))
tk.MustExec("commit")
// Test that in a transaction, when a constraint failed in an update statement, the record is not inserted.
tk.MustExec("create table update_unique (id int primary key, name int unique)")
tk.MustExec("insert update_unique values (1, 1), (2, 2);")
tk.MustExec("begin")
_, err = tk.Exec("update update_unique set name = 1 where id = 2")
c.Assert(err, NotNil)
tk.MustExec("commit")
tk.MustQuery("select * from update_unique").Check(testkit.Rows("1 1", "2 2"))
// test update ignore for pimary key
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a bigint, primary key (a));")
tk.MustExec("insert into t values (1)")
tk.MustExec("insert into t values (2)")
_, err = tk.Exec("update ignore t set a = 1 where a = 2;")
c.Assert(err, IsNil)
r = tk.MustQuery("SHOW WARNINGS;")
r.Check(testkit.Rows("Warning 1062 Duplicate entry '1' for key 'PRIMARY'"))
tk.MustQuery("select * from t").Check(testkit.Rows("1", "2"))
// test update ignore for truncate as warning
_, err = tk.Exec("update ignore t set a = 1 where a = (select '2a')")
c.Assert(err, IsNil)
r = tk.MustQuery("SHOW WARNINGS;")
r.Check(testkit.Rows("Warning 1265 Data Truncated", "Warning 1265 Data Truncated", "Warning 1062 Duplicate entry '1' for key 'PRIMARY'"))
// test update ignore for unique key
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a bigint, unique key I_uniq (a));")
tk.MustExec("insert into t values (1)")
tk.MustExec("insert into t values (2)")
_, err = tk.Exec("update ignore t set a = 1 where a = 2;")
c.Assert(err, IsNil)
r = tk.MustQuery("SHOW WARNINGS;")
r.Check(testkit.Rows("Warning 1062 key already exist"))
tk.MustQuery("select * from t").Check(testkit.Rows("1", "2"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(id integer auto_increment, t1 datetime, t2 datetime, primary key (id))")
tk.MustExec("insert into t(t1, t2) values('2000-10-01 01:01:01', '2017-01-01 10:10:10')")
tk.MustQuery("select * from t").Check(testkit.Rows("1 2000-10-01 01:01:01 2017-01-01 10:10:10"))
tk.MustExec("update t set t1 = '2017-10-01 10:10:11', t2 = date_add(t1, INTERVAL 10 MINUTE) where id = 1")
tk.MustQuery("select * from t").Check(testkit.Rows("1 2017-10-01 10:10:11 2017-10-01 10:20:11"))
// for issue #5132
tk.MustExec("CREATE TABLE `tt1` (" +
"`a` int(11) NOT NULL," +
"`b` varchar(32) DEFAULT NULL," +
"`c` varchar(32) DEFAULT NULL," +
"PRIMARY KEY (`a`)," +
"UNIQUE KEY `b_idx` (`b`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;")
tk.MustExec("insert into tt1 values(1, 'a', 'a');")
tk.MustExec("insert into tt1 values(2, 'd', 'b');")
r = tk.MustQuery("select * from tt1;")
r.Check(testkit.Rows("1 a a", "2 d b"))
tk.MustExec("update tt1 set a=5 where c='b';")
r = tk.MustQuery("select * from tt1;")
r.Check(testkit.Rows("1 a a", "5 d b"))
// Automatic Updating for TIMESTAMP
tk.MustExec("CREATE TABLE `tsup` (" +
"`a` int," +
"`ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," +
"KEY `idx` (`ts`)" +
");")
tk.MustExec("insert into tsup values(1, '0000-00-00 00:00:00');")
tk.MustExec("update tsup set a=5;")
r1 := tk.MustQuery("select ts from tsup use index (idx);")
r2 := tk.MustQuery("select ts from tsup;")
r1.Check(r2.Rows())
}
// TestUpdateCastOnlyModifiedValues for issue #4514.
func (s *testSuite) TestUpdateCastOnlyModifiedValues(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table update_modified (col_1 int, col_2 enum('a', 'b'))")
tk.MustExec("set SQL_MODE=''")
tk.MustExec("insert into update_modified values (0, 3)")
r := tk.MustQuery("SELECT * FROM update_modified")
r.Check(testkit.Rows("0 "))
tk.MustExec("set SQL_MODE=STRICT_ALL_TABLES")
tk.MustExec("update update_modified set col_1 = 1")
r = tk.MustQuery("SELECT * FROM update_modified")
r.Check(testkit.Rows("1 "))
_, err := tk.Exec("update update_modified set col_1 = 2, col_2 = 'c'")
c.Assert(err, NotNil)
r = tk.MustQuery("SELECT * FROM update_modified")
r.Check(testkit.Rows("1 "))
tk.MustExec("update update_modified set col_1 = 3, col_2 = 'a'")
r = tk.MustQuery("SELECT * FROM update_modified")
r.Check(testkit.Rows("3 a"))
// Test update a field with different column type.
tk.MustExec(`CREATE TABLE update_with_diff_type (a int, b JSON)`)
tk.MustExec(`INSERT INTO update_with_diff_type VALUES(3, '{"a": "测试"}')`)
tk.MustExec(`UPDATE update_with_diff_type SET a = '300'`)
r = tk.MustQuery("SELECT a FROM update_with_diff_type")
r.Check(testkit.Rows("300"))
tk.MustExec(`UPDATE update_with_diff_type SET b = '{"a": "\\u6d4b\\u8bd5"}'`)
r = tk.MustQuery("SELECT b FROM update_with_diff_type")
r.Check(testkit.Rows(`{"a":"测试"}`))
}
func (s *testSuite) fillMultiTableForUpdate(tk *testkit.TestKit) {
// Create and fill table items
tk.MustExec("CREATE TABLE items (id int, price TEXT);")
tk.MustExec(`insert into items values (11, "items_price_11"), (12, "items_price_12"), (13, "items_price_13");`)
tk.CheckExecResult(3, 0)
// Create and fill table month
tk.MustExec("CREATE TABLE month (mid int, mprice TEXT);")
tk.MustExec(`insert into month values (11, "month_price_11"), (22, "month_price_22"), (13, "month_price_13");`)
tk.CheckExecResult(3, 0)
}
func (s *testSuite) TestMultipleTableUpdate(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
s.fillMultiTableForUpdate(tk)
tk.MustExec(`UPDATE items, month SET items.price=month.mprice WHERE items.id=month.mid;`)
tk.MustExec("begin")
r := tk.MustQuery("SELECT * FROM items")
r.Check(testkit.Rows("11 month_price_11", "12 items_price_12", "13 month_price_13"))
tk.MustExec("commit")
// Single-table syntax but with multiple tables
tk.MustExec(`UPDATE items join month on items.id=month.mid SET items.price=month.mid;`)
tk.MustExec("begin")
r = tk.MustQuery("SELECT * FROM items")
r.Check(testkit.Rows("11 11", "12 items_price_12", "13 13"))
tk.MustExec("commit")
// JoinTable with alias table name.
tk.MustExec(`UPDATE items T0 join month T1 on T0.id=T1.mid SET T0.price=T1.mprice;`)
tk.MustExec("begin")
r = tk.MustQuery("SELECT * FROM items")
r.Check(testkit.Rows("11 month_price_11", "12 items_price_12", "13 month_price_13"))
tk.MustExec("commit")
// fix https://github.com/pingcap/tidb/issues/369
testSQL := `
DROP TABLE IF EXISTS t1, t2;
create table t1 (c int);
create table t2 (c varchar(256));
insert into t1 values (1), (2);
insert into t2 values ("a"), ("b");
update t1, t2 set t1.c = 10, t2.c = "abc";`
tk.MustExec(testSQL)
// fix https://github.com/pingcap/tidb/issues/376
testSQL = `DROP TABLE IF EXISTS t1, t2;
create table t1 (c1 int);
create table t2 (c2 int);
insert into t1 values (1), (2);
insert into t2 values (1), (2);
update t1, t2 set t1.c1 = 10, t2.c2 = 2 where t2.c2 = 1;`
tk.MustExec(testSQL)
r = tk.MustQuery("select * from t1")
r.Check(testkit.Rows("10", "10"))
// test https://github.com/pingcap/tidb/issues/3604
tk.MustExec("drop table if exists t, t")
tk.MustExec("create table t (a int, b int)")
tk.MustExec("insert into t values(1, 1), (2, 2), (3, 3)")
tk.MustExec("update t m, t n set m.a = m.a + 1")
tk.MustQuery("select * from t").Check(testkit.Rows("2 1", "3 2", "4 3"))
tk.MustExec("update t m, t n set n.a = n.a - 1, n.b = n.b + 1")
tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "2 3", "3 4"))
}
func (s *testSuite) TestDelete(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.fillData(tk, "delete_test")
tk.MustExec(`update delete_test set name = "abc" where id = 2;`)
tk.CheckExecResult(1, 0)
tk.MustExec(`delete from delete_test where id = 2 limit 1;`)
tk.CheckExecResult(1, 0)
// Test delete with false condition
tk.MustExec(`delete from delete_test where 0;`)
tk.CheckExecResult(0, 0)
tk.MustExec("insert into delete_test values (2, 'abc')")
tk.MustExec(`delete from delete_test where delete_test.id = 2 limit 1`)
tk.CheckExecResult(1, 0)
// Select data
tk.MustExec("begin")
rows := tk.MustQuery(`SELECT * from delete_test limit 2;`)
rows.Check(testkit.Rows("1 hello"))
tk.MustExec("commit")
// Test delete ignore
tk.MustExec("insert into delete_test values (2, 'abc')")
_, err := tk.Exec("delete from delete_test where id = (select '2a')")
c.Assert(err, NotNil)
_, err = tk.Exec("delete ignore from delete_test where id = (select '2a')")
c.Assert(err, IsNil)
tk.CheckExecResult(1, 0)
r := tk.MustQuery("SHOW WARNINGS;")
r.Check(testkit.Rows("Warning 1265 Data Truncated", "Warning 1265 Data Truncated"))
tk.MustExec(`delete from delete_test ;`)
tk.CheckExecResult(1, 0)
}
func (s *testSuite) fillDataMultiTable(tk *testkit.TestKit) {
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2, t3")
// Create and fill table t1
tk.MustExec("create table t1 (id int, data int);")
tk.MustExec("insert into t1 values (11, 121), (12, 122), (13, 123);")
tk.CheckExecResult(3, 0)
// Create and fill table t2
tk.MustExec("create table t2 (id int, data int);")
tk.MustExec("insert into t2 values (11, 221), (22, 222), (23, 223);")
tk.CheckExecResult(3, 0)
// Create and fill table t3
tk.MustExec("create table t3 (id int, data int);")
tk.MustExec("insert into t3 values (11, 321), (22, 322), (23, 323);")
tk.CheckExecResult(3, 0)
}
func (s *testSuite) TestMultiTableDelete(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.fillDataMultiTable(tk)
tk.MustExec(`delete t1, t2 from t1 inner join t2 inner join t3 where t1.id=t2.id and t2.id=t3.id;`)
tk.CheckExecResult(2, 0)
// Select data
r := tk.MustQuery("select * from t3")
c.Assert(r.Rows(), HasLen, 3)
}
func (s *testSuite) TestQualifiedDelete(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1 (c1 int, c2 int, index (c1))")
tk.MustExec("create table t2 (c1 int, c2 int)")
tk.MustExec("insert into t1 values (1, 1), (2, 2)")
// delete with index
tk.MustExec("delete from t1 where t1.c1 = 1")
tk.CheckExecResult(1, 0)
// delete with no index
tk.MustExec("delete from t1 where t1.c2 = 2")
tk.CheckExecResult(1, 0)
r := tk.MustQuery("select * from t1")
c.Assert(r.Rows(), HasLen, 0)
_, err := tk.Exec("delete from t1 as a where a.c1 = 1")
c.Assert(err, NotNil)
tk.MustExec("insert into t1 values (1, 1), (2, 2)")
tk.MustExec("insert into t2 values (2, 1), (3,1)")
tk.MustExec("delete t1, t2 from t1 join t2 where t1.c1 = t2.c2")
tk.CheckExecResult(3, 0)
tk.MustExec("insert into t2 values (2, 1), (3,1)")
tk.MustExec("delete a, b from t1 as a join t2 as b where a.c2 = b.c1")
tk.CheckExecResult(2, 0)
_, err = tk.Exec("delete t1, t2 from t1 as a join t2 as b where a.c2 = b.c1")
c.Assert(err, NotNil)
}
func (s *testSuite) TestLoadData(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
createSQL := `drop table if exists load_data_test;
create table load_data_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 varchar(255) default "def", c3 int);`
_, err := tk.Exec("load data local infile '/tmp/nonexistence.csv' into table load_data_test")
c.Assert(err, NotNil)
tk.MustExec(createSQL)
_, err = tk.Exec("load data infile '/tmp/nonexistence.csv' into table load_data_test")
c.Assert(err, NotNil)
tk.MustExec("load data local infile '/tmp/nonexistence.csv' into table load_data_test")
ctx := tk.Se.(context.Context)
ld := makeLoadDataInfo(4, nil, ctx, c)
deleteSQL := "delete from load_data_test"
selectSQL := "select * from load_data_test;"
// data1 = nil, data2 = nil, fields and lines is default
_, reachLimit, err := ld.InsertData(nil, nil)
c.Assert(err, IsNil)
c.Assert(reachLimit, IsFalse)
r := tk.MustQuery(selectSQL)
r.Check(nil)
sc := ctx.GetSessionVars().StmtCtx
originIgnoreTruncate := sc.IgnoreTruncate
defer func() {
sc.IgnoreTruncate = originIgnoreTruncate
}()
sc.IgnoreTruncate = false
// fields and lines are default, InsertData returns data is nil
tests := []testCase{
// data1 = nil, data2 != nil
{nil, []byte("\n"), []string{"1|0||0"}, nil},
{nil, []byte("\t\n"), []string{"2|0||0"}, nil},
{nil, []byte("3\t2\t3\t4\n"), []string{"3|2|3|4"}, nil},
{nil, []byte("3*1\t2\t3\t4\n"), []string{"3|2|3|4"}, nil},
{nil, []byte("4\t2\t\t3\t4\n"), []string{"4|2||3"}, nil},
{nil, []byte("\t1\t2\t3\t4\n"), []string{"5|1|2|3"}, nil},
{nil, []byte("6\t2\t3\n"), []string{"6|2|3|0"}, nil},
{nil, []byte("\t2\t3\t4\n\t22\t33\t44\n"), []string{"7|2|3|4", "8|22|33|44"}, nil},
{nil, []byte("7\t2\t3\t4\n7\t22\t33\t44\n"), []string{"7|2|3|4"}, nil},
// data1 != nil, data2 = nil
{[]byte("\t2\t3\t4"), nil, []string{"9|2|3|4"}, nil},
// data1 != nil, data2 != nil
{[]byte("\t2\t3"), []byte("\t4\t5\n"), []string{"10|2|3|4"}, nil},
{[]byte("\t2\t3"), []byte("4\t5\n"), []string{"11|2|34|5"}, nil},
// data1 != nil, data2 != nil, InsertData returns data isn't nil
{[]byte("\t2\t3"), []byte("\t4\t5"), nil, []byte("\t2\t3\t4\t5")},
}
checkCases(tests, ld, c, tk, ctx, selectSQL, deleteSQL)
c.Assert(sc.WarningCount(), Equals, uint16(3))
// lines starting symbol is "" and terminated symbol length is 2, InsertData returns data is nil
ld.LinesInfo.Terminated = "||"
tests = []testCase{
// data1 != nil, data2 != nil
{[]byte("0\t2\t3"), []byte("\t4\t5||"), []string{"12|2|3|4"}, nil},
{[]byte("1\t2\t3\t4\t5|"), []byte("|"), []string{"1|2|3|4"}, nil},
{[]byte("2\t2\t3\t4\t5|"), []byte("|3\t22\t33\t44\t55||"),
[]string{"2|2|3|4", "3|22|33|44"}, nil},
{[]byte("3\t2\t3\t4\t5|"), []byte("|4\t22\t33||"), []string{
"3|2|3|4", "4|22|33|0"}, nil},
{[]byte("4\t2\t3\t4\t5|"), []byte("|5\t22\t33||6\t222||"),
[]string{"4|2|3|4", "5|22|33|0", "6|222||0"}, nil},
{[]byte("6\t2\t3"), []byte("4\t5||"), []string{"6|2|34|5"}, nil},
}
checkCases(tests, ld, c, tk, ctx, selectSQL, deleteSQL)
// fields and lines aren't default, InsertData returns data is nil
ld.FieldsInfo.Terminated = "\\"
ld.LinesInfo.Starting = "xxx"
ld.LinesInfo.Terminated = "|!#^"
tests = []testCase{
// data1 = nil, data2 != nil
{nil, []byte("xxx|!#^"), []string{"13|0||0"}, nil},
{nil, []byte("xxx\\|!#^"), []string{"14|0||0"}, nil},
{nil, []byte("xxx3\\2\\3\\4|!#^"), []string{"3|2|3|4"}, nil},
{nil, []byte("xxx4\\2\\\\3\\4|!#^"), []string{"4|2||3"}, nil},
{nil, []byte("xxx\\1\\2\\3\\4|!#^"), []string{"15|1|2|3"}, nil},
{nil, []byte("xxx6\\2\\3|!#^"), []string{"6|2|3|0"}, nil},
{nil, []byte("xxx\\2\\3\\4|!#^xxx\\22\\33\\44|!#^"), []string{
"16|2|3|4",
"17|22|33|44"}, nil},
{nil, []byte("\\2\\3\\4|!#^\\22\\33\\44|!#^xxx\\222\\333\\444|!#^"), []string{
"18|222|333|444"}, nil},
// data1 != nil, data2 = nil
{[]byte("xxx\\2\\3\\4"), nil, []string{"19|2|3|4"}, nil},
{[]byte("\\2\\3\\4|!#^"), nil, []string{}, nil},
{[]byte("\\2\\3\\4|!#^xxx18\\22\\33\\44|!#^"), nil,
[]string{"18|22|33|44"}, nil},
// data1 != nil, data2 != nil
{[]byte("xxx10\\2\\3"), []byte("\\4|!#^"),
[]string{"10|2|3|4"}, nil},
{[]byte("10\\2\\3xx"), []byte("x11\\4\\5|!#^"),
[]string{"11|4|5|0"}, nil},
{[]byte("xxx21\\2\\3\\4\\5|!"), []byte("#^"),
[]string{"21|2|3|4"}, nil},
{[]byte("xxx22\\2\\3\\4\\5|!"), []byte("#^xxx23\\22\\33\\44\\55|!#^"),
[]string{"22|2|3|4", "23|22|33|44"}, nil},
{[]byte("xxx23\\2\\3\\4\\5|!"), []byte("#^xxx24\\22\\33|!#^"),
[]string{"23|2|3|4", "24|22|33|0"}, nil},
{[]byte("xxx24\\2\\3\\4\\5|!"), []byte("#^xxx25\\22\\33|!#^xxx26\\222|!#^"),
[]string{"24|2|3|4", "25|22|33|0", "26|222||0"}, nil},
{[]byte("xxx25\\2\\3\\4\\5|!"), []byte("#^26\\22\\33|!#^xxx27\\222|!#^"),
[]string{"25|2|3|4", "27|222||0"}, nil},
{[]byte("xxx\\2\\3"), []byte("4\\5|!#^"), []string{"28|2|34|5"}, nil},
// InsertData returns data isn't nil
{nil, []byte("\\2\\3\\4|!#^"), nil, []byte("#^")},
{nil, []byte("\\4\\5"), nil, []byte("\\5")},
{[]byte("\\2\\3"), []byte("\\4\\5"), nil, []byte("\\5")},
{[]byte("xxx1\\2\\3|"), []byte("!#^\\4\\5|!#"),
[]string{"1|2|3|0"}, []byte("!#")},
{[]byte("xxx1\\2\\3\\4\\5|!"), []byte("#^xxx2\\22\\33|!#^3\\222|!#^"),
[]string{"1|2|3|4", "2|22|33|0"}, []byte("#^")},
{[]byte("xx1\\2\\3"), []byte("\\4\\5|!#^"), nil, []byte("#^")},
}
checkCases(tests, ld, c, tk, ctx, selectSQL, deleteSQL)
// lines starting symbol is the same as terminated symbol, InsertData returns data is nil
ld.LinesInfo.Terminated = "xxx"
tests = []testCase{
// data1 = nil, data2 != nil
{nil, []byte("xxxxxx"), []string{"29|0||0"}, nil},
{nil, []byte("xxx3\\2\\3\\4xxx"), []string{"3|2|3|4"}, nil},
{nil, []byte("xxx\\2\\3\\4xxxxxx\\22\\33\\44xxx"),
[]string{"30|2|3|4", "31|22|33|44"}, nil},
// data1 != nil, data2 = nil
{[]byte("xxx\\2\\3\\4"), nil, []string{"32|2|3|4"}, nil},
// data1 != nil, data2 != nil
{[]byte("xxx10\\2\\3"), []byte("\\4\\5xxx"), []string{"10|2|3|4"}, nil},
{[]byte("xxxxx10\\2\\3"), []byte("\\4\\5xxx"), []string{"33|2|3|4"}, nil},
{[]byte("xxx21\\2\\3\\4\\5xx"), []byte("x"), []string{"21|2|3|4"}, nil},
{[]byte("xxx32\\2\\3\\4\\5x"), []byte("xxxxx33\\22\\33\\44\\55xxx"),
[]string{"32|2|3|4", "33|22|33|44"}, nil},
{[]byte("xxx33\\2\\3\\4\\5xxx"), []byte("xxx34\\22\\33xxx"),
[]string{"33|2|3|4", "34|22|33|0"}, nil},
{[]byte("xxx34\\2\\3\\4\\5xx"), []byte("xxxx35\\22\\33xxxxxx36\\222xxx"),