@@ -3921,6 +3921,122 @@ func (s *testSuite) TestSplitRegion(c *C) {
3921
3921
tk .MustExec (`split table t by (0),(1000),(1000000)` )
3922
3922
}
3923
3923
3924
+ func (s * testSuite ) TestShowTableRegion (c * C ) {
3925
+ tk := testkit .NewTestKit (c , s .store )
3926
+ tk .MustExec ("use test" )
3927
+ tk .MustExec ("drop table if exists t_regions1, t_regions" )
3928
+ tk .MustExec ("create table t_regions1 (a int key, b int, index idx(b))" )
3929
+ tk .MustExec ("create table t_regions (a int key, b int, index idx(b))" )
3930
+
3931
+ // Test show table regions.
3932
+ tk .MustExec (`split table t_regions1 by (0)` )
3933
+ tk .MustExec (`split table t_regions between (-10000) and (10000) regions 4;` )
3934
+ re := tk .MustQuery ("show table t_regions regions" )
3935
+ rows := re .Rows ()
3936
+ // Table t_regions should have 4 regions now.
3937
+ c .Assert (len (rows ), Equals , 4 )
3938
+ c .Assert (len (rows [0 ]), Equals , 7 )
3939
+ tbl1 := testGetTableByName (c , tk .Se , "test" , "t_regions1" )
3940
+ tbl := testGetTableByName (c , tk .Se , "test" , "t_regions" )
3941
+ // Check the region start key.
3942
+ c .Assert (rows [0 ][1 ], Matches , fmt .Sprintf ("t_%d_.*" , tbl1 .Meta ().ID ))
3943
+ c .Assert (rows [1 ][1 ], Equals , fmt .Sprintf ("t_%d_r_-5000" , tbl .Meta ().ID ))
3944
+ c .Assert (rows [2 ][1 ], Equals , fmt .Sprintf ("t_%d_r_0" , tbl .Meta ().ID ))
3945
+ c .Assert (rows [3 ][1 ], Equals , fmt .Sprintf ("t_%d_r_5000" , tbl .Meta ().ID ))
3946
+
3947
+ // Test show table index regions.
3948
+ tk .MustExec (`split table t_regions index idx between (-1000) and (1000) regions 4;` )
3949
+ re = tk .MustQuery ("show table t_regions index idx regions" )
3950
+ rows = re .Rows ()
3951
+ // The index `idx` of table t_regions should have 4 regions now.
3952
+ c .Assert (len (rows ), Equals , 4 )
3953
+ // Check the region start key.
3954
+ c .Assert (rows [0 ][1 ], Equals , fmt .Sprintf ("t_%d_i_1_" , tbl .Meta ().ID ))
3955
+ c .Assert (rows [1 ][1 ], Matches , fmt .Sprintf ("t_%d_i_1_.*" , tbl .Meta ().ID ))
3956
+ c .Assert (rows [2 ][1 ], Matches , fmt .Sprintf ("t_%d_i_1_.*" , tbl .Meta ().ID ))
3957
+ c .Assert (rows [3 ][1 ], Matches , fmt .Sprintf ("t_%d_i_1_.*" , tbl .Meta ().ID ))
3958
+
3959
+ re = tk .MustQuery ("show table t_regions regions" )
3960
+ rows = re .Rows ()
3961
+ // The index `idx` of table t_regions should have 4 regions now.
3962
+ c .Assert (len (rows ), Equals , 7 )
3963
+ // Check the region start key.
3964
+ c .Assert (rows [0 ][1 ], Matches , fmt .Sprintf ("t_%d_i_1_.*" , tbl .Meta ().ID ))
3965
+ c .Assert (rows [1 ][1 ], Equals , fmt .Sprintf ("t_%d_r_-5000" , tbl .Meta ().ID ))
3966
+ c .Assert (rows [2 ][1 ], Equals , fmt .Sprintf ("t_%d_r_0" , tbl .Meta ().ID ))
3967
+ c .Assert (rows [3 ][1 ], Equals , fmt .Sprintf ("t_%d_r_5000" , tbl .Meta ().ID ))
3968
+ c .Assert (rows [4 ][1 ], Matches , fmt .Sprintf ("t_%d_i_1_.*" , tbl .Meta ().ID ))
3969
+ c .Assert (rows [5 ][1 ], Matches , fmt .Sprintf ("t_%d_i_1_.*" , tbl .Meta ().ID ))
3970
+ c .Assert (rows [6 ][1 ], Matches , fmt .Sprintf ("t_%d_i_1_.*" , tbl .Meta ().ID ))
3971
+
3972
+ // Test unsigned primary key and wait scatter finish.
3973
+ tk .MustExec ("drop table if exists t_regions" )
3974
+ tk .MustExec ("create table t_regions (a int unsigned key, b int, index idx(b))" )
3975
+
3976
+ // Test show table regions.
3977
+ tk .MustExec (`set @@session.tidb_wait_split_region_finish=1;` )
3978
+ tk .MustExec (`split table t_regions between (0) and (10000) regions 4;` )
3979
+ re = tk .MustQuery ("show table t_regions regions" )
3980
+ rows = re .Rows ()
3981
+ // Table t_regions should have 4 regions now.
3982
+ c .Assert (len (rows ), Equals , 4 )
3983
+ tbl = testGetTableByName (c , tk .Se , "test" , "t_regions" )
3984
+ // Check the region start key.
3985
+ c .Assert (rows [0 ][1 ], Matches , "t_.*" )
3986
+ c .Assert (rows [1 ][1 ], Equals , fmt .Sprintf ("t_%d_r_2500" , tbl .Meta ().ID ))
3987
+ c .Assert (rows [2 ][1 ], Equals , fmt .Sprintf ("t_%d_r_5000" , tbl .Meta ().ID ))
3988
+ c .Assert (rows [3 ][1 ], Equals , fmt .Sprintf ("t_%d_r_7500" , tbl .Meta ().ID ))
3989
+
3990
+ // Test show table index regions.
3991
+ tk .MustExec (`split table t_regions index idx between (0) and (1000) regions 4;` )
3992
+ re = tk .MustQuery ("show table t_regions index idx regions" )
3993
+ rows = re .Rows ()
3994
+ // The index `idx` of table t_regions should have 4 regions now.
3995
+ c .Assert (len (rows ), Equals , 4 )
3996
+ // Check the region start key.
3997
+ c .Assert (rows [0 ][1 ], Equals , fmt .Sprintf ("t_%d_i_1_" , tbl .Meta ().ID ))
3998
+ c .Assert (rows [1 ][1 ], Matches , fmt .Sprintf ("t_%d_i_1_.*" , tbl .Meta ().ID ))
3999
+ c .Assert (rows [2 ][1 ], Matches , fmt .Sprintf ("t_%d_i_1_.*" , tbl .Meta ().ID ))
4000
+ c .Assert (rows [3 ][1 ], Matches , fmt .Sprintf ("t_%d_i_1_.*" , tbl .Meta ().ID ))
4001
+
4002
+ // Test show table regions for partition table when disable split region when create table.
4003
+ atomic .StoreUint32 (& ddl .EnableSplitTableRegion , 0 )
4004
+ tk .MustExec ("drop table if exists partition_t;" )
4005
+ tk .MustExec ("set @@session.tidb_enable_table_partition = '1';" )
4006
+ tk .MustExec ("create table partition_t (a int, b int,index(a)) partition by hash (a) partitions 3" )
4007
+ re = tk .MustQuery ("show table partition_t regions" )
4008
+ rows = re .Rows ()
4009
+ // Table t_regions should have 4 regions now.
4010
+ c .Assert (len (rows ), Equals , 1 )
4011
+ c .Assert (rows [0 ][1 ], Matches , "t_.*" )
4012
+
4013
+ // Test show table regions for partition table when enable split region when create table.
4014
+ atomic .StoreUint32 (& ddl .EnableSplitTableRegion , 1 )
4015
+ tk .MustExec ("set @@global.tidb_scatter_region=1;" )
4016
+ tk .MustExec ("drop table if exists partition_t;" )
4017
+ tk .MustExec ("create table partition_t (a int, b int,index(a)) partition by hash (a) partitions 3" )
4018
+ re = tk .MustQuery ("show table partition_t regions" )
4019
+ rows = re .Rows ()
4020
+ // Table t_regions should have 4 regions now.
4021
+ c .Assert (len (rows ), Equals , 3 )
4022
+ tbl = testGetTableByName (c , tk .Se , "test" , "partition_t" )
4023
+ partitionDef := tbl .Meta ().GetPartitionInfo ().Definitions
4024
+ c .Assert (rows [0 ][1 ], Matches , fmt .Sprintf ("t_%d_.*" , partitionDef [0 ].ID ))
4025
+ c .Assert (rows [1 ][1 ], Matches , fmt .Sprintf ("t_%d_.*" , partitionDef [1 ].ID ))
4026
+ c .Assert (rows [2 ][1 ], Matches , fmt .Sprintf ("t_%d_.*" , partitionDef [2 ].ID ))
4027
+ atomic .StoreUint32 (& ddl .EnableSplitTableRegion , 0 )
4028
+ }
4029
+
4030
+ func testGetTableByName (c * C , ctx sessionctx.Context , db , table string ) table.Table {
4031
+ dom := domain .GetDomain (ctx )
4032
+ // Make sure the table schema is the new schema.
4033
+ err := dom .Reload ()
4034
+ c .Assert (err , IsNil )
4035
+ tbl , err := dom .InfoSchema ().TableByName (model .NewCIStr (db ), model .NewCIStr (table ))
4036
+ c .Assert (err , IsNil )
4037
+ return tbl
4038
+ }
4039
+
3924
4040
func (s * testSuite ) TestIssue10435 (c * C ) {
3925
4041
tk := testkit .NewTestKit (c , s .store )
3926
4042
tk .MustExec ("use test" )
0 commit comments