-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathdelete_test.go
130 lines (120 loc) · 3.91 KB
/
delete_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package executor_test
import (
"sync"
"testing"
"time"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/testkit"
)
func TestDeleteLockKey(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`drop table if exists t1, t2, t3, t4, t5, t6;`)
cases := []struct {
ddl string
pre string
tk1Stmt string
tk2Stmt string
}{
{
"create table t1(k int, kk int, val int, primary key(k, kk), unique key(val))",
"insert into t1 values(1, 2, 3)",
"delete from t1 where val = 3",
"insert into t1 values(1, 3, 3)",
},
{
"create table t2(k int, kk int, val int, primary key(k, kk))",
"insert into t2 values(1, 1, 1)",
"delete from t2 where k = 1",
"insert into t2 values(1, 1, 2)",
},
{
"create table t3(k int, kk int, val int, vv int, primary key(k, kk), unique key(val))",
"insert into t3 values(1, 2, 3, 4)",
"delete from t3 where vv = 4",
"insert into t3 values(1, 2, 3, 5)",
},
{
"create table t4(k int, kk int, val int, vv int, primary key(k, kk), unique key(val))",
"insert into t4 values(1, 2, 3, 4)",
"delete from t4 where 1",
"insert into t4 values(1, 2, 3, 5)",
},
{
"create table t5(k int, kk int, val int, vv int, primary key(k, kk), unique key(val))",
"insert into t5 values(1, 2, 3, 4), (2, 3, 4, 5)",
"delete from t5 where k in (1, 2, 3, 4)",
"insert into t5 values(1, 2, 3, 5)",
},
{
"create table t6(k int, kk int, val int, vv int, primary key(k, kk), unique key(val))",
"insert into t6 values(1, 2, 3, 4), (2, 3, 4, 5)",
"delete from t6 where kk between 0 and 10",
"insert into t6 values(1, 2, 3, 5), (2, 3, 4, 6)",
},
}
var wg sync.WaitGroup
for _, testCase := range cases {
wg.Add(1)
go func(testCase struct {
ddl string
pre string
tk1Stmt string
tk2Stmt string
}) {
tk1, tk2 := testkit.NewTestKit(t, store), testkit.NewTestKit(t, store)
tk1.MustExec("use test")
tk2.MustExec("use test")
tk1.Session().GetSessionVars().EnableClusteredIndex = variable.ClusteredIndexDefModeIntOnly
tk1.MustExec(testCase.ddl)
tk1.MustExec(testCase.pre)
tk1.MustExec("begin pessimistic")
tk2.MustExec("begin pessimistic")
tk1.MustExec(testCase.tk1Stmt)
doneCh := make(chan struct{}, 1)
go func() {
tk2.MustExec(testCase.tk2Stmt)
doneCh <- struct{}{}
}()
time.Sleep(50 * time.Millisecond)
tk1.MustExec("commit")
<-doneCh
tk2.MustExec("commit")
wg.Done()
}(testCase)
}
wg.Wait()
}
func TestIssue21200(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("drop database if exists TEST1")
tk.MustExec("create database TEST1")
tk.MustExec("use TEST1")
tk.MustExec("create table t(a int)")
tk.MustExec("create table t1(a int)")
tk.MustExec("insert into t values(1)")
tk.MustExec("insert into t1 values(1)")
tk.MustExec("delete a from t a where exists (select 1 from t1 where t1.a=a.a)")
tk.MustQuery("select * from t").Check(testkit.Rows())
tk.MustExec("insert into t values(1), (2)")
tk.MustExec("insert into t1 values(2)")
tk.MustExec("prepare stmt from 'delete a from t a where exists (select 1 from t1 where a.a=t1.a and t1.a=?)'")
tk.MustExec("set @a=1")
tk.MustExec("execute stmt using @a")
tk.MustQuery("select * from t").Check(testkit.Rows("2"))
}