Skip to content

Commit

Permalink
executor: fix update float panic (#8045)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored and zz-jason committed Oct 29, 2018
1 parent d07e5fb commit 736c313
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
28 changes: 24 additions & 4 deletions executor/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ func (e *UpdateExec) Next(ctx context.Context, chk *chunk.Chunk) error {

func (e *UpdateExec) fetchChunkRows(ctx context.Context) error {
fields := e.children[0].retTypes()
schema := e.children[0].Schema()
colsInfo := make([]*table.Column, len(fields))
for id, cols := range schema.TblID2Handle {
tbl := e.tblID2table[id]
for _, col := range cols {
offset := getTableOffset(schema, col)
for i, c := range tbl.WritableCols() {
colsInfo[offset+i] = c
}
}
}
globalRowIdx := 0
chk := e.children[0].newFirstChunk()
e.evalBuffer = chunk.MutRowFromTypes(fields)
Expand All @@ -156,7 +167,7 @@ func (e *UpdateExec) fetchChunkRows(ctx context.Context) error {
for rowIdx := 0; rowIdx < chk.NumRows(); rowIdx++ {
chunkRow := chk.GetRow(rowIdx)
datumRow := chunkRow.GetDatumRow(fields)
newRow, err1 := e.composeNewRow(globalRowIdx, datumRow)
newRow, err1 := e.composeNewRow(globalRowIdx, datumRow, colsInfo)
if err1 != nil {
return errors.Trace(err1)
}
Expand All @@ -181,7 +192,7 @@ func (e *UpdateExec) handleErr(colName model.CIStr, rowIdx int, err error) error
return errors.Trace(err)
}

func (e *UpdateExec) composeNewRow(rowIdx int, oldRow []types.Datum) ([]types.Datum, error) {
func (e *UpdateExec) composeNewRow(rowIdx int, oldRow []types.Datum, cols []*table.Column) ([]types.Datum, error) {
newRowData := types.CopyRow(oldRow)
e.evalBuffer.SetDatums(newRowData...)
for _, assign := range e.OrderedList {
Expand All @@ -190,10 +201,19 @@ func (e *UpdateExec) composeNewRow(rowIdx int, oldRow []types.Datum) ([]types.Da
continue
}
val, err := assign.Expr.Eval(e.evalBuffer.ToRow())
if err = e.handleErr(assign.Col.ColName, rowIdx, err); err != nil {
return nil, err
}

if err1 := e.handleErr(assign.Col.ColName, rowIdx, err); err1 != nil {
return nil, err1
// info of `_tidb_rowid` column is nil.
// No need to cast `_tidb_rowid` column value.
if cols[assign.Col.Index] != nil {
val, err = table.CastValue(e.ctx, val, cols[assign.Col.Index].ColumnInfo)
if err = e.handleErr(assign.Col.ColName, rowIdx, err); err != nil {
return nil, errors.Trace(err)
}
}

newRowData[assign.Col.Index] = *val.Copy()
e.evalBuffer.SetDatum(assign.Col.Index, val)
}
Expand Down
6 changes: 6 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,12 @@ func (s *testSuite) TestUpdate(c *C) {
_, err = tk.Exec("update t, (select * from t) as b set b.k = t.k")
c.Assert(err.Error(), Equals, "[planner:1288]The target table b of the UPDATE is not updatable")
tk.MustExec("update t, (select * from t) as b set t.k = b.k")

// issue 8045
tk.MustExec("drop table if exists t1")
tk.MustExec(`CREATE TABLE t1 (c1 float)`)
tk.MustExec("INSERT INTO t1 SET c1 = 1")
tk.MustExec("UPDATE t1 SET c1 = 1.2 WHERE c1=1;")
}

func (s *testSuite) TestPartitionedTableUpdate(c *C) {
Expand Down

0 comments on commit 736c313

Please sign in to comment.