Skip to content

Commit

Permalink
ddl: fix the modify column operation on the bit column (#12008) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored Apr 13, 2020
1 parent 3541c0f commit dc54c5d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ func columnDefToCol(ctx sessionctx.Context, offset int, colDef *ast.ColumnDef, o
col.Flag &= ^mysql.BinaryFlag
col.Flag |= mysql.ZerofillFlag
}

// If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column.
// See https://dev.mysql.com/doc/refman/5.7/en/numeric-type-overview.html for more details.
// But some types like bit and year, won't show its unsigned flag in `show create table`.
Expand Down Expand Up @@ -1985,9 +1986,6 @@ func setColumnComment(ctx sessionctx.Context, col *table.Column, option *ast.Col

// setDefaultAndComment is only used in getModifiableColumnJob.
func setDefaultAndComment(ctx sessionctx.Context, col *table.Column, options []*ast.ColumnOption) error {
if len(options) == 0 {
return nil
}
var hasDefaultValue, setOnUpdateNow bool
var err error
for _, opt := range options {
Expand Down Expand Up @@ -2047,6 +2045,10 @@ func setDefaultAndComment(ctx sessionctx.Context, col *table.Column, options []*
// it is `not null` and not an `AUTO_INCREMENT` field or `TIMESTAMP` field.
setNoDefaultValueFlag(col, hasDefaultValue)

if col.Tp == mysql.TypeBit {
col.Flag |= mysql.UnsignedFlag
}

if hasDefaultValue {
return errors.Trace(checkDefaultValue(ctx, col, true))
}
Expand Down
5 changes: 3 additions & 2 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (s *testSuite6) TestAlterTableModifyColumn(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists mc")
tk.MustExec("create table mc(c1 int, c2 varchar(10))")
tk.MustExec("create table mc(c1 int, c2 varchar(10), c3 bit)")
_, err := tk.Exec("alter table mc modify column c1 short")
c.Assert(err, NotNil)
tk.MustExec("alter table mc modify column c1 bigint")
Expand All @@ -266,9 +266,10 @@ func (s *testSuite6) TestAlterTableModifyColumn(c *C) {
tk.MustExec("alter table mc modify column c2 varchar(11)")
tk.MustExec("alter table mc modify column c2 text(13)")
tk.MustExec("alter table mc modify column c2 text")
tk.MustExec("alter table mc modify column c3 bit")
result := tk.MustQuery("show create table mc")
createSQL := result.Rows()[0][1]
expected := "CREATE TABLE `mc` (\n `c1` bigint(20) DEFAULT NULL,\n `c2` text DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
expected := "CREATE TABLE `mc` (\n `c1` bigint(20) DEFAULT NULL,\n `c2` text DEFAULT NULL,\n `c3` bit(1) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
c.Assert(createSQL, Equals, expected)

// test multiple collate modification in column.
Expand Down

0 comments on commit dc54c5d

Please sign in to comment.