From 4ada7538cf28035949cb38ff03e7243f8bd617ab Mon Sep 17 00:00:00 2001 From: Haibin Xie Date: Wed, 5 Jun 2019 14:19:50 +0800 Subject: [PATCH] *: fix panic for create view using prepare (#10651) (#10713) --- ddl/ddl_api.go | 15 ++++----------- executor/ddl_test.go | 3 +++ go.mod | 2 +- go.sum | 5 +++-- planner/core/planbuilder.go | 8 +++----- 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index f94454c93d24b..2ba16ef6f3016 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1400,19 +1400,12 @@ func (d *ddl) CreateView(ctx sessionctx.Context, s *ast.CreateViewStmt) (err err func buildViewInfoWithTableColumns(ctx sessionctx.Context, s *ast.CreateViewStmt) (*model.ViewInfo, []*table.Column) { viewInfo := &model.ViewInfo{Definer: s.Definer, Algorithm: s.Algorithm, - Security: s.Security, SelectStmt: s.Select.Text(), CheckOption: s.CheckOption} - - var schemaCols = s.Select.(*ast.SelectStmt).Fields.Fields - viewInfo.Cols = make([]model.CIStr, len(schemaCols)) - for i, v := range schemaCols { - viewInfo.Cols[i] = v.AsName - } - - var tableColumns = make([]*table.Column, len(schemaCols)) + Security: s.Security, SelectStmt: s.Select.Text(), CheckOption: s.CheckOption, Cols: s.SchemaCols} + var tableColumns = make([]*table.Column, len(s.SchemaCols)) if s.Cols == nil { - for i, v := range schemaCols { + for i, v := range s.SchemaCols { tableColumns[i] = table.ToColumn(&model.ColumnInfo{ - Name: v.AsName, + Name: v, ID: int64(i), Offset: i, State: model.StatePublic, diff --git a/executor/ddl_test.go b/executor/ddl_test.go index bc43c6bd8dbf4..ebfc667bbae42 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -187,6 +187,9 @@ func (s *testSuite3) TestCreateView(c *C) { tk.MustExec("create table if not exists t1 (a int ,b int)") _, err = tk.Exec("create or replace view t1 as select * from t1") c.Assert(err.Error(), Equals, ddl.ErrWrongObject.GenWithStackByArgs("test", "t1", "VIEW").Error()) + // create view using prepare + tk.MustExec(`prepare stmt from "create view v10 (x) as select 1";`) + tk.MustExec("execute stmt") } func (s *testSuite3) TestCreateDropDatabase(c *C) { diff --git a/go.mod b/go.mod index 800e9f668101e..99313cb8135e8 100644 --- a/go.mod +++ b/go.mod @@ -40,7 +40,7 @@ require ( github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e github.com/pingcap/kvproto v0.0.0-20190517030054-ff2e03f6fdfe github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 - github.com/pingcap/parser v0.0.0-20190523113241-89ae120307cc + github.com/pingcap/parser v0.0.0-20190603120328-7cb252e677b5 github.com/pingcap/pd v0.0.0-20190424024702-bd1e2496a669 github.com/pingcap/tidb-tools v2.1.3-0.20190321065848-1e8b48f5c168+incompatible github.com/pingcap/tipb v0.0.0-20190428032612-535e1abaa330 diff --git a/go.sum b/go.sum index 1904215e74c3d..8d22cb13ace0d 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,7 @@ github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx2 github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20171208011716-f6d7a1f6fbf3/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd h1:qMd81Ts1T2OTKmB4acZcyKaMtRnY5Y44NuXGX2GFJ1w= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= @@ -162,8 +163,8 @@ github.com/pingcap/kvproto v0.0.0-20190517030054-ff2e03f6fdfe/go.mod h1:QMdbTAXC github.com/pingcap/log v0.0.0-20190214045112-b37da76f67a7/go.mod h1:xsfkWVaFVV5B8e1K9seWfyJWFrIhbtUTAD8NV1Pq3+w= github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 h1:t2OQTpPJnrPDGlvA+3FwJptMTt6MEPdzK1Wt99oaefQ= github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596/go.mod h1:WpHUKhNZ18v116SvGrmjkA9CBhYmuUTKL+p8JC9ANEw= -github.com/pingcap/parser v0.0.0-20190523113241-89ae120307cc h1:FLouvtxa2Xn82ggR43M0HQ/0aZV2rSZDn5qEmAl5HhI= -github.com/pingcap/parser v0.0.0-20190523113241-89ae120307cc/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= +github.com/pingcap/parser v0.0.0-20190603120328-7cb252e677b5 h1:Sk4qtg+9KIZAJVu/TtgL7r4JnNKQsYmvJmMOilA0RME= +github.com/pingcap/parser v0.0.0-20190603120328-7cb252e677b5/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= github.com/pingcap/pd v0.0.0-20190424024702-bd1e2496a669 h1:ZoKjndm/Ig7Ru/wojrQkc/YLUttUdQXoH77gtuWCvL4= github.com/pingcap/pd v0.0.0-20190424024702-bd1e2496a669/go.mod h1:MUCxRzOkYiWZtlyi4MhxjCIj9PgQQ/j+BLNGm7aUsnM= github.com/pingcap/tidb-tools v2.1.3-0.20190321065848-1e8b48f5c168+incompatible h1:MkWCxgZpJBgY2f4HtwWMMFzSBb3+JPzeJgF3VrXE/bU= diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index d0e8d36bdc6eb..a87abfb89642d 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -1750,12 +1750,10 @@ func (b *PlanBuilder) buildDDL(node ast.DDLNode) (Plan, error) { if v.Cols != nil && len(v.Cols) != schema.Len() { return nil, ddl.ErrViewWrongList } - // we use fieldList to store schema.Columns temporary - var fieldList = make([]*ast.SelectField, schema.Len()) + v.SchemaCols = make([]model.CIStr, schema.Len()) for i, col := range schema.Columns { - fieldList[i] = &ast.SelectField{AsName: col.ColName} + v.SchemaCols[i] = col.ColName } - v.Select.(*ast.SelectStmt).Fields.Fields = fieldList if _, ok := plan.(LogicalPlan); ok { if b.ctx.GetSessionVars().User != nil { authErr = ErrTableaccessDenied.GenWithStackByArgs("CREATE VIEW", b.ctx.GetSessionVars().User.Hostname, @@ -1764,7 +1762,7 @@ func (b *PlanBuilder) buildDDL(node ast.DDLNode) (Plan, error) { b.visitInfo = appendVisitInfo(b.visitInfo, mysql.CreateViewPriv, v.ViewName.Schema.L, v.ViewName.Name.L, "", authErr) } - if v.Definer.CurrentUser { + if v.Definer.CurrentUser && b.ctx.GetSessionVars().User != nil { v.Definer = b.ctx.GetSessionVars().User } if b.ctx.GetSessionVars().User != nil && v.Definer.String() != b.ctx.GetSessionVars().User.String() {