Skip to content

Commit

Permalink
planner: fix update privilege error (pingcap#10085)
Browse files Browse the repository at this point in the history
  • Loading branch information
haplone authored and winoros committed Nov 11, 2019
1 parent 10576ae commit cb75e17
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ func (s *testSuite) TestGeneratedColumnRead(c *C) {
tk.MustExec("insert into t values(18)")
tk.MustExec("update test_gc_read set a = a+1 where a in (select a from t)")
result = tk.MustQuery("select * from test_gc_read order by a")
result.Check(testkit.Rows(`10 <nil> <nil> <nil> <nil>`, `11 2 13 22 26`, `13 4 17 52 34`, `19 8 27 152 54`))
result.Check(testkit.Rows(`10 <nil> <nil> <nil>`, `11 2 13 22`, `13 4 17 52`, `19 8 27 152`))

// Test different types between generation expression and generated column.
tk.MustExec(`CREATE TABLE test_gc_read_cast(a VARCHAR(255), b VARCHAR(255), c INT AS (JSON_EXTRACT(a, b)), d INT AS (JSON_EXTRACT(a, b)) STORED)`)
Expand Down
14 changes: 13 additions & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2290,7 +2290,10 @@ func (b *planBuilder) buildUpdate(update *ast.UpdateStmt) (Plan, error) {
proj.SetChildren(p)
p = proj
}
orderedList, np, err := b.buildUpdateLists(tableList, update.List, p)

var updateTableList []*ast.TableName
updateTableList = extractTableList(sel.From.TableRefs, updateTableList, true)
orderedList, np, err := b.buildUpdateLists(updateTableList, update.List, p)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down Expand Up @@ -2379,10 +2382,19 @@ func (b *planBuilder) buildUpdateLists(tableList []*ast.TableName, list []*ast.A
p = np
newList = append(newList, &expression.Assignment{Col: col, Expr: newExpr})
}

tblDbMap := make(map[string]string, len(tableList))
for _, tbl := range tableList {
tblDbMap[tbl.Name.L] = tbl.DBInfo.Name.L
}
for _, assign := range newList {
col := assign.Col

dbName := col.DBName.L
// To solve issue#10028, we need to get database name by the table alias name.
if dbNameTmp, ok := tblDbMap[col.TblName.L]; ok {
dbName = dbNameTmp
}
if dbName == "" {
dbName = b.ctx.GetSessionVars().CurrentDB
}
Expand Down
15 changes: 15 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2424,6 +2424,21 @@ s.a = t.a
and t.c >= 1 and t.c <= 10000
and s.b !='xx';`)

// Fix issue 10028
tk.MustExec("create database ap")
tk.MustExec("create database tp")
tk.MustExec("grant all privileges on ap.* to xxx")
tk.MustExec("grant select on tp.* to xxx")
tk.MustExec("flush privileges")
tk.MustExec("create table tp.record( id int,name varchar(128),age int)")
tk.MustExec("insert into tp.record (id,name,age) values (1,'john',18),(2,'lary',19),(3,'lily',18)")
tk.MustExec("create table ap.record( id int,name varchar(128),age int)")
tk.MustExec("insert into ap.record(id) values(1)")
c.Assert(tk1.Se.Auth(&auth.UserIdentity{Username: "xxx", Hostname: "localhost"},
[]byte(""),
[]byte("")), IsTrue)
_, err2 := tk1.Exec("update ap.record t inner join tp.record tt on t.id=tt.id set t.name=tt.name")
c.Assert(err2, IsNil)
}

func (s *testSessionSuite) TestTxnGoString(c *C) {
Expand Down

0 comments on commit cb75e17

Please sign in to comment.