@@ -2606,6 +2606,124 @@ func TestExchangePartitionTableCompatiable(t *testing.T) {
2606
2606
require .NoError (t , err )
2607
2607
}
2608
2608
2609
+ func TestExchangePartitionMultiTable (t * testing.T ) {
2610
+ store := testkit .CreateMockStore (t )
2611
+ tk1 := testkit .NewTestKit (t , store )
2612
+
2613
+ dbName := "ExchangeMultiTable"
2614
+ tk1 .MustExec (`create schema ` + dbName )
2615
+ tk1 .MustExec (`use ` + dbName )
2616
+ tk1 .MustExec (`CREATE TABLE t1 (a int)` )
2617
+ tk1 .MustExec (`CREATE TABLE t2 (a int)` )
2618
+ tk1 .MustExec (`CREATE TABLE tp (a int) partition by hash(a) partitions 3` )
2619
+ tk1 .MustExec (`insert into t1 values (0)` )
2620
+ tk1 .MustExec (`insert into t2 values (3)` )
2621
+ tk1 .MustExec (`insert into tp values (6)` )
2622
+
2623
+ tk2 := testkit .NewTestKit (t , store )
2624
+ tk2 .MustExec (`use ` + dbName )
2625
+ tk3 := testkit .NewTestKit (t , store )
2626
+ tk3 .MustExec (`use ` + dbName )
2627
+ tk4 := testkit .NewTestKit (t , store )
2628
+ tk4 .MustExec (`use ` + dbName )
2629
+ waitFor := func (col int , tableName , s string ) {
2630
+ for {
2631
+ tk4 := testkit .NewTestKit (t , store )
2632
+ tk4 .MustExec (`use test` )
2633
+ sql := `admin show ddl jobs where db_name = '` + strings .ToLower (dbName ) + `' and table_name = '` + tableName + `' and job_type = 'exchange partition'`
2634
+ res := tk4 .MustQuery (sql ).Rows ()
2635
+ if len (res ) == 1 && res [0 ][col ] == s {
2636
+ break
2637
+ }
2638
+ time .Sleep (10 * time .Millisecond )
2639
+ }
2640
+ }
2641
+ alterChan1 := make (chan error )
2642
+ alterChan2 := make (chan error )
2643
+ tk3 .MustExec (`BEGIN` )
2644
+ tk3 .MustExec (`insert into tp values (1)` )
2645
+ go func () {
2646
+ alterChan1 <- tk1 .ExecToErr (`alter table tp exchange partition p0 with table t1` )
2647
+ }()
2648
+ waitFor (11 , "t1" , "running" )
2649
+ go func () {
2650
+ alterChan2 <- tk2 .ExecToErr (`alter table tp exchange partition p0 with table t2` )
2651
+ }()
2652
+ waitFor (11 , "t2" , "queueing" )
2653
+ tk3 .MustExec (`rollback` )
2654
+ require .NoError (t , <- alterChan1 )
2655
+ err := <- alterChan2
2656
+ tk3 .MustQuery (`select * from t1` ).Check (testkit .Rows ("6" ))
2657
+ tk3 .MustQuery (`select * from t2` ).Check (testkit .Rows ("0" ))
2658
+ tk3 .MustQuery (`select * from tp` ).Check (testkit .Rows ("3" ))
2659
+ require .NoError (t , err )
2660
+ }
2661
+
2662
+ func TestExchangePartitionValidation (t * testing.T ) {
2663
+ store := testkit .CreateMockStore (t )
2664
+ tk := testkit .NewTestKit (t , store )
2665
+
2666
+ dbName := "ExchangeValidation"
2667
+ tk .MustExec (`create schema ` + dbName )
2668
+ tk .MustExec (`use ` + dbName )
2669
+ tk .MustExec (`CREATE TABLE t1 (
2670
+ d date NOT NULL ,
2671
+ name varchar(10) NOT NULL,
2672
+ UNIQUE KEY (d,name))` )
2673
+
2674
+ tk .MustExec (`CREATE TABLE t1p (
2675
+ d date NOT NULL ,
2676
+ name varchar(10) NOT NULL,
2677
+ UNIQUE KEY (d,name)
2678
+ )
2679
+ PARTITION BY RANGE COLUMNS(d)
2680
+ (PARTITION p202307 VALUES LESS THAN ('2023-08-01'),
2681
+ PARTITION p202308 VALUES LESS THAN ('2023-09-01'),
2682
+ PARTITION p202309 VALUES LESS THAN ('2023-10-01'),
2683
+ PARTITION p202310 VALUES LESS THAN ('2023-11-01'),
2684
+ PARTITION p202311 VALUES LESS THAN ('2023-12-01'),
2685
+ PARTITION p202312 VALUES LESS THAN ('2024-01-01'),
2686
+ PARTITION pfuture VALUES LESS THAN (MAXVALUE))` )
2687
+
2688
+ tk .MustExec (`insert into t1 values ("2023-08-06","0000")` )
2689
+ tk .MustContainErrMsg (`alter table t1p exchange partition p202307 with table t1 with validation` ,
2690
+ "[ddl:1737]Found a row that does not match the partition" )
2691
+ tk .MustExec (`insert into t1 values ("2023-08-06","0001")` )
2692
+ }
2693
+
2694
+ func TestExchangePartitionPlacementPolicy (t * testing.T ) {
2695
+ store := testkit .CreateMockStore (t )
2696
+ tk := testkit .NewTestKit (t , store )
2697
+
2698
+ tk .MustExec (`create schema ExchangePartWithPolicy` )
2699
+ tk .MustExec (`use ExchangePartWithPolicy` )
2700
+ tk .MustExec (`CREATE PLACEMENT POLICY rule1 FOLLOWERS=1` )
2701
+ tk .MustExec (`CREATE PLACEMENT POLICY rule2 FOLLOWERS=2` )
2702
+ tk .MustExec (`CREATE TABLE t1 (
2703
+ d date NOT NULL ,
2704
+ name varchar(10) NOT NULL,
2705
+ UNIQUE KEY (d,name)
2706
+ ) PLACEMENT POLICY="rule1"` )
2707
+
2708
+ tk .MustExec (`CREATE TABLE t1p (
2709
+ d date NOT NULL ,
2710
+ name varchar(10) NOT NULL,
2711
+ UNIQUE KEY (d,name)
2712
+ ) PLACEMENT POLICY="rule2"
2713
+ PARTITION BY RANGE COLUMNS(d)
2714
+ (PARTITION p202307 VALUES LESS THAN ('2023-08-01'),
2715
+ PARTITION p202308 VALUES LESS THAN ('2023-09-01'),
2716
+ PARTITION p202309 VALUES LESS THAN ('2023-10-01'),
2717
+ PARTITION p202310 VALUES LESS THAN ('2023-11-01'),
2718
+ PARTITION p202311 VALUES LESS THAN ('2023-12-01'),
2719
+ PARTITION p202312 VALUES LESS THAN ('2024-01-01'),
2720
+ PARTITION pfuture VALUES LESS THAN (MAXVALUE))` )
2721
+
2722
+ tk .MustContainErrMsg (`alter table t1p exchange partition p202307 with table t1` ,
2723
+ "[ddl:1736]Tables have different definitions" )
2724
+ tk .MustExec (`insert into t1 values ("2023-08-06","0000")` )
2725
+ }
2726
+
2609
2727
func TestExchangePartitionHook (t * testing.T ) {
2610
2728
store , dom := testkit .CreateMockStoreAndDomain (t )
2611
2729
tk := testkit .NewTestKit (t , store )
0 commit comments