Skip to content

Commit

Permalink
plan: build separate required physical property for children (pingcap…
Browse files Browse the repository at this point in the history
…#8635)


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 authored and iamzhoug37 committed Dec 13, 2018
1 parent 1b3c617 commit eb9150d
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 @@ -857,9 +858,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 @@ -880,9 +882,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 eb9150d

Please sign in to comment.