Skip to content

Commit

Permalink
*: fix add record panic (pingcap#1482)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala authored Jul 22, 2016
1 parent 1e6e1ca commit cbddbcd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
40 changes: 39 additions & 1 deletion ddl/column_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,50 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
c.Assert(errors.ErrorStack(checkErr), Equals, "")
testCheckJobDone(c, d, job, true)
s.testColumnDrop(c, ctx, d, publicTable)
s.testAddColumnNoDefault(c, ctx, d, tblInfo)
d.close()
}

func (s *testColumnChangeSuite) testAddColumnNoDefault(c *C, ctx context.Context, d *ddl, tblInfo *model.TableInfo) {
d.close()
tc := &testDDLCallback{}
// set up hook
prevState := model.StateNone
var checkErr error
var writeOnlyTable, publicTable table.Table
tc.onJobUpdated = func(job *model.Job) {
if job.SchemaState == prevState {
return
}
prevState = job.SchemaState
var err error
switch job.SchemaState {
case model.StateWriteOnly:
writeOnlyTable, err = getCurrentTable(d, s.dbInfo.ID, tblInfo.ID)
if err != nil {
checkErr = errors.Trace(err)
}
case model.StatePublic:
publicTable, err = getCurrentTable(d, s.dbInfo.ID, tblInfo.ID)
if err != nil {
checkErr = errors.Trace(err)
}
_, err = writeOnlyTable.AddRecord(ctx, types.MakeDatums(10, 10))
if err != nil {
checkErr = errors.Trace(err)
}
}
}
d.hook = tc
d.start()
job := testCreateColumn(c, ctx, d, s.dbInfo, tblInfo, "c3", &ast.ColumnPosition{Tp: ast.ColumnPositionNone}, nil)
c.Assert(errors.ErrorStack(checkErr), Equals, "")
testCheckJobDone(c, d, job, true)
}

func (s *testColumnChangeSuite) testColumnDrop(c *C, ctx context.Context, d *ddl, tbl table.Table) {
d.close()
dropCol := tbl.Cols()[0]
dropCol := tbl.Cols()[2]
tc := &testDDLCallback{}
// set up hook
prevState := model.StateNone
Expand Down
8 changes: 4 additions & 4 deletions table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,6 @@ func (t *Table) AddRecord(ctx context.Context, r []types.Datum) (recordID int64,
if col.IsPKHandleColumn(t.meta) {
continue
}
if col.DefaultValue == nil && r[col.Offset].IsNull() {
// Save storage space by not storing null value.
continue
}
var value types.Datum
if col.State == model.StateWriteOnly || col.State == model.StateWriteReorganization {
// if col is in write only or write reorganization state, we must add it with its default value.
Expand All @@ -342,6 +338,10 @@ func (t *Table) AddRecord(ctx context.Context, r []types.Datum) (recordID int64,
}
} else {
value = r[col.Offset]
if col.DefaultValue == nil && r[col.Offset].IsNull() {
// Save storage space by not storing null value.
continue
}
}
colIDs = append(colIDs, col.ID)
row = append(row, value)
Expand Down

0 comments on commit cbddbcd

Please sign in to comment.