Skip to content

Commit

Permalink
ddl: fix check column field length when add column (#9143)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored and winkyao committed Feb 27, 2019
1 parent 9da1749 commit 511a80b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
3 changes: 2 additions & 1 deletion ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2189,7 +2189,8 @@ func (s *testDBSuite) TestCheckTooBigFieldLength(c *C) {
s.tk.MustExec("create table tr_03 (id int, name varchar(65534), purchased date ) default charset=latin1;")

s.tk.MustExec("drop table if exists tr_04;")
s.tk.MustExec("create table tr_04 (a varchar(20000)) default charset utf8;")
s.tk.MustExec("create table tr_04 (a varchar(20000) ) default charset utf8;")
assertErrorCode(c, s.tk, "alter table tr_04 add column b varchar(20000) charset utf8mb4;", tmysql.ErrTooBigFieldlength)
assertErrorCode(c, s.tk, "alter table tr_04 convert to character set utf8mb4;", tmysql.ErrTooBigFieldlength)
assertErrorCode(c, s.tk, "create table tr (id int, name varchar(30000), purchased date ) default charset=utf8 collate=utf8_bin;", tmysql.ErrTooBigFieldlength)
assertErrorCode(c, s.tk, "create table tr (id int, name varchar(20000) charset utf8mb4, purchased date ) default charset=utf8 collate=utf8;", tmysql.ErrTooBigFieldlength)
Expand Down
18 changes: 4 additions & 14 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ func columnDefToCol(ctx sessionctx.Context, offset int, colDef *ast.ColumnDef, o
if err != nil {
return nil, nil, errors.Trace(err)
}
err = checkColumnFieldLength(col)
if err != nil {
return nil, nil, errors.Trace(err)
}
return col, constraints, nil
}

Expand Down Expand Up @@ -725,16 +729,6 @@ func checkColumnsAttributes(colDefs []*ast.ColumnDef) error {
return nil
}

// checkColumnsFieldLength check the maximum length limit for different character set varchar type columns.
func checkColumnsFieldLength(cols []*table.Column) error {
for _, col := range cols {
if err := checkColumnFieldLength(col); err != nil {
return errors.Trace(err)
}
}
return nil
}

func checkColumnFieldLength(col *table.Column) error {
if col.Tp == mysql.TypeVarchar {
if err := IsTooBigFieldLength(col.Flen, col.Name.O, col.Charset); err != nil {
Expand Down Expand Up @@ -1036,10 +1030,6 @@ func buildTableInfoWithCheck(ctx sessionctx.Context, d *ddl, s *ast.CreateTableS
return nil, errors.Trace(err)
}

if err = checkColumnsFieldLength(cols); err != nil {
return nil, errors.Trace(err)
}

err = checkConstraintNames(newConstraints)
if err != nil {
return nil, errors.Trace(err)
Expand Down

0 comments on commit 511a80b

Please sign in to comment.