Skip to content

Commit

Permalink
ddl: Add a limit for columns in a table (pingcap#4464)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala authored and coocood committed Sep 7, 2017
1 parent f380a74 commit 20a928e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ const (
ddlPrompt = "ddl"
)

var (
// TableColumnCountLimit is limit of the number of columns in a table.
// It's exported for testing.
TableColumnCountLimit = 512
)

var (
// errWorkerClosed means we have already closed the DDL worker.
errInvalidWorker = terror.ClassDDL.New(codeInvalidWorker, "invalid worker")
Expand Down Expand Up @@ -81,6 +87,7 @@ var (
errErrorOnRename = terror.ClassDDL.New(codeErrorOnRename, "Error on rename of './%s/%s' to './%s/%s'")
errBadField = terror.ClassDDL.New(codeBadField, "Unknown column '%s' in '%s'")
errInvalidUseOfNull = terror.ClassDDL.New(codeInvalidUseOfNull, "Invalid use of NULL value")
errTooManyFields = terror.ClassDDL.New(codeTooManyFields, "Too many columns")

// errWrongKeyColumn is for table column cannot be indexed.
errWrongKeyColumn = terror.ClassDDL.New(codeWrongKeyColumn, mysql.MySQLErrName[mysql.ErrWrongKeyColumn])
Expand Down Expand Up @@ -541,6 +548,7 @@ const (
codeBlobCantHaveDefault = 1101
codeWrongDBName = 1102
codeWrongTableName = 1103
codeTooManyFields = 1117
codeInvalidUseOfNull = 1138
codeWrongColumnName = 1166
codeWrongKeyColumn = 1167
Expand Down Expand Up @@ -579,6 +587,7 @@ func init() {
codeWrongColumnName: mysql.ErrWrongColumnName,
codeWrongKeyColumn: mysql.ErrWrongKeyColumn,
codeWrongNameForIndex: mysql.ErrWrongNameForIndex,
codeTooManyFields: mysql.ErrTooManyFields,
}
terror.ErrClassToMySQLCodes[terror.ClassDDL] = ddlMySQLErrCodes
}
10 changes: 10 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,13 @@ func checkTooLongColumn(colDefs []*ast.ColumnDef) error {
return nil
}

func checkTooManyColumns(colDefs []*ast.ColumnDef) error {
if len(colDefs) > TableColumnCountLimit {
return errTooManyFields
}
return nil
}

func checkDuplicateConstraint(namesMap map[string]bool, name string, foreign bool) error {
if name == "" {
return nil
Expand Down Expand Up @@ -708,6 +715,9 @@ func (d *ddl) CreateTable(ctx context.Context, ident ast.Ident, colDefs []*ast.C
if err = checkTooLongColumn(colDefs); err != nil {
return errors.Trace(err)
}
if err = checkTooManyColumns(colDefs); err != nil {
return errors.Trace(err)
}

cols, newConstraints, err := buildColumnsAndConstraints(ctx, colDefs, constraints)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion ddl/ddl_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1094,8 +1094,13 @@ func (s *testDBSuite) TestCreateTableTooLarge(c *C) {
}
}
sql += ");"
s.testErrorCode(c, sql, tmysql.ErrTooManyFields)

originLimit := ddl.TableColumnCountLimit
ddl.TableColumnCountLimit = cnt * 4
_, err := s.tk.Exec(sql)
c.Assert(kv.ErrEntryTooLarge.Equal(err), IsTrue, Commentf("sql:%v", sql))
c.Assert(kv.ErrEntryTooLarge.Equal(err), IsTrue, Commentf("err:%v", err))
ddl.TableColumnCountLimit = originLimit
}

func (s *testDBSuite) TestCreateTableWithLike(c *C) {
Expand Down

0 comments on commit 20a928e

Please sign in to comment.