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, planner: fix collation for hash join building #23770

Merged
merged 9 commits into from
Apr 1, 2021
Merged
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
6 changes: 6 additions & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2360,6 +2360,12 @@ func (b *executorBuilder) buildIndexLookUpJoin(v *plannercore.PhysicalIndexJoin)
}
}

// Use the probe table's collation.
for i, col := range v.OuterHashKeys {
outerTypes[col.Index] = outerTypes[col.Index].Clone()
outerTypes[col.Index].Collate = innerTypes[v.InnerHashKeys[i].Index].Collate
}

var (
outerFilter []expression.Expression
leftTypes, rightTypes []*types.FieldType
Expand Down
27 changes: 27 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8289,6 +8289,32 @@ func (s *testIntegrationSerialSuite) TestCollationIndexJoin(c *C) {
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 Optimizer Hint /*+ INL_MERGE_JOIN(t2) */ is inapplicable"))
}

func (s *testIntegrationSerialSuite) TestCollationMergeJoin(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (" +
" `col_10` blob DEFAULT NULL," +
" `col_11` decimal(17,5) NOT NULL," +
" `col_13` varchar(381) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Yr'," +
" PRIMARY KEY (`col_13`,`col_11`) CLUSTERED," +
" KEY `idx_4` (`col_10`(3))" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
tk.MustExec("insert into t values ('a', 12523, 'A');")
tk.MustExec("insert into t values ('A', 2, 'a');")
tk.MustExec("insert into t values ('a', 23, 'A');")
tk.MustExec("insert into t values ('a', 23, 'h2');")
tk.MustExec("insert into t values ('a', 23, 'h3');")
tk.MustExec("insert into t values ('a', 23, 'h4');")
tk.MustExec("insert into t values ('a', 23, 'h5');")
tk.MustExec("insert into t values ('a', 23, 'h6');")
tk.MustExec("insert into t values ('a', 23, 'h7');")
tk.MustQuery("select /*+ MERGE_JOIN(t) */ t.* from t where col_13 in ( select col_10 from t where t.col_13 in ( 'a', 'b' ) ) order by col_10 ;").Check(
testkit.Rows("\x41 2.00000 a", "\x61 23.00000 A", "\x61 12523.00000 A"))
}

func (s *testIntegrationSuite) TestIssue19892(c *C) {
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
Expand Down Expand Up @@ -8980,6 +9006,7 @@ func (s *testIntegrationSerialSuite) TestCollationPrefixClusteredIndex(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)

tk.MustExec("drop table if exists t")
tk.MustExec("create table t (k char(20), v int, primary key (k(4)) clustered, key (k)) collate utf8mb4_general_ci;")
tk.MustExec("insert into t values('01233', 1);")
tk.MustExec("create index idx on t(k(2))")
Expand Down
11 changes: 1 addition & 10 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,7 @@ func (p *LogicalJoin) constructIndexJoin(
IsNullEQ: newIsNullEQ,
DefaultValues: p.DefaultValues,
}
// Correct the collation used by hash.
for i := range outerHashKeys {
// Make compiler happy.
if len(innerHashKeys) == 0 {
return nil
}
chs, coll := expression.DeriveCollationFromExprs(nil, outerHashKeys[i], innerHashKeys[i])
outerHashKeys[i].GetType().Charset, outerHashKeys[i].GetType().Collate = chs, coll
innerHashKeys[i].GetType().Charset, innerHashKeys[i].GetType().Collate = chs, coll
}

join := PhysicalIndexJoin{
basePhysicalJoin: baseJoin,
innerTask: innerTask,
Expand Down