Skip to content

Commit 98f5610

Browse files
mjonssti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#45877
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 015241b commit 98f5610

File tree

12 files changed

+433
-126
lines changed

12 files changed

+433
-126
lines changed

ddl/db_partition_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,124 @@ func TestExchangePartitionTableCompatiable(t *testing.T) {
26062606
require.NoError(t, err)
26072607
}
26082608

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+
26092727
func TestExchangePartitionHook(t *testing.T) {
26102728
store, dom := testkit.CreateMockStoreAndDomain(t)
26112729
tk := testkit.NewTestKit(t, store)

ddl/ddl_api.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4522,7 +4522,6 @@ func checkExchangePartition(pt *model.TableInfo, nt *model.TableInfo) error {
45224522
return errors.Trace(dbterror.ErrPartitionExchangeForeignKey.GenWithStackByArgs(nt.Name))
45234523
}
45244524

4525-
// NOTE: if nt is temporary table, it should be checked
45264525
return nil
45274526
}
45284527

ddl/ddl_worker.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,24 +1366,28 @@ func updateSchemaVersion(d *ddlCtx, t *meta.Meta, job *model.Job, multiInfos ...
13661366
diff.OldSchemaID = oldSchemaIDs[0]
13671367
diff.AffectedOpts = affects
13681368
case model.ActionExchangeTablePartition:
1369-
var (
1370-
ptSchemaID int64
1371-
ptTableID int64
1372-
partName string
1373-
withValidation bool
1374-
)
1375-
err = job.DecodeArgs(&diff.TableID, &ptSchemaID, &ptTableID, &partName, &withValidation)
1376-
if err != nil {
1377-
return 0, errors.Trace(err)
1378-
}
13791369
diff.OldTableID = job.TableID
1380-
affects := make([]*model.AffectedOption, 1)
1381-
affects[0] = &model.AffectedOption{
1382-
SchemaID: ptSchemaID,
1383-
TableID: ptTableID,
1384-
OldTableID: ptTableID,
1370+
diff.OldSchemaID = job.SchemaID
1371+
if job.SchemaState != model.StatePublic {
1372+
diff.TableID = job.TableID
1373+
diff.SchemaID = job.SchemaID
1374+
} else {
1375+
// Update the partitioned table (it is only done in the last state)
1376+
var (
1377+
ptSchemaID int64
1378+
ptTableID int64
1379+
ptDefID int64 // Not needed, will reload the whole table
1380+
partName string // Not used
1381+
withValidation bool // Not used
1382+
)
1383+
// See ddl.ExchangeTablePartition
1384+
err = job.DecodeArgs(&ptDefID, &ptSchemaID, &ptTableID, &partName, &withValidation)
1385+
if err != nil {
1386+
return 0, errors.Trace(err)
1387+
}
1388+
diff.SchemaID = ptSchemaID
1389+
diff.TableID = ptTableID
13851390
}
1386-
diff.AffectedOpts = affects
13871391
case model.ActionTruncateTablePartition:
13881392
diff.TableID = job.TableID
13891393
if len(job.CtxVars) > 0 {

ddl/failtest/fail_db_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ func TestHalfwayCancelOperations(t *testing.T) {
135135
tk.MustExec("insert into pt values(1), (3), (5)")
136136
tk.MustExec("create table nt(a int)")
137137
tk.MustExec("insert into nt values(7)")
138-
tk.MustExec("set @@tidb_enable_exchange_partition=1")
139-
defer tk.MustExec("set @@tidb_enable_exchange_partition=0")
140138
err = tk.ExecToErr("alter table pt exchange partition p1 with table nt")
141139
require.Error(t, err)
142140

0 commit comments

Comments
 (0)