Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: Support some definitions and fix a bug to modify|change column #2629

Merged
merged 3 commits into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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'")
errInvalidDefault = terror.ClassDDL.New(codeInvalidDefault, "Invalid default value for '%s'")
errInvalidUseOfNull = terror.ClassDDL.New(codeInvalidUseOfNull, "Invalid use of NULL value")

// ErrInvalidDBState returns for invalid database state.
ErrInvalidDBState = terror.ClassDDL.New(codeInvalidDBState, "invalid database state")
Expand Down Expand Up @@ -422,6 +423,7 @@ const (
codeCantDropFieldOrKey = 1091
codeWrongDBName = 1102
codeWrongTableName = 1103
codeInvalidUseOfNull = 1138
codeBlobKeyWithoutLength = 1170
codeInvalidOnUpdate = 1294
)
Expand All @@ -444,6 +446,7 @@ func init() {
codeErrorOnRename: mysql.ErrErrorOnRename,
codeBadField: mysql.ErrBadField,
codeInvalidDefault: mysql.ErrInvalidDefault,
codeInvalidUseOfNull: mysql.ErrInvalidUseOfNull,
}
terror.ErrClassToMySQLCodes[terror.ClassDDL] = ddlMySQLErrCodes
}
47 changes: 41 additions & 6 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func columnDefToCol(ctx context.Context, offset int, colDef *ast.ColumnDef) (*ta
return nil, nil, errors.Trace(err)
}
case ast.ColumnOptionFulltext:
// Do nothing.
// TODO: Support this type.
}
}
}
Expand Down Expand Up @@ -846,6 +846,10 @@ func modifiable(origin *types.FieldType, to *types.FieldType) bool {
return false
}
default:
if origin.Tp == to.Tp {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to check Flen, Decimal and Flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are checked in line819-830.

return true
}

return false
}
}
Expand Down Expand Up @@ -873,24 +877,52 @@ func setDefaultAndComment(ctx context.Context, col *table.Column, options []*ast
return nil
}

for _, v := range options {
switch v.Tp {
var (
hasDefaultValue bool
hasNotNull bool
setOnUpdateNow bool
)
for _, opt := range options {
switch opt.Tp {
case ast.ColumnOptionDefaultValue:
err := setDefaultValue(ctx, col, v)
value, err := getDefaultValue(ctx, opt, col.Tp, col.Decimal)
if err != nil {
return errors.Trace(err)
return ErrColumnBadNull.Gen("invalid default value - %s", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test case for this case.

}
col.DefaultValue = value
hasDefaultValue = true
case ast.ColumnOptionComment:
err := setColumnComment(ctx, col, v)
err := setColumnComment(ctx, col, opt)
if err != nil {
return errors.Trace(err)
}
case ast.ColumnOptionNotNull:
col.Flag |= mysql.NotNullFlag
hasNotNull = true
case ast.ColumnOptionNull:
col.Flag &= ^uint(mysql.NotNullFlag)
case ast.ColumnOptionOnUpdate:
if !expression.IsCurrentTimeExpr(opt.Expr) {
return ErrInvalidOnUpdate.Gen("invalid ON UPDATE for - %s", col.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test case for this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is done at the parsing layer In MySQL. But we didn't do that. I will fix it in the next PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And now we only support the type of current_timestamp. I will add a TODO here.

}

col.Flag |= mysql.OnUpdateNowFlag
setOnUpdateNow = true
default:
// TODO: Support other types.
return errors.Trace(errUnsupportedModifyColumn)
}
}

setTimestampDefaultValue(col, hasDefaultValue, setOnUpdateNow)
if mysql.HasTimestampFlag(col.Flag) && hasNotNull {
return errors.Trace(errInvalidUseOfNull)
}

if hasDefaultValue {
return errors.Trace(checkDefaultValue(ctx, col, true))
}

return nil
}

Expand All @@ -915,6 +947,7 @@ func (d *ddl) getModifiableColumnJob(ctx context.Context, ident ast.Ident, origi
// Make sure the column definition is simple field type.
return nil, errors.Trace(errUnsupportedModifyColumn)
}

if err := setDefaultAndComment(ctx, col, spec.NewColumn.Options); err != nil {
return nil, errors.Trace(err)
}
Expand All @@ -925,6 +958,8 @@ func (d *ddl) getModifiableColumnJob(ctx context.Context, ident ast.Ident, origi

newCol := *col
newCol.FieldType = *spec.NewColumn.Tp
// TODO: Remove this line after merged PR 2627.
newCol.Flag = col.Flag
newCol.Name = spec.NewColumn.Name.Name
job := &model.Job{
SchemaID: schema.ID,
Expand Down
27 changes: 24 additions & 3 deletions ddl/ddl_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,21 +733,42 @@ func (s *testDBSuite) TestChangeColumn(c *C) {
s.tk.MustQuery("select a from t3").Check(testkit.Rows("1", "2"))
s.mustExec(c, "alter table t3 change a aa bigint")
s.tk.MustQuery("select aa from t3").Check(testkit.Rows("1", "2"))
s.mustExec(c, "alter table t3 modify b varchar(20) default 'c' comment 'my comment'")
// for the following definitions: 'not null', 'null', 'default value' and 'comment'
s.mustExec(c, "alter table t3 change b b varchar(20) not null default 'c' comment 'my comment'")
ctx := s.tk.Se.(context.Context)
is := sessionctx.GetDomain(ctx).InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test_db"), model.NewCIStr("t3"))
c.Assert(err, IsNil)
tblInfo := tbl.Meta()
c.Assert(tblInfo.Columns[1].Comment, Equals, "my comment")
colB := tblInfo.Columns[1]
c.Assert(colB.Comment, Equals, "my comment")
hasNotNull := tmysql.HasNotNullFlag(colB.Flag)
c.Assert(hasNotNull, IsTrue)
s.mustExec(c, "insert into t3 set aa = 3")
rowStr := fmt.Sprintf("%v", []byte("a"))
rowStr1 := fmt.Sprintf("%v", []byte("b"))
rowStr2 := fmt.Sprintf("%v", []byte("c"))
s.tk.MustQuery("select b from t3").Check(testkit.Rows(rowStr, rowStr1, rowStr2))
// for timestamp
s.mustExec(c, "alter table t3 add column c timestamp not null")
s.mustExec(c, "alter table t3 change c c timestamp default '2017-02-11' comment 'col c comment' on update current_timestamp")
is = sessionctx.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test_db"), model.NewCIStr("t3"))
c.Assert(err, IsNil)
tblInfo = tbl.Meta()
colC := tblInfo.Columns[2]
c.Assert(colC.Comment, Equals, "col c comment")
hasNotNull = tmysql.HasNotNullFlag(colC.Flag)
c.Assert(hasNotNull, IsTrue)

// for failing tests
sql := "alter table t3 change a testx.t3.aa bigint"
sql := "alter table t3 change c c timestamp not null"
s.testErrorCode(c, sql, tmysql.ErrInvalidUseOfNull)
sql = "alter table t3 change c c timestamp not null null"
s.testErrorCode(c, sql, tmysql.ErrInvalidUseOfNull)
sql = "alter table t3 change aa aa int default ''"
s.testErrorCode(c, sql, tmysql.ErrInvalidDefault)
sql = "alter table t3 change a testx.t3.aa bigint"
s.testErrorCode(c, sql, tmysql.ErrWrongDBName)
sql = "alter table t3 change t.a aa bigint"
s.testErrorCode(c, sql, tmysql.ErrWrongTableName)
Expand Down