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

planner: fix wrongly check for update statement #24614

Merged
merged 5 commits into from
May 14, 2021
Merged
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
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"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this test?

Copy link
Contributor Author

@xiongjiwei xiongjiwei May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test here is not suitable, when building planner, we will Preprocess the AST to build some extra info such as model.TableInfo in TableName. This test BuildLogicalPlan directly, so that model.TableInfo in TableName is missing.

{"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