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: fix incorrect origin default bit value in ColumnInfo (#12168) #12489

Merged
merged 7 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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