Skip to content

Commit

Permalink
planner: fix wrongly check for update statement (#24614)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongjiwei authored May 14, 2021
1 parent 42b12f7 commit e92df20
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
5 changes: 2 additions & 3 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ func (s *testSuite8) TestUpdate(c *C) {
_, err = tk.Exec("UPDATE t SET c2=16777215 WHERE c1>= -8388608 AND c1 < -9 ORDER BY c1 LIMIT 2")
c.Assert(err, IsNil)

tk.MustExec("update (select * from t) t set c1 = 1111111")
tk.MustGetErrCode("update (select * from t) t set c1 = 1111111", mysql.ErrNonUpdatableTable)

// test update ignore for bad null error
tk.MustExec("drop table if exists t;")
Expand Down Expand Up @@ -1604,8 +1604,7 @@ func (s *testSuite8) TestUpdate(c *C) {
tk.MustExec("drop view v")

tk.MustExec("create sequence seq")
_, err = tk.Exec("update seq set minvalue=1")
c.Assert(err.Error(), Equals, "update sequence seq is not supported now.")
tk.MustGetErrCode("update seq set minvalue=1", mysql.ErrBadField)
tk.MustExec("drop sequence seq")

tk.MustExec("drop table if exists t1, t2")
Expand Down
14 changes: 14 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ func (s *testIntegrationSuite) TestIssue22298(c *C) {
tk.MustGetErrMsg(`select * from t where 0 and c = 10;`, "[planner:1054]Unknown column 'c' in 'where clause'")
}

func (s *testIntegrationSuite) TestIssue24571(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec(`create view v as select 1 as b;`)
tk.MustExec(`create table t (a int);`)
tk.MustExec(`update v, t set a=2;`)
tk.MustGetErrCode(`update v, t set b=2;`, mysql.ErrNonUpdatableTable)
tk.MustExec("create database db1")
tk.MustExec("use db1")
tk.MustExec("update test.t, (select 1 as a) as t set test.t.a=1;")
// bug in MySQL: ERROR 1288 (HY000): The target table t of the UPDATE is not updatable
tk.MustExec("update (select 1 as a) as t, test.t set test.t.a=1;")
}

func (s *testIntegrationSuite) TestIssue22828(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
39 changes: 21 additions & 18 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4239,17 +4239,6 @@ func (b *PlanBuilder) buildUpdate(ctx context.Context, update *ast.UpdateStmt) (
b.popTableHints()
}()

// update subquery table should be forbidden
var notUpdatableTbl []string
notUpdatableTbl = extractTableSourceAsNames(update.TableRefs.TableRefs, notUpdatableTbl, true)
for _, asName := range notUpdatableTbl {
for _, assign := range update.List {
if assign.Column.Table.L == asName {
return nil, ErrNonUpdatableTable.GenWithStackByArgs(asName, "UPDATE")
}
}
}

b.inUpdateStmt = true
b.isForUpdateRead = true

Expand All @@ -4265,12 +4254,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)
}
if t.TableInfo.IsSequence() {
return nil, errors.Errorf("update sequence %s is not supported now.", t.Name.O)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, dbName, t.Name.L, "", nil)
}

Expand Down Expand Up @@ -4314,6 +4297,10 @@ func (b *PlanBuilder) buildUpdate(ctx context.Context, update *ast.UpdateStmt) (
proj.SetChildren(p)
p = proj

// update subquery table should be forbidden
var notUpdatableTbl []string
notUpdatableTbl = extractTableSourceAsNames(update.TableRefs.TableRefs, notUpdatableTbl, true)

var updateTableList []*ast.TableName
updateTableList = extractTableList(update.TableRefs.TableRefs, updateTableList, true)
orderedList, np, allAssignmentsAreConstant, err := b.buildUpdateLists(ctx, updateTableList, update.List, p, notUpdatableTbl)
Expand Down Expand Up @@ -4417,6 +4404,21 @@ func (b *PlanBuilder) buildUpdateLists(ctx context.Context, tableList []*ast.Tab
columnsIdx[assign.Column] = idx
}
name := p.OutputNames()[idx]
for _, tl := range tableList {
if (tl.Schema.L == "" || tl.Schema.L == name.DBName.L) && (tl.Name.L == name.TblName.L) {
if tl.TableInfo.IsView() || tl.TableInfo.IsSequence() {
return nil, nil, false, ErrNonUpdatableTable.GenWithStackByArgs(name.TblName.O, "UPDATE")
}
// may be a subquery
if tl.Schema.L == "" {
for _, nTbl := range notUpdatableTbl {
if nTbl == name.TblName.L {
return nil, nil, false, ErrNonUpdatableTable.GenWithStackByArgs(name.TblName.O, "UPDATE")
}
}
}
}
}
columnFullName := fmt.Sprintf("%s.%s.%s", name.DBName.L, name.TblName.L, name.ColName.L)
// We save a flag for the column in map `modifyColumns`
// This flag indicated if assign keyword `DEFAULT` to the column
Expand All @@ -4439,9 +4441,10 @@ func (b *PlanBuilder) buildUpdateLists(ctx context.Context, tableList []*ast.Tab
break
}
}
if !updatable {
if !updatable || tn.TableInfo.IsView() || tn.TableInfo.IsSequence() {
continue
}

tableInfo := tn.TableInfo
tableVal, found := b.is.TableByID(tableInfo.ID)
if !found {
Expand Down
1 change: 0 additions & 1 deletion planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,6 @@ func (s *testPlanSuite) TestNameResolver(c *C) {
{"delete a from (select * from t ) as a, t", "[planner:1288]The target table a of the DELETE is not updatable"},
{"delete b from (select * from t ) as a, t", "[planner:1109]Unknown table 'b' in MULTI DELETE"},
{"select '' as fakeCol from t group by values(fakeCol)", "[planner:1054]Unknown column '' in 'VALUES() function'"},
{"update t, (select * from t) as b set b.a = t.a", "[planner:1288]The target table b of the UPDATE is not updatable"},
{"select row_number() over () from t group by 1", "[planner:1056]Can't group on 'row_number() over ()'"},
{"select row_number() over () as x from t group by 1", "[planner:1056]Can't group on 'x'"},
{"select sum(a) as x from t group by 1", "[planner:1056]Can't group on 'x'"},
Expand Down

0 comments on commit e92df20

Please sign in to comment.