Skip to content

Commit 4d5ea32

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 447ed39 commit 4d5ea32

File tree

12 files changed

+2512
-126
lines changed

12 files changed

+2512
-126
lines changed

ddl/db_partition_test.go

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

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

ddl/ddl_api.go

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

4201-
// NOTE: if nt is temporary table, it should be checked
42024201
return nil
42034202
}
42044203

ddl/ddl_worker.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,24 +1553,28 @@ func updateSchemaVersion(d *ddlCtx, t *meta.Meta, job *model.Job, multiInfos ...
15531553
diff.OldSchemaID = oldSchemaIDs[0]
15541554
diff.AffectedOpts = affects
15551555
case model.ActionExchangeTablePartition:
1556-
var (
1557-
ptSchemaID int64
1558-
ptTableID int64
1559-
partName string
1560-
withValidation bool
1561-
)
1562-
err = job.DecodeArgs(&diff.TableID, &ptSchemaID, &ptTableID, &partName, &withValidation)
1563-
if err != nil {
1564-
return 0, errors.Trace(err)
1565-
}
15661556
diff.OldTableID = job.TableID
1567-
affects := make([]*model.AffectedOption, 1)
1568-
affects[0] = &model.AffectedOption{
1569-
SchemaID: ptSchemaID,
1570-
TableID: ptTableID,
1571-
OldTableID: ptTableID,
1557+
diff.OldSchemaID = job.SchemaID
1558+
if job.SchemaState != model.StatePublic {
1559+
diff.TableID = job.TableID
1560+
diff.SchemaID = job.SchemaID
1561+
} else {
1562+
// Update the partitioned table (it is only done in the last state)
1563+
var (
1564+
ptSchemaID int64
1565+
ptTableID int64
1566+
ptDefID int64 // Not needed, will reload the whole table
1567+
partName string // Not used
1568+
withValidation bool // Not used
1569+
)
1570+
// See ddl.ExchangeTablePartition
1571+
err = job.DecodeArgs(&ptDefID, &ptSchemaID, &ptTableID, &partName, &withValidation)
1572+
if err != nil {
1573+
return 0, errors.Trace(err)
1574+
}
1575+
diff.SchemaID = ptSchemaID
1576+
diff.TableID = ptTableID
15721577
}
1573-
diff.AffectedOpts = affects
15741578
case model.ActionTruncateTablePartition:
15751579
diff.TableID = job.TableID
15761580
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)