@@ -2513,6 +2513,124 @@ func TestExchangePartitionTableCompatiable(t *testing.T) {
2513
2513
require .NoError (t , err )
2514
2514
}
2515
2515
2516
+ func TestExchangePartitionMultiTable (t * testing.T ) {
2517
+ store := testkit .CreateMockStore (t )
2518
+ tk1 := testkit .NewTestKit (t , store )
2519
+
2520
+ dbName := "ExchangeMultiTable"
2521
+ tk1 .MustExec (`create schema ` + dbName )
2522
+ tk1 .MustExec (`use ` + dbName )
2523
+ tk1 .MustExec (`CREATE TABLE t1 (a int)` )
2524
+ tk1 .MustExec (`CREATE TABLE t2 (a int)` )
2525
+ tk1 .MustExec (`CREATE TABLE tp (a int) partition by hash(a) partitions 3` )
2526
+ tk1 .MustExec (`insert into t1 values (0)` )
2527
+ tk1 .MustExec (`insert into t2 values (3)` )
2528
+ tk1 .MustExec (`insert into tp values (6)` )
2529
+
2530
+ tk2 := testkit .NewTestKit (t , store )
2531
+ tk2 .MustExec (`use ` + dbName )
2532
+ tk3 := testkit .NewTestKit (t , store )
2533
+ tk3 .MustExec (`use ` + dbName )
2534
+ tk4 := testkit .NewTestKit (t , store )
2535
+ tk4 .MustExec (`use ` + dbName )
2536
+ waitFor := func (col int , tableName , s string ) {
2537
+ for {
2538
+ tk4 := testkit .NewTestKit (t , store )
2539
+ tk4 .MustExec (`use test` )
2540
+ sql := `admin show ddl jobs where db_name = '` + strings .ToLower (dbName ) + `' and table_name = '` + tableName + `' and job_type = 'exchange partition'`
2541
+ res := tk4 .MustQuery (sql ).Rows ()
2542
+ if len (res ) == 1 && res [0 ][col ] == s {
2543
+ break
2544
+ }
2545
+ time .Sleep (10 * time .Millisecond )
2546
+ }
2547
+ }
2548
+ alterChan1 := make (chan error )
2549
+ alterChan2 := make (chan error )
2550
+ tk3 .MustExec (`BEGIN` )
2551
+ tk3 .MustExec (`insert into tp values (1)` )
2552
+ go func () {
2553
+ alterChan1 <- tk1 .ExecToErr (`alter table tp exchange partition p0 with table t1` )
2554
+ }()
2555
+ waitFor (11 , "t1" , "running" )
2556
+ go func () {
2557
+ alterChan2 <- tk2 .ExecToErr (`alter table tp exchange partition p0 with table t2` )
2558
+ }()
2559
+ waitFor (11 , "t2" , "queueing" )
2560
+ tk3 .MustExec (`rollback` )
2561
+ require .NoError (t , <- alterChan1 )
2562
+ err := <- alterChan2
2563
+ tk3 .MustQuery (`select * from t1` ).Check (testkit .Rows ("6" ))
2564
+ tk3 .MustQuery (`select * from t2` ).Check (testkit .Rows ("0" ))
2565
+ tk3 .MustQuery (`select * from tp` ).Check (testkit .Rows ("3" ))
2566
+ require .NoError (t , err )
2567
+ }
2568
+
2569
+ func TestExchangePartitionValidation (t * testing.T ) {
2570
+ store := testkit .CreateMockStore (t )
2571
+ tk := testkit .NewTestKit (t , store )
2572
+
2573
+ dbName := "ExchangeValidation"
2574
+ tk .MustExec (`create schema ` + dbName )
2575
+ tk .MustExec (`use ` + dbName )
2576
+ tk .MustExec (`CREATE TABLE t1 (
2577
+ d date NOT NULL ,
2578
+ name varchar(10) NOT NULL,
2579
+ UNIQUE KEY (d,name))` )
2580
+
2581
+ tk .MustExec (`CREATE TABLE t1p (
2582
+ d date NOT NULL ,
2583
+ name varchar(10) NOT NULL,
2584
+ UNIQUE KEY (d,name)
2585
+ )
2586
+ PARTITION BY RANGE COLUMNS(d)
2587
+ (PARTITION p202307 VALUES LESS THAN ('2023-08-01'),
2588
+ PARTITION p202308 VALUES LESS THAN ('2023-09-01'),
2589
+ PARTITION p202309 VALUES LESS THAN ('2023-10-01'),
2590
+ PARTITION p202310 VALUES LESS THAN ('2023-11-01'),
2591
+ PARTITION p202311 VALUES LESS THAN ('2023-12-01'),
2592
+ PARTITION p202312 VALUES LESS THAN ('2024-01-01'),
2593
+ PARTITION pfuture VALUES LESS THAN (MAXVALUE))` )
2594
+
2595
+ tk .MustExec (`insert into t1 values ("2023-08-06","0000")` )
2596
+ tk .MustContainErrMsg (`alter table t1p exchange partition p202307 with table t1 with validation` ,
2597
+ "[ddl:1737]Found a row that does not match the partition" )
2598
+ tk .MustExec (`insert into t1 values ("2023-08-06","0001")` )
2599
+ }
2600
+
2601
+ func TestExchangePartitionPlacementPolicy (t * testing.T ) {
2602
+ store := testkit .CreateMockStore (t )
2603
+ tk := testkit .NewTestKit (t , store )
2604
+
2605
+ tk .MustExec (`create schema ExchangePartWithPolicy` )
2606
+ tk .MustExec (`use ExchangePartWithPolicy` )
2607
+ tk .MustExec (`CREATE PLACEMENT POLICY rule1 FOLLOWERS=1` )
2608
+ tk .MustExec (`CREATE PLACEMENT POLICY rule2 FOLLOWERS=2` )
2609
+ tk .MustExec (`CREATE TABLE t1 (
2610
+ d date NOT NULL ,
2611
+ name varchar(10) NOT NULL,
2612
+ UNIQUE KEY (d,name)
2613
+ ) PLACEMENT POLICY="rule1"` )
2614
+
2615
+ tk .MustExec (`CREATE TABLE t1p (
2616
+ d date NOT NULL ,
2617
+ name varchar(10) NOT NULL,
2618
+ UNIQUE KEY (d,name)
2619
+ ) PLACEMENT POLICY="rule2"
2620
+ PARTITION BY RANGE COLUMNS(d)
2621
+ (PARTITION p202307 VALUES LESS THAN ('2023-08-01'),
2622
+ PARTITION p202308 VALUES LESS THAN ('2023-09-01'),
2623
+ PARTITION p202309 VALUES LESS THAN ('2023-10-01'),
2624
+ PARTITION p202310 VALUES LESS THAN ('2023-11-01'),
2625
+ PARTITION p202311 VALUES LESS THAN ('2023-12-01'),
2626
+ PARTITION p202312 VALUES LESS THAN ('2024-01-01'),
2627
+ PARTITION pfuture VALUES LESS THAN (MAXVALUE))` )
2628
+
2629
+ tk .MustContainErrMsg (`alter table t1p exchange partition p202307 with table t1` ,
2630
+ "[ddl:1736]Tables have different definitions" )
2631
+ tk .MustExec (`insert into t1 values ("2023-08-06","0000")` )
2632
+ }
2633
+
2516
2634
func TestExchangePartitionHook (t * testing.T ) {
2517
2635
store , dom := testkit .CreateMockStoreAndDomain (t )
2518
2636
tk := testkit .NewTestKit (t , store )
0 commit comments