Skip to content

Commit 865b283

Browse files
authored
*: Truncate partition with global index improvement (#55831)
close #55819
1 parent 4442d49 commit 865b283

File tree

13 files changed

+834
-313
lines changed

13 files changed

+834
-313
lines changed

pkg/ddl/partition.go

Lines changed: 199 additions & 262 deletions
Large diffs are not rendered by default.

pkg/ddl/tests/partition/db_partition_test.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,15 @@ func TestTruncatePartitionWithGlobalIndex(t *testing.T) {
14291429
time.Sleep(10 * time.Millisecond)
14301430
}
14311431
}
1432+
waitFor(4, "write only")
1433+
tkTmp := testkit.NewTestKit(t, store)
1434+
tkTmp.MustExec(`begin`)
1435+
tkTmp.MustExec("use test")
1436+
tkTmp.MustQuery(`select count(*) from test_global`).Check(testkit.Rows("5"))
1437+
tk2.MustExec(`rollback`)
1438+
tk2.MustExec(`begin`)
1439+
tk2.MustExec(`insert into test_global values (5,5,5)`)
1440+
tkTmp.MustExec(`rollback`)
14321441
waitFor(4, "delete only")
14331442
tk3 := testkit.NewTestKit(t, store)
14341443
tk3.MustExec(`begin`)
@@ -1437,16 +1446,21 @@ func TestTruncatePartitionWithGlobalIndex(t *testing.T) {
14371446
tk3.MustQuery(`explain format='brief' select c from test_global use index(idx_c) where c = 15`).CheckContain("Point_Get")
14381447
tk3.MustQuery(`select b from test_global use index(idx_b) where b = 15`).Check(testkit.Rows())
14391448
tk3.MustQuery(`select c from test_global use index(idx_c) where c = 15`).Check(testkit.Rows())
1440-
// Here it will fail with
1441-
// the partition is not in public.
14421449
err := tk3.ExecToErr(`insert into test_global values (15,15,15)`)
14431450
require.Error(t, err)
1444-
require.ErrorContains(t, err, "the partition is in not in public")
1451+
require.ErrorContains(t, err, "[kv:1062]Duplicate entry '15' for key 'test_global.idx_b'")
14451452
tk2.MustExec(`commit`)
1453+
waitFor(4, "delete reorganization")
1454+
tk2.MustQuery(`select b from test_global use index(idx_b) where b = 15`).Check(testkit.Rows())
1455+
tk2.MustQuery(`select c from test_global use index(idx_c) where c = 15`).Check(testkit.Rows())
1456+
err = tk2.ExecToErr(`insert into test_global values (15,15,15)`)
1457+
require.NoError(t, err)
1458+
tk2.MustExec(`begin`)
14461459
tk3.MustExec(`commit`)
1460+
tk.MustExec(`commit`)
14471461
<-syncChan
14481462
result := tk.MustQuery("select * from test_global;")
1449-
result.Sort().Check(testkit.Rows(`1 1 1`, `2 2 2`, `5 5 5`))
1463+
result.Sort().Check(testkit.Rows(`1 1 1`, `15 15 15`, `2 2 2`, `5 5 5`))
14501464

14511465
tt = external.GetTableByName(t, tk, "test", "test_global")
14521466
idxInfo := tt.Meta().FindIndexByName("idx_b")
@@ -1487,12 +1501,12 @@ func TestGlobalIndexUpdateInTruncatePartition(t *testing.T) {
14871501
tk1 := testkit.NewTestKit(t, store)
14881502
tk1.MustExec("use test")
14891503
err := tk1.ExecToErr("update test_global set a = 2 where a = 11")
1490-
assert.NotNil(t, err)
1504+
assert.NoError(t, err)
14911505
}
14921506
})
14931507

14941508
tk.MustExec("alter table test_global truncate partition p1")
1495-
tk.MustQuery("select * from test_global use index(idx_b) order by a").Check(testkit.Rows("11 11 11", "12 12 12"))
1509+
tk.MustQuery("select * from test_global use index(idx_b) order by a").Check(testkit.Rows("2 11 11", "12 12 12"))
14961510
}
14971511

14981512
func TestGlobalIndexUpdateInTruncatePartition4Hash(t *testing.T) {
@@ -1515,7 +1529,7 @@ func TestGlobalIndexUpdateInTruncatePartition4Hash(t *testing.T) {
15151529
tk1 := testkit.NewTestKit(t, store)
15161530
tk1.MustExec("use test")
15171531
err = tk1.ExecToErr("update test_global set a = 1 where a = 12")
1518-
assert.NotNil(t, err)
1532+
assert.NoError(t, err)
15191533
}
15201534
})
15211535

@@ -1577,7 +1591,7 @@ func TestGlobalIndexInsertInTruncatePartition(t *testing.T) {
15771591
tk1 := testkit.NewTestKit(t, store)
15781592
tk1.MustExec("use test")
15791593
err = tk1.ExecToErr("insert into test_global values(2, 2, 2)")
1580-
assert.NotNil(t, err)
1594+
assert.NoError(t, err)
15811595
}
15821596
})
15831597

@@ -3168,6 +3182,8 @@ func TestRemovePartitioningAutoIDs(t *testing.T) {
31683182
tk2.MustExec(`COMMIT`)
31693183

31703184
/*
3185+
// Currently there is an duplicate entry issue, so it will rollback in WriteReorganization
3186+
// instead of continuing.
31713187
waitFor(4, "t", "delete reorganization")
31723188
tk2.MustExec(`BEGIN`)
31733189
tk2.MustExec(`insert into t values (null, 24)`)
@@ -3655,3 +3671,23 @@ func checkGlobalAndPK(t *testing.T, tk *testkit.TestKit, name string, indexes in
36553671
require.True(t, idxInfo.Primary)
36563672
}
36573673
}
3674+
3675+
func TestTruncateNumberOfPhases(t *testing.T) {
3676+
store := testkit.CreateMockStore(t)
3677+
tk := testkit.NewTestKit(t, store)
3678+
tk.MustExec("use test")
3679+
tk.MustExec(`create table t (a int primary key , b varchar(255)) partition by hash(a) partitions 3`)
3680+
ctx := tk.Session()
3681+
dom := domain.GetDomain(ctx)
3682+
schemaVersion := dom.InfoSchema().SchemaMetaVersion()
3683+
tk.MustExec(`insert into t values (1,1),(2,2),(3,3)`)
3684+
tk.MustExec(`alter table t truncate partition p1`)
3685+
// Without global index, truncate partition should be a single state change
3686+
require.Equal(t, int64(4), dom.InfoSchema().SchemaMetaVersion()-schemaVersion)
3687+
tk.MustExec(`drop table t`)
3688+
tk.MustExec(`create table t (a int primary key , b varchar(255), unique key (b) global) partition by hash(a) partitions 3`)
3689+
schemaVersion = dom.InfoSchema().SchemaMetaVersion()
3690+
tk.MustExec(`insert into t values (1,1),(2,2),(3,3)`)
3691+
tk.MustExec(`alter table t truncate partition p1`)
3692+
require.Equal(t, int64(4), dom.InfoSchema().SchemaMetaVersion()-schemaVersion)
3693+
}

0 commit comments

Comments
 (0)