Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: fix a encode bug causes the wrong result of hashJoin with set and enum #19175

Merged
merged 5 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5099,7 +5099,25 @@ func (s *testIntegrationSuite) TestIssue19012(c *C) {
tk.Se.GetSessionVars().MaxChunkSize = 1
tk.Se.GetSessionVars().InitChunkSize = 1
rs := tk.MustQuery(`select a.province, a.city, case when sum(s1should_count) = 0 then 0 else sum(s1complete_count)/sum(s1should_count) end as aa from t a where a.province = "江苏省" group by a.province, a.city
union all
union all
select a.province, a.province city, case when sum(s1should_count) = 0 then 0 else sum(s1complete_count)/sum(s1should_count) end as aa from t a where a.province = "江苏省" group by a.province, a.province;`)
rs.Sort().Check(testkit.Rows("江苏省 南京市 9.0000", "江苏省 南通市 9.0000", "江苏省 宿迁市 9.0000", "江苏省 常州市 9.0000", "江苏省 徐州市 -8.0000", "江苏省 扬州市 9.0000", "江苏省 无锡市 6.0000", "江苏省 江苏省 10.0000", "江苏省 泰州市 9.0000", "江苏省 淮安市 9.0000", "江苏省 盐城市 6.0000", "江苏省 苏州市 9.0000", "江苏省 连云港市 9.0000", "江苏省 镇江市 9.0000"))
}

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"))
}
5 changes: 3 additions & 2 deletions util/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/hack"
)

// First byte in the encoded value which specifies the encoding type.
Expand Down Expand Up @@ -251,9 +252,9 @@ func encodeHashChunkRow(sc *stmtctx.StatementContext, b []byte, row chunk.Row, a
}
b = append(b, bin...)
case mysql.TypeEnum:
b = encodeUnsignedInt(b, uint64(row.GetEnum(i).ToNumber()), comparable)
b = encodeBytes(b, hack.Slice(allTypes[i].Elems[uint64(row.GetEnum(i).ToNumber())-1]), comparable)
case mysql.TypeSet:
b = encodeUnsignedInt(b, uint64(row.GetSet(i).ToNumber()), comparable)
b = encodeBytes(b, hack.Slice(allTypes[i].Elems[uint64(row.GetSet(i).ToNumber())-1]), comparable)
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.
var val uint64
Expand Down