forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_partition_test.go
3713 lines (3406 loc) · 146 KB
/
db_partition_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/rand"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/ddl/schematracker"
"github.com/pingcap/tidb/ddl/testutil"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/errno"
tmysql "github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"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/codec"
"github.com/pingcap/tidb/util/dbterror"
"github.com/pingcap/tidb/util/logutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
func checkGlobalIndexCleanUpDone(t *testing.T, ctx sessionctx.Context, tblInfo *model.TableInfo, idxInfo *model.IndexInfo, pid int64) int {
require.NoError(t, sessiontxn.NewTxn(context.Background(), ctx))
txn, err := ctx.Txn(true)
require.NoError(t, err)
defer func() {
err := txn.Rollback()
require.NoError(t, err)
}()
cnt := 0
prefix := tablecodec.EncodeTableIndexPrefix(tblInfo.ID, idxInfo.ID)
it, err := txn.Iter(prefix, nil)
require.NoError(t, err)
for it.Valid() {
if !it.Key().HasPrefix(prefix) {
break
}
segs := tablecodec.SplitIndexValue(it.Value())
require.NotNil(t, segs.PartitionID)
_, pi, err := codec.DecodeInt(segs.PartitionID)
require.NoError(t, err)
require.NotEqual(t, pid, pi)
cnt++
err = it.Next()
require.NoError(t, err)
}
return cnt
}
func TestCreateTableWithPartition(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
ddlChecker := schematracker.NewChecker(dom.DDL())
dom.SetDDL(ddlChecker)
ddlChecker.CreateTestDB()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("drop table if exists tp;")
tk.MustExec(`CREATE TABLE tp (a int) PARTITION BY RANGE(a) (
PARTITION p0 VALUES LESS THAN (10),
PARTITION p1 VALUES LESS THAN (20),
PARTITION p2 VALUES LESS THAN (MAXVALUE)
);`)
ctx := tk.Session()
is := domain.GetDomain(ctx).InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("tp"))
require.NoError(t, err)
require.NotNil(t, tbl.Meta().Partition)
part := tbl.Meta().Partition
require.Equal(t, model.PartitionTypeRange, part.Type)
require.Equal(t, "`a`", part.Expr)
for _, pdef := range part.Definitions {
require.Greater(t, pdef.ID, int64(0))
}
require.Len(t, part.Definitions, 3)
require.Equal(t, "10", part.Definitions[0].LessThan[0])
require.Equal(t, "p0", part.Definitions[0].Name.L)
require.Equal(t, "20", part.Definitions[1].LessThan[0])
require.Equal(t, "p1", part.Definitions[1].Name.L)
require.Equal(t, "MAXVALUE", part.Definitions[2].LessThan[0])
require.Equal(t, "p2", part.Definitions[2].Name.L)
tk.MustExec("drop table if exists employees;")
sql1 := `create table employees (
id int not null,
hired int not null
)
partition by range( hired ) (
partition p1 values less than (1991),
partition p2 values less than (1996),
partition p2 values less than (2001)
);`
tk.MustGetErrCode(sql1, tmysql.ErrSameNamePartition)
sql2 := `create table employees (
id int not null,
hired int not null
)
partition by range( hired ) (
partition p1 values less than (1998),
partition p2 values less than (1996),
partition p3 values less than (2001)
);`
tk.MustGetErrCode(sql2, tmysql.ErrRangeNotIncreasing)
sql3 := `create table employees (
id int not null,
hired int not null
)
partition by range( hired ) (
partition p1 values less than (1998),
partition p2 values less than maxvalue,
partition p3 values less than (2001)
);`
tk.MustGetErrCode(sql3, tmysql.ErrPartitionMaxvalue)
sql4 := `create table t4 (
a int not null,
b int not null
)
partition by range( a ) (
partition p1 values less than maxvalue,
partition p2 values less than (1991),
partition p3 values less than (1995)
);`
tk.MustGetErrCode(sql4, tmysql.ErrPartitionMaxvalue)
tk.MustExec(`CREATE TABLE rc (
a INT NOT NULL,
b INT NOT NULL,
c INT NOT NULL
)
partition by range columns(a,b,c) (
partition p0 values less than (10,5,1),
partition p2 values less than (50,maxvalue,10),
partition p3 values less than (65,30,13),
partition p4 values less than (maxvalue,30,40)
);`)
sql6 := `create table employees (
id int not null,
hired int not null
)
partition by range( hired ) (
partition p0 values less than (6 , 10)
);`
tk.MustGetErrCode(sql6, tmysql.ErrTooManyValues)
sql7 := `create table t7 (
a int not null,
b int not null
)
partition by range( a ) (
partition p1 values less than (1991),
partition p2 values less than maxvalue,
partition p3 values less than maxvalue,
partition p4 values less than (1995),
partition p5 values less than maxvalue
);`
tk.MustGetErrCode(sql7, tmysql.ErrPartitionMaxvalue)
sql18 := `create table t8 (
a int not null,
b int not null
)
partition by range( a ) (
partition p1 values less than (19xx91),
partition p2 values less than maxvalue
);`
tk.MustGetErrCode(sql18, mysql.ErrBadField)
sql9 := `create TABLE t9 (
col1 int
)
partition by range( case when col1 > 0 then 10 else 20 end ) (
partition p0 values less than (2),
partition p1 values less than (6)
);`
tk.MustGetErrCode(sql9, tmysql.ErrPartitionFunctionIsNotAllowed)
tk.MustGetDBError(`CREATE TABLE t9 (
a INT NOT NULL,
b INT NOT NULL,
c INT NOT NULL
)
partition by range columns(a) (
partition p0 values less than (10),
partition p2 values less than (20),
partition p3 values less than (20)
);`, dbterror.ErrRangeNotIncreasing)
tk.MustGetErrCode(`create TABLE t10 (c1 int,c2 int) partition by range(c1 / c2 ) (partition p0 values less than (2));`, tmysql.ErrPartitionFunctionIsNotAllowed)
tk.MustExec(`create TABLE t11 (c1 int,c2 int) partition by range(c1 div c2 ) (partition p0 values less than (2));`)
tk.MustExec(`create TABLE t12 (c1 int,c2 int) partition by range(c1 + c2 ) (partition p0 values less than (2));`)
tk.MustExec(`create TABLE t13 (c1 int,c2 int) partition by range(c1 - c2 ) (partition p0 values less than (2));`)
tk.MustExec(`create TABLE t14 (c1 int,c2 int) partition by range(c1 * c2 ) (partition p0 values less than (2));`)
tk.MustExec(`create TABLE t15 (c1 int,c2 int) partition by range( abs(c1) ) (partition p0 values less than (2));`)
tk.MustExec(`create TABLE t16 (c1 int) partition by range( c1) (partition p0 values less than (10));`)
tk.MustGetErrCode(`create TABLE t17 (c1 int,c2 float) partition by range(c1 + c2 ) (partition p0 values less than (2));`, tmysql.ErrPartitionFuncNotAllowed)
tk.MustGetErrCode(`create TABLE t18 (c1 int,c2 float) partition by range( floor(c2) ) (partition p0 values less than (2));`, tmysql.ErrPartitionFuncNotAllowed)
tk.MustExec(`create TABLE t19 (c1 int,c2 float) partition by range( floor(c1) ) (partition p0 values less than (2));`)
tk.MustExec(`create TABLE t20 (c1 int,c2 bit(10)) partition by range(c2) (partition p0 values less than (10));`)
tk.MustExec(`create TABLE t21 (c1 int,c2 year) partition by range( c2 ) (partition p0 values less than (2000));`)
tk.MustGetErrCode(`create TABLE t24 (c1 float) partition by range( c1 ) (partition p0 values less than (2000));`, tmysql.ErrFieldTypeNotAllowedAsPartitionField)
// test check order. The sql below have 2 problem: 1. ErrFieldTypeNotAllowedAsPartitionField 2. ErrPartitionMaxvalue , mysql will return ErrPartitionMaxvalue.
tk.MustGetErrCode(`create TABLE t25 (c1 float) partition by range( c1 ) (partition p1 values less than maxvalue,partition p0 values less than (2000));`, tmysql.ErrPartitionMaxvalue)
// Fix issue 7362.
tk.MustExec("create table test_partition(id bigint, name varchar(255), primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 PARTITION BY RANGE COLUMNS(id) (PARTITION p1 VALUES LESS THAN (10) ENGINE = InnoDB);")
// 'Less than' in partition expression could be a constant expression, notice that
// the SHOW result changed.
tk.MustExec(`create table t26 (a date)
partition by range(to_seconds(a))(
partition p0 values less than (to_seconds('2004-01-01')),
partition p1 values less than (to_seconds('2005-01-01')));`)
tk.MustQuery("show create table t26").Check(
testkit.Rows("t26 CREATE TABLE `t26` (\n `a` date DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\nPARTITION BY RANGE (TO_SECONDS(`a`))\n(PARTITION `p0` VALUES LESS THAN (63240134400),\n PARTITION `p1` VALUES LESS THAN (63271756800))"))
tk.MustExec(`create table t27 (a bigint unsigned not null)
partition by range(a) (
partition p0 values less than (10),
partition p1 values less than (100),
partition p2 values less than (1000),
partition p3 values less than (18446744073709551000),
partition p4 values less than (18446744073709551614)
);`)
tk.MustExec(`create table t28 (a bigint unsigned not null)
partition by range(a) (
partition p0 values less than (10),
partition p1 values less than (100),
partition p2 values less than (1000),
partition p3 values less than (18446744073709551000 + 1),
partition p4 values less than (18446744073709551000 + 10)
);`)
tk.MustExec("set @@tidb_enable_table_partition = 1")
tk.MustExec("set @@tidb_enable_table_partition = 1")
tk.MustExec(`create table t30 (
a int,
b float,
c varchar(30))
partition by range columns (a, b)
(partition p0 values less than (10, 10.0))`)
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 8200 Unsupported partition type RANGE, treat as normal table"))
tk.MustGetErrCode(`create table t31 (a int not null) partition by range( a );`, tmysql.ErrPartitionsMustBeDefined)
tk.MustGetErrCode(`create table t32 (a int not null) partition by range columns( a );`, tmysql.ErrPartitionsMustBeDefined)
tk.MustGetErrCode(`create table t33 (a int, b int) partition by hash(a) partitions 0;`, tmysql.ErrNoParts)
tk.MustGetErrCode(`create table t33 (a timestamp, b int) partition by hash(a) partitions 30;`, tmysql.ErrFieldTypeNotAllowedAsPartitionField)
tk.MustGetErrCode(`CREATE TABLE t34 (c0 INT) PARTITION BY HASH((CASE WHEN 0 THEN 0 ELSE c0 END )) PARTITIONS 1;`, tmysql.ErrPartitionFunctionIsNotAllowed)
tk.MustGetErrCode(`CREATE TABLE t0(c0 INT) PARTITION BY HASH((c0<CURRENT_USER())) PARTITIONS 1;`, tmysql.ErrPartitionFunctionIsNotAllowed)
// TODO: fix this one
// tk.MustGetErrCode(`create table t33 (a timestamp, b int) partition by hash(unix_timestamp(a)) partitions 30;`, tmysql.ErrPartitionFuncNotAllowed)
// Fix issue 8647
tk.MustGetErrCode(`CREATE TABLE trb8 (
id int(11) DEFAULT NULL,
name varchar(50) DEFAULT NULL,
purchased date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
PARTITION BY RANGE ( year(notexist.purchased) - 1 ) (
PARTITION p0 VALUES LESS THAN (1990),
PARTITION p1 VALUES LESS THAN (1995),
PARTITION p2 VALUES LESS THAN (2000),
PARTITION p3 VALUES LESS THAN (2005)
);`, tmysql.ErrBadField)
// Fix a timezone dependent check bug introduced in https://github.com/pingcap/tidb/pull/10655
tk.MustExec(`create table t34 (dt timestamp(3)) partition by range (floor(unix_timestamp(dt))) (
partition p0 values less than (unix_timestamp('2020-04-04 00:00:00')),
partition p1 values less than (unix_timestamp('2020-04-05 00:00:00')));`)
tk.MustGetErrCode(`create table t34 (dt timestamp(3)) partition by range (unix_timestamp(date(dt))) (
partition p0 values less than (unix_timestamp('2020-04-04 00:00:00')),
partition p1 values less than (unix_timestamp('2020-04-05 00:00:00')));`, tmysql.ErrWrongExprInPartitionFunc)
tk.MustGetErrCode(`create table t34 (dt datetime) partition by range (unix_timestamp(dt)) (
partition p0 values less than (unix_timestamp('2020-04-04 00:00:00')),
partition p1 values less than (unix_timestamp('2020-04-05 00:00:00')));`, tmysql.ErrWrongExprInPartitionFunc)
// Fix https://github.com/pingcap/tidb/issues/16333
tk.MustExec(`create table t35 (dt timestamp) partition by range (unix_timestamp(dt))
(partition p0 values less than (unix_timestamp('2020-04-15 00:00:00')));`)
tk.MustExec(`drop table if exists too_long_identifier`)
tk.MustGetErrCode(`create table too_long_identifier(a int)
partition by range (a)
(partition p0pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp values less than (10));`, tmysql.ErrTooLongIdent)
tk.MustExec(`drop table if exists too_long_identifier`)
tk.MustExec("create table too_long_identifier(a int) partition by range(a) (partition p0 values less than(10))")
tk.MustGetErrCode("alter table too_long_identifier add partition "+
"(partition p0pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp values less than(20))", tmysql.ErrTooLongIdent)
tk.MustExec(`create table t36 (a date, b datetime) partition by range (EXTRACT(YEAR_MONTH FROM a)) (
partition p0 values less than (200),
partition p1 values less than (300),
partition p2 values less than maxvalue)`)
}
func TestCreateTableWithHashPartition(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
ddlChecker := schematracker.NewChecker(dom.DDL())
dom.SetDDL(ddlChecker)
ddlChecker.CreateTestDB()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("drop table if exists employees;")
tk.MustExec("set @@session.tidb_enable_table_partition = 1")
tk.MustExec(`
create table employees (
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int,
store_id int
)
partition by hash(store_id) partitions 4;`)
tk.MustExec("drop table if exists employees;")
tk.MustExec(`
create table employees (
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int,
store_id int
)
partition by hash( year(hired) ) partitions 4;`)
// This query makes tidb OOM without partition count check.
tk.MustGetErrCode(`CREATE TABLE employees (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT
) PARTITION BY HASH(store_id) PARTITIONS 102400000000;`, tmysql.ErrTooManyPartitions)
tk.MustExec("CREATE TABLE t_linear (a int, b varchar(128)) PARTITION BY LINEAR HASH(a) PARTITIONS 4")
tk.MustGetErrCode("select * from t_linear partition (p0)", tmysql.ErrPartitionClauseOnNonpartitioned)
tk.MustExec(`CREATE TABLE t_sub (a int, b varchar(128)) PARTITION BY RANGE( a ) SUBPARTITION BY HASH( a )
SUBPARTITIONS 2 (
PARTITION p0 VALUES LESS THAN (100),
PARTITION p1 VALUES LESS THAN (200),
PARTITION p2 VALUES LESS THAN MAXVALUE)`)
tk.MustGetErrCode("select * from t_sub partition (p0)", tmysql.ErrPartitionClauseOnNonpartitioned)
// Fix create partition table using extract() function as partition key.
tk.MustExec("create table t2 (a date, b datetime) partition by hash (EXTRACT(YEAR_MONTH FROM a)) partitions 7")
tk.MustExec("create table t3 (a int, b int) partition by hash(ceiling(a-b)) partitions 10")
tk.MustExec("create table t4 (a int, b int) partition by hash(floor(a-b)) partitions 10")
}
func TestCreateTableWithRangeColumnPartition(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
ddlChecker := schematracker.NewChecker(dom.DDL())
dom.SetDDL(ddlChecker)
ddlChecker.CreateTestDB()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("drop table if exists log_message_1;")
tk.MustExec("set @@session.tidb_enable_list_partition = ON")
tk.MustExec(`
create table log_message_1 (
add_time datetime not null default '2000-01-01 00:00:00',
log_level int unsigned not null default '0',
log_host varchar(32) not null,
service_name varchar(32) not null,
message varchar(2000)
) partition by range columns(add_time)(
partition p201403 values less than ('2014-04-01'),
partition p201404 values less than ('2014-05-01'),
partition p201405 values less than ('2014-06-01'),
partition p201406 values less than ('2014-07-01'),
partition p201407 values less than ('2014-08-01'),
partition p201408 values less than ('2014-09-01'),
partition p201409 values less than ('2014-10-01'),
partition p201410 values less than ('2014-11-01')
)`)
tk.MustExec("drop table if exists log_message_1;")
tk.MustExec(`
create table log_message_1 (
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int,
store_id int
)
partition by hash( year(hired) ) partitions 4;`)
tk.MustExec("drop table if exists t")
type testCase struct {
sql string
err *terror.Error
}
cases := []testCase{
{
"create table t (id int) partition by range columns (id);",
ast.ErrPartitionsMustBeDefined,
},
{
"create table t(a datetime) partition by range columns (a) (partition p1 values less than ('2000-02-01'), partition p2 values less than ('20000102'));",
dbterror.ErrRangeNotIncreasing,
},
{
"create table t(a time) partition by range columns (a) (partition p1 values less than ('202020'), partition p2 values less than ('20:20:10'));",
dbterror.ErrRangeNotIncreasing,
},
{
"create table t(a time) partition by range columns (a) (partition p1 values less than ('202090'));",
dbterror.ErrWrongTypeColumnValue,
},
{
"create table t (id int) partition by range columns (id) (partition p0 values less than (1, 2));",
ast.ErrPartitionColumnList,
},
{
"create table t (a int) partition by range columns (b) (partition p0 values less than (1, 2));",
ast.ErrPartitionColumnList,
},
{
"create table t (a int) partition by range columns (b) (partition p0 values less than (1));",
dbterror.ErrFieldNotFoundPart,
},
{
"create table t (a date) partition by range (to_days(to_days(a))) (partition p0 values less than (1));",
dbterror.ErrWrongExprInPartitionFunc,
},
{
"create table t (id timestamp) partition by range columns (id) (partition p0 values less than ('2019-01-09 11:23:34'));",
dbterror.ErrNotAllowedTypeInPartition,
},
{
`create table t29 (
a decimal
)
partition by range columns (a)
(partition p0 values less than (0));`,
dbterror.ErrNotAllowedTypeInPartition,
},
{
"create table t (id text) partition by range columns (id) (partition p0 values less than ('abc'));",
dbterror.ErrNotAllowedTypeInPartition,
},
// create as normal table, warning.
// {
// "create table t (a int, b varchar(64)) partition by range columns (a, b) (" +
// "partition p0 values less than (1, 'a')," +
// "partition p1 values less than (1, 'a'))",
// dbterror.ErrRangeNotIncreasing,
// },
{
"create table t (a int, b varchar(64)) partition by range columns ( b) (" +
"partition p0 values less than ( 'a')," +
"partition p1 values less than ('a'))",
dbterror.ErrRangeNotIncreasing,
},
// create as normal table, warning.
// {
// "create table t (a int, b varchar(64)) partition by range columns (a, b) (" +
// "partition p0 values less than (1, 'b')," +
// "partition p1 values less than (1, 'a'))",
// dbterror.ErrRangeNotIncreasing,
// },
{
"create table t (a int, b varchar(64)) partition by range columns (b) (" +
"partition p0 values less than ('b')," +
"partition p1 values less than ('a'))",
dbterror.ErrRangeNotIncreasing,
},
// create as normal table, warning.
// {
// "create table t (a int, b varchar(64)) partition by range columns (a, b) (" +
// "partition p0 values less than (1, maxvalue)," +
// "partition p1 values less than (1, 'a'))",
// dbterror.ErrRangeNotIncreasing,
// },
{
"create table t (a int, b varchar(64)) partition by range columns ( b) (" +
"partition p0 values less than ( maxvalue)," +
"partition p1 values less than ('a'))",
dbterror.ErrRangeNotIncreasing,
},
{
"create table t (col datetime not null default '2000-01-01')" +
"partition by range columns (col) (" +
"PARTITION p0 VALUES LESS THAN (20190905)," +
"PARTITION p1 VALUES LESS THAN (20190906));",
dbterror.ErrWrongTypeColumnValue,
},
{
"create table t(a char(10) collate utf8mb4_bin) " +
"partition by range columns (a) (" +
"partition p0 values less than ('a'), " +
"partition p1 values less than ('G'));",
dbterror.ErrRangeNotIncreasing,
},
{
"create table t(a char(10) collate utf8mb4_bin) " +
"partition by range columns (a) (" +
"partition p0 values less than ('g'), " +
"partition p1 values less than ('A'));",
dbterror.ErrRangeNotIncreasing,
},
{
"CREATE TABLE t1(c0 INT) PARTITION BY HASH((NOT c0)) PARTITIONS 2;",
dbterror.ErrPartitionFunctionIsNotAllowed,
},
{
"CREATE TABLE t1(c0 INT) PARTITION BY HASH((!c0)) PARTITIONS 2;",
dbterror.ErrPartitionFunctionIsNotAllowed,
},
{
"CREATE TABLE t1(c0 INT) PARTITION BY LIST((NOT c0)) (partition p0 values in (0), partition p1 values in (1));",
dbterror.ErrPartitionFunctionIsNotAllowed,
},
{
"CREATE TABLE t1(c0 INT) PARTITION BY LIST((!c0)) (partition p0 values in (0), partition p1 values in (1));",
dbterror.ErrPartitionFunctionIsNotAllowed,
},
{
"CREATE TABLE t1 (a TIME, b DATE) PARTITION BY range(DATEDIFF(a, b)) (partition p1 values less than (20));",
dbterror.ErrWrongExprInPartitionFunc,
},
{
"CREATE TABLE t1 (a DATE, b VARCHAR(10)) PARTITION BY range(DATEDIFF(a, b)) (partition p1 values less than (20));",
dbterror.ErrWrongExprInPartitionFunc,
},
{
"create table t1 (a bigint unsigned) partition by list (a) (partition p0 values in (10, 20, 30, -1));",
dbterror.ErrPartitionConstDomain,
},
{
"create table t1 (a bigint unsigned) partition by range (a) (partition p0 values less than (-1));",
dbterror.ErrPartitionConstDomain,
},
{
"create table t1 (a int unsigned) partition by range (a) (partition p0 values less than (-1));",
dbterror.ErrPartitionConstDomain,
},
{
"create table t1 (a tinyint(20) unsigned) partition by range (a) (partition p0 values less than (-1));",
dbterror.ErrPartitionConstDomain,
},
{
"CREATE TABLE new (a TIMESTAMP NOT NULL PRIMARY KEY) PARTITION BY RANGE (a % 2) (PARTITION p VALUES LESS THAN (20080819));",
dbterror.ErrWrongExprInPartitionFunc,
},
{
"CREATE TABLE new (a TIMESTAMP NOT NULL PRIMARY KEY) PARTITION BY RANGE (a+2) (PARTITION p VALUES LESS THAN (20080819));",
dbterror.ErrWrongExprInPartitionFunc,
},
}
for i, tt := range cases {
_, err := tk.Exec(tt.sql)
require.Truef(t, tt.err.Equal(err),
"case %d fail, sql = `%s`\nexpected error = `%v`\n actual error = `%v`",
i, tt.sql, tt.err, err,
)
}
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t1 (a int, b char(3)) partition by range columns (a, b) (" +
"partition p0 values less than (1, 'a')," +
"partition p1 values less than (2, maxvalue))")
tk.MustExec("drop table if exists t2;")
tk.MustExec("create table t2 (a int, b char(3)) partition by range columns (b) (" +
"partition p0 values less than ( 'a')," +
"partition p1 values less than (maxvalue))")
tk.MustExec("drop table if exists t;")
tk.MustExec(`create table t(a char(10) collate utf8mb4_unicode_ci) partition by range columns (a) (
partition p0 values less than ('a'),
partition p1 values less than ('G'));`)
tk.MustExec("drop table if exists t;")
tk.MustExec(`create table t (a varchar(255) charset utf8mb4 collate utf8mb4_bin) ` +
`partition by range columns (a) ` +
`(partition pnull values less than (""),` +
`partition puppera values less than ("AAA"),` +
`partition plowera values less than ("aaa"),` +
`partition pmax values less than (MAXVALUE))`)
tk.MustExec("drop table if exists t;")
tk.MustExec(`create table t(a int) partition by range columns (a) (
partition p0 values less than (10),
partition p1 values less than (20));`)
tk.MustExec("drop table if exists t;")
tk.MustExec(`create table t(a int) partition by range (a) (partition p0 values less than (18446744073709551615));`)
tk.MustExec("drop table if exists t;")
tk.MustExec(`create table t(a binary) partition by range columns (a) (partition p0 values less than (X'0C'));`)
// TODO: we haven't implement AlterTable in SchemaTracker yet
ddlChecker.Disable()
tk.MustExec(`alter table t add partition (partition p1 values less than (X'0D'), partition p2 values less than (X'0E'));`)
tk.MustExec(`insert into t values (X'0B'), (X'0C'), (X'0D')`)
tk.MustQuery(`select * from t where a < X'0D' order by a`).Check(testkit.Rows("\x0B", "\x0C"))
}
func TestPartitionRangeColumnsCollate(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("create schema PartitionRangeColumnsCollate")
tk.MustExec("use PartitionRangeColumnsCollate")
tk.MustExec(`create table t (a varchar(255) charset utf8mb4 collate utf8mb4_bin) partition by range columns (a)
(partition p0A values less than ("A"),
partition p1AA values less than ("AA"),
partition p2Aa values less than ("Aa"),
partition p3BB values less than ("BB"),
partition p4Bb values less than ("Bb"),
partition p5aA values less than ("aA"),
partition p6aa values less than ("aa"),
partition p7bB values less than ("bB"),
partition p8bb values less than ("bb"),
partition pMax values less than (MAXVALUE))`)
tk.MustExec(`insert into t values ("A"),("a"),("b"),("B"),("aa"),("AA"),("aA"),("Aa"),("BB"),("Bb"),("bB"),("bb"),("AB"),("BA"),("Ab"),("Ba"),("aB"),("bA"),("ab"),("ba")`)
tk.MustQuery(`explain select * from t where a = "AA" collate utf8mb4_general_ci`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:all data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] eq(partitionrangecolumnscollate.t.a, "AA")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a = "AA" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows("AA", "Aa", "aA", "aa"))
tk.MustQuery(`explain select * from t where a = "aa" collate utf8mb4_general_ci`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:all data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] eq(partitionrangecolumnscollate.t.a, "aa")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a = "aa" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows("AA", "Aa", "aA", "aa"))
tk.MustQuery(`explain select * from t where a >= "aa" collate utf8mb4_general_ci`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:all data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] ge(partitionrangecolumnscollate.t.a, "aa")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a >= "aa" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows(
"AA", "AB", "Aa", "Ab", "B", "BA", "BB", "Ba", "Bb", "aA", "aB", "aa", "ab", "b", "bA", "bB", "ba", "bb"))
tk.MustQuery(`explain select * from t where a > "aa" collate utf8mb4_general_ci`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:all data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] gt(partitionrangecolumnscollate.t.a, "aa")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a > "aa" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows(
"AB", "Ab", "B", "BA", "BB", "Ba", "Bb", "aB", "ab", "b", "bA", "bB", "ba", "bb"))
tk.MustQuery(`explain select * from t where a <= "aa" collate utf8mb4_general_ci`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:all data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] le(partitionrangecolumnscollate.t.a, "aa")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a <= "aa" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows(
"A", "AA", "Aa", "a", "aA", "aa"))
tk.MustQuery(`explain select * from t where a < "aa" collate utf8mb4_general_ci`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:all data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] lt(partitionrangecolumnscollate.t.a, "aa")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a < "aa" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows(
"A", "a"))
tk.MustExec("drop table t")
tk.MustExec(` create table t (a varchar(255) charset utf8mb4 collate utf8mb4_general_ci) partition by range columns (a)
(partition p0 values less than ("A"),
partition p1 values less than ("aa"),
partition p2 values less than ("AAA"),
partition p3 values less than ("aaaa"),
partition p4 values less than ("B"),
partition p5 values less than ("bb"),
partition pMax values less than (MAXVALUE))`)
tk.MustExec(`insert into t values ("A"),("a"),("b"),("B"),("aa"),("AA"),("aA"),("Aa"),("BB"),("Bb"),("bB"),("bb"),("AB"),("BA"),("Ab"),("Ba"),("aB"),("bA"),("ab"),("ba"),("ä"),("ÄÄÄ")`)
tk.MustQuery(`explain select * from t where a = "aa" collate utf8mb4_general_ci`).Check(testkit.Rows(
`TableReader_7 10.00 root partition:p2 data:Selection_6`,
`└─Selection_6 10.00 cop[tikv] eq(partitionrangecolumnscollate.t.a, "aa")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a = "aa" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows(
"AA", "Aa", "aA", "aa"))
tk.MustQuery(`explain select * from t where a = "aa" collate utf8mb4_bin`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:p2 data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] eq(partitionrangecolumnscollate.t.a, "aa")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a = "aa" collate utf8mb4_bin`).Sort().Check(testkit.Rows("aa"))
// 'a' < 'b' < 'ä' in _bin
tk.MustQuery(`explain select * from t where a = "ä" collate utf8mb4_bin`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:p1 data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] eq(partitionrangecolumnscollate.t.a, "ä")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a = "ä" collate utf8mb4_bin`).Sort().Check(testkit.Rows("ä"))
tk.MustQuery(`explain select * from t where a = "b" collate utf8mb4_bin`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:p5 data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] eq(partitionrangecolumnscollate.t.a, "b")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a = "b" collate utf8mb4_bin`).Sort().Check(testkit.Rows("b"))
tk.MustQuery(`explain select * from t where a <= "b" collate utf8mb4_bin`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:all data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] le(partitionrangecolumnscollate.t.a, "b")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a <= "b" collate utf8mb4_bin`).Sort().Check(testkit.Rows("A", "AA", "AB", "Aa", "Ab", "B", "BA", "BB", "Ba", "Bb", "a", "aA", "aB", "aa", "ab", "b"))
tk.MustQuery(`explain select * from t where a < "b" collate utf8mb4_bin`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:all data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] lt(partitionrangecolumnscollate.t.a, "b")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
// Missing upper case B if not p5 is included!
tk.MustQuery(`select * from t where a < "b" collate utf8mb4_bin`).Sort().Check(testkit.Rows("A", "AA", "AB", "Aa", "Ab", "B", "BA", "BB", "Ba", "Bb", "a", "aA", "aB", "aa", "ab"))
tk.MustQuery(`explain select * from t where a >= "b" collate utf8mb4_bin`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:all data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] ge(partitionrangecolumnscollate.t.a, "b")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a >= "b" collate utf8mb4_bin`).Sort().Check(testkit.Rows("b", "bA", "bB", "ba", "bb", "ÄÄÄ", "ä"))
tk.MustQuery(`explain select * from t where a > "b" collate utf8mb4_bin`).Check(testkit.Rows(
`TableReader_7 8000.00 root partition:all data:Selection_6`,
`└─Selection_6 8000.00 cop[tikv] gt(partitionrangecolumnscollate.t.a, "b")`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery(`select * from t where a > "b" collate utf8mb4_bin`).Sort().Check(testkit.Rows("bA", "bB", "ba", "bb", "ÄÄÄ", "ä"))
}
func TestDisableTablePartition(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
for _, v := range []string{"'AUTO'", "'OFF'", "0", "'ON'"} {
tk.MustExec("set @@session.tidb_enable_table_partition = " + v)
tk.MustExec("set @@session.tidb_enable_list_partition = OFF")
tk.MustExec("drop table if exists t")
tk.MustExec(`create table t (id int) partition by list (id) (
partition p0 values in (1,2),partition p1 values in (3,4));`)
tbl := external.GetTableByName(t, tk, "test", "t")
require.Nil(t, tbl.Meta().Partition)
_, err := tk.Exec(`alter table t add partition (
partition p4 values in (7),
partition p5 values in (8,9));`)
require.True(t, dbterror.ErrPartitionMgmtOnNonpartitioned.Equal(err))
tk.MustExec("insert into t values (1),(3),(5),(100),(null)")
}
}
func generatePartitionTableByNum(num int) string {
buf := bytes.NewBuffer(make([]byte, 0, 1024*1024))
buf.WriteString("create table gen_t (id int) partition by list (id) (")
for i := 0; i < num; i++ {
if i > 0 {
buf.WriteString(",")
}
buf.WriteString(fmt.Sprintf("partition p%v values in (%v)", i, i))
}
buf.WriteString(")")
return buf.String()
}
func TestCreateTableWithListPartition(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
ddlChecker := schematracker.NewChecker(dom.DDL())
dom.SetDDL(ddlChecker)
ddlChecker.CreateTestDB()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("set @@session.tidb_enable_list_partition = ON")
tk.MustExec("drop table if exists t")
type errorCase struct {
sql string
err *terror.Error
}
cases := []errorCase{
{
"create table t (id int) partition by list (id);",
ast.ErrPartitionsMustBeDefined,
},
{
"create table t (a int) partition by list (b) (partition p0 values in (1));",
dbterror.ErrBadField,
},
{
"create table t (id timestamp) partition by list (id) (partition p0 values in ('2019-01-09 11:23:34'));",
dbterror.ErrValuesIsNotIntType,
},
{
"create table t (id decimal) partition by list (id) (partition p0 values in ('2019-01-09 11:23:34'));",
dbterror.ErrValuesIsNotIntType,
},
{
"create table t (id float) partition by list (id) (partition p0 values in (1));",
dbterror.ErrNotAllowedTypeInPartition,
},
{
"create table t (id double) partition by list (id) (partition p0 values in (1));",
dbterror.ErrNotAllowedTypeInPartition,
},
{
"create table t (id text) partition by list (id) (partition p0 values in ('abc'));",
dbterror.ErrValuesIsNotIntType,
},
{
"create table t (id blob) partition by list (id) (partition p0 values in ('abc'));",
dbterror.ErrValuesIsNotIntType,
},
{
"create table t (id enum('a','b')) partition by list (id) (partition p0 values in ('a'));",
dbterror.ErrValuesIsNotIntType,
},
{
"create table t (id set('a','b')) partition by list (id) (partition p0 values in ('a'));",
dbterror.ErrValuesIsNotIntType,
},
{
"create table t (a int) partition by list (a) (partition p0 values in (1), partition p0 values in (2));",
dbterror.ErrSameNamePartition,
},
{
"create table t (a int) partition by list (a) (partition p0 values in (1), partition P0 values in (2));",
dbterror.ErrSameNamePartition,
},
{
"create table t (id bigint) partition by list (cast(id as unsigned)) (partition p0 values in (1))",
dbterror.ErrPartitionFunctionIsNotAllowed,
},
{
"create table t (id float) partition by list (ceiling(id)) (partition p0 values in (1))",
dbterror.ErrPartitionFuncNotAllowed,
},
{
"create table t(b char(10)) partition by range columns (b) (partition p1 values less than ('G' collate utf8mb4_unicode_ci));",
dbterror.ErrPartitionFunctionIsNotAllowed,
},
{
"create table t (a date) partition by list (to_days(to_days(a))) (partition p0 values in (1), partition P1 values in (2));",
dbterror.ErrWrongExprInPartitionFunc,
},
{
"create table t (a int) partition by list (a) (partition p0 values in (1), partition p1 values in (1));",
dbterror.ErrMultipleDefConstInListPart,
},
{
"create table t (a int) partition by list (a) (partition p0 values in (1), partition p1 values in (+1));",
dbterror.ErrMultipleDefConstInListPart,
},
{
"create table t (a int) partition by list (a) (partition p0 values in (null), partition p1 values in (NULL));",
dbterror.ErrMultipleDefConstInListPart,
},
{
`create table t1 (id int key, name varchar(10), unique index idx(name)) partition by list (id) (
partition p0 values in (3,5,6,9,17),
partition p1 values in (1,2,10,11,19,20),
partition p2 values in (4,12,13,14,18),
partition p3 values in (7,8,15,16)
);`,
dbterror.ErrUniqueKeyNeedAllFieldsInPf,
},
{
generatePartitionTableByNum(ddl.PartitionCountLimit + 1),
dbterror.ErrTooManyPartitions,
},
}
for i, tt := range cases {
_, err := tk.Exec(tt.sql)
require.Truef(t, tt.err.Equal(err),
"case %d fail, sql = `%s`\nexpected error = `%v`\n actual error = `%v`",
i, tt.sql, tt.err, err,
)
}
validCases := []string{
"create table t (a int) partition by list (a) (partition p0 values in (1));",
"create table t (a bigint unsigned) partition by list (a) (partition p0 values in (18446744073709551615));",
"create table t (a bigint unsigned) partition by list (a) (partition p0 values in (18446744073709551615 - 1));",
"create table t (a int) partition by list (a) (partition p0 values in (1,null));",
"create table t (a int) partition by list (a) (partition p0 values in (1), partition p1 values in (2));",
`create table t (id int, name varchar(10), age int) partition by list (id) (
partition p0 values in (3,5,6,9,17),
partition p1 values in (1,2,10,11,19,20),
partition p2 values in (4,12,13,-14,18),
partition p3 values in (7,8,15,+16)
);`,
"create table t (id year) partition by list (id) (partition p0 values in (2000));",
"create table t (a tinyint) partition by list (a) (partition p0 values in (65536));",
"create table t (a tinyint) partition by list (a*100) (partition p0 values in (65536));",
"create table t (a bigint) partition by list (a) (partition p0 values in (to_seconds('2020-09-28 17:03:38'),to_seconds('2020-09-28 17:03:39')));",
"create table t (a datetime) partition by list (to_seconds(a)) (partition p0 values in (to_seconds('2020-09-28 17:03:38'),to_seconds('2020-09-28 17:03:39')));",
"create table t (a int, b int generated always as (a+1) virtual) partition by list (b + 1) (partition p0 values in (1));",
"create table t(a binary) partition by list columns (a) (partition p0 values in (X'0C'));",
generatePartitionTableByNum(ddl.PartitionCountLimit),
}
for id, sql := range validCases {
tk.MustExec("drop table if exists t")
tk.MustExec(sql)
tblName := "t"
if id == len(validCases)-1 {
tblName = "gen_t"
}
tbl := external.GetTableByName(t, tk, "test", tblName)
tblInfo := tbl.Meta()
require.NotNil(t, tblInfo.Partition)
require.True(t, tblInfo.Partition.Enable)
require.Equal(t, model.PartitionTypeList, tblInfo.Partition.Type)
}
}
func TestCreateTableWithListColumnsPartition(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
ddlChecker := schematracker.NewChecker(dom.DDL())
dom.SetDDL(ddlChecker)
ddlChecker.CreateTestDB()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("set @@session.tidb_enable_list_partition = ON")
tk.MustExec("drop table if exists t")
type errorCase struct {
sql string
err *terror.Error
}
cases := []errorCase{
{
"create table t (id int) partition by list columns (id);",
ast.ErrPartitionsMustBeDefined,
},
{
"create table t (a int) partition by list columns (b) (partition p0 values in (1));",
dbterror.ErrFieldNotFoundPart,
},
{
"create table t (id timestamp) partition by list columns (id) (partition p0 values in ('2019-01-09 11:23:34'));",
dbterror.ErrNotAllowedTypeInPartition,
},
{
"create table t (id decimal) partition by list columns (id) (partition p0 values in ('2019-01-09 11:23:34'));",
dbterror.ErrNotAllowedTypeInPartition,
},
{
"create table t (id year) partition by list columns (id) (partition p0 values in (2000));",
dbterror.ErrNotAllowedTypeInPartition,
},
{
"create table t (id float) partition by list columns (id) (partition p0 values in (1));",
dbterror.ErrNotAllowedTypeInPartition,
},
{