From fbb6dfc86178dc120225285d73532d76b5e96570 Mon Sep 17 00:00:00 2001 From: ThySinner Date: Fri, 14 Aug 2020 19:44:48 +0800 Subject: [PATCH 1/3] cherry pick #19032 to release-4.0 Signed-off-by: ti-srebot --- executor/join_test.go | 69 +++++++++++++++++++++++++++++++++++++++++++ util/codec/codec.go | 14 ++++----- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/executor/join_test.go b/executor/join_test.go index db2eca2fc5084..9962c81f946f6 100644 --- a/executor/join_test.go +++ b/executor/join_test.go @@ -2162,3 +2162,72 @@ func (s *testSuite9) TestIssue18572_3(c *C) { _, err = session.GetRows4Test(context.Background(), nil, rs) c.Assert(strings.Contains(err.Error(), "mockIndexHashJoinBuildErr"), IsTrue) } +<<<<<<< HEAD +======= + +func (s *testSuite9) TestIssue19112(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")) +} + +func (s *testSuiteJoin3) TestIssue11896(c *C) { + tk := testkit.NewTestKitWithInit(c, s.store) + + // 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")) +} +>>>>>>> 5184a0d... executor: fix the bug: can not join if join keys are type bigint and type bit (#19032) 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) } From 02f8c718ccabeda71d65eb49a4e39c0af330e3df Mon Sep 17 00:00:00 2001 From: Feng Liyuan Date: Wed, 19 Aug 2020 14:50:52 +0800 Subject: [PATCH 2/3] Update join_test.go --- executor/join_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/executor/join_test.go b/executor/join_test.go index 9962c81f946f6..750b16894b988 100644 --- a/executor/join_test.go +++ b/executor/join_test.go @@ -2162,8 +2162,6 @@ func (s *testSuite9) TestIssue18572_3(c *C) { _, err = session.GetRows4Test(context.Background(), nil, rs) c.Assert(strings.Contains(err.Error(), "mockIndexHashJoinBuildErr"), IsTrue) } -<<<<<<< HEAD -======= func (s *testSuite9) TestIssue19112(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) @@ -2230,4 +2228,3 @@ func (s *testSuiteJoin3) TestIssue11896(c *C) { tk.MustQuery("select * from t, t1 where t.c1 = t1.c1").Check( testkit.Rows("1 \x01")) } ->>>>>>> 5184a0d... executor: fix the bug: can not join if join keys are type bigint and type bit (#19032) From a39bfed33b2b12db8b6c6dc2706df9593cff670c Mon Sep 17 00:00:00 2001 From: Feng Liyuan Date: Wed, 19 Aug 2020 14:52:46 +0800 Subject: [PATCH 3/3] Update join_test.go --- executor/join_test.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/executor/join_test.go b/executor/join_test.go index 750b16894b988..a5347cb12a80c 100644 --- a/executor/join_test.go +++ b/executor/join_test.go @@ -2163,18 +2163,6 @@ func (s *testSuite9) TestIssue18572_3(c *C) { c.Assert(strings.Contains(err.Error(), "mockIndexHashJoinBuildErr"), IsTrue) } -func (s *testSuite9) TestIssue19112(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")) -} - func (s *testSuiteJoin3) TestIssue11896(c *C) { tk := testkit.NewTestKitWithInit(c, s.store)