forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_integration_test.go
3864 lines (3437 loc) · 170 KB
/
db_integration_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 2018 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 (
"bytes"
"context"
"fmt"
"math"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/testkit/external"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/dbterror"
"github.com/pingcap/tidb/util/mock"
"github.com/stretchr/testify/require"
)
func TestNoZeroDateMode(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
defer tk.MustExec("set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';")
tk.MustExec("use test;")
tk.MustExec("set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION';")
tk.MustGetErrCode("create table test_zero_date(agent_start_time date NOT NULL DEFAULT '0000-00-00')", errno.ErrInvalidDefault)
tk.MustGetErrCode("create table test_zero_date(agent_start_time datetime NOT NULL DEFAULT '0000-00-00 00:00:00')", errno.ErrInvalidDefault)
tk.MustGetErrCode("create table test_zero_date(agent_start_time timestamp NOT NULL DEFAULT '0000-00-00 00:00:00')", errno.ErrInvalidDefault)
tk.MustGetErrCode("create table test_zero_date(a timestamp default '0000-00-00 00');", errno.ErrInvalidDefault)
tk.MustGetErrCode("create table test_zero_date(a timestamp default 0);", errno.ErrInvalidDefault)
defer tk.MustExec(`drop table if exists test_zero_date`)
tk.MustExec("set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';")
tk.MustExec("create table test_zero_date (a timestamp default 0)")
tk.MustExec(`insert into test_zero_date values (0)`)
tk.MustQuery(`select a, unix_timestamp(a) from test_zero_date`).Check(testkit.Rows("0000-00-00 00:00:00 0"))
tk.MustExec(`update test_zero_date set a = '2001-01-01 11:11:11' where a = 0`)
tk.MustExec(`replace into test_zero_date values (0)`)
tk.MustExec(`delete from test_zero_date where a = 0`)
tk.MustExec(`update test_zero_date set a = 0 where a = '2001-01-01 11:11:11'`)
tk.MustExec("set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';")
tk.MustGetErrCode(`insert into test_zero_date values (0)`, errno.ErrTruncatedWrongValue)
tk.MustGetErrCode(`replace into test_zero_date values (0)`, errno.ErrTruncatedWrongValue)
tk.MustGetErrCode(`update test_zero_date set a = 0 where a = 0`, errno.ErrTruncatedWrongValue)
tk.MustExec(`delete from test_zero_date where a = 0`)
tk.MustQuery(`select a, unix_timestamp(a) from test_zero_date`).Check(testkit.Rows())
tk.MustExec(`drop table test_zero_date`)
tk.MustExec("set session sql_mode=''")
tk.MustExec("create table test_zero_date (a timestamp default 0)")
tk.MustExec(`drop table test_zero_date`)
tk.MustExec(`create table test_zero_date (a int)`)
tk.MustExec(`insert into test_zero_date values (0)`)
tk.MustExec(`alter table test_zero_date modify a date`)
tk.MustExec("set session sql_mode='NO_ZERO_DATE'")
tk.MustExec(`drop table test_zero_date`)
tk.MustExec("create table test_zero_date (a timestamp default 0)")
tk.MustExec(`drop table test_zero_date`)
tk.MustExec(`create table test_zero_date (a int)`)
tk.MustExec(`insert into test_zero_date values (0)`)
tk.MustExec(`alter table test_zero_date modify a date`)
tk.MustExec("set session sql_mode='STRICT_TRANS_TABLES'")
tk.MustExec(`drop table test_zero_date`)
tk.MustExec("create table test_zero_date (a timestamp default 0)")
tk.MustExec(`drop table test_zero_date`)
tk.MustExec(`create table test_zero_date (a int)`)
tk.MustExec(`insert into test_zero_date values (0)`)
tk.MustGetErrCode(`alter table test_zero_date modify a date`, errno.ErrTruncatedWrongValue)
tk.MustExec("set session sql_mode='NO_ZERO_DATE,STRICT_TRANS_TABLES'")
tk.MustExec(`drop table test_zero_date`)
tk.MustGetErrCode("create table test_zero_date (a timestamp default 0)", errno.ErrInvalidDefault)
tk.MustExec(`create table test_zero_date (a int)`)
tk.MustExec(`insert into test_zero_date values (0)`)
tk.MustGetErrCode(`alter table test_zero_date modify a date`, errno.ErrTruncatedWrongValue)
}
func TestInvalidDefault(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("USE test;")
_, err := tk.Exec("create table t(c1 decimal default 1.7976931348623157E308)")
require.Error(t, err)
require.Truef(t, terror.ErrorEqual(err, types.ErrInvalidDefault), "err %v", err)
_, err = tk.Exec("create table t( c1 varchar(2) default 'TiDB');")
require.Error(t, err)
require.Truef(t, terror.ErrorEqual(err, types.ErrInvalidDefault), "err %v", err)
}
// TestKeyWithoutLength for issue #13452
func TestKeyWithoutLengthCreateTable(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("USE test")
_, err := tk.Exec("create table t_without_length (a text primary key)")
require.Error(t, err)
require.Regexp(t, ".*BLOB/TEXT column 'a' used in key specification without a key length", err.Error())
}
// TestInvalidNameWhenCreateTable for issue #3848
func TestInvalidNameWhenCreateTable(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("USE test;")
tk.MustGetErrCode("create table t(xxx.t.a bigint)", errno.ErrWrongDBName)
tk.MustGetErrCode("create table t(test.tttt.a bigint)", errno.ErrWrongTableName)
tk.MustGetErrCode("create table t(t.tttt.a bigint)", errno.ErrWrongDBName)
}
// TestCreateTableIfNotExists for issue #6879
func TestCreateTableIfNotExists(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("USE test;")
tk.MustExec("create table ct1(a bigint)")
tk.MustExec("create table ct(a bigint)")
// Test duplicate create-table with `LIKE` clause
tk.MustExec("create table if not exists ct like ct1;")
warnings := tk.Session().GetSessionVars().StmtCtx.GetWarnings()
require.GreaterOrEqual(t, len(warnings), 1)
lastWarn := warnings[len(warnings)-1]
require.Truef(t, terror.ErrorEqual(infoschema.ErrTableExists, lastWarn.Err), "err %v", lastWarn.Err)
require.Equal(t, stmtctx.WarnLevelNote, lastWarn.Level)
// Test duplicate create-table without `LIKE` clause
tk.MustExec("create table if not exists ct(b bigint, c varchar(60));")
warnings = tk.Session().GetSessionVars().StmtCtx.GetWarnings()
require.GreaterOrEqual(t, len(warnings), 1)
lastWarn = warnings[len(warnings)-1]
require.True(t, terror.ErrorEqual(infoschema.ErrTableExists, lastWarn.Err))
}
// for issue #9910
func TestCreateTableWithKeyWord(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("USE test;")
_, err := tk.Exec("create table t1(pump varchar(20), drainer varchar(20), node_id varchar(20), node_state varchar(20));")
require.NoError(t, err)
}
func TestUniqueKeyNullValue(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("USE test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int primary key, b varchar(255))")
tk.MustExec("insert into t values(1, NULL)")
tk.MustExec("insert into t values(2, NULL)")
tk.MustExec("alter table t add unique index b(b);")
res := tk.MustQuery("select count(*) from t use index(b);")
res.Check(testkit.Rows("2"))
tk.MustExec("admin check table t")
tk.MustExec("admin check index t b")
}
func TestUniqueKeyNullValueClusterIndex(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("drop database if exists unique_null_val;")
tk.MustExec("create database unique_null_val;")
tk.MustExec("use unique_null_val;")
tk.MustExec("create table t (a varchar(10), b float, c varchar(255), primary key (a, b));")
tk.MustExec("insert into t values ('1', 1, NULL);")
tk.MustExec("insert into t values ('2', 2, NULL);")
tk.MustExec("alter table t add unique index c(c);")
tk.MustQuery("select count(*) from t use index(c);").Check(testkit.Rows("2"))
tk.MustExec("admin check table t;")
tk.MustExec("admin check index t c;")
}
// TestModifyColumnAfterAddIndex Issue 5134
func TestModifyColumnAfterAddIndex(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table city (city VARCHAR(2) KEY);")
tk.MustExec("alter table city change column city city varchar(50);")
tk.MustExec(`insert into city values ("abc"), ("abd");`)
}
func TestIssue2293(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t_issue_2293 (a int)")
tk.MustGetErrCode("alter table t_issue_2293 add b int not null default 'a'", errno.ErrInvalidDefault)
tk.MustExec("insert into t_issue_2293 value(1)")
tk.MustQuery("select * from t_issue_2293").Check(testkit.Rows("1"))
}
func TestIssue6101(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1 (quantity decimal(2) unsigned);")
_, err := tk.Exec("insert into t1 values (500), (-500), (~0), (-1);")
terr := errors.Cause(err).(*terror.Error)
require.Equal(t, errors.ErrCode(errno.ErrWarnDataOutOfRange), terr.Code())
tk.MustExec("drop table t1")
tk.MustExec("set sql_mode=''")
tk.MustExec("create table t1 (quantity decimal(2) unsigned);")
tk.MustExec("insert into t1 values (500), (-500), (~0), (-1);")
tk.MustQuery("select * from t1").Check(testkit.Rows("99", "0", "99", "0"))
tk.MustExec("drop table t1")
}
func TestIssue19229(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("CREATE TABLE enumt (type enum('a', 'b') );")
_, err := tk.Exec("insert into enumt values('xxx');")
terr := errors.Cause(err).(*terror.Error)
require.Equal(t, errors.ErrCode(errno.WarnDataTruncated), terr.Code())
_, err = tk.Exec("insert into enumt values(-1);")
terr = errors.Cause(err).(*terror.Error)
require.Equal(t, errors.ErrCode(errno.WarnDataTruncated), terr.Code())
tk.MustExec("drop table enumt")
tk.MustExec("CREATE TABLE sett (type set('a', 'b') );")
_, err = tk.Exec("insert into sett values('xxx');")
terr = errors.Cause(err).(*terror.Error)
require.Equal(t, errors.ErrCode(errno.WarnDataTruncated), terr.Code())
_, err = tk.Exec("insert into sett values(-1);")
terr = errors.Cause(err).(*terror.Error)
require.Equal(t, errors.ErrCode(errno.WarnDataTruncated), terr.Code())
tk.MustExec("drop table sett")
}
func TestIndexLength(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table idx_len(a int(0), b timestamp(0), c datetime(0), d time(0), f float(0), g decimal(0))")
tk.MustExec("create index idx on idx_len(a)")
tk.MustExec("alter table idx_len add index idxa(a)")
tk.MustExec("create index idx1 on idx_len(b)")
tk.MustExec("alter table idx_len add index idxb(b)")
tk.MustExec("create index idx2 on idx_len(c)")
tk.MustExec("alter table idx_len add index idxc(c)")
tk.MustExec("create index idx3 on idx_len(d)")
tk.MustExec("alter table idx_len add index idxd(d)")
tk.MustExec("create index idx4 on idx_len(f)")
tk.MustExec("alter table idx_len add index idxf(f)")
tk.MustExec("create index idx5 on idx_len(g)")
tk.MustExec("alter table idx_len add index idxg(g)")
tk.MustExec("create table idx_len1(a int(0), b timestamp(0), c datetime(0), d time(0), f float(0), g decimal(0), index(a), index(b), index(c), index(d), index(f), index(g))")
tk.MustExec("drop table idx_len;")
tk.MustExec("create table idx_len(a text, b text charset ascii, c blob, index(a(768)), index (b(3072)), index (c(3072)));")
tk.MustExec("drop table idx_len;")
tk.MustExec("create table idx_len(a text, b text charset ascii, c blob);")
tk.MustExec("alter table idx_len add index (a(768))")
tk.MustExec("alter table idx_len add index (b(3072))")
tk.MustExec("alter table idx_len add index (c(3072))")
tk.MustExec("drop table idx_len;")
}
func TestIssue3833(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table issue3833 (b char(0), c binary(0), d varchar(0))")
tk.MustGetErrCode("create index idx on issue3833 (b)", errno.ErrWrongKeyColumn)
tk.MustGetErrCode("alter table issue3833 add index idx (b)", errno.ErrWrongKeyColumn)
tk.MustGetErrCode("create table issue3833_2 (b char(0), c binary(0), d varchar(0), index(b))", errno.ErrWrongKeyColumn)
tk.MustGetErrCode("create index idx on issue3833 (c)", errno.ErrWrongKeyColumn)
tk.MustGetErrCode("alter table issue3833 add index idx (c)", errno.ErrWrongKeyColumn)
tk.MustGetErrCode("create table issue3833_2 (b char(0), c binary(0), d varchar(0), index(c))", errno.ErrWrongKeyColumn)
tk.MustGetErrCode("create index idx on issue3833 (d)", errno.ErrWrongKeyColumn)
tk.MustGetErrCode("alter table issue3833 add index idx (d)", errno.ErrWrongKeyColumn)
tk.MustGetErrCode("create table issue3833_2 (b char(0), c binary(0), d varchar(0), index(d))", errno.ErrWrongKeyColumn)
}
func TestIssue2858And2717(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t_issue_2858_bit (a bit(64) default b'0')")
tk.MustExec("insert into t_issue_2858_bit value ()")
tk.MustExec(`insert into t_issue_2858_bit values (100), ('10'), ('\0')`)
tk.MustQuery("select a+0 from t_issue_2858_bit").Check(testkit.Rows("0", "100", "12592", "0"))
tk.MustExec(`alter table t_issue_2858_bit alter column a set default '\0'`)
tk.MustExec("create table t_issue_2858_hex (a int default 0x123)")
tk.MustExec("insert into t_issue_2858_hex value ()")
tk.MustExec("insert into t_issue_2858_hex values (123), (0x321)")
tk.MustQuery("select a from t_issue_2858_hex").Check(testkit.Rows("291", "123", "801"))
tk.MustExec(`alter table t_issue_2858_hex alter column a set default 0x321`)
}
func TestIssue4432(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table tx (col bit(10) default 'a')")
tk.MustExec("insert into tx value ()")
tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
tk.MustExec("drop table tx")
tk.MustExec("create table tx (col bit(10) default 0x61)")
tk.MustExec("insert into tx value ()")
tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
tk.MustExec("drop table tx")
tk.MustExec("create table tx (col bit(10) default 97)")
tk.MustExec("insert into tx value ()")
tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
tk.MustExec("drop table tx")
tk.MustExec("create table tx (col bit(10) default 0b1100001)")
tk.MustExec("insert into tx value ()")
tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
tk.MustExec("drop table tx")
}
func TestIssue5092(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t_issue_5092 (a int)")
tk.MustExec("alter table t_issue_5092 add column (b int, c int)")
tk.MustExec("alter table t_issue_5092 add column if not exists (b int, c int)")
tk.MustExec("alter table t_issue_5092 add column b1 int after b, add column c1 int after c")
tk.MustExec("alter table t_issue_5092 add column d int after b, add column e int first, add column f int after c1, add column g int, add column h int first")
tk.MustQuery("show create table t_issue_5092").Check(testkit.Rows("t_issue_5092 CREATE TABLE `t_issue_5092` (\n" +
" `h` int(11) DEFAULT NULL,\n" +
" `e` int(11) DEFAULT NULL,\n" +
" `a` int(11) DEFAULT NULL,\n" +
" `b` int(11) DEFAULT NULL,\n" +
" `d` int(11) DEFAULT NULL,\n" +
" `b1` int(11) DEFAULT NULL,\n" +
" `c` int(11) DEFAULT NULL,\n" +
" `c1` int(11) DEFAULT NULL,\n" +
" `f` int(11) DEFAULT NULL,\n" +
" `g` int(11) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
// The following two statements are consistent with MariaDB.
tk.MustGetErrCode("alter table t_issue_5092 add column if not exists d int, add column d int", errno.ErrDupFieldName)
tk.MustExec("alter table t_issue_5092 add column dd int, add column if not exists dd int")
tk.MustExec("alter table t_issue_5092 add column if not exists (d int, e int), add column ff text")
tk.MustExec("alter table t_issue_5092 add column b2 int after b1, add column c2 int first")
tk.MustQuery("show create table t_issue_5092").Check(testkit.Rows("t_issue_5092 CREATE TABLE `t_issue_5092` (\n" +
" `c2` int(11) DEFAULT NULL,\n" +
" `h` int(11) DEFAULT NULL,\n" +
" `e` int(11) DEFAULT NULL,\n" +
" `a` int(11) DEFAULT NULL,\n" +
" `b` int(11) DEFAULT NULL,\n" +
" `d` int(11) DEFAULT NULL,\n" +
" `b1` int(11) DEFAULT NULL,\n" +
" `b2` int(11) DEFAULT NULL,\n" +
" `c` int(11) DEFAULT NULL,\n" +
" `c1` int(11) DEFAULT NULL,\n" +
" `f` int(11) DEFAULT NULL,\n" +
" `g` int(11) DEFAULT NULL,\n" +
" `dd` int(11) DEFAULT NULL,\n" +
" `ff` text DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
tk.MustExec("drop table t_issue_5092")
tk.MustExec("create table t_issue_5092 (a int default 1)")
tk.MustExec("alter table t_issue_5092 add column (b int default 2, c int default 3)")
tk.MustExec("alter table t_issue_5092 add column b1 int default 22 after b, add column c1 int default 33 after c")
tk.MustExec("insert into t_issue_5092 value ()")
tk.MustQuery("select * from t_issue_5092").Check(testkit.Rows("1 2 22 3 33"))
tk.MustExec("alter table t_issue_5092 add column d int default 4 after c1, add column aa int default 0 first")
tk.MustQuery("select * from t_issue_5092").Check(testkit.Rows("0 1 2 22 3 33 4"))
tk.MustQuery("show create table t_issue_5092").Check(testkit.Rows("t_issue_5092 CREATE TABLE `t_issue_5092` (\n" +
" `aa` int(11) DEFAULT '0',\n" +
" `a` int(11) DEFAULT '1',\n" +
" `b` int(11) DEFAULT '2',\n" +
" `b1` int(11) DEFAULT '22',\n" +
" `c` int(11) DEFAULT '3',\n" +
" `c1` int(11) DEFAULT '33',\n" +
" `d` int(11) DEFAULT '4'\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
tk.MustExec("drop table t_issue_5092")
tk.MustExec("create table t_issue_5092 (a int)")
tk.MustExec("alter table t_issue_5092 add column (b int, c int)")
tk.MustExec("alter table t_issue_5092 drop column b,drop column c")
tk.MustGetErrCode("alter table t_issue_5092 drop column c, drop column c", errno.ErrCantDropFieldOrKey)
tk.MustExec("alter table t_issue_5092 drop column if exists b,drop column if exists c")
tk.MustGetErrCode("alter table t_issue_5092 drop column g, drop column d", errno.ErrCantDropFieldOrKey)
tk.MustExec("drop table t_issue_5092")
tk.MustExec("create table t_issue_5092 (a int)")
tk.MustExec("alter table t_issue_5092 add column (b int, c int)")
tk.MustGetErrCode("alter table t_issue_5092 drop column if exists a, drop column b, drop column c", errno.ErrCantRemoveAllFields)
tk.MustGetErrCode("alter table t_issue_5092 drop column if exists c, drop column c", errno.ErrCantDropFieldOrKey)
tk.MustExec("alter table t_issue_5092 drop column c, drop column if exists c")
tk.MustExec("drop table t_issue_5092")
}
func TestErrnoErrorCode(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
// create database
sql := "create database aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
tk.MustGetErrCode(sql, errno.ErrTooLongIdent)
sql = "create database test"
tk.MustGetErrCode(sql, errno.ErrDBCreateExists)
sql = "create database test1 character set uft8;"
tk.MustGetErrCode(sql, errno.ErrUnknownCharacterSet)
sql = "create database test2 character set gkb;"
tk.MustGetErrCode(sql, errno.ErrUnknownCharacterSet)
sql = "create database test3 character set laitn1;"
tk.MustGetErrCode(sql, errno.ErrUnknownCharacterSet)
// drop database
sql = "drop database db_not_exist"
tk.MustGetErrCode(sql, errno.ErrDBDropExists)
// create table
tk.MustExec("create table test_error_code_succ (c1 int, c2 int, c3 int, primary key(c3))")
sql = "create table test_error_code_succ (c1 int, c2 int, c3 int)"
tk.MustGetErrCode(sql, errno.ErrTableExists)
sql = "create table test_error_code1 (c1 int, c2 int, c2 int)"
tk.MustGetErrCode(sql, errno.ErrDupFieldName)
sql = "create table test_error_code1 (c1 int, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int)"
tk.MustGetErrCode(sql, errno.ErrTooLongIdent)
sql = "create table test_error_code1 (c1 int, `_tidb_rowid` int)"
tk.MustGetErrCode(sql, errno.ErrWrongColumnName)
sql = "create table aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(a int)"
tk.MustGetErrCode(sql, errno.ErrTooLongIdent)
sql = "create table test_error_code1 (c1 int, c2 int, key aa (c1, c2), key aa (c1))"
tk.MustGetErrCode(sql, errno.ErrDupKeyName)
sql = "create table test_error_code1 (c1 int, c2 int, c3 int, key(c_not_exist))"
tk.MustGetErrCode(sql, errno.ErrKeyColumnDoesNotExits)
sql = "create table test_error_code1 (c1 int, c2 int, c3 int, primary key(c_not_exist))"
tk.MustGetErrCode(sql, errno.ErrKeyColumnDoesNotExits)
sql = "create table test_error_code1 (c1 int not null default '')"
tk.MustGetErrCode(sql, errno.ErrInvalidDefault)
sql = "CREATE TABLE `t` (`a` double DEFAULT 1.0 DEFAULT 2.0 DEFAULT now());"
tk.MustGetErrCode(sql, errno.ErrInvalidDefault)
sql = "CREATE TABLE `t` (`a` double DEFAULT now());"
tk.MustGetErrCode(sql, errno.ErrInvalidDefault)
sql = "create table t1(a int) character set uft8;"
tk.MustGetErrCode(sql, errno.ErrUnknownCharacterSet)
sql = "create table t1(a int) character set gkb;"
tk.MustGetErrCode(sql, errno.ErrUnknownCharacterSet)
sql = "create table t1(a int) character set laitn1;"
tk.MustGetErrCode(sql, errno.ErrUnknownCharacterSet)
sql = "create table test_error_code (a int not null ,b int not null,c int not null, d int not null, foreign key (b, c) references product(id));"
tk.MustGetErrCode(sql, errno.ErrWrongFkDef)
sql = "create table test_error_code_2;"
tk.MustGetErrCode(sql, errno.ErrTableMustHaveColumns)
sql = "create table test_error_code_2 (unique(c1));"
tk.MustGetErrCode(sql, errno.ErrTableMustHaveColumns)
sql = "create table test_error_code_2(c1 int, c2 int, c3 int, primary key(c1), primary key(c2));"
tk.MustGetErrCode(sql, errno.ErrMultiplePriKey)
sql = "create table test_error_code_3(pt blob ,primary key (pt));"
tk.MustGetErrCode(sql, errno.ErrBlobKeyWithoutLength)
sql = "create table test_error_code_3(a text, unique (a(769)));"
tk.MustGetErrCode(sql, errno.ErrTooLongKey)
sql = "create table test_error_code_3(a text charset ascii, unique (a(3073)));"
tk.MustGetErrCode(sql, errno.ErrTooLongKey)
sql = "create table test_error_code_3(`id` int, key `primary`(`id`));"
tk.MustGetErrCode(sql, errno.ErrWrongNameForIndex)
sql = "create table t2(c1.c2 blob default null);"
tk.MustGetErrCode(sql, errno.ErrWrongTableName)
sql = "create table t2 (id int default null primary key , age int);"
tk.MustGetErrCode(sql, errno.ErrInvalidDefault)
sql = "create table t2 (id int null primary key , age int);"
tk.MustGetErrCode(sql, errno.ErrPrimaryCantHaveNull)
sql = "create table t2 (id int default null, age int, primary key(id));"
tk.MustGetErrCode(sql, errno.ErrPrimaryCantHaveNull)
sql = "create table t2 (id int null, age int, primary key(id));"
tk.MustGetErrCode(sql, errno.ErrPrimaryCantHaveNull)
sql = "create table t2 (id int auto_increment);"
tk.MustGetErrCode(sql, errno.ErrWrongAutoKey)
sql = "create table t2 (id int auto_increment, a int key);"
tk.MustGetErrCode(sql, errno.ErrWrongAutoKey)
sql = "create table t2 (a datetime(2) default current_timestamp(3));"
tk.MustGetErrCode(sql, errno.ErrInvalidDefault)
sql = "create table t2 (a datetime(2) default current_timestamp(2) on update current_timestamp);"
tk.MustGetErrCode(sql, errno.ErrInvalidOnUpdate)
sql = "create table t2 (a datetime default current_timestamp on update current_timestamp(2));"
tk.MustGetErrCode(sql, errno.ErrInvalidOnUpdate)
sql = "create table t2 (a datetime(2) default current_timestamp(2) on update current_timestamp(3));"
tk.MustGetErrCode(sql, errno.ErrInvalidOnUpdate)
sql = "create table t(a blob(10), index(a(0)));"
tk.MustGetErrCode(sql, errno.ErrKeyPart0)
sql = "create table t(a char(10), index(a(0)));"
tk.MustGetErrCode(sql, errno.ErrKeyPart0)
sql = "create table t2 (id int primary key , age int);"
tk.MustExec(sql)
// add column
sql = "alter table test_error_code_succ add column c1 int"
tk.MustGetErrCode(sql, errno.ErrDupFieldName)
sql = "alter table test_error_code_succ add column aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int"
tk.MustGetErrCode(sql, errno.ErrTooLongIdent)
sql = "alter table test_comment comment 'test comment'"
tk.MustGetErrCode(sql, errno.ErrNoSuchTable)
sql = "alter table test_error_code_succ add column `a ` int ;"
tk.MustGetErrCode(sql, errno.ErrWrongColumnName)
sql = "alter table test_error_code_succ add column `_tidb_rowid` int ;"
tk.MustGetErrCode(sql, errno.ErrWrongColumnName)
tk.MustExec("create table test_on_update (c1 int, c2 int);")
sql = "alter table test_on_update add column c3 int on update current_timestamp;"
tk.MustGetErrCode(sql, errno.ErrInvalidOnUpdate)
sql = "create table test_on_update_2(c int on update current_timestamp);"
tk.MustGetErrCode(sql, errno.ErrInvalidOnUpdate)
// add columns
sql = "alter table test_error_code_succ add column c1 int, add column c1 int"
tk.MustGetErrCode(sql, errno.ErrDupFieldName)
sql = "alter table test_error_code_succ add column (aa int, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int)"
tk.MustGetErrCode(sql, errno.ErrTooLongIdent)
sql = "alter table test_error_code_succ add column `a ` int, add column `b ` int;"
tk.MustGetErrCode(sql, errno.ErrWrongColumnName)
tk.MustExec("create table test_add_columns_on_update (c1 int, c2 int);")
sql = "alter table test_add_columns_on_update add column cc int, add column c3 int on update current_timestamp;"
tk.MustGetErrCode(sql, errno.ErrInvalidOnUpdate)
// drop column
sql = "alter table test_error_code_succ drop c_not_exist"
tk.MustGetErrCode(sql, errno.ErrCantDropFieldOrKey)
tk.MustExec("create table test_drop_column (c1 int );")
sql = "alter table test_drop_column drop column c1;"
tk.MustGetErrCode(sql, errno.ErrCantRemoveAllFields)
// drop columns
sql = "alter table test_error_code_succ drop c_not_exist, drop cc_not_exist"
tk.MustGetErrCode(sql, errno.ErrCantDropFieldOrKey)
tk.MustExec("create table test_drop_columns (c1 int);")
tk.MustExec("alter table test_drop_columns add column c2 int first, add column c3 int after c1")
sql = "alter table test_drop_columns drop column c1, drop column c2, drop column c3;"
tk.MustGetErrCode(sql, errno.ErrCantRemoveAllFields)
sql = "alter table test_drop_columns drop column c1, add column c2 int;"
tk.MustGetErrCode(sql, errno.ErrUnsupportedDDLOperation)
sql = "alter table test_drop_columns drop column c1, drop column c1;"
tk.MustGetErrCode(sql, errno.ErrCantDropFieldOrKey)
// add index
sql = "alter table test_error_code_succ add index idx (c_not_exist)"
tk.MustGetErrCode(sql, errno.ErrKeyColumnDoesNotExits)
tk.MustExec("alter table test_error_code_succ add index idx (c1)")
sql = "alter table test_error_code_succ add index idx (c1)"
tk.MustGetErrCode(sql, errno.ErrDupKeyName)
// drop index
sql = "alter table test_error_code_succ drop index idx_not_exist"
tk.MustGetErrCode(sql, errno.ErrCantDropFieldOrKey)
sql = "alter table test_error_code_succ drop column c3"
tk.MustGetErrCode(sql, errno.ErrUnsupportedDDLOperation)
// modify column
sql = "alter table test_error_code_succ modify testx.test_error_code_succ.c1 bigint"
tk.MustGetErrCode(sql, errno.ErrWrongDBName)
sql = "alter table test_error_code_succ modify t.c1 bigint"
tk.MustGetErrCode(sql, errno.ErrWrongTableName)
sql = "alter table test_error_code_succ change c1 _tidb_rowid bigint"
tk.MustGetErrCode(sql, errno.ErrWrongColumnName)
sql = "alter table test_error_code_succ rename column c1 to _tidb_rowid"
tk.MustGetErrCode(sql, errno.ErrWrongColumnName)
// insert value
tk.MustExec("create table test_error_code_null(c1 char(100) not null);")
sql = "insert into test_error_code_null (c1) values(null);"
tk.MustGetErrCode(sql, errno.ErrBadNull)
// disable tidb_enable_change_multi_schema
tk.MustExec("set global tidb_enable_change_multi_schema = false")
sql = "alter table test_error_code_null add column (x1 int, x2 int)"
tk.MustGetErrCode(sql, errno.ErrUnsupportedDDLOperation)
sql = "alter table test_error_code_null add column (x1 int, x2 int)"
tk.MustGetErrCode(sql, errno.ErrUnsupportedDDLOperation)
tk.MustExec("set global tidb_enable_change_multi_schema = true")
}
func TestTableDDLWithFloatType(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustGetErrCode("create table t (a decimal(1, 2))", errno.ErrMBiggerThanD)
tk.MustGetErrCode("create table t (a float(1, 2))", errno.ErrMBiggerThanD)
tk.MustGetErrCode("create table t (a double(1, 2))", errno.ErrMBiggerThanD)
tk.MustExec("create table t (a double(1, 1))")
tk.MustGetErrCode("alter table t add column b decimal(1, 2)", errno.ErrMBiggerThanD)
// add multi columns now not support, so no case.
tk.MustGetErrCode("alter table t modify column a float(1, 4)", errno.ErrMBiggerThanD)
tk.MustGetErrCode("alter table t change column a aa float(1, 4)", errno.ErrMBiggerThanD)
tk.MustExec("drop table t")
}
func TestTableDDLWithTimeType(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustGetErrCode("create table t (a time(7))", errno.ErrTooBigPrecision)
tk.MustGetErrCode("create table t (a datetime(7))", errno.ErrTooBigPrecision)
tk.MustGetErrCode("create table t (a timestamp(7))", errno.ErrTooBigPrecision)
_, err := tk.Exec("create table t (a time(-1))")
require.Error(t, err)
tk.MustExec("create table t (a datetime)")
tk.MustGetErrCode("alter table t add column b time(7)", errno.ErrTooBigPrecision)
tk.MustGetErrCode("alter table t add column b datetime(7)", errno.ErrTooBigPrecision)
tk.MustGetErrCode("alter table t add column b timestamp(7)", errno.ErrTooBigPrecision)
tk.MustGetErrCode("alter table t modify column a time(7)", errno.ErrTooBigPrecision)
tk.MustGetErrCode("alter table t modify column a datetime(7)", errno.ErrTooBigPrecision)
tk.MustGetErrCode("alter table t modify column a timestamp(7)", errno.ErrTooBigPrecision)
tk.MustGetErrCode("alter table t change column a aa time(7)", errno.ErrTooBigPrecision)
tk.MustGetErrCode("alter table t change column a aa datetime(7)", errno.ErrTooBigPrecision)
tk.MustGetErrCode("alter table t change column a aa timestamp(7)", errno.ErrTooBigPrecision)
tk.MustExec("alter table t change column a aa datetime(0)")
tk.MustExec("drop table t")
}
func TestUpdateMultipleTable(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("create database umt_db")
tk.MustExec("use umt_db")
tk.MustExec("create table t1 (c1 int, c2 int)")
tk.MustExec("insert t1 values (1, 1), (2, 2)")
tk.MustExec("create table t2 (c1 int, c2 int)")
tk.MustExec("insert t2 values (1, 3), (2, 5)")
ctx := tk.Session()
dom := domain.GetDomain(ctx)
is := dom.InfoSchema()
db, ok := is.SchemaByName(model.NewCIStr("umt_db"))
require.True(t, ok)
t1Tbl, err := is.TableByName(model.NewCIStr("umt_db"), model.NewCIStr("t1"))
require.NoError(t, err)
t1Info := t1Tbl.Meta()
// Add a new column in write only state.
newColumn := &model.ColumnInfo{
ID: 100,
Name: model.NewCIStr("c3"),
Offset: 2,
DefaultValue: 9,
OriginDefaultValue: 9,
FieldType: *types.NewFieldType(mysql.TypeLonglong),
State: model.StateWriteOnly,
}
t1Info.Columns = append(t1Info.Columns, newColumn)
err = kv.RunInNewTxn(context.Background(), store, false, func(ctx context.Context, txn kv.Transaction) error {
m := meta.NewMeta(txn)
_, err = m.GenSchemaVersion()
require.NoError(t, err)
require.Nil(t, m.UpdateTable(db.ID, t1Info))
return nil
})
require.NoError(t, err)
err = dom.Reload()
require.NoError(t, err)
tk.MustExec("update t1, t2 set t1.c1 = 8, t2.c2 = 10 where t1.c2 = t2.c1")
tk.MustQuery("select * from t1").Check(testkit.Rows("8 1", "8 2"))
tk.MustQuery("select * from t2").Check(testkit.Rows("1 10", "2 10"))
newColumn.State = model.StatePublic
err = kv.RunInNewTxn(context.Background(), store, false, func(ctx context.Context, txn kv.Transaction) error {
m := meta.NewMeta(txn)
_, err = m.GenSchemaVersion()
require.NoError(t, err)
require.Nil(t, m.UpdateTable(db.ID, t1Info))
return nil
})
require.NoError(t, err)
err = dom.Reload()
require.NoError(t, err)
tk.MustQuery("select * from t1").Check(testkit.Rows("8 1 9", "8 2 9"))
tk.MustExec("drop database umt_db")
}
func TestNullGeneratedColumn(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (" +
"`a` int(11) DEFAULT NULL," +
"`b` int(11) DEFAULT NULL," +
"`c` int(11) GENERATED ALWAYS AS (`a` + `b`) VIRTUAL," +
"`h` varchar(10) DEFAULT NULL," +
"`m` int(11) DEFAULT NULL" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
tk.MustExec("insert into t values()")
tk.MustExec("alter table t add index idx_c(c)")
tk.MustExec("drop table t")
}
func TestDependedGeneratedColumnPrior2GeneratedColumn(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (" +
"`a` int(11) DEFAULT NULL," +
"`b` int(11) GENERATED ALWAYS AS (`a` + 1) VIRTUAL," +
"`c` int(11) GENERATED ALWAYS AS (`b` + 1) VIRTUAL" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
// should check unknown column first, then the prior ones.
sql := "alter table t add column d int as (c + f + 1) first"
tk.MustGetErrCode(sql, errno.ErrBadField)
// depended generated column should be prior to generated column self
sql = "alter table t add column d int as (c+1) first"
tk.MustGetErrCode(sql, errno.ErrGeneratedColumnNonPrior)
// correct case
tk.MustExec("alter table t add column d int as (c+1) after c")
// check position nil case
tk.MustExec("alter table t add column(e int as (c+1))")
}
func TestChangingTableCharset(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("USE test")
tk.MustExec("create table t(a char(10)) charset latin1 collate latin1_bin")
tk.MustGetErrCode("alter table t charset gbk", errno.ErrUnsupportedDDLOperation)
tk.MustGetErrCode("alter table t charset ''", errno.ErrUnknownCharacterSet)
tk.MustGetErrCode("alter table t charset utf8mb4 collate '' collate utf8mb4_bin;", errno.ErrUnknownCollation)
tk.MustGetErrCode("alter table t charset utf8 collate latin1_bin", errno.ErrCollationCharsetMismatch)
tk.MustGetErrCode("alter table t charset utf8 collate utf8mb4_bin;", errno.ErrCollationCharsetMismatch)
tk.MustGetErrCode("alter table t charset utf8 collate utf8_bin collate utf8mb4_bin collate utf8_bin;", errno.ErrCollationCharsetMismatch)
tk.MustGetErrCode("alter table t charset utf8", errno.ErrUnsupportedDDLOperation)
tk.MustGetErrCode("alter table t charset utf8mb4", errno.ErrUnsupportedDDLOperation)
tk.MustGetErrCode("alter table t charset utf8mb4 collate utf8mb4_bin", errno.ErrUnsupportedDDLOperation)
tk.MustGetErrCode("alter table t charset latin1 charset utf8 charset utf8mb4 collate utf8_bin;", errno.ErrConflictingDeclarations)
// Test change column charset when changing table charset.
tk.MustExec("drop table t;")
tk.MustExec("create table t(a varchar(10)) charset utf8")
tk.MustExec("alter table t convert to charset utf8mb4;")
checkCharset := func(chs, coll string) {
tbl := external.GetTableByName(t, tk, "test", "t")
require.NotNil(t, tbl)
require.Equal(t, chs, tbl.Meta().Charset)
require.Equal(t, coll, tbl.Meta().Collate)
for _, col := range tbl.Meta().Columns {
require.Equal(t, chs, col.Charset)
require.Equal(t, coll, col.Collate)
}
}
checkCharset(charset.CharsetUTF8MB4, charset.CollationUTF8MB4)
// Test when column charset can not convert to the target charset.
tk.MustExec("drop table t;")
tk.MustExec("create table t(a varchar(10) character set ascii) charset utf8mb4")
tk.MustGetErrCode("alter table t convert to charset utf8mb4;", errno.ErrUnsupportedDDLOperation)
tk.MustExec("drop table t;")
tk.MustExec("create table t(a varchar(10) character set utf8) charset utf8")
tk.MustExec("alter table t convert to charset utf8 collate utf8_general_ci;")
checkCharset(charset.CharsetUTF8, "utf8_general_ci")
// Test when table charset is equal to target charset but column charset is not equal.
tk.MustExec("drop table t;")
tk.MustExec("create table t(a varchar(10) character set utf8) charset utf8mb4")
tk.MustExec("alter table t convert to charset utf8mb4 collate utf8mb4_general_ci;")
checkCharset(charset.CharsetUTF8MB4, "utf8mb4_general_ci")
// Mock table info with charset is "". Old TiDB maybe create table with charset is "".
db, ok := dom.InfoSchema().SchemaByName(model.NewCIStr("test"))
require.True(t, ok)
tbl := external.GetTableByName(t, tk, "test", "t")
tblInfo := tbl.Meta().Clone()
tblInfo.Charset = ""
tblInfo.Collate = ""
updateTableInfo := func(tblInfo *model.TableInfo) {
mockCtx := mock.NewContext()
mockCtx.Store = store
err := mockCtx.NewTxn(context.Background())
require.NoError(t, err)
txn, err := mockCtx.Txn(true)
require.NoError(t, err)
mt := meta.NewMeta(txn)
err = mt.UpdateTable(db.ID, tblInfo)
require.NoError(t, err)
err = txn.Commit(context.Background())
require.NoError(t, err)
}
updateTableInfo(tblInfo)
// check table charset is ""
tk.MustExec("alter table t add column b varchar(10);") // load latest schema.
tbl = external.GetTableByName(t, tk, "test", "t")
require.NotNil(t, tbl)
require.Equal(t, "", tbl.Meta().Charset)
require.Equal(t, "", tbl.Meta().Collate)
// Test when table charset is "", this for compatibility.
tk.MustExec("alter table t convert to charset utf8mb4;")
checkCharset(charset.CharsetUTF8MB4, charset.CollationUTF8MB4)
// Test when column charset is "".
tbl = external.GetTableByName(t, tk, "test", "t")
tblInfo = tbl.Meta().Clone()
tblInfo.Columns[0].Charset = ""
tblInfo.Columns[0].Collate = ""
updateTableInfo(tblInfo)
// check table charset is ""
tk.MustExec("alter table t drop column b;") // load latest schema.
tbl = external.GetTableByName(t, tk, "test", "t")
require.NotNil(t, tbl)
require.Equal(t, "", tbl.Meta().Columns[0].Charset)
require.Equal(t, "", tbl.Meta().Columns[0].Collate)
tk.MustExec("alter table t convert to charset utf8mb4;")
checkCharset(charset.CharsetUTF8MB4, charset.CollationUTF8MB4)
tk.MustExec("drop table t")
tk.MustExec("create table t (a blob) character set utf8;")
tk.MustExec("alter table t charset=utf8mb4 collate=utf8mb4_bin;")
tk.MustQuery("show create table t").Check(testkit.RowsWithSep("|",
"t CREATE TABLE `t` (\n"+
" `a` blob DEFAULT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))
tk.MustExec("drop table t")
tk.MustExec("create table t(a varchar(5) charset utf8) charset utf8")
tk.MustExec("alter table t charset utf8mb4")
tbl = external.GetTableByName(t, tk, "test", "t")
require.NotNil(t, tbl)
require.Equal(t, "utf8mb4", tbl.Meta().Charset)
require.Equal(t, "utf8mb4_bin", tbl.Meta().Collate)
for _, col := range tbl.Meta().Columns {
// Column charset and collate should remain unchanged.
require.Equal(t, "utf8", col.Charset)
require.Equal(t, "utf8_bin", col.Collate)
}
tk.MustExec("drop table t")
tk.MustExec("create table t(a varchar(5) charset utf8 collate utf8_unicode_ci) charset utf8 collate utf8_unicode_ci")
tk.MustExec("alter table t collate utf8_general_ci")
tbl = external.GetTableByName(t, tk, "test", "t")
require.NotNil(t, tbl)
require.Equal(t, "utf8", tbl.Meta().Charset)
require.Equal(t, "utf8_general_ci", tbl.Meta().Collate)
for _, col := range tbl.Meta().Columns {
require.Equal(t, "utf8", col.Charset)
// Column collate should remain unchanged.
require.Equal(t, "utf8_unicode_ci", col.Collate)
}
}
func TestModifyColumnOption(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("create database if not exists test")
tk.MustExec("use test")
errMsg := "[ddl:8200]" // unsupported modify column with references
assertErrCode := func(sql string, errCodeStr string) {
_, err := tk.Exec(sql)
require.Error(t, err)
require.Equal(t, errCodeStr, err.Error()[:len(errCodeStr)])
}
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (b char(1) default null) engine=InnoDB default charset=utf8mb4 collate=utf8mb4_general_ci")
tk.MustExec("alter table t1 modify column b char(1) character set utf8mb4 collate utf8mb4_general_ci")
tk.MustExec("drop table t1")
tk.MustExec("create table t1 (b char(1) collate utf8mb4_general_ci)")
tk.MustExec("alter table t1 modify b char(1) character set utf8mb4 collate utf8mb4_general_ci")
tk.MustExec("drop table t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1 (a int(11) default null)")
tk.MustExec("create table t2 (b char, c int)")
assertErrCode("alter table t2 modify column c int references t1(a)", errMsg)
_, err := tk.Exec("alter table t1 change a a varchar(16)")
require.NoError(t, err)
_, err = tk.Exec("alter table t1 change a a varchar(10)")
require.NoError(t, err)
_, err = tk.Exec("alter table t1 change a a datetime")
require.NoError(t, err)
_, err = tk.Exec("alter table t1 change a a int(11) unsigned")
require.NoError(t, err)
_, err = tk.Exec("alter table t2 change b b int(11) unsigned")
require.NoError(t, err)
}
func TestIndexOnMultipleGeneratedColumn(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("create database if not exists test")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int as (a + 1), c int as (b + 1))")
tk.MustExec("insert into t (a) values (1)")
tk.MustExec("create index idx on t (c)")
tk.MustQuery("select * from t where c > 1").Check(testkit.Rows("1 2 3"))
res := tk.MustQuery("select * from t use index(idx) where c > 1")
tk.MustQuery("select * from t ignore index(idx) where c > 1").Check(res.Rows())
tk.MustExec("admin check table t")
}
func TestIndexOnMultipleGeneratedColumn1(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("create database if not exists test")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int as (a + 1), c int as (b + 1), d int as (c + 1))")
tk.MustExec("insert into t (a) values (1)")
tk.MustExec("create index idx on t (d)")
tk.MustQuery("select * from t where d > 2").Check(testkit.Rows("1 2 3 4"))
res := tk.MustQuery("select * from t use index(idx) where d > 2")
tk.MustQuery("select * from t ignore index(idx) where d > 2").Check(res.Rows())
tk.MustExec("admin check table t")
}
func TestIndexOnMultipleGeneratedColumn2(t *testing.T) {