Skip to content

Commit

Permalink
executor: fix the bug: can not join if join keys are type bigint and …
Browse files Browse the repository at this point in the history
…type bit (#19032) (#19215)

Signed-off-by: ti-srebot <[email protected]>
  • Loading branch information
ti-srebot authored Sep 1, 2020
1 parent 83fc2d8 commit bfdc09f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
60 changes: 51 additions & 9 deletions executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 6 additions & 8 deletions util/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit bfdc09f

Please sign in to comment.