Skip to content

Commit

Permalink
planner: remove the fields LogicalJoin.LeftJoinKeys and RightJoinKeys (
Browse files Browse the repository at this point in the history
  • Loading branch information
francis0407 authored and sre-bot committed Jan 19, 2020
1 parent 5017579 commit cea80ae
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 41 deletions.
3 changes: 2 additions & 1 deletion planner/cascades/testdata/integration_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"select t1.a, t1.b from t1, t2 where t1.a > t2.a and t2.b > 200",
"select t1.a, t1.b from t1 left join t2 on t1.a = t2.a where t1.a > 2 and t2.b > 200",
"select t2.a, t2.b from t1 right join t2 on t1.a = t2.a where t1.a > 2 and t2.b > 200",
"select t1.a, t1.b from t1, t2 where t1.a = t2.a order by t1.a"
"select t1.a, t1.b from t1, t2 where t1.a = t2.a order by t1.a",
"select * from t1 join t2 on t1.a = t2.a"
]
},
{
Expand Down
15 changes: 15 additions & 0 deletions planner/cascades/testdata/integration_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,21 @@
"2 22",
"3 33"
]
},
{
"SQL": "select * from t1 join t2 on t1.a = t2.a",
"Plan": [
"MergeJoin_11 12500.00 root inner join, left key:test.t1.a, right key:test.t2.a",
"├─TableReader_16 10000.00 root data:TableScan_17",
"│ └─TableScan_17 10000.00 cop[tikv] table:t1, range:[-inf,+inf], keep order:true, stats:pseudo",
"└─TableReader_19 10000.00 root data:TableScan_20",
" └─TableScan_20 10000.00 cop[tikv] table:t2, range:[-inf,+inf], keep order:true, stats:pseudo"
],
"Result": [
"1 11 1 111",
"2 22 2 222",
"3 33 3 333"
]
}
]
},
Expand Down
4 changes: 0 additions & 4 deletions planner/cascades/transformation_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,10 +894,6 @@ func (r *PushSelDownJoin) OnTransform(old *memo.ExprIter) (newExprs []*memo.Grou
}
leftCond = expression.RemoveDupExprs(sctx, leftCond)
rightCond = expression.RemoveDupExprs(sctx, rightCond)
for _, eqCond := range join.EqualConditions {
join.LeftJoinKeys = append(join.LeftJoinKeys, eqCond.GetArgs()[0].(*expression.Column))
join.RightJoinKeys = append(join.RightJoinKeys, eqCond.GetArgs()[1].(*expression.Column))
}
// TODO: Update EqualConditions like what we have done in the method join.updateEQCond() before.
leftGroup = buildChildSelectionGroup(sel, leftCond, leftGroup)
rightGroup = buildChildSelectionGroup(sel, rightCond, rightGroup)
Expand Down
34 changes: 16 additions & 18 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,15 @@ func (p *PhysicalMergeJoin) tryToGetChildReqProp(prop *property.PhysicalProperty
func (p *LogicalJoin) GetMergeJoin(prop *property.PhysicalProperty, schema *expression.Schema) []PhysicalPlan {
joins := make([]PhysicalPlan, 0, len(p.leftProperties)+1)
// The leftProperties caches all the possible properties that are provided by its children.
leftJoinKeys, rightJoinKeys := p.GetJoinKeys()
for _, lhsChildProperty := range p.leftProperties {
offsets := getMaxSortPrefix(lhsChildProperty, p.LeftJoinKeys)
offsets := getMaxSortPrefix(lhsChildProperty, leftJoinKeys)
if len(offsets) == 0 {
continue
}

leftKeys := lhsChildProperty[:len(offsets)]
rightKeys := expression.NewSchema(p.RightJoinKeys...).ColumnsByIndices(offsets)
rightKeys := expression.NewSchema(rightJoinKeys...).ColumnsByIndices(offsets)

prefixLen := findMaxPrefixLen(p.rightProperties, rightKeys)
if prefixLen == 0 {
Expand Down Expand Up @@ -198,20 +199,21 @@ func getNewJoinKeysByOffsets(oldJoinKeys []*expression.Column, offsets []int) []

func (p *LogicalJoin) getEnforcedMergeJoin(prop *property.PhysicalProperty) []PhysicalPlan {
// Check whether SMJ can satisfy the required property
offsets := make([]int, 0, len(p.LeftJoinKeys))
leftJoinKeys, rightJoinKeys := p.GetJoinKeys()
offsets := make([]int, 0, len(leftJoinKeys))
all, desc := prop.AllSameOrder()
if !all {
return nil
}
for _, item := range prop.Items {
isExist := false
for joinKeyPos := 0; joinKeyPos < len(p.LeftJoinKeys); joinKeyPos++ {
for joinKeyPos := 0; joinKeyPos < len(leftJoinKeys); joinKeyPos++ {
var key *expression.Column
if item.Col.Equal(p.ctx, p.LeftJoinKeys[joinKeyPos]) {
key = p.LeftJoinKeys[joinKeyPos]
if item.Col.Equal(p.ctx, leftJoinKeys[joinKeyPos]) {
key = leftJoinKeys[joinKeyPos]
}
if item.Col.Equal(p.ctx, p.RightJoinKeys[joinKeyPos]) {
key = p.RightJoinKeys[joinKeyPos]
if item.Col.Equal(p.ctx, rightJoinKeys[joinKeyPos]) {
key = rightJoinKeys[joinKeyPos]
}
if key == nil {
continue
Expand All @@ -233,8 +235,8 @@ func (p *LogicalJoin) getEnforcedMergeJoin(prop *property.PhysicalProperty) []Ph
}
}
// Generate the enforced sort merge join
leftKeys := getNewJoinKeysByOffsets(p.LeftJoinKeys, offsets)
rightKeys := getNewJoinKeysByOffsets(p.RightJoinKeys, offsets)
leftKeys := getNewJoinKeysByOffsets(leftJoinKeys, offsets)
rightKeys := getNewJoinKeysByOffsets(rightJoinKeys, offsets)
lProp := property.NewPhysicalProperty(property.RootTaskType, leftKeys, desc, math.MaxFloat64, true)
rProp := property.NewPhysicalProperty(property.RootTaskType, rightKeys, desc, math.MaxFloat64, true)
baseJoin := basePhysicalJoin{
Expand Down Expand Up @@ -311,11 +313,9 @@ func (p *LogicalJoin) constructIndexJoin(
outerJoinKeys []*expression.Column
)
if outerIdx == 0 {
outerJoinKeys = p.LeftJoinKeys
innerJoinKeys = p.RightJoinKeys
outerJoinKeys, innerJoinKeys = p.GetJoinKeys()
} else {
innerJoinKeys = p.LeftJoinKeys
outerJoinKeys = p.RightJoinKeys
innerJoinKeys, outerJoinKeys = p.GetJoinKeys()
}
chReqProps := make([]*property.PhysicalProperty, 2)
chReqProps[outerIdx] = &property.PhysicalProperty{TaskTp: property.RootTaskType, ExpectedCnt: math.MaxFloat64, Items: prop.Items}
Expand Down Expand Up @@ -478,11 +478,9 @@ func (p *LogicalJoin) getIndexJoinByOuterIdx(prop *property.PhysicalProperty, ou
outerJoinKeys []*expression.Column
)
if outerIdx == 0 {
outerJoinKeys = p.LeftJoinKeys
innerJoinKeys = p.RightJoinKeys
outerJoinKeys, innerJoinKeys = p.GetJoinKeys()
} else {
innerJoinKeys = p.LeftJoinKeys
outerJoinKeys = p.RightJoinKeys
innerJoinKeys, outerJoinKeys = p.GetJoinKeys()
}
ds, isDataSource := innerChild.(*DataSource)
us, isUnionScan := innerChild.(*LogicalUnionScan)
Expand Down
11 changes: 9 additions & 2 deletions planner/core/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ type LogicalJoin struct {
RightConditions expression.CNFExprs
OtherConditions expression.CNFExprs

LeftJoinKeys []*expression.Column
RightJoinKeys []*expression.Column
leftProperties [][]*expression.Column
rightProperties [][]*expression.Column

Expand All @@ -153,6 +151,15 @@ type LogicalJoin struct {
equalCondOutCnt float64
}

// GetJoinKeys extracts join keys(columns) from EqualConditions.
func (p *LogicalJoin) GetJoinKeys() (leftKeys, rightKeys []*expression.Column) {
for _, expr := range p.EqualConditions {
leftKeys = append(leftKeys, expr.GetArgs()[0].(*expression.Column))
rightKeys = append(rightKeys, expr.GetArgs()[1].(*expression.Column))
}
return
}

func (p *LogicalJoin) columnSubstitute(schema *expression.Schema, exprs []expression.Expression) {
for i, cond := range p.LeftConditions {
p.LeftConditions[i] = expression.ColumnSubstitute(cond, schema, exprs)
Expand Down
5 changes: 3 additions & 2 deletions planner/core/physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,13 @@ type PhysicalHashJoin struct {

// NewPhysicalHashJoin creates a new PhysicalHashJoin from LogicalJoin.
func NewPhysicalHashJoin(p *LogicalJoin, innerIdx int, useOuterToBuild bool, newStats *property.StatsInfo, prop ...*property.PhysicalProperty) *PhysicalHashJoin {
leftJoinKeys, rightJoinKeys := p.GetJoinKeys()
baseJoin := basePhysicalJoin{
LeftConditions: p.LeftConditions,
RightConditions: p.RightConditions,
OtherConditions: p.OtherConditions,
LeftJoinKeys: p.LeftJoinKeys,
RightJoinKeys: p.RightJoinKeys,
LeftJoinKeys: leftJoinKeys,
RightJoinKeys: rightJoinKeys,
JoinType: p.JoinType,
DefaultValues: p.DefaultValues,
InnerChildIdx: innerIdx,
Expand Down
4 changes: 0 additions & 4 deletions planner/core/rule_join_reorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ func (s *baseSingleGroupJoinOrderSolver) newJoinWithEdges(lChild, rChild Logical
newJoin := s.newCartesianJoin(lChild, rChild)
newJoin.EqualConditions = eqEdges
newJoin.OtherConditions = otherConds
for _, eqCond := range newJoin.EqualConditions {
newJoin.LeftJoinKeys = append(newJoin.LeftJoinKeys, eqCond.GetArgs()[0].(*expression.Column))
newJoin.RightJoinKeys = append(newJoin.RightJoinKeys, eqCond.GetArgs()[1].(*expression.Column))
}
return newJoin
}

Expand Down
4 changes: 0 additions & 4 deletions planner/core/rule_join_reorder_dp.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,5 @@ func (s *joinReorderDPSolver) newJoinWithConds(leftPlan, rightPlan LogicalPlan,
join := s.newCartesianJoin(leftPlan, rightPlan)
join.EqualConditions = eqConds
join.OtherConditions = otherConds
for _, eqCond := range join.EqualConditions {
join.LeftJoinKeys = append(join.LeftJoinKeys, eqCond.GetArgs()[0].(*expression.Column))
join.RightJoinKeys = append(join.RightJoinKeys, eqCond.GetArgs()[1].(*expression.Column))
}
return join
}
4 changes: 0 additions & 4 deletions planner/core/rule_predicate_push_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,6 @@ func (p *LogicalJoin) PredicatePushDown(predicates []expression.Expression) (ret
addSelection(p, lCh, leftRet, 0)
addSelection(p, rCh, rightRet, 1)
p.updateEQCond()
for _, eqCond := range p.EqualConditions {
p.LeftJoinKeys = append(p.LeftJoinKeys, eqCond.GetArgs()[0].(*expression.Column))
p.RightJoinKeys = append(p.RightJoinKeys, eqCond.GetArgs()[1].(*expression.Column))
}
p.mergeSchema()
buildKeyInfo(p)
return ret, p.self
Expand Down
5 changes: 3 additions & 2 deletions planner/core/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,13 @@ func (la *LogicalAggregation) DeriveStats(childStats []*property.StatsInfo, self
// every matched bucket.
func (p *LogicalJoin) DeriveStats(childStats []*property.StatsInfo, selfSchema *expression.Schema, childSchema []*expression.Schema) (*property.StatsInfo, error) {
leftProfile, rightProfile := childStats[0], childStats[1]
leftJoinKeys, rightJoinKeys := p.GetJoinKeys()
helper := &fullJoinRowCountHelper{
cartesian: 0 == len(p.EqualConditions),
leftProfile: leftProfile,
rightProfile: rightProfile,
leftJoinKeys: p.LeftJoinKeys,
rightJoinKeys: p.RightJoinKeys,
leftJoinKeys: leftJoinKeys,
rightJoinKeys: rightJoinKeys,
leftSchema: childSchema[0],
rightSchema: childSchema[1],
}
Expand Down

0 comments on commit cea80ae

Please sign in to comment.