Skip to content

Commit

Permalink
planner: DELETE cannot delete data in some cases when the database …
Browse files Browse the repository at this point in the history
…name is capitalized (#21202) (#21206)

Co-authored-by: Yiding Cui <winoros@gmail.com>
  • Loading branch information
ti-srebot and winoros authored Nov 23, 2020
1 parent 3cfa1d4 commit 98d60e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
20 changes: 20 additions & 0 deletions executor/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,23 @@ func (s *testSuite8) TestDeleteLockKey(c *C) {
}
wg.Wait()
}

func (s *testSuite8) TestIssue21200(c *C) {
tk := testkit.NewTestKit(c, s.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"))
}
10 changes: 5 additions & 5 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3809,12 +3809,12 @@ func (b *PlanBuilder) buildDelete(ctx context.Context, delete *ast.DeleteStmt) (
for _, tn := range delete.Tables.Tables {
foundMatch := false
for _, v := range tableList {
dbName := v.Schema.L
if dbName == "" {
dbName = b.ctx.GetSessionVars().CurrentDB
dbName := v.Schema
if dbName.L == "" {
dbName = model.NewCIStr(b.ctx.GetSessionVars().CurrentDB)
}
if (tn.Schema.L == "" || tn.Schema.L == dbName) && tn.Name.L == v.Name.L {
tn.Schema.L = dbName
if (tn.Schema.L == "" || tn.Schema.L == dbName.L) && tn.Name.L == v.Name.L {
tn.Schema = dbName
tn.DBInfo = v.DBInfo
tn.TableInfo = v.TableInfo
foundMatch = true
Expand Down

0 comments on commit 98d60e5

Please sign in to comment.