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 subquery update with generated column (#23955) #24658

Merged
merged 3 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 5 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3919,6 +3919,11 @@ func (s *testSuite6) TestUpdateJoin(c *C) {
tk.MustExec("insert into t7 values (5, 1, 'a')")
tk.MustExec("update t6, t7 set t6.v = t7.v where t6.id = t7.id and t7.x = 5")
tk.MustQuery("select v from t6").Check(testkit.Rows("a"))

tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(id int primary key, v int, gv int GENERATED ALWAYS AS (v * 2) STORED)")
tk.MustExec("create table t2(id int, v int)")
tk.MustExec("update t1 tt1 inner join (select count(t1.id) a, t1.id from t1 left join t2 on t1.id = t2.id group by t1.id) x on tt1.id = x.id set tt1.v = tt1.v + x.a")
}

func (s *testSuite3) TestMaxOneRow(c *C) {
Expand Down
32 changes: 17 additions & 15 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3925,9 +3925,9 @@ func (b *PlanBuilder) buildUpdate(ctx context.Context, update *ast.UpdateStmt) (
}()

// update subquery table should be forbidden
var asNameList []string
asNameList = extractTableSourceAsNames(update.TableRefs.TableRefs, asNameList, true)
for _, asName := range asNameList {
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")
Expand Down Expand Up @@ -3999,7 +3999,7 @@ func (b *PlanBuilder) buildUpdate(ctx context.Context, update *ast.UpdateStmt) (

var updateTableList []*ast.TableName
updateTableList = extractTableList(update.TableRefs.TableRefs, updateTableList, true)
orderedList, np, allAssignmentsAreConstant, err := b.buildUpdateLists(ctx, updateTableList, update.List, p)
orderedList, np, allAssignmentsAreConstant, err := b.buildUpdateLists(ctx, updateTableList, update.List, p, notUpdatableTbl)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -4082,16 +4082,8 @@ func checkUpdateList(ctx sessionctx.Context, tblID2table map[int64]table.Table,
return nil
}

func (b *PlanBuilder) buildUpdateLists(
ctx context.Context,
tableList []*ast.TableName,
list []*ast.Assignment,
p LogicalPlan,
) (newList []*expression.Assignment,
po LogicalPlan,
allAssignmentsAreConstant bool,
e error,
) {
func (b *PlanBuilder) buildUpdateLists(ctx context.Context, tableList []*ast.TableName, list []*ast.Assignment, p LogicalPlan,
notUpdatableTbl []string) (newList []*expression.Assignment, po LogicalPlan, allAssignmentsAreConstant bool, e error) {
b.curClause = fieldList
// modifyColumns indicates which columns are in set list,
// and if it is set to `DEFAULT`
Expand Down Expand Up @@ -4127,8 +4119,18 @@ func (b *PlanBuilder) buildUpdateLists(
// If columns in set list contains generated columns, raise error.
// And, fill virtualAssignments here; that's for generated columns.
virtualAssignments := make([]*ast.Assignment, 0)

for _, tn := range tableList {
// Only generate virtual to updatable table, skip not updatable table(i.e. table in update's subQuery)
updatable := true
for _, nTbl := range notUpdatableTbl {
if tn.Name.L == nTbl {
updatable = false
break
}
}
if !updatable {
continue
}
tableInfo := tn.TableInfo
tableVal, found := b.is.TableByID(tableInfo.ID)
if !found {
Expand Down