Skip to content
Permalink
Browse files

*: fix panic for create view using prepare (#10651)

  • Loading branch information...
lamxTyler committed Jun 4, 2019
1 parent 89bfed3 commit ce91d250087a8648768bd05436453143b301402f
Showing with 14 additions and 19 deletions.
  1. +4 −11 ddl/ddl_api.go
  2. +3 −0 executor/ddl_test.go
  3. +1 −1 go.mod
  4. +3 −2 go.sum
  5. +3 −5 planner/core/planbuilder.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,
@@ -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) {
2 go.mod
@@ -41,7 +41,7 @@ require (
github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e
github.com/pingcap/kvproto v0.0.0-20190528074401-b942b3f4108f
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596
github.com/pingcap/parser v0.0.0-20190529103304-95494e4022ce
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
5 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=
@@ -164,8 +165,8 @@ github.com/pingcap/kvproto v0.0.0-20190528074401-b942b3f4108f/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-20190529103304-95494e4022ce h1:qpX1uGhjKSWCNiY1vy0/stirh/ud1nF7Hi3dZzFSuio=
github.com/pingcap/parser v0.0.0-20190529103304-95494e4022ce/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=
@@ -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() {

0 comments on commit ce91d25

Please sign in to comment.
You can’t perform that action at this time.