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

planner: fix bugs related to TIDB_INLJ hint #11253

Merged
merged 9 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 9 additions & 3 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,15 +1029,21 @@ func (p *LogicalJoin) tryToGetIndexJoin(prop *property.PhysicalProperty) (indexJ
}

if leftJoins != nil && lhsCardinality < rhsCardinality {
return leftJoins, hasIndexJoinHint
return leftJoins, leftOuter
}

if rightJoins != nil && rhsCardinality < lhsCardinality {
return rightJoins, hasIndexJoinHint
return rightJoins, rightOuter
}

// At this position, at least one of leftJoins and rightJoins is nil
Copy link
Member

Choose a reason for hiding this comment

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

They can both be not nil? consider the situation that leftJoins != nil && rightJoins != nil && lhsCardinality == rhsCardinality

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This case have been listed in the next line of comments. Do I need to explain more specifically in the comments?
In this case, leftOuter and rightOuter must be both false or both true, and the result of enforced are all correct in these cases.

Copy link
Member

Choose a reason for hiding this comment

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

I think this comment can be removed, it explains nothing useful for the reviewer to understand the following code logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, It's really confusing.

// Unless (leftOuter == rightOuter && lhsCardinality == rhsCardinality)
canForceLeft := leftJoins != nil && leftOuter
canForceRight := rightJoins != nil && rightOuter

joins := append(leftJoins, rightJoins...)
return joins, hasIndexJoinHint && len(joins) != 0
forced = canForceLeft || canForceRight
return joins, forced
}

return nil, false
Expand Down
6 changes: 6 additions & 0 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ func (p *LogicalJoin) extractOnCondition(conditions []expression.Expression, der

func extractTableAlias(p LogicalPlan) *model.CIStr {
foreyes marked this conversation as resolved.
Show resolved Hide resolved
if p.Schema().Len() > 0 && p.Schema().Columns[0].TblName.L != "" {
tblName := p.Schema().Columns[0].TblName.L
for _, column := range p.Schema().Columns {
if column.TblName.L != tblName {
winoros marked this conversation as resolved.
Show resolved Hide resolved
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

What about returns all alias table name here. Then index lookup join hint may be effective in more situation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This feature will be added later, we will have some Join Order Hints. By the way, index lookup join doesn't seem to be able to work when there are multiple alias.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because there is no available index in this situation.

Copy link
Contributor

Choose a reason for hiding this comment

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

tidb(localhost:4000) > drop table if exists t;
Query OK, 0 rows affected (0.02 sec)

tidb(localhost:4000) > create table t(a int, b int, primary key(a));
Query OK, 0 rows affected (0.00 sec)

tidb(localhost:4000) > explain select /*+ TIDB_INLJ(t1) */ t1.b, t2.a from t t1, t t2, t t3 where t1.a = t2.a and t1.a = t3.a;
+----------------------------+----------+------+------------------------------------------------------------+
| id                         | count    | task | operator info                                              |
+----------------------------+----------+------+------------------------------------------------------------+
| Projection_9               | 15625.00 | root | test.t1.b, test.t2.a                                       |
| └─MergeJoin_10             | 15625.00 | root | inner join, left key:test.t3.a, right key:test.t1.a        |
|   ├─TableReader_17         | 10000.00 | root | data:TableScan_16                                          |
|   │ └─TableScan_16         | 10000.00 | cop  | table:t3, range:[-inf,+inf], keep order:true, stats:pseudo |
|   └─MergeJoin_18           | 12500.00 | root | inner join, left key:test.t1.a, right key:test.t2.a        |
|     ├─TableReader_25       | 10000.00 | root | data:TableScan_24                                          |
|     │ └─TableScan_24       | 10000.00 | cop  | table:t1, range:[-inf,+inf], keep order:true, stats:pseudo |
|     └─TableReader_27       | 10000.00 | root | data:TableScan_26                                          |
|       └─TableScan_26       | 10000.00 | cop  | table:t2, range:[-inf,+inf], keep order:true, stats:pseudo |
+----------------------------+----------+------+------------------------------------------------------------+
9 rows in set, 1 warning (0.00 sec)

Actually, in this case, t2.a and t3.a are indices. But we can't choose the index join with TIDB_INLJ hint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because Hint only influence joins involving t1 , it should not enforce parent join to use Index Join. The parent join compares Index Join + Index Join(Hint works) and Merge Join + Merge Join(Hint doesn't work because Merge Join require order, t1 can't be inner table in this situation), and choose the second as the best one.

If change the condition into t1.a = t2.a AND t2.a = t3.a, optimizer will choose Merge Join + Index Join, because t1 is able to be inner table in this situation. Actually, they are the same...Maybe we can do some optimize?

Kenan & me are going to enhance Optimizer Hints.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use Hint TIDB_INLJ(t1, t3) may lead to Index Join + Index Join.

}
}
return &(p.Schema().Columns[0].TblName)
}
return nil
Expand Down
49 changes: 49 additions & 0 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1526,3 +1526,52 @@ func (s *testPlanSuite) TestUnmatchedTableInHint(c *C) {
}
}
}

func (s *testPlanSuite) TestIndexJoinHint(c *C) {
defer testleak.AfterTest(c)()
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer func() {
dom.Close()
store.Close()
}()
se, err := session.CreateSession4Test(store)
c.Assert(err, IsNil)
_, err = se.Execute(context.Background(), "use test")
c.Assert(err, IsNil)

tests := []struct {
sql string
best string
warning string
}{
{
sql: "select /*+ TIDB_INLJ(t1) */ t1.a, t2.a, t3.a from t t1, t t2, t t3 where t1.a = t2.a and t2.a = t3.a;",
best: "MergeInnerJoin{TableReader(Table(t))->IndexJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t2.a,test.t1.a)}(test.t3.a,test.t2.a)->Projection",
warning: "",
},
{
sql: "select /*+ TIDB_INLJ(t1) */ t1.b, t2.a from t t1, t t2 where t1.b = t2.a;",
best: "LeftHashJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t1.b,test.t2.a)",
warning: "[planner:1815]Optimizer Hint /*+ TIDB_INLJ(t1) */ is inapplicable",
},
}
for i, test := range tests {
comment := Commentf("case:%v sql:%s", i, test)
stmt, err := s.ParseOneStmt(test.sql, "", "")
c.Assert(err, IsNil, comment)

p, err := planner.Optimize(se, stmt, s.is)
c.Assert(err, IsNil)
c.Assert(core.ToString(p), Equals, test.best)

warnings := se.GetSessionVars().StmtCtx.GetWarnings()
if test.warning == "" {
c.Assert(len(warnings), Equals, 0)
} else {
c.Assert(len(warnings), Equals, 1)
c.Assert(warnings[0].Level, Equals, stmtctx.WarnLevelWarning)
c.Assert(warnings[0].Err.Error(), Equals, test.warning)
}
}
}