Skip to content

Commit

Permalink
ddl: fix incorrect origin default bit value in ColumnInfo (#12168) (#…
Browse files Browse the repository at this point in the history
…12489)

* ddl: fix incorrect origin default bit value in ColumnInfo

* add query result check
  • Loading branch information
sre-bot authored and ngaut committed Oct 10, 2019
1 parent efc7e50 commit 0aa2fd4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,14 @@ func (s *testIntegrationSuite6) TestBitDefaultValue(c *C) {
tk.MustExec("insert into t_bit set c2=1;")
tk.MustQuery("select bin(c1),c2 from t_bit").Check(testkit.Rows("11111010 1"))
tk.MustExec("drop table t_bit")

tk.MustExec("create table t_bit (a int)")
tk.MustExec("insert into t_bit value (1)")
tk.MustExec("alter table t_bit add column c bit(16) null default b'1100110111001'")
tk.MustQuery("select c from t_bit").Check(testkit.Rows("\x19\xb9"))
tk.MustExec("update t_bit set c = b'11100000000111'")
tk.MustQuery("select c from t_bit").Check(testkit.Rows("\x38\x07"))

tk.MustExec(`create table testalltypes1 (
field_1 bit default 1,
field_2 tinyint null default null
Expand Down
6 changes: 6 additions & 0 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ func CheckNotNull(cols []*Column, row []types.Datum) error {

// GetColOriginDefaultValue gets default value of the column from original default value.
func GetColOriginDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo) (types.Datum, error) {
// If the column type is BIT, both `OriginDefaultValue` and `DefaultValue` of ColumnInfo are corrupted, because
// after JSON marshaling and unmarshaling against the field with type `interface{}`, the content with actual type `[]byte` is changed.
// We need `DefaultValueBit` to restore OriginDefaultValue before reading it.
if col.Tp == mysql.TypeBit && col.DefaultValueBit != nil && col.OriginDefaultValue != nil {
col.OriginDefaultValue = col.DefaultValueBit
}
return getColDefaultValue(ctx, col, col.OriginDefaultValue)
}

Expand Down

0 comments on commit 0aa2fd4

Please sign in to comment.