Skip to content

Commit

Permalink
planner: refactor the code of buildUpdate to solve some bugs (pingcap…
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Nov 11, 2019
1 parent 845e25a commit 10576ae
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 49 deletions.
7 changes: 7 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,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>`, `11 2 13 22`, `13 4 17 52`, `18 8 26 144`))

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 @@ -227,3 +227,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 @@ -2323,8 +2323,6 @@ func (b *planBuilder) buildUpdateLists(tableList []*ast.TableName, list []*ast.A
// 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 @@ -2340,12 +2338,10 @@ func (b *planBuilder) buildUpdateLists(tableList []*ast.TableName, list []*ast.A
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 @@ -2395,47 +2391,6 @@ func (b *planBuilder) buildUpdateLists(tableList []*ast.TableName, list []*ast.A
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(delete *ast.DeleteStmt) (Plan, error) {
if b.pushTableHints(delete.TableHints) {
// table hints are only visible in the current DELETE statement.
Expand Down Expand Up @@ -2569,6 +2524,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

0 comments on commit 10576ae

Please sign in to comment.