Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

privilege: fix update privilege error for release-2.1 #10104

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2145,7 +2145,8 @@ func (b *planBuilder) buildUpdate(update *ast.UpdateStmt) (Plan, error) {

var tableList []*ast.TableName
tableList = extractTableList(sel.From.TableRefs, tableList)
for _, t := range tableList {
tableListForUpdate := extractTableListForUpdate(tableList, update.List)
for _, t := range tableListForUpdate {
dbName := t.Schema.L
if dbName == "" {
dbName = b.ctx.GetSessionVars().CurrentDB
Expand Down Expand Up @@ -2435,6 +2436,33 @@ func extractTableList(node ast.ResultSetNode, input []*ast.TableName) []*ast.Tab
return input
}

func extractTableListForUpdate(refTables []*ast.TableName, sets []*ast.Assignment) []*ast.TableName {
// if there's only one tbl , we won't bother
if len(refTables) == 1 {
return refTables
}
tbls := make([]*ast.TableName, 0, len(sets))
var tblsMap map[string]*ast.TableName

for _, a := range sets {
c := a.Column

if c.Table.L != "" {
for _, tbl := range refTables {
if strings.EqualFold(tbl.Name.L, c.Name.L) {
tblsMap["`"+tbl.Schema.L+"`.`"+tbl.Name.L+"`"] = tbl
}
}
}
}

for _, tbl := range tblsMap {
tbls = append(tbls, tbl)
}

return tbls
}

// extractTableSourceAsNames extracts TableSource.AsNames from node.
// if onlySelectStmt is set to be true, only extracts AsNames when TableSource.Source.(type) == *ast.SelectStmt
func extractTableSourceAsNames(node ast.ResultSetNode, input []string, onlySelectStmt bool) []string {
Expand Down
23 changes: 23 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2367,3 +2367,26 @@ func (s *testSessionSuite) TestTxnGoString(c *C) {
tk.MustExec("rollback")
c.Assert(fmt.Sprintf("%#v", txn), Equals, "Txn{state=invalid}")
}

func (s *testSessionSuite) TestUpdatePrivilege(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create user xxx;")

tk1 := testkit.NewTestKitWithInit(c, s.store)

// 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)
}