Skip to content

Commit

Permalink
plan: build separate required physical property for children
Browse files Browse the repository at this point in the history
For logical operator which passes the required physical property
down to its children, make a copy for it instead of direct assignment
using the original pointer.
  • Loading branch information
eurekaka committed Dec 18, 2018
1 parent 2106f61 commit 7924db0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
12 changes: 12 additions & 0 deletions executor/merge_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ func (s *testSuite) TestMergeJoin(c *C) {
result.Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7"))
result = tk.MustQuery("select /*+ TIDB_SMJ(a, b) */ a.c1 from t a , (select * from t1 limit 3) b where a.c1 = b.c1 order by b.c1;")
result.Check(testkit.Rows("1", "2", "3"))
// Test LogicalSelection under LogicalJoin.
result = tk.MustQuery("select /*+ TIDB_SMJ(a, b) */ a.c1 from t a , (select * from t1 limit 3) b where a.c1 = b.c1 and b.c1 is not null order by b.c1;")
result.Check(testkit.Rows("1", "2", "3"))
tk.MustExec("begin;")
// Test LogicalLock under LogicalJoin.
result = tk.MustQuery("select /*+ TIDB_SMJ(a, b) */ a.c1 from t a , (select * from t1 for update) b where a.c1 = b.c1 order by a.c1;")
result.Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7"))
// Test LogicalUnionScan under LogicalJoin.
tk.MustExec("insert into t1 values(8);")
result = tk.MustQuery("select /*+ TIDB_SMJ(a, b) */ a.c1 from t a , t1 b where a.c1 = b.c1;")
result.Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7"))
tk.MustExec("rollback;")

plannercore.AllowCartesianProduct = false
_, err := tk.Exec("select /*+ TIDB_SMJ(t,t1) */ * from t, t1")
Expand Down
9 changes: 6 additions & 3 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import (
)

func (p *LogicalUnionScan) exhaustPhysicalPlans(prop *property.PhysicalProperty) []PhysicalPlan {
us := PhysicalUnionScan{Conditions: p.conditions}.init(p.ctx, p.stats, prop)
childProp := prop.Clone()
us := PhysicalUnionScan{Conditions: p.conditions}.init(p.ctx, p.stats, childProp)
return []PhysicalPlan{us}
}

Expand Down Expand Up @@ -846,9 +847,10 @@ func (la *LogicalAggregation) exhaustPhysicalPlans(prop *property.PhysicalProper
}

func (p *LogicalSelection) exhaustPhysicalPlans(prop *property.PhysicalProperty) []PhysicalPlan {
childProp := prop.Clone()
sel := PhysicalSelection{
Conditions: p.Conditions,
}.init(p.ctx, p.stats.ScaleByExpectCnt(prop.ExpectedCnt), prop)
}.init(p.ctx, p.stats.ScaleByExpectCnt(prop.ExpectedCnt), childProp)
return []PhysicalPlan{sel}
}

Expand All @@ -869,9 +871,10 @@ func (p *LogicalLimit) exhaustPhysicalPlans(prop *property.PhysicalProperty) []P
}

func (p *LogicalLock) exhaustPhysicalPlans(prop *property.PhysicalProperty) []PhysicalPlan {
childProp := prop.Clone()
lock := PhysicalLock{
Lock: p.Lock,
}.init(p.ctx, p.stats.ScaleByExpectCnt(prop.ExpectedCnt), prop)
}.init(p.ctx, p.stats.ScaleByExpectCnt(prop.ExpectedCnt), childProp)
return []PhysicalPlan{lock}
}

Expand Down
14 changes: 14 additions & 0 deletions planner/property/physical_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,17 @@ func (p *PhysicalProperty) HashCode() []byte {
func (p *PhysicalProperty) String() string {
return fmt.Sprintf("Prop{cols: %v, desc: %v, TaskTp: %s, expectedCount: %v}", p.Cols, p.Desc, p.TaskTp, p.ExpectedCnt)
}

// Clone returns a copy of PhysicalProperty. Currently, this function is only used to build new
// required property for children plan in `exhaustPhysicalPlans`, so we don't copy `Enforced` field
// because if `Enforced` is true, the `Cols` must be empty now, this makes `Enforced` meaningless
// for children nodes.
func (p *PhysicalProperty) Clone() *PhysicalProperty {
prop := &PhysicalProperty{
Cols: p.Cols,
Desc: p.Desc,
TaskTp: p.TaskTp,
ExpectedCnt: p.ExpectedCnt,
}
return prop
}

0 comments on commit 7924db0

Please sign in to comment.