From bfdc09f4eb9954a3937728eb51bd9e7c2d927e14 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Tue, 1 Sep 2020 17:32:52 +0800 Subject: [PATCH] executor: fix the bug: can not join if join keys are type bigint and type bit (#19032) (#19215) Signed-off-by: ti-srebot --- executor/join_test.go | 60 ++++++++++++++++++++++++++++++++++++------- util/codec/codec.go | 14 +++++----- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/executor/join_test.go b/executor/join_test.go index 47c843d245e46..d6150933928e4 100644 --- a/executor/join_test.go +++ b/executor/join_test.go @@ -2171,16 +2171,58 @@ func (s *testSuite9) TestIssue18572_3(c *C) { c.Assert(strings.Contains(err.Error(), "mockIndexHashJoinBuildErr"), IsTrue) } -func (s *testSuite9) TestIssue19112(c *C) { +func (s *testSuiteJoin3) TestIssue11896(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) - tk.MustExec("drop table if exists t1, t2") - tk.MustExec("create table t1 ( c_int int, c_decimal decimal(12, 6), key(c_int), unique key(c_decimal) )") - tk.MustExec("create table t2 like t1") - tk.MustExec("insert into t1 (c_int, c_decimal) values (1, 4.064000), (2, 0.257000), (3, 1.010000)") - tk.MustExec("insert into t2 (c_int, c_decimal) values (1, 4.064000), (3, 1.010000)") - tk.MustQuery("select /*+ HASH_JOIN(t1,t2) */ * from t1 join t2 on t1.c_decimal = t2.c_decimal order by t1.c_int").Check(testkit.Rows( - "1 4.064000 1 4.064000", - "3 1.010000 3 1.010000")) + + // compare bigint to bit(64) + tk.MustExec("drop table if exists t") + tk.MustExec("drop table if exists t1") + tk.MustExec("create table t(c1 bigint)") + tk.MustExec("create table t1(c1 bit(64))") + tk.MustExec("insert into t value(1)") + tk.MustExec("insert into t1 value(1)") + tk.MustQuery("select * from t, t1 where t.c1 = t1.c1").Check( + testkit.Rows("1 \x00\x00\x00\x00\x00\x00\x00\x01")) + + // compare int to bit(32) + tk.MustExec("drop table if exists t") + tk.MustExec("drop table if exists t1") + tk.MustExec("create table t(c1 int)") + tk.MustExec("create table t1(c1 bit(32))") + tk.MustExec("insert into t value(1)") + tk.MustExec("insert into t1 value(1)") + tk.MustQuery("select * from t, t1 where t.c1 = t1.c1").Check( + testkit.Rows("1 \x00\x00\x00\x01")) + + // compare mediumint to bit(24) + tk.MustExec("drop table if exists t") + tk.MustExec("drop table if exists t1") + tk.MustExec("create table t(c1 mediumint)") + tk.MustExec("create table t1(c1 bit(24))") + tk.MustExec("insert into t value(1)") + tk.MustExec("insert into t1 value(1)") + tk.MustQuery("select * from t, t1 where t.c1 = t1.c1").Check( + testkit.Rows("1 \x00\x00\x01")) + + // compare smallint to bit(16) + tk.MustExec("drop table if exists t") + tk.MustExec("drop table if exists t1") + tk.MustExec("create table t(c1 smallint)") + tk.MustExec("create table t1(c1 bit(16))") + tk.MustExec("insert into t value(1)") + tk.MustExec("insert into t1 value(1)") + tk.MustQuery("select * from t, t1 where t.c1 = t1.c1").Check( + testkit.Rows("1 \x00\x01")) + + // compare tinyint to bit(8) + tk.MustExec("drop table if exists t") + tk.MustExec("drop table if exists t1") + tk.MustExec("create table t(c1 tinyint)") + tk.MustExec("create table t1(c1 bit(8))") + tk.MustExec("insert into t value(1)") + tk.MustExec("insert into t1 value(1)") + tk.MustQuery("select * from t, t1 where t.c1 = t1.c1").Check( + testkit.Rows("1 \x01")) } func (s *testSuiteJoin3) TestIssue19498(c *C) { diff --git a/util/codec/codec.go b/util/codec/codec.go index 3da99782cc386..597701b04bae3 100644 --- a/util/codec/codec.go +++ b/util/codec/codec.go @@ -300,11 +300,9 @@ func encodeHashChunkRowIdx(sc *stmtctx.StatementContext, row chunk.Row, tp *type } switch tp.Tp { case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong, mysql.TypeYear: - flag = varintFlag - if mysql.HasUnsignedFlag(tp.Flag) { - if integer := row.GetInt64(idx); integer < 0 { - flag = uvarintFlag - } + flag = uvarintFlag + if !mysql.HasUnsignedFlag(tp.Flag) && row.GetInt64(idx) < 0 { + flag = varintFlag } b = row.GetRaw(idx) case mysql.TypeFloat: @@ -406,9 +404,9 @@ func HashChunkSelected(sc *stmtctx.StatementContext, h []hash.Hash64, chk *chunk buf[0], b = NilFlag, nil isNull[i] = true } else { - buf[0] = varintFlag - if mysql.HasUnsignedFlag(tp.Flag) && v < 0 { - buf[0] = uvarintFlag + buf[0] = uvarintFlag + if !mysql.HasUnsignedFlag(tp.Flag) && v < 0 { + buf[0] = varintFlag } b = column.GetRaw(i) }