Skip to content

Commit

Permalink
util: fix a encode bug causes the wrong result of ... (#18855) (#18866)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 committed Jul 29, 2020
1 parent 7fde4bd commit c61fc72
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
18 changes: 18 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6588,6 +6588,24 @@ func (s *testIntegrationSuite) TestIssue17727(c *C) {
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("1"))
}

func (s *testIntegrationSuite) TestIssue18850(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, t1")
tk.MustExec("create table t(a int, b enum('A', 'B'));")
tk.MustExec("create table t1(a1 int, b1 enum('B', 'A'));")
tk.MustExec("insert into t values (1, 'A');")
tk.MustExec("insert into t1 values (1, 'A');")
tk.MustQuery("select /*+ HASH_JOIN(t, t1) */ * from t join t1 on t.b = t1.b1;").Check(testkit.Rows("1 A 1 A"))

tk.MustExec("drop table t, t1")
tk.MustExec("create table t(a int, b set('A', 'B'));")
tk.MustExec("create table t1(a1 int, b1 set('B', 'A'));")
tk.MustExec("insert into t values (1, 'A');")
tk.MustExec("insert into t1 values (1, 'A');")
tk.MustQuery("select /*+ HASH_JOIN(t, t1) */ * from t join t1 on t.b = t1.b1;").Check(testkit.Rows("1 A 1 A"))
}

func (s *testIntegrationSerialSuite) TestIssue18702(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)
Expand Down
20 changes: 12 additions & 8 deletions util/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,15 @@ func encodeHashChunkRowIdx(sc *stmtctx.StatementContext, row chunk.Row, tp *type
return
}
case mysql.TypeEnum:
flag = uvarintFlag
flag = compactBytesFlag
v := uint64(row.GetEnum(idx).ToNumber())
b = (*[8]byte)(unsafe.Pointer(&v))[:]
str := tp.Elems[v-1]
b = ConvertByCollation(hack.Slice(str), tp)
case mysql.TypeSet:
flag = uvarintFlag
flag = compactBytesFlag
v := uint64(row.GetSet(idx).ToNumber())
b = (*[unsafe.Sizeof(v)]byte)(unsafe.Pointer(&v))[:]
str := tp.Elems[v-1]
b = ConvertByCollation(hack.Slice(str), tp)
case mysql.TypeBit:
// We don't need to handle errors here since the literal is ensured to be able to store in uint64 in convertToMysqlBit.
flag = uvarintFlag
Expand Down Expand Up @@ -567,9 +569,10 @@ func HashChunkSelected(sc *stmtctx.StatementContext, h []hash.Hash64, chk *chunk
buf[0], b = NilFlag, nil
isNull[i] = true
} else {
buf[0] = uvarintFlag
buf[0] = compactBytesFlag
v := uint64(column.GetEnum(i).ToNumber())
b = (*[sizeUint64]byte)(unsafe.Pointer(&v))[:]
str := tp.Elems[v-1]
b = ConvertByCollation(hack.Slice(str), tp)
}

// As the golang doc described, `Hash.Write` never returns an error.
Expand All @@ -586,9 +589,10 @@ func HashChunkSelected(sc *stmtctx.StatementContext, h []hash.Hash64, chk *chunk
buf[0], b = NilFlag, nil
isNull[i] = true
} else {
buf[0] = uvarintFlag
buf[0] = compactBytesFlag
v := uint64(column.GetSet(i).ToNumber())
b = (*[sizeUint64]byte)(unsafe.Pointer(&v))[:]
str := tp.Elems[v-1]
b = ConvertByCollation(hack.Slice(str), tp)
}

// As the golang doc described, `Hash.Write` never returns an error.
Expand Down

0 comments on commit c61fc72

Please sign in to comment.