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

executor: fix wrong index hash join when hash col is enum (#28032) #28079

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2127,6 +2127,7 @@ func (b *executorBuilder) buildIndexLookUpJoin(v *plannercore.PhysicalIndexJoin)
for i, col := range v.OuterHashKeys {
outerTypes[col.Index] = outerTypes[col.Index].Clone()
outerTypes[col.Index].Collate = innerTypes[v.InnerHashKeys[i].Index].Collate
outerTypes[col.Index].Flag = col.RetType.Flag
}

var (
Expand Down
93 changes: 93 additions & 0 deletions executor/index_lookup_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,96 @@ func (s *testSuite5) TestIssue23656(c *C) {
"1 clever jang 1 clever jang",
"2 blissful aryabhata 2 blissful aryabhata"))
}
<<<<<<< HEAD
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL @lzmhhh123

=======

func (s *testSuite5) TestIssue23722(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b char(10), c blob, primary key (c(5)) clustered);")
tk.MustExec("insert into t values (20301,'Charlie',x'7a');")
tk.MustQuery("select * from t;").Check(testkit.Rows("20301 Charlie z"))
tk.MustQuery("select * from t where c in (select c from t where t.c >= 'a');").Check(testkit.Rows("20301 Charlie z"))

// Test lookup content exceeds primary key prefix.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b char(10), c varchar(255), primary key (c(5)) clustered);")
tk.MustExec("insert into t values (20301,'Charlie','aaaaaaa');")
tk.MustQuery("select * from t;").Check(testkit.Rows("20301 Charlie aaaaaaa"))
tk.MustQuery("select * from t where c in (select c from t where t.c >= 'a');").Check(testkit.Rows("20301 Charlie aaaaaaa"))

// Test the original case.
tk.MustExec("drop table if exists t;")
tk.MustExec(`CREATE TABLE t (
col_15 decimal(49,3),
col_16 smallint(5),
col_17 char(118) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT 'tLOOjbIXuuLKPFjkLo',
col_18 set('Alice','Bob','Charlie','David') NOT NULL,
col_19 tinyblob,
PRIMARY KEY (col_19(5),col_16) /*T![clustered_index] NONCLUSTERED */,
UNIQUE KEY idx_10 (col_19(5),col_16)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;`)
tk.MustExec("INSERT INTO `t` VALUES (38799.400,20301,'KETeFZhkoxnwMAhA','Charlie',x'7a7968584570705a647179714e56');")
tk.MustQuery("select t.* from t where col_19 in " +
"( select col_19 from t where t.col_18 <> 'David' and t.col_19 >= 'jDzNn' ) " +
"order by col_15 , col_16 , col_17 , col_18 , col_19;").Check(testkit.Rows("38799.400 20301 KETeFZhkoxnwMAhA Charlie zyhXEppZdqyqNV"))
}

func (s *testSuite5) TestIssue24547(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists a")
tk.MustExec("drop table if exists b")
tk.MustExec("CREATE TABLE `a` (\n `v` varchar(100) DEFAULT NULL,\n `k1` varchar(100) NOT NULL,\n `k2` varchar(100) NOT NULL,\n PRIMARY KEY (`k1`(3),`k2`(3)) /*T![clustered_index] CLUSTERED */,\n KEY `kk2` (`k2`(3)),\n UNIQUE KEY `uk1` (`v`)\n)")
tk.MustExec("CREATE TABLE `b` (\n `v` varchar(100) DEFAULT NULL,\n `k1` varchar(100) NOT NULL,\n `k2` varchar(100) NOT NULL,\n PRIMARY KEY (`k1`(3),`k2`(3)) /*T![clustered_index] CLUSTERED */,\n KEY `kk2` (`k2`(3))\n)")
tk.MustExec("insert into a(v, k1, k2) values('1', '1', '1'), ('22', '22', '22'), ('333', '333', '333'), ('3444', '3444', '3444'), ('444', '444', '444')")
tk.MustExec("insert into b(v, k1, k2) values('1', '1', '1'), ('22', '22', '22'), ('333', '333', '333'), ('2333', '2333', '2333'), ('555', '555', '555')")
tk.MustExec("delete a from a inner join b on a.k1 = b.k1 and a.k2 = b.k2 where b.k2 <> '333'")
}

func (s *testSuite5) TestIssue27893(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1 (a enum('x','y'))")
tk.MustExec("create table t2 (a int, key(a))")
tk.MustExec("insert into t1 values('x')")
tk.MustExec("insert into t2 values(1)")
tk.MustQuery("select /*+ inl_join(t2) */ count(*) from t1 join t2 on t1.a = t2.a").Check(testkit.Rows("1"))
tk.MustQuery("select /*+ inl_hash_join(t2) */ count(*) from t1 join t2 on t1.a = t2.a").Check(testkit.Rows("1"))
}

func (s *testSuite5) TestPartitionTableIndexJoinAndIndexReader(c *C) {
if israce.RaceEnabled {
c.Skip("exhaustive types test, skip race test")
}
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_partition_prune_mode='dynamic'")
tk.MustExec(`create table t (a int, b int, key(a)) partition by hash(a) partitions 4`)
tk.MustExec("create table tnormal (a int, b int, key(a), key(b))")
nRows := 512
values := make([]string, 0, nRows)
for i := 0; i < nRows; i++ {
values = append(values, fmt.Sprintf("(%v, %v)", rand.Intn(nRows), rand.Intn(nRows)))
}
tk.MustExec(fmt.Sprintf("insert into t values %v", strings.Join(values, ", ")))
tk.MustExec(fmt.Sprintf("insert into tnormal values %v", strings.Join(values, ", ")))

randRange := func() (int, int) {
a, b := rand.Intn(nRows), rand.Intn(nRows)
if a > b {
return b, a
}
return a, b
}
for i := 0; i < nRows; i++ {
lb, rb := randRange()
cond := fmt.Sprintf("(t2.b between %v and %v)", lb, rb)
result := tk.MustQuery("select t1.a from tnormal t1, tnormal t2 where t1.a=t2.b and " + cond).Sort().Rows()
tk.MustQuery("select /*+ TIDB_INLJ(t1, t2) */ t1.a from t t1, t t2 where t1.a=t2.b and " + cond).Sort().Check(result)
}
}
>>>>>>> c10331066... executor: fix wrong index hash join when hash col is enum (#28032)