From 7bd64e0f2797b05da91f41045c5e30a5f154f01b Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Sun, 29 Sep 2019 19:15:30 +0800 Subject: [PATCH 1/2] planner: update's select should not change the output columns (#12476) --- go.mod | 2 -- planner/core/logical_plan_builder.go | 11 ++++++++++- planner/core/logical_plan_test.go | 4 ++++ tools/check/go.mod | 2 ++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 8dc89708410de..a9ecde49c5771 100644 --- a/go.mod +++ b/go.mod @@ -77,5 +77,3 @@ require ( sourcegraph.com/sourcegraph/appdash v0.0.0-20180531100431-4c381bd170b4 sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67 ) - -go 1.13 diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 5ff63363a8e68..26ffc3dcb260c 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -2584,12 +2584,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 sel.Where != nil { - p, err = b.buildSelection(ctx, p, sel.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) + proj.SetSchema(expression.NewSchema(make([]*expression.Column, oldSchemaLen)...)) + copy(proj.schema.Columns, p.Schema().Columns[:oldSchemaLen]) + proj.SetChildren(p) + p = proj + } if sel.OrderBy != nil { p, err = b.buildSort(ctx, p, sel.OrderBy.Items, nil, nil) if err != nil { diff --git a/planner/core/logical_plan_test.go b/planner/core/logical_plan_test.go index 7044937619e67..442be0987c4ff 100644 --- a/planner/core/logical_plan_test.go +++ b/planner/core/logical_plan_test.go @@ -940,6 +940,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))}(test.t.a,test.t.b)->IndexReader(Index(t.c_d_e)[[42,42]])}(test.t.b,test.t.a)->Sel([or(6_aux_0, 10_aux_0)])->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() diff --git a/tools/check/go.mod b/tools/check/go.mod index ca5d580f6d6a4..6dfc12cecadbd 100644 --- a/tools/check/go.mod +++ b/tools/check/go.mod @@ -20,3 +20,5 @@ require ( gopkg.in/yaml.v2 v2.2.2 // indirect honnef.co/go/tools v0.0.0-20180920025451-e3ad64cb4ed3 ) + +go 1.13 From 625cf38b1af7e866a8715953f4da21a22d5f4bbf Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Sun, 29 Sep 2019 20:06:46 +0800 Subject: [PATCH 2/2] fix test --- planner/core/logical_plan_test.go | 2 +- tools/check/go.mod | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/planner/core/logical_plan_test.go b/planner/core/logical_plan_test.go index 442be0987c4ff..eb086694f3e0e 100644 --- a/planner/core/logical_plan_test.go +++ b/planner/core/logical_plan_test.go @@ -942,7 +942,7 @@ func (s *testPlanSuite) TestPlanBuilder(c *C) { }, { 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", + plan: "LeftHashJoin{TableReader(Table(t))->IndexReader(Index(t.c_d_e)[[NULL,+inf]])->StreamAgg}(test.t.b,test.t.c)->Projection->Update", }, } diff --git a/tools/check/go.mod b/tools/check/go.mod index 6dfc12cecadbd..ca5d580f6d6a4 100644 --- a/tools/check/go.mod +++ b/tools/check/go.mod @@ -20,5 +20,3 @@ require ( gopkg.in/yaml.v2 v2.2.2 // indirect honnef.co/go/tools v0.0.0-20180920025451-e3ad64cb4ed3 ) - -go 1.13