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

*: Fix add index after add column with default value #3510

Merged
merged 5 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions ddl/ddl_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ func (s *testDBSuite) TestMySQLErrorCode(c *C) {
s.testErrorCode(c, sql, tmysql.ErrWrongTableName)
}

func (s *testDBSuite) TestAddIndexAfterAddColumn(c *C) {
defer testleak.AfterTest(c)()
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)

s.tk.MustExec("create table test_add_index_after_add_col(a int, b int not null default '0')")
s.tk.MustExec("insert into test_add_index_after_add_col values(1, 2),(2,2)")
s.tk.MustExec("alter table test_add_index_after_add_col add column c int not null default '0'")
sql := "alter table test_add_index_after_add_col add unique index cc(c) "
s.testErrorCode(c, sql, tmysql.ErrDupEntry)
}

func (s *testDBSuite) TestIndex(c *C) {
defer testleak.AfterTest(c)()
s.tk = testkit.NewTestKit(c, s.store)
Expand Down
13 changes: 12 additions & 1 deletion ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,9 @@ func (d *ddl) fetchRowColVals(txn kv.Transaction, t table.Table, taskOpInfo *ind
}

cols := t.Cols()
ctx := d.newContext()
idxInfo := taskOpInfo.tblIndex.Meta()
defaultVals := make([]types.Datum, len(cols))
for i, idxRecord := range idxRecords {
rowMap, err := tablecodec.DecodeRow(rawRecords[i], taskOpInfo.colMap, time.UTC)
if err != nil {
Expand All @@ -431,7 +433,16 @@ func (d *ddl) fetchRowColVals(txn kv.Transaction, t table.Table, taskOpInfo *ind
idxVal := make([]types.Datum, 0, len(idxInfo.Columns))
for _, v := range idxInfo.Columns {
col := cols[v.Offset]
idxVal = append(idxVal, rowMap[col.ID])
idxColumnVal := rowMap[col.ID]
if _, ok := rowMap[col.ID]; ok {
idxVal = append(idxVal, idxColumnVal)
continue
}
idxColumnVal, ret.err = tables.GetColDefaultValue(ctx, col, defaultVals)
if ret.err != nil {
ret.err = errors.Trace(err)
Copy link
Member

Choose a reason for hiding this comment

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

return here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}
idxVal = append(idxVal, idxColumnVal)
}
idxRecord.vals = idxVal
}
Expand Down
65 changes: 37 additions & 28 deletions table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,34 +459,30 @@ func (t *Table) RowWithCols(ctx context.Context, h int64, cols []*table.Column)
}
colTps[col.ID] = &col.FieldType
}
row, err := tablecodec.DecodeRow(value, colTps, ctx.GetSessionVars().GetTimeZone())
rowMap, err := tablecodec.DecodeRow(value, colTps, ctx.GetSessionVars().GetTimeZone())
if err != nil {
return nil, errors.Trace(err)
}
defaultVals := make([]types.Datum, len(cols))
for i, col := range cols {
if col == nil {
continue
}
if col.IsPKHandleColumn(t.meta) {
continue
}
ri, ok := row[col.ID]
ri, ok := rowMap[col.ID]
if ok {
v[i] = ri
continue
}

if col.OriginDefaultValue != nil && col.State == model.StatePublic {
ri, err = table.GetColOriginDefaultValue(ctx, col.ToInfo())
if err != nil {
return nil, errors.Trace(err)
}
v[i] = ri
continue
}
if mysql.HasNotNullFlag(col.Flag) {
return nil, errors.New("Miss column")
}
v[i], err = GetColDefaultValue(ctx, col, defaultVals)
Copy link
Member

Choose a reason for hiding this comment

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

Should we move this before line 479?

if err != nil {
return nil, errors.Trace(err)
}
}
return v, nil
}
Expand Down Expand Up @@ -633,29 +629,21 @@ func (t *Table) IterRecords(ctx context.Context, startKey kv.Key, cols []*table.
}
data := make([]types.Datum, len(cols))
for _, col := range cols {
if col.IsPKHandleColumn(t.Meta()) {
data[col.Offset] = types.NewIntDatum(handle)
if col.IsPKHandleColumn(t.meta) {
if mysql.HasUnsignedFlag(col.Flag) {
data[col.Offset].SetUint64(uint64(handle))
} else {
data[col.Offset].SetInt64(handle)
}
continue
}
if _, ok := rowMap[col.ID]; ok {
data[col.Offset] = rowMap[col.ID]
continue
}
if col.OriginDefaultValue == nil && mysql.HasNotNullFlag(col.Flag) {
return errors.New("Miss column")
}
if col.State != model.StatePublic {
continue
}
if defaultVals[col.Offset].IsNull() {
d, err := table.GetColOriginDefaultValue(ctx, col.ToInfo())
if err != nil {
return errors.Trace(err)
}
data[col.Offset] = d
defaultVals[col.Offset] = d
} else {
data[col.Offset] = defaultVals[col.Offset]
data[col.Offset], err = GetColDefaultValue(ctx, col, defaultVals)
if err != nil {
return errors.Trace(err)
}
}
more, err := fn(handle, data, cols)
Expand All @@ -673,6 +661,27 @@ func (t *Table) IterRecords(ctx context.Context, startKey kv.Key, cols []*table.
return nil
}

func GetColDefaultValue(ctx context.Context, col *table.Column, defaultVals []types.Datum) (
Copy link
Member

Choose a reason for hiding this comment

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

Add comment.

Copy link
Member

@hanfei1991 hanfei1991 Jun 19, 2017

Choose a reason for hiding this comment

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

I think you can implement a GetAllColDefaultValues to return default values for every column at one time. Passing a slice every time is a little strange.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If implement a GetAllColDefaultValues, it will be an interface and its arguments are hard to understand. I tried and thought it was not very good. @hanfei1991

colVal types.Datum, err error) {
if col.OriginDefaultValue == nil && mysql.HasNotNullFlag(col.Flag) {
return colVal, errors.New("Miss column")
}
if col.State != model.StatePublic {
return colVal, nil
}
if defaultVals[col.Offset].IsNull() {
colVal, err = table.GetColOriginDefaultValue(ctx, col.ToInfo())
if err != nil {
return colVal, errors.Trace(err)
}
defaultVals[col.Offset] = colVal
} else {
colVal = defaultVals[col.Offset]
}

return colVal, nil
}

// AllocAutoID implements table.Table AllocAutoID interface.
func (t *Table) AllocAutoID() (int64, error) {
return t.alloc.Alloc(t.ID)
Expand Down