From 4e38f924a36005e9d2d9b4fd40d92c40eec6a8dc Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Tue, 24 Mar 2020 16:26:36 +0800 Subject: [PATCH] cherry-pick manually --- expression/builtin_other.go | 13 +++++++++++++ expression/integration_test.go | 19 +++++++++++++++++-- util/chunk/row.go | 13 +++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/expression/builtin_other.go b/expression/builtin_other.go index 124ad704c195e..f6ebf4a0ff779 100644 --- a/expression/builtin_other.go +++ b/expression/builtin_other.go @@ -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) diff --git a/expression/integration_test.go b/expression/integration_test.go index c151a5c5e7c83..fb53047f548fa 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4035,9 +4035,9 @@ from (select * from t1) a left join (select bussid,date(from_unixtime(ct)) date8 from t2) b -on +on a.period_id = b.bussid -where +where datediff(b.date8, date(from_unixtime(a.starttime))) >= 0` tk.MustQuery(q) } @@ -4500,3 +4500,18 @@ func (s *testIntegrationSuite) TestIssue11309And11319(c *C) { tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_HOUR)`).Check(testkit.Rows("2007-03-31 00:08:28")) tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR_MONTH)`).Check(testkit.Rows("2009-05-28 22:08:28")) } + +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;") +} diff --git a/util/chunk/row.go b/util/chunk/row.go index df2bd96df91f6..234c008475ba5 100644 --- a/util/chunk/row.go +++ b/util/chunk/row.go @@ -217,6 +217,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)