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 corrupted default value for bit type column #18036

Merged
merged 31 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
507394e
ddl: fix incorrect default value for bit type column
tangenta Jun 15, 2020
2a37b5c
update parser dependency
tangenta Jun 18, 2020
d725eb6
Merge remote-tracking branch 'upstream/master' into fix-origin-defaul…
tangenta Jun 18, 2020
5378e2e
make fmt
tangenta Jun 24, 2020
7183fb7
Merge remote-tracking branch 'upstream/master' into fix-origin-defaul…
tangenta Jun 24, 2020
fd29100
go mod tidy
tangenta Jun 24, 2020
b366033
Merge branch 'master' into fix-origin-default-val-bit
ti-srebot Jun 24, 2020
4072b00
Merge branch 'master' into fix-origin-default-val-bit
ti-srebot Jun 24, 2020
0fea65b
Merge remote-tracking branch 'upstream/master' into fix-origin-defaul…
tangenta Jul 23, 2020
883b657
Merge branch 'master' into fix-origin-default-val-bit
tangenta Jul 28, 2020
d608777
Merge branch 'master' into fix-origin-default-val-bit
tangenta Jul 28, 2020
1d6430e
Merge branch 'master' into fix-origin-default-val-bit
tangenta Jul 28, 2020
998d93c
Merge branch 'master' into fix-origin-default-val-bit
tangenta Jul 29, 2020
d70abc9
Merge branch 'master' into fix-origin-default-val-bit
zz-jason Jul 29, 2020
fc601f3
Merge branch 'master' into fix-origin-default-val-bit
tangenta Jul 31, 2020
705359b
Merge branch 'master' into fix-origin-default-val-bit
tangenta Aug 3, 2020
ccf9122
Merge branch 'master' into fix-origin-default-val-bit
tangenta Aug 3, 2020
95f1223
Merge branch 'master' into fix-origin-default-val-bit
tangenta Aug 7, 2020
62450e2
Merge branch 'master' into fix-origin-default-val-bit
tangenta Aug 10, 2020
d573660
Merge branch 'master' into fix-origin-default-val-bit
wjhuang2016 Aug 25, 2020
810c55a
Merge branch 'master' into fix-origin-default-val-bit
tangenta Aug 26, 2020
b648435
Merge branch 'master' into fix-origin-default-val-bit
tangenta Aug 26, 2020
3088af6
update parser dependency
tangenta Aug 27, 2020
953d280
Merge remote-tracking branch 'upstream/master' into fix-origin-defaul…
tangenta Aug 27, 2020
caa92ae
Merge branch 'master' into fix-origin-default-val-bit
ti-srebot Aug 27, 2020
a48e717
Merge branch 'master' into fix-origin-default-val-bit
ti-srebot Aug 27, 2020
63a062a
Merge branch 'master' into fix-origin-default-val-bit
ti-srebot Sep 2, 2020
ca33907
Merge branch 'master' into fix-origin-default-val-bit
ti-srebot Sep 2, 2020
25eb563
Merge remote-tracking branch 'upstream/master' into fix-origin-defaul…
tangenta Sep 9, 2020
c4bde4c
Merge branch 'master' into fix-origin-default-val-bit
ti-srebot Sep 9, 2020
16e87f1
Merge branch 'master' into fix-origin-default-val-bit
zz-jason Oct 4, 2020
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
5 changes: 3 additions & 2 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,17 +516,18 @@ func checkDropColumnForStatePublic(tblInfo *model.TableInfo, colInfo *model.Colu
// When the dropping column has not-null flag and it hasn't the default value, we can backfill the column value like "add column".
// NOTE: If the state of StateWriteOnly can be rollbacked, we'd better reconsider the original default value.
// And we need consider the column without not-null flag.
if colInfo.OriginDefaultValue == nil && mysql.HasNotNullFlag(colInfo.Flag) {
if colInfo.GetOriginDefaultValue() == nil && mysql.HasNotNullFlag(colInfo.Flag) {
// If the column is timestamp default current_timestamp, and DDL owner is new version TiDB that set column.Version to 1,
// then old TiDB update record in the column write only stage will uses the wrong default value of the dropping column.
// Because new version of the column default value is UTC time, but old version TiDB will think the default value is the time in system timezone.
// But currently will be ok, because we can't cancel the drop column job when the job is running,
// so the column will be dropped succeed and client will never see the wrong default value of the dropped column.
// More info about this problem, see PR#9115.
colInfo.OriginDefaultValue, err = generateOriginDefaultValue(colInfo)
originDefVal, err := generateOriginDefaultValue(colInfo)
if err != nil {
return err
}
return colInfo.SetOriginDefaultValue(originDefVal)
}
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,18 @@ func (s *testIntegrationSuite5) TestBitDefaultValue(c *C) {
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("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 b bit(1) default b'0';")
tk.MustExec("alter table t_bit modify column b bit(1) default b'1';")
tk.MustQuery("select b from t_bit").Check(testkit.Rows("\x00"))
tk.MustExec("drop table t_bit")

tk.MustExec("create table t_bit (a bit);")
tk.MustExec("insert into t_bit values (null);")
tk.MustQuery("select count(*) from t_bit where a is null;").Check(testkit.Rows("1"))

tk.MustExec(`create table testalltypes1 (
field_1 bit default 1,
Expand Down
19 changes: 11 additions & 8 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2643,11 +2643,13 @@ func checkAndCreateNewColumn(ctx sessionctx.Context, ti ast.Ident, schema *model
return nil, errors.Trace(err)
}

col.OriginDefaultValue, err = generateOriginDefaultValue(col.ToInfo())
originDefVal, err := generateOriginDefaultValue(col.ToInfo())
if err != nil {
return nil, errors.Trace(err)
}
return col, nil

err = col.SetOriginDefaultValue(originDefVal)
return col, err
}

// AddColumn will add a new column to the table.
Expand Down Expand Up @@ -3563,12 +3565,13 @@ func (d *ddl) getModifiableColumnJob(ctx sessionctx.Context, ident ast.Ident, or
// a new version TiDB builds the DDL job that doesn't be set the column's offset and state,
// and the old version TiDB is the DDL owner, it doesn't get offset and state from the store. Then it will encounter errors.
// So here we set offset and state to support the rolling upgrade.
Offset: col.Offset,
State: col.State,
OriginDefaultValue: col.OriginDefaultValue,
FieldType: *specNewColumn.Tp,
Name: newColName,
Version: col.Version,
Offset: col.Offset,
State: col.State,
OriginDefaultValue: col.OriginDefaultValue,
OriginDefaultValueBit: col.OriginDefaultValueBit,
FieldType: *specNewColumn.Tp,
Name: newColName,
Version: col.Version,
})

var chs, coll string
Expand Down
2 changes: 1 addition & 1 deletion planner/core/plan_to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func SetPBColumnsDefaultValue(ctx sessionctx.Context, pbColumns []*tipb.ColumnIn
if c.IsGenerated() && !c.GeneratedStored {
pbColumns[i].DefaultVal = []byte{codec.NilFlag}
}
if c.OriginDefaultValue == nil {
if c.GetOriginDefaultValue() == nil {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion statistics/handle/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (h *Handle) insertColStats2KV(physicalID int64, colInfos []*model.ColumnInf
count := req.GetRow(0).GetInt64(0)
sqls := make([]string, 0, len(colInfos))
for _, colInfo := range colInfos {
value := types.NewDatum(colInfo.OriginDefaultValue)
value := types.NewDatum(colInfo.GetOriginDefaultValue())
value, err = value.ConvertTo(h.mu.ctx.GetSessionVars().StmtCtx, &colInfo.FieldType)
if err != nil {
return
Expand Down
8 changes: 1 addition & 7 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,7 @@ 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)
return getColDefaultValue(ctx, col, col.GetOriginDefaultValue())
}

// GetColDefaultValue gets default value of the column.
Expand Down
2 changes: 1 addition & 1 deletion table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ func tryDecodeColumnFromCommonHandle(col *table.Column, handle kv.Handle, pkIds
// The defaultVals is used to avoid calculating the default value multiple times.
func GetColDefaultValue(ctx sessionctx.Context, col *table.Column, defaultVals []types.Datum) (
colVal types.Datum, err error) {
if col.OriginDefaultValue == nil && mysql.HasNotNullFlag(col.Flag) {
if col.GetOriginDefaultValue() == nil && mysql.HasNotNullFlag(col.Flag) {
return colVal, errors.New("Miss column")
}
if col.State != model.StatePublic {
Expand Down
2 changes: 1 addition & 1 deletion util/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func CheckRecordAndIndex(sessCtx sessionctx.Context, txn kv.Transaction, t table
for i, val := range vals1 {
col := cols[i]
if val.IsNull() {
if mysql.HasNotNullFlag(col.Flag) && col.ToInfo().OriginDefaultValue == nil {
if mysql.HasNotNullFlag(col.Flag) && col.ToInfo().GetOriginDefaultValue() == nil {
return false, errors.Errorf("Column %v define as not null, but can't find the value where handle is %v", col.Name, h1)
}
// NULL value is regarded as its default value.
Expand Down