Skip to content

Commit

Permalink
cherry-pick
Browse files Browse the repository at this point in the history
  • Loading branch information
wshwsh12 committed Mar 24, 2020
1 parent 8750363 commit 2eb589d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions expression/builtin_other.go
Expand Up @@ -524,6 +524,19 @@ func (b *builtinValuesIntSig) evalInt(_ chunk.Row) (int64, bool, error) {
if row.IsNull(b.offset) {
return 0, true, nil
}
// For BinaryLiteral, see issue #15310
val := row.GetRaw(b.offset)
if len(val) > 8 {
return 0, true, errors.New("Session current insert values is too long")
}
if len(val) < 8 {
var binary types.BinaryLiteral = val
v, err := binary.ToInt(b.ctx.GetSessionVars().StmtCtx)
if err != nil {
return 0, true, errors.Trace(err)
}
return int64(v), false, nil
}
return row.GetInt64(b.offset), false, nil
}
return 0, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)
Expand Down
15 changes: 15 additions & 0 deletions expression/integration_test.go
Expand Up @@ -4903,3 +4903,18 @@ func (s *testIntegrationSuite) TestNotExistFunc(c *C) {
c.Assert(err.Error(), Equals, "[expression:1305]FUNCTION test.yyy does not exist")

}

func (s *testIntegrationSuite) TestValuesForBinaryLiteral(c *C) {
// See issue #15310
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test;")
tk.MustExec("create table testValuesBinary(id int primary key auto_increment, a bit(1));")
tk.MustExec("insert into testValuesBinary values(1,1);")
err := tk.ExecToErr("insert into testValuesBinary values(1,1) on duplicate key update id = values(id),a = values(a);")
c.Assert(err, IsNil)
tk.MustQuery("select a=0 from testValuesBinary;").Check(testkit.Rows("0"))
err = tk.ExecToErr("insert into testValuesBinary values(1,0) on duplicate key update id = values(id),a = values(a);")
c.Assert(err, IsNil)
tk.MustQuery("select a=0 from testValuesBinary;").Check(testkit.Rows("1"))
tk.MustExec("drop table testValuesBinary;")
}
13 changes: 13 additions & 0 deletions util/chunk/row.go
Expand Up @@ -218,6 +218,19 @@ func (r Row) GetDatum(colIdx int, tp *types.FieldType) types.Datum {
return d
}

// GetRaw returns the underlying raw bytes with the colIdx.
func (r Row) GetRaw(colIdx int) []byte {
var data []byte
c := r.c.columns[colIdx]
if c.isFixed() {
elemLen := len(c.elemBuf)
data = c.data[r.idx*elemLen : r.idx*elemLen+elemLen]
} else {
data = c.data[c.offsets[r.idx]:c.offsets[r.idx+1]]
}
return data
}

// IsNull returns if the datum in the chunk.Row is null.
func (r Row) IsNull(colIdx int) bool {
return r.c.columns[colIdx].isNull(r.idx)
Expand Down

0 comments on commit 2eb589d

Please sign in to comment.