-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathmulti_schema_change_test.go
1318 lines (1169 loc) · 57.5 KB
/
multi_schema_change_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 2022 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,
// 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 ddl_test
import (
"strconv"
"testing"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMultiSchemaChangeAddColumns(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
// Test add multiple columns in multiple specs.
tk.MustExec("create table t (a int);")
tk.MustExec("insert into t values (1);")
tk.MustExec("alter table t add column b int default 2, add column c int default 3;")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3"))
// Test add multiple columns and constraints in one spec.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
tk.MustExec("insert into t values (1);")
tk.MustExec("alter table t add column (b int default 2, c int default 3);")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int);")
tk.MustExec("insert into t values (1, 2, 3);")
tk.MustExec("alter table t add column (d int default 4, e int default 5);")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3 4 5"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
tk.MustExec("insert into t values (1);")
tk.MustExec("alter table t add column (index i(a), index i1(a));")
tk.MustQuery("select * from t use index (i, i1);").Check(testkit.Rows("1"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
tk.MustExec("insert into t values (1);")
tk.MustExec("alter table t add column (b int default 2, index i(a), primary key (a));")
tk.MustQuery("select * from t use index (i, primary)").Check(testkit.Rows("1 2"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
tk.MustExec("insert into t values (1);")
tk.MustExec("alter table t add column (index i(a));")
tk.MustQuery("select * from t use index (i)").Check(testkit.Rows("1"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int);")
tk.MustExec("insert into t values (1, 2, 3);")
tk.MustExec("alter table t add column (index i1(a, b, c), index i2(c, b, a), index i3((a + 1)), index i4((c - 1)));")
tk.MustQuery("select * from t use index (i1, i2);").Check(testkit.Rows("1 2 3"))
tk.MustExec("admin check table t;")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1);")
tk.MustExec("insert into t values ();")
tk.MustExec("alter table t add column if not exists (b int default 2, c int default 3);")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3"))
tk.MustExec("alter table t add column if not exists (c int default 3, d int default 4);")
tk.MustQuery("show warnings;").Check(testkit.Rows("Note 1060 Duplicate column name 'c'"))
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3 4"))
// Test referencing previous column in multi-schema change is not supported.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
tk.MustGetErrCode("alter table t add column b int after a, add column c int after b", errno.ErrBadField)
tk.MustGetErrCode("alter table t add column c int after b, add column b int", errno.ErrBadField)
// Test add multiple columns with different position.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int);")
tk.MustExec("insert into t values (1, 2, 3);")
tk.MustExec(`alter table t
add column d int default 4 first,
add column e int default 5 after b,
add column f int default 6 after b;`)
tk.MustQuery("select * from t;").Check(testkit.Rows("4 1 2 6 5 3"))
// Test [if not exists] for adding columns.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1);")
tk.MustExec("insert into t values ();")
tk.MustExec("alter table t add column b int default 2, add column if not exists a int;")
tk.MustQuery("show warnings;").Check(testkit.Rows("Note 1060 Duplicate column name 'a'"))
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2"))
// Test add generate column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int, b int);")
tk.MustExec("insert into t values (1, 2);")
tk.MustExec("alter table t add column c double default 3.0, add column d double as (a + b);")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3 3"))
// Test add columns with same name
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, c int default 4);")
tk.MustGetErrCode("alter table t add column b int default 2, add column b int default 3", errno.ErrUnsupportedDDLOperation)
// Test add generate column dependents on a modifying column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int, b int);")
tk.MustExec("insert into t values (1, 2);")
tk.MustGetErrCode("alter table t modify column b double, add column c double as (a + b);", errno.ErrUnsupportedDDLOperation)
}
func TestMultiSchemaChangeAddColumnsCancelled(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
originHook := dom.DDL().GetHook()
tk.MustExec("create table t (a int);")
tk.MustExec("insert into t values (1);")
hook := newCancelJobHook(t, store, dom, func(job *model.Job) bool {
// Cancel job when the column 'c' is in write-reorg.
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 3)
return job.MultiSchemaInfo.SubJobs[1].SchemaState == model.StateWriteReorganization
})
dom.DDL().SetHook(hook)
sql := "alter table t add column b int default 2, add column c int default 3, add column d int default 4;"
tk.MustGetErrCode(sql, errno.ErrCancelledDDLJob)
dom.DDL().SetHook(originHook)
hook.MustCancelDone(t)
tk.MustQuery("select * from t;").Check(testkit.Rows("1"))
}
func TestMultiSchemaChangeAddColumnsParallel(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (a int default 1);")
tk.MustExec("insert into t values ();")
putTheSameDDLJobTwice(t, func() {
tk.MustExec("alter table t add column if not exists b int default 2, " +
"add column if not exists c int default 3;")
tk.MustQuery("show warnings").Check(testkit.Rows(
"Note 1060 Duplicate column name 'b'",
"Note 1060 Duplicate column name 'c'",
))
})
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
putTheSameDDLJobTwice(t, func() {
tk.MustGetErrCode("alter table t add column b int, add column c int;", errno.ErrDupFieldName)
})
}
func TestMultiSchemaChangeDropColumns(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
// Test drop all columns
tk.MustExec("create table t (a int, b int);")
tk.MustGetErrCode("alter table t drop column a, drop column b;", errno.ErrCantRemoveAllFields)
// Test drop multiple columns in multiple specs
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int, d int, e int);")
tk.MustExec("insert into t values (1, 2, 3, 4, 5);")
tk.MustExec("alter table t drop column a, drop column d, drop column b;")
tk.MustQuery("select * from t;").Check(testkit.Rows("3 5"))
// Test drop same column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, c int default 4);")
tk.MustGetErrCode("alter table t drop column a, drop column a", errno.ErrUnsupportedDDLOperation)
// Test drop if exists column.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustExec("alter table t drop column if exists c, drop column a;")
tk.MustQuery("show warnings;").Check(testkit.Rows("Note 1091 Can't DROP 'c'; check that column/key exists"))
tk.MustQuery("select * from t;").Check(testkit.Rows("2"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2, c int default 3);")
tk.MustExec("insert into t values ();")
tk.MustExec("alter table t drop column a, drop column if exists d, drop column c;")
tk.MustQuery("show warnings;").Check(testkit.Rows("Note 1091 Can't DROP 'd'; check that column/key exists"))
tk.MustQuery("select * from t;").Check(testkit.Rows("2"))
}
func TestMultiSchemaChangeDropColumnsCancelled(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
originHook := dom.DDL().GetHook()
// Test for cancelling the job in a middle state.
tk.MustExec("create table t (a int default 1, b int default 2, c int default 3, d int default 4);")
tk.MustExec("insert into t values ();")
hook := newCancelJobHook(t, store, dom, func(job *model.Job) bool {
// Cancel job when the column 'a' is in delete-reorg.
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 3)
return job.MultiSchemaInfo.SubJobs[1].SchemaState == model.StateDeleteReorganization
})
dom.DDL().SetHook(hook)
tk.MustExec("alter table t drop column b, drop column a, drop column d;")
dom.DDL().SetHook(originHook)
hook.MustCancelFailed(t)
tk.MustQuery("select * from t;").Check(testkit.Rows("3"))
// Test for cancelling the job in public.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2, c int default 3, d int default 4);")
tk.MustExec("insert into t values ();")
hook = newCancelJobHook(t, store, dom, func(job *model.Job) bool {
// Cancel job when the column 'a' is in public.
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 3)
return job.MultiSchemaInfo.SubJobs[1].SchemaState == model.StatePublic
})
dom.DDL().SetHook(hook)
tk.MustGetErrCode("alter table t drop column b, drop column a, drop column d;", errno.ErrCancelledDDLJob)
dom.DDL().SetHook(originHook)
hook.MustCancelDone(t)
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3 4"))
}
func TestMultiSchemaChangeDropIndexedColumnsCancelled(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
originHook := dom.DDL().GetHook()
// Test for cancelling the job in a middle state.
tk.MustExec("create table t (a int default 1, b int default 2, c int default 3, d int default 4, " +
"index(a), index(b), index(c), index(d));")
tk.MustExec("insert into t values ();")
hook := newCancelJobHook(t, store, dom, func(job *model.Job) bool {
// Cancel job when the column 'a' is in delete-reorg.
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 3)
return job.MultiSchemaInfo.SubJobs[1].SchemaState == model.StateDeleteReorganization
})
dom.DDL().SetHook(hook)
tk.MustExec("alter table t drop column b, drop column a, drop column d;")
dom.DDL().SetHook(originHook)
hook.MustCancelFailed(t)
tk.MustQuery("select * from t;").Check(testkit.Rows("3"))
}
func TestMultiSchemaChangeDropColumnsParallel(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (a int, b int, c int);")
putTheSameDDLJobTwice(t, func() {
tk.MustExec("alter table t drop column if exists b, drop column if exists c;")
tk.MustQuery("show warnings").Check(testkit.Rows(
"Note 1091 column b doesn't exist",
"Note 1091 column c doesn't exist"))
})
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int);")
putTheSameDDLJobTwice(t, func() {
tk.MustGetErrCode("alter table t drop column b, drop column a;", errno.ErrCantDropFieldOrKey)
})
}
func TestMultiSchemaChangeAddDropColumns(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
// [a, b] -> [+c, -a, +d, -b] -> [c, d]
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustExec("alter table t add column c int default 3, drop column a, add column d int default 4, drop column b;")
tk.MustQuery("select * from t;").Check(testkit.Rows("3 4"))
// [a, b] -> [-a, -b, +c, +d] -> [c, d]
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustExec("alter table t drop column a, drop column b, add column c int default 3, add column d int default 4;")
tk.MustQuery("select * from t;").Check(testkit.Rows("3 4"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t add column c int default 3 after a, add column d int default 4 first, drop column a, drop column b;", errno.ErrUnsupportedDDLOperation)
}
func TestMultiSchemaChangeRenameColumns(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
originHook := dom.DDL().GetHook()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
// unsupported ddl operations
{
// Test add and rename to same column name
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t rename column b to c, add column c int", errno.ErrUnsupportedDDLOperation)
// Test add column related with rename column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t rename column b to c, add column e int after b", errno.ErrUnsupportedDDLOperation)
// Test drop and rename with same column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t drop column b, rename column b to c", errno.ErrUnsupportedDDLOperation)
// Test add index and rename with same column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2, index t(a, b));")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t rename column b to c, add index t1(a, b)", errno.ErrUnsupportedDDLOperation)
}
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2, index t(a, b));")
tk.MustExec("insert into t values ();")
tk.MustExec("alter table t rename column b to c, add column e int default 3")
tk.MustQuery("select c from t").Check(testkit.Rows("2"))
tk.MustQuery("select * from t").Check(testkit.Rows("1 2 3"))
// Test cancel job with rename columns
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int default 1, b int default 2)")
tk.MustExec("insert into t values ()")
hook := newCancelJobHook(t, store, dom, func(job *model.Job) bool {
// Cancel job when the column 'c' is in write-reorg.
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 2)
return job.MultiSchemaInfo.SubJobs[0].SchemaState == model.StateWriteReorganization
})
dom.DDL().SetHook(hook)
tk.MustGetErrCode("alter table t add column c int default 3, rename column b to d;", errno.ErrCancelledDDLJob)
dom.DDL().SetHook(originHook)
tk.MustQuery("select b from t").Check(testkit.Rows("2"))
tk.MustGetErrCode("select d from t", errno.ErrBadField)
// Test dml stmts when do rename
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int default 1, b int default 2)")
tk.MustExec("insert into t values ()")
hook1 := &ddl.TestDDLCallback{Do: dom}
hook1.OnJobRunBeforeExported = func(job *model.Job) {
assert.Equal(t, model.ActionMultiSchemaChange, job.Type)
if job.MultiSchemaInfo.SubJobs[0].SchemaState == model.StateWriteReorganization {
rs, _ := tk.Exec("select b from t")
assert.Equal(t, tk.ResultSetToResult(rs, "").Rows()[0][0], "2")
}
}
dom.DDL().SetHook(hook1)
tk.MustExec("alter table t add column c int default 3, rename column b to d;")
dom.DDL().SetHook(originHook)
tk.MustQuery("select d from t").Check(testkit.Rows("2"))
tk.MustGetErrCode("select b from t", errno.ErrBadField)
}
func TestMultiSchemaChangeAlterColumns(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
originHook := dom.DDL().GetHook()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
// unsupported ddl operations
{
// Test alter and drop with same column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t alter column b set default 3, drop column b", errno.ErrUnsupportedDDLOperation)
// Test alter and rename with same column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t alter column b set default 3, rename column b to c", errno.ErrUnsupportedDDLOperation)
// Test alter and drop modify same column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t alter column b set default 3, modify column b double", errno.ErrUnsupportedDDLOperation)
}
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2, index t(a, b));")
tk.MustExec("insert into t values ();")
tk.MustQuery("select * from t").Check(testkit.Rows("1 2"))
tk.MustExec("alter table t rename column a to c, alter column b set default 3;")
tk.MustExec("truncate table t;")
tk.MustExec("insert into t values ();")
tk.MustQuery("select * from t").Check(testkit.Rows("1 3"))
// Test cancel job with alter columns
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int default 1, b int default 2)")
hook := newCancelJobHook(t, store, dom, func(job *model.Job) bool {
// Cancel job when the column 'a' is in write-reorg.
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 2)
return job.MultiSchemaInfo.SubJobs[0].SchemaState == model.StateWriteReorganization
})
dom.DDL().SetHook(hook)
tk.MustGetErrCode("alter table t add column c int default 3, alter column b set default 3;", errno.ErrCancelledDDLJob)
dom.DDL().SetHook(originHook)
tk.MustExec("insert into t values ()")
tk.MustQuery("select * from t").Check(testkit.Rows("1 2"))
// Test dml stmts when do alter
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int default 1, b int default 2)")
hook1 := &ddl.TestDDLCallback{Do: dom}
hook1.OnJobRunBeforeExported = func(job *model.Job) {
assert.Equal(t, model.ActionMultiSchemaChange, job.Type)
if job.MultiSchemaInfo.SubJobs[0].SchemaState == model.StateWriteOnly {
tk2 := testkit.NewTestKit(t, store)
tk2.MustExec("insert into test.t values ()")
}
}
dom.DDL().SetHook(hook1)
tk.MustExec("alter table t add column c int default 3, alter column b set default 3;")
dom.DDL().SetHook(originHook)
tk.MustQuery("select * from t").Check(testkit.Rows("1 2 3"))
}
func TestMultiSchemaChangeChangeColumns(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
originHook := dom.DDL().GetHook()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
// unsupported ddl operations
{
// Test change and drop with same column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t change column b c double, drop column b", errno.ErrUnsupportedDDLOperation)
// Test change and add with same column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t change column b c double, add column c int", errno.ErrUnsupportedDDLOperation)
// Test add index and rename with same column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2, index t(a, b));")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t change column b c double, add index t1(a, b)", errno.ErrUnsupportedDDLOperation)
}
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2, index t(a, b));")
tk.MustExec("insert into t values ();")
tk.MustExec("alter table t rename column b to c, change column a e bigint default 3;")
tk.MustQuery("select e,c from t").Check(testkit.Rows("1 2"))
tk.MustExec("truncate table t;")
tk.MustExec("insert into t values ();")
tk.MustQuery("select e,c from t").Check(testkit.Rows("3 2"))
// Test cancel job with change columns
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int default 1, b int default 2)")
tk.MustExec("insert into t values ()")
hook := newCancelJobHook(t, store, dom, func(job *model.Job) bool {
// Cancel job when the column 'c' is in write-reorg.
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 2)
return job.MultiSchemaInfo.SubJobs[0].SchemaState == model.StateWriteReorganization
})
dom.DDL().SetHook(hook)
tk.MustGetErrCode("alter table t add column c int default 3, change column b d bigint default 4;", errno.ErrCancelledDDLJob)
dom.DDL().SetHook(originHook)
tk.MustQuery("select b from t").Check(testkit.Rows("2"))
tk.MustGetErrCode("select d from t", errno.ErrBadField)
}
func TestMultiSchemaChangeAddIndexes(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
// Test add multiple indexes with same column.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int);")
tk.MustExec("insert into t values (1, 2, 3);")
tk.MustExec("alter table t add index t(a, b), add index t1(a);")
tk.MustExec("alter table t add index t2(a), add index t3(a, b);")
tk.MustQuery("select * from t use index (t, t1, t2, t3);").Check(testkit.Rows("1 2 3"))
tk.MustExec("admin check table t;")
// Test add multiple indexes with same name.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, c int)")
tk.MustGetErrCode("alter table t add index t(a), add index t(b)", errno.ErrUnsupportedDDLOperation)
tk.MustQuery("show index from t;").Check(testkit.Rows( /* no index */ ))
// Test add indexes with drop column.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, c int)")
tk.MustGetErrCode("alter table t add index t(a), drop column a", errno.ErrUnsupportedDDLOperation)
tk.MustGetErrCode("alter table t add index t(a, b), drop column a", errno.ErrUnsupportedDDLOperation)
tk.MustQuery("show index from t;").Check(testkit.Rows( /* no index */ ))
// Test add index failed.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int);")
tk.MustExec("insert into t values (1, 1, 1), (2, 2, 2), (3, 3, 1);")
tk.MustGetErrCode("alter table t add unique index i1(a), add unique index i2(a, b), add unique index i3(c);",
errno.ErrDupEntry)
tk.MustQuery("show index from t;").Check(testkit.Rows( /* no index */ ))
tk.MustExec("alter table t add index i1(a), add index i2(a, b), add index i3(c);")
}
func TestMultiSchemaChangeAddIndexesCancelled(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
originHook := dom.DDL().GetHook()
// Test cancel successfully.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int);")
tk.MustExec("insert into t values (1, 2, 3);")
cancelHook := newCancelJobHook(t, store, dom, func(job *model.Job) bool {
// Cancel the job when index 't2' is in write-reorg.
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 4)
return job.MultiSchemaInfo.SubJobs[2].SchemaState == model.StateWriteReorganization
})
dom.DDL().SetHook(cancelHook)
tk.MustGetErrCode("alter table t "+
"add index t(a, b), add index t1(a), "+
"add index t2(a), add index t3(a, b);", errno.ErrCancelledDDLJob)
dom.DDL().SetHook(originHook)
cancelHook.MustCancelDone(t)
tk.MustQuery("show index from t;").Check(testkit.Rows( /* no index */ ))
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3"))
tk.MustExec("admin check table t;")
// Test cancel failed when some sub-jobs have been finished.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int);")
tk.MustExec("insert into t values (1, 2, 3);")
cancelHook = newCancelJobHook(t, store, dom, func(job *model.Job) bool {
// Cancel the job when index 't1' is in public.
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 4)
return job.MultiSchemaInfo.SubJobs[1].SchemaState == model.StatePublic
})
dom.DDL().SetHook(cancelHook)
tk.MustExec("alter table t add index t(a, b), add index t1(a), " +
"add index t2(a), add index t3(a, b);")
dom.DDL().SetHook(originHook)
cancelHook.MustCancelFailed(t)
tk.MustQuery("select * from t use index(t, t1, t2, t3);").Check(testkit.Rows("1 2 3"))
tk.MustExec("admin check table t;")
}
func TestMultiSchemaChangeDropIndexes(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
// Test drop same index.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int, index t(a));")
tk.MustGetErrCode("alter table t drop index t, drop index t", errno.ErrUnsupportedDDLOperation)
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (id int, c1 int, c2 int, primary key(id) nonclustered, key i1(c1), key i2(c2), key i3(c1, c2));")
tk.MustExec("insert into t values (1, 2, 3);")
tk.MustExec("alter table t drop index i1, drop index i2;")
tk.MustGetErrCode("select * from t use index(i1);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index(i2);", errno.ErrKeyDoesNotExist)
tk.MustExec("alter table t drop index i3, drop primary key;")
tk.MustGetErrCode("select * from t use index(primary);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index(i3);", errno.ErrKeyDoesNotExist)
// Test drop index with drop column.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int default 1, b int default 2, c int default 3, index t(a))")
tk.MustExec("insert into t values ();")
tk.MustExec("alter table t drop index t, drop column a")
tk.MustGetErrCode("select * from t force index(t)", errno.ErrKeyDoesNotExist)
}
func TestMultiSchemaChangeDropIndexesCancelled(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
originHook := dom.DDL().GetHook()
// Test for cancelling the job in a middle state.
tk.MustExec("create table t (a int, b int, index(a), unique index(b), index idx(a, b));")
hook := newCancelJobHook(t, store, dom, func(job *model.Job) bool {
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 3)
return job.MultiSchemaInfo.SubJobs[1].SchemaState == model.StateDeleteOnly
})
dom.DDL().SetHook(hook)
tk.MustExec("alter table t drop index a, drop index b, drop index idx;")
dom.DDL().SetHook(originHook)
hook.MustCancelFailed(t)
tk.MustGetErrCode("select * from t use index (a);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index (b);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index (idx);", errno.ErrKeyDoesNotExist)
// Test for cancelling the job in none state.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, index(a), unique index(b), index idx(a, b));")
hook = newCancelJobHook(t, store, dom, func(job *model.Job) bool {
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 3)
return job.MultiSchemaInfo.SubJobs[1].SchemaState == model.StatePublic
})
dom.DDL().SetHook(hook)
tk.MustGetErrCode("alter table t drop index a, drop index b, drop index idx;", errno.ErrCancelledDDLJob)
dom.DDL().SetHook(originHook)
hook.MustCancelDone(t)
tk.MustQuery("select * from t use index (a);").Check(testkit.Rows())
tk.MustQuery("select * from t use index (b);").Check(testkit.Rows())
tk.MustQuery("select * from t use index (idx);").Check(testkit.Rows())
}
func TestMultiSchemaChangeDropIndexesParallel(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (a int, b int, c int, index(a), index(b), index(c));")
putTheSameDDLJobTwice(t, func() {
tk.MustExec("alter table t drop index if exists b, drop index if exists c;")
tk.MustQuery("show warnings").Check(testkit.Rows(
"Note 1091 index b doesn't exist",
"Note 1091 index c doesn't exist"))
})
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int, index (a), index(b), index(c));")
putTheSameDDLJobTwice(t, func() {
tk.MustGetErrCode("alter table t drop index b, drop index a;", errno.ErrCantDropFieldOrKey)
})
}
func TestMultiSchemaChangeAddDropIndexes(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
// Test add and drop same index.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, c int, index t(a))")
tk.MustGetErrCode("alter table t drop index t, add index t(b)", errno.ErrDupKeyName)
// Test add and drop same index.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, c int, index t(a))")
tk.MustGetErrCode("alter table t add index t1(b), drop index t1", errno.ErrCantDropFieldOrKey)
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int, index (a), index(b), index(c));")
tk.MustExec("insert into t values (1, 2, 3);")
tk.MustExec("alter table t add index xa(a), drop index a, add index xc(c), drop index b, drop index c, add index xb(b);")
tk.MustQuery("select * from t use index(xa, xb, xc);").Check(testkit.Rows("1 2 3"))
tk.MustGetErrCode("select * from t use index(a);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index(b);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index(c);", errno.ErrKeyDoesNotExist)
tk.MustExec("admin check table t;")
}
func TestMultiSchemaChangeRenameIndexes(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
originHook := dom.DDL().GetHook()
// Test rename index.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, c int, index t(a), index t1(b))")
tk.MustExec("alter table t rename index t to x, rename index t1 to x1")
tk.MustExec("select * from t use index (x);")
tk.MustExec("select * from t use index (x1);")
tk.MustGetErrCode("select * from t use index (t);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index (t1);", errno.ErrKeyDoesNotExist)
// Test drop and rename same index.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, c int, index t(a))")
tk.MustGetErrCode("alter table t drop index t, rename index t to t1", errno.ErrUnsupportedDDLOperation)
// Test add and rename to same index name.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, c int, index t(a))")
tk.MustGetErrCode("alter table t add index t1(b), rename index t to t1", errno.ErrUnsupportedDDLOperation)
// Test drop column with rename index.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int default 1, b int default 2, c int default 3, index t(a))")
tk.MustExec("insert into t values ();")
tk.MustExec("alter table t drop column a, rename index t to x")
tk.MustGetErrCode("select * from t use index (x);", errno.ErrKeyDoesNotExist)
tk.MustQuery("select * from t;").Check(testkit.Rows("2 3"))
// Test cancel job with renameIndex
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int default 1, b int default 2, index t(a))")
tk.MustExec("insert into t values ()")
hook := newCancelJobHook(t, store, dom, func(job *model.Job) bool {
// Cancel job when the column 'c' is in write-reorg.
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 2)
return job.MultiSchemaInfo.SubJobs[0].SchemaState == model.StateWriteReorganization
})
dom.DDL().SetHook(hook)
tk.MustGetErrCode("alter table t add column c int default 3, rename index t to t1;", errno.ErrCancelledDDLJob)
dom.DDL().SetHook(originHook)
tk.MustQuery("select * from t use index (t);").Check(testkit.Rows("1 2"))
tk.MustGetErrCode("select * from t use index (t1);", errno.ErrKeyDoesNotExist)
}
func TestMultiSchemaChangeModifyColumns(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
// unsupported ddl operations
{
// Test modify the same column twice.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t modify column a int default 2, modify column a bigint;", errno.ErrUnsupportedDDLOperation)
// Test modify and drop with same column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t modify column b double, drop column b", errno.ErrUnsupportedDDLOperation)
// Test modify column related with dropped column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2, c int default 3);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t modify column b double after c, drop column c", errno.ErrUnsupportedDDLOperation)
// Test modify column related with add index
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int, b int);")
tk.MustExec("insert into t values (1, 2);")
tk.MustGetErrCode("alter table t add index i(a), modify column a int null default 1 after a;", errno.ErrUnsupportedDDLOperation)
// Test modify column related with add primary index
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int, b int);")
tk.MustExec("insert into t values (1, 2);")
tk.MustGetErrCode("alter table t add primary key(a), modify column a int null default 1 after a;", errno.ErrUnsupportedDDLOperation)
// Test modify column related with expression index
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int, b int);")
tk.MustExec("insert into t values (1, 2);")
tk.MustGetErrCode("alter table t modify column b double, add index idx((a + b));", errno.ErrUnsupportedDDLOperation)
}
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustExec("alter table t modify column b double default 2 after a, add column c int default 3 after a;")
tk.MustQuery("select * from t").Check(testkit.Rows("1 3 2"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int);")
tk.MustExec("insert into t values (1, 2, 3);")
tk.MustExec("alter table t modify column a bigint, modify column b bigint;")
tk.MustExec("insert into t values (9223372036854775807, 9223372036854775807, 1);")
tk.MustQuery("select * from t;").Check(
testkit.Rows("1 2 3", "9223372036854775807 9223372036854775807 1"))
// Modify index-covered columns.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int, index i1(a), index i2(b), index i3(c), index i4(a, b), index i5(a, b, c));")
tk.MustExec("insert into t values (1, 2, 3);")
tk.MustExec("alter table t modify column a tinyint, modify column b tinyint, modify column c tinyint;")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3"))
tk.MustQuery("select * from t use index(i1, i2, i3, i4, i5);").Check(testkit.Rows("1 2 3"))
tk.MustExec("admin check table t;")
// Modify index-covered columns with position change.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int, index i1(a), index i2(b), index i3(c), index i4(a, b), index i5(a, b, c));")
tk.MustExec("insert into t values (1, 2, 3);")
tk.MustExec("alter table t modify column a tinyint after c, modify column b tinyint, modify column c tinyint first;")
tk.MustQuery("select * from t;").Check(testkit.Rows("3 2 1"))
tk.MustQuery("select * from t use index(i1, i2, i3, i4, i5);").Check(testkit.Rows("3 2 1"))
tk.MustExec("admin check table t;")
// Modify columns that require and don't require reorganization of data.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int, index i1(a), index i2(c, b));")
tk.MustExec("insert into t values (1, 2, 3), (11, 22, 33);")
tk.MustExec("alter table t modify column b char(255) after c, modify column a bigint;")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 3 2", "11 33 22"))
tk.MustQuery("select * from t use index(i1, i2);").Check(testkit.Rows("1 3 2", "11 33 22"))
tk.MustExec("admin check table t;")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a bigint null default '1761233443433596323', index t(a));")
tk.MustExec("insert into t set a = '-7184819032643664798';")
tk.MustGetErrCode("alter table t change column a b datetime null default '8972-12-24 10:56:03', rename index t to t1;", errno.ErrTruncatedWrongValue)
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b double, index i(a, b));")
tk.MustExec("alter table t rename index i to i1, change column b c int;")
tk.MustQuery("select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1';").Check(testkit.Rows("1"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b double, index i(a, b), index _Idx$_i(a, b));")
tk.MustExec("alter table t rename index i to i1, change column b c int;")
tk.MustQuery("select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1';").Check(testkit.Rows("1"))
tk.MustQuery("select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='_Idx$_i';").Check(testkit.Rows("1"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, _Col$_a double, index _Idx$_i(a, _Col$_a), index i(a, _Col$_a));")
tk.MustExec("alter table t modify column a tinyint;")
tk.MustQuery("select count(distinct KEY_NAME) from information_schema.TIDB_INDEXES where TABLE_NAME='t';").Check(testkit.Rows("2"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a BIGINT NULL DEFAULT '-283977870758975838', b double);")
tk.MustExec("insert into t values (-283977870758975838, 0);")
tk.MustGetErrCode("alter table t change column a c tinyint null default '111' after b, modify column b time null default '13:51:02' FIRST;", errno.ErrDataOutOfRange)
rows := tk.MustQuery("admin show ddl jobs 1").Rows()
require.Equal(t, rows[0][11], "rollback done")
require.Equal(t, rows[1][11], "rollback done")
require.Equal(t, rows[2][11], "cancelled")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int, b int);")
tk.MustExec("insert into t values (1, 2);")
tk.MustGetErrCode("alter table t add index i(b), modify column a int null default 1 after a;", errno.ErrBadField)
// Test rolling back modify column with reorganization.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a char(3), b int, unique index i1(a), index i2(a, b));")
tk.MustExec("insert into t values ('aaa', 1), ('aa', 2);")
tk.MustExec("set @@sql_mode = '';") // Make it possible to truncate 'aaa' to 'aa'.
tk.MustGetErrCode("alter table t modify column b tinyint, modify column a char(2);", errno.ErrDupEntry)
}
func TestMultiSchemaChangeModifyColumnsCancelled(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
originHook := dom.DDL().GetHook()
// Test for cancelling the job in a middle state.
tk.MustExec("create table t (a int, b int, c int, index i1(a), unique index i2(b), index i3(a, b));")
tk.MustExec("insert into t values (1, 2, 3);")
hook := newCancelJobHook(t, store, dom, func(job *model.Job) bool {
if job.Type != model.ActionMultiSchemaChange {
return false
}
assertMultiSchema(t, job, 3)
return job.MultiSchemaInfo.SubJobs[2].SchemaState == model.StateWriteReorganization
})
dom.DDL().SetHook(hook)
sql := "alter table t modify column a tinyint, modify column b bigint, modify column c char(20);"
tk.MustGetErrCode(sql, errno.ErrCancelledDDLJob)
dom.DDL().SetHook(originHook)
hook.MustCancelDone(t)
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3"))
tk.MustQuery("select * from t use index (i1, i2, i3);").Check(testkit.Rows("1 2 3"))
tk.MustExec("admin check table t;")
tk.MustQuery("select data_type from information_schema.columns where table_name = 't' and column_name = 'c';").
Check(testkit.Rows("int"))
}
func TestMultiSchemaChangeAlterIndex(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
// unsupported ddl operations
{
// Test alter the same index
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, index idx(a, b));")
tk.MustGetErrCode("alter table t alter index idx visible, alter index idx invisible;", errno.ErrUnsupportedDDLOperation)
// Test drop and alter the same index
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, index idx(a, b));")
tk.MustGetErrCode("alter table t drop index idx, alter index idx visible;", errno.ErrUnsupportedDDLOperation)
// Test add and alter the same index
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int);")
tk.MustGetErrCode("alter table t add index idx(a, b), alter index idx invisible", errno.ErrKeyDoesNotExist)
}
tk.MustExec("drop table t;")
tk.MustExec("create table t (a int, b int, index i1(a, b), index i2(b));")
tk.MustExec("insert into t values (1, 2);")
tk.MustExec("alter table t modify column a tinyint, alter index i2 invisible, alter index i1 invisible;")
tk.MustGetErrCode("select * from t use index (i1);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index (i2);", errno.ErrKeyDoesNotExist)
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2"))
tk.MustExec("admin check table t;")
tk.MustExec("drop table t;")
tk.MustExec("create table t (a int, b int, index i1(a, b), index i2(b));")
tk.MustExec("insert into t values (1, 2);")
originHook := dom.DDL().GetHook()
var checked bool
callback := &ddl.TestDDLCallback{Do: dom}
onJobUpdatedExportedFunc := func(job *model.Job) {
assert.NotNil(t, job.MultiSchemaInfo)
// "modify column a tinyint" in write-reorg.
if job.MultiSchemaInfo.SubJobs[1].SchemaState == model.StateWriteReorganization {
checked = true
rs, err := tk.Exec("select * from t use index(i1);")
assert.NoError(t, err)
assert.NoError(t, rs.Close())
}
}
callback.OnJobUpdatedExported.Store(&onJobUpdatedExportedFunc)
dom.DDL().SetHook(callback)
tk.MustExec("alter table t alter index i1 invisible, modify column a tinyint, alter index i2 invisible;")
dom.DDL().SetHook(originHook)
require.True(t, checked)
tk.MustGetErrCode("select * from t use index (i1);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index (i2);", errno.ErrKeyDoesNotExist)
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2"))
tk.MustExec("admin check table t;")
}
func TestMultiSchemaChangeMix(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")