diff --git a/planner/core/hints.go b/planner/core/hints.go index 86019b204c334..dc7dc370f8dc3 100644 --- a/planner/core/hints.go +++ b/planner/core/hints.go @@ -45,6 +45,11 @@ func getTableName(tblName model.CIStr, asName *model.CIStr) model.CIStr { } func extractTableAsName(p PhysicalPlan) (*model.CIStr, *model.CIStr) { + _, isProj := p.(*PhysicalProjection) + _, isUnionScan := p.(*PhysicalUnionScan) + if isProj || isUnionScan { + return extractTableAsName(p.Children()[0]) + } if len(p.Children()) > 1 { return nil, nil } diff --git a/planner/core/physical_plan_test.go b/planner/core/physical_plan_test.go index 744da3bf62d62..4f3fa07e0cc27 100644 --- a/planner/core/physical_plan_test.go +++ b/planner/core/physical_plan_test.go @@ -784,6 +784,23 @@ func (s *testPlanSuite) TestAggregationHints(c *C) { } } +func (s *testPlanSuite) TestExplainJoinHints(c *C) { + defer testleak.AfterTest(c)() + store, dom, err := newStoreWithBootstrap() + c.Assert(err, IsNil) + defer func() { + dom.Close() + store.Close() + }() + tk := testkit.NewTestKit(c, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int, b int, c int, key(b), key(c))") + tk.MustQuery("explain format='hint' select /*+ inl_hash_join(t2) */ * from t t1 inner join t t2 on t1.b = t2.b and t1.c = 1").Check(testkit.Rows( + "use_index(@`sel_1` `test`.`t1` `c`), use_index(@`sel_1` `test`.`t2` `b`), inl_hash_join(@`sel_1` `test`.`t2`), inl_hash_join(`t2`)", + )) +} + func (s *testPlanSuite) TestAggToCopHint(c *C) { defer testleak.AfterTest(c)() store, dom, err := newStoreWithBootstrap()