Skip to content

Commit

Permalink
planner: update's select should not change the output columns (#12476)
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros authored and sre-bot committed Sep 29, 2019
1 parent 41ac571 commit 6169553
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2743,12 +2743,21 @@ func (b *PlanBuilder) buildUpdate(ctx context.Context, update *ast.UpdateStmt) (
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, dbName, t.Name.L, "", nil)
}

oldSchemaLen := p.Schema().Len()
if update.Where != nil {
p, err = b.buildSelection(ctx, p, update.Where, nil)
if err != nil {
return nil, err
}
}
// TODO: expression rewriter should not change the output columns. We should cut the columns here.
if p.Schema().Len() != oldSchemaLen {
proj := LogicalProjection{Exprs: expression.Column2Exprs(p.Schema().Columns[:oldSchemaLen])}.Init(b.ctx, b.getSelectOffset())
proj.SetSchema(expression.NewSchema(make([]*expression.Column, oldSchemaLen)...))
copy(proj.schema.Columns, p.Schema().Columns[:oldSchemaLen])
proj.SetChildren(p)
p = proj
}
if update.Order != nil {
p, err = b.buildSort(ctx, p, update.Order.Items, nil, nil)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,10 @@ func (s *testPlanSuite) TestPlanBuilder(c *C) {
// binlog columns, because the schema and data are not consistent.
plan: "LeftHashJoin{LeftHashJoin{TableReader(Table(t))->IndexLookUp(Index(t.c_d_e)[[666,666]], Table(t))}(Column#1,Column#14)->IndexReader(Index(t.c_d_e)[[42,42]])}(Column#2,Column#27)->Sel([or(Column#26, Column#40)])->Projection->Delete",
},
{
sql: "update t set a = 2 where b in (select c from t)",
plan: "LeftHashJoin{TableReader(Table(t))->IndexReader(Index(t.c_d_e)[[NULL,+inf]])->HashAgg}(Column#2,Column#25)->Projection->Update",
},
}

ctx := context.Background()
Expand Down

0 comments on commit 6169553

Please sign in to comment.