Skip to content

Commit

Permalink
planner, executor: enable update if update list do not contains view (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
XuHuaiyu authored Apr 24, 2020
1 parent 445e69b commit db99fb1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
24 changes: 24 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,30 @@ func (s *testSuite3) TestCreateView(c *C) {
c.Assert(terror.ErrorEqual(err, plannercore.ErrNoSuchTable), IsTrue)
tk.MustExec("drop table test_v_nested")
tk.MustExec("drop view v_nested, v_nested2")

// issue 16253
tk.MustExec("drop table if exists t, t1")
tk.MustExec("create table t(a int)")
tk.MustExec("create table t1(a int)")
tk.MustExec("create definer='root'@'localhost' view v_issue_16253 as select * from t1")
tk.MustExec("insert into t values(1)")
tk.MustExec("insert into t1 values(1)")
tk.MustQuery("select a from t").Check(testkit.Rows("1"))
tk.MustExec("update t, v_issue_16253 set t.a = 2 where t.a = v_issue_16253.a")
tk.MustQuery("select a from t").Check(testkit.Rows("2"))
_, err = tk.Exec("update t, v_issue_16253 set v_issue_16253.a = 2 where t.a = v_issue_16253.a")
c.Assert(err.Error(), Equals, "update view v_issue_16253 is not supported now.")

tk.MustExec("drop database if exists test2")
tk.MustExec("create database test2")
tk.MustExec("use test2")
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values(1)")
tk.MustQuery("select a from t").Check(testkit.Rows("1"))
tk.MustExec("update t, test.v_issue_16253 set t.a = 2 where t.a = test.v_issue_16253.a")
tk.MustQuery("select a from t").Check(testkit.Rows("2"))
_, err = tk.Exec("update t, test.v_issue_16253 set test.v_issue_16253.a = 2 where t.a = test.v_issue_16253.a")
c.Assert(err.Error(), Equals, "update view v_issue_16253 is not supported now.")
}

func (s *testSuite3) TestIssue16250(c *C) {
Expand Down
10 changes: 7 additions & 3 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2631,9 +2631,6 @@ func (b *PlanBuilder) buildUpdate(ctx context.Context, update *ast.UpdateStmt) (
if dbName == "" {
dbName = b.ctx.GetSessionVars().CurrentDB
}
if t.TableInfo.IsView() {
return nil, errors.Errorf("update view %s is not supported now.", t.Name.O)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, dbName, t.Name.L, "", nil)
}

Expand Down Expand Up @@ -2828,8 +2825,12 @@ func (b *PlanBuilder) buildUpdateLists(ctx context.Context, tableList []*ast.Tab
}

tblDbMap := make(map[string]string, len(tableList))
views := make(map[string]struct{}, len(tableList))
for _, tbl := range tableList {
tblDbMap[tbl.Name.L] = tbl.DBInfo.Name.L
if tbl.TableInfo.IsView() {
views[tbl.DBInfo.Name.L+"."+tbl.Name.L] = struct{}{}
}
}
for _, assign := range newList {
col := assign.Col
Expand All @@ -2839,6 +2840,9 @@ func (b *PlanBuilder) buildUpdateLists(ctx context.Context, tableList []*ast.Tab
if dbNameTmp, ok := tblDbMap[col.TblName.L]; ok {
dbName = dbNameTmp
}
if _, ok := views[dbName+"."+col.TblName.L]; ok {
return nil, nil, errors.Errorf("update view %s is not supported now.", col.TblName.O)
}
if dbName == "" {
dbName = b.ctx.GetSessionVars().CurrentDB
}
Expand Down

0 comments on commit db99fb1

Please sign in to comment.