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: refactor the code of buildUpdate to solve some bugs (#13208) #13350

Merged
merged 2 commits into from
Nov 11, 2019
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
7 changes: 7 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,13 @@ func (s *testSuite) TestGeneratedColumnRead(c *C) {
result = tk.MustQuery(`SELECT * FROM test_gc_read ORDER BY a`)
result.Check(testkit.Rows(`10 <nil> <nil> <nil> <nil>`, `11 2 13 22 26`, `13 4 17 52 34`, `18 8 26 144 52`))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values(18)")
tk.MustExec("update test_gc_read set a = a+1 where a in (select a from t)")
result = tk.MustQuery("select * from test_gc_read order by a")
result.Check(testkit.Rows(`10 <nil> <nil> <nil> <nil>`, `11 2 13 22 26`, `13 4 17 52 34`, `19 8 27 152 54`))

// Test different types between generation expression and generated column.
tk.MustExec(`CREATE TABLE test_gc_read_cast(a VARCHAR(255), b VARCHAR(255), c INT AS (JSON_EXTRACT(a, b)), d INT AS (JSON_EXTRACT(a, b)) STORED)`)
tk.MustExec(`INSERT INTO test_gc_read_cast (a, b) VALUES ('{"a": "3"}', '$.a')`)
Expand Down
10 changes: 10 additions & 0 deletions executor/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,13 @@ func (s *testUpdateSuite) TestUpdateWithSubquery(c *C) {
tk.MustExec("update t1 set status = 'N' where status = 'F' and (id in (select id from t2 where field = 'MAIN') or id2 in (select id from t2 where field = 'main'))")
tk.MustQuery("select * from t1").Check(testkit.Rows("abc N abc"))
}

func (s *testUpdateSuite) TestUpdateMultiDatabaseTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop database if exists test2")
tk.MustExec("create database test2")
tk.MustExec("create table t(a int, b int generated always as (a+1) virtual)")
tk.MustExec("create table test2.t(a int, b int generated always as (a+1) virtual)")
tk.MustExec("update t, test2.t set test.t.a=1")
}
54 changes: 5 additions & 49 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2679,8 +2679,6 @@ func (b *PlanBuilder) buildUpdateLists(ctx context.Context, tableList []*ast.Tab
// If columns in set list contains generated columns, raise error.
// And, fill virtualAssignments here; that's for generated columns.
virtualAssignments := make([]*ast.Assignment, 0)
tableAsName := make(map[*model.TableInfo][]*model.CIStr)
extractTableAsNameForUpdate(p, tableAsName)

for _, tn := range tableList {
tableInfo := tn.TableInfo
Expand All @@ -2696,12 +2694,10 @@ func (b *PlanBuilder) buildUpdateLists(ctx context.Context, tableList []*ast.Tab
if _, ok := modifyColumns[columnFullName]; ok {
return nil, nil, ErrBadGeneratedColumn.GenWithStackByArgs(colInfo.Name.O, tableInfo.Name.O)
}
for _, asName := range tableAsName[tableInfo] {
virtualAssignments = append(virtualAssignments, &ast.Assignment{
Column: &ast.ColumnName{Table: *asName, Name: colInfo.Name},
Expr: tableVal.Cols()[i].GeneratedExpr,
})
}
virtualAssignments = append(virtualAssignments, &ast.Assignment{
Column: &ast.ColumnName{Schema: tn.Schema, Table: tn.Name, Name: colInfo.Name},
Expr: tableVal.Cols()[i].GeneratedExpr,
})
}
}

Expand Down Expand Up @@ -2760,47 +2756,6 @@ func (b *PlanBuilder) buildUpdateLists(ctx context.Context, tableList []*ast.Tab
return newList, p, nil
}

// extractTableAsNameForUpdate extracts tables' alias names for update.
func extractTableAsNameForUpdate(p LogicalPlan, asNames map[*model.TableInfo][]*model.CIStr) {
switch x := p.(type) {
case *DataSource:
alias := extractTableAlias(p)
if alias != nil {
if _, ok := asNames[x.tableInfo]; !ok {
asNames[x.tableInfo] = make([]*model.CIStr, 0, 1)
}
asNames[x.tableInfo] = append(asNames[x.tableInfo], alias)
}
case *LogicalProjection:
if !x.calculateGenCols {
return
}

ds, isDS := x.Children()[0].(*DataSource)
if !isDS {
// try to extract the DataSource below a LogicalUnionScan.
if us, isUS := x.Children()[0].(*LogicalUnionScan); isUS {
ds, isDS = us.Children()[0].(*DataSource)
}
}
if !isDS {
return
}

alias := extractTableAlias(x)
if alias != nil {
if _, ok := asNames[ds.tableInfo]; !ok {
asNames[ds.tableInfo] = make([]*model.CIStr, 0, 1)
}
asNames[ds.tableInfo] = append(asNames[ds.tableInfo], alias)
}
default:
for _, child := range p.Children() {
extractTableAsNameForUpdate(child, asNames)
}
}
}

func (b *PlanBuilder) buildDelete(ctx context.Context, delete *ast.DeleteStmt) (Plan, error) {
if b.pushTableHints(delete.TableHints) {
// table hints are only visible in the current DELETE statement.
Expand Down Expand Up @@ -3550,6 +3505,7 @@ func extractTableList(node ast.ResultSetNode, input []*ast.TableName, asName boo
if x.AsName.L != "" && asName {
newTableName := *s
newTableName.Name = x.AsName
newTableName.Schema = model.NewCIStr("")
input = append(input, &newTableName)
} else {
input = append(input, s)
Expand Down