Skip to content

Commit

Permalink
plan: add variable countReliable to physicalPlanInfo (pingcap#3011)
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros authored and shenli committed Apr 24, 2017
1 parent 1685dcb commit 93f4780
Show file tree
Hide file tree
Showing 17 changed files with 270 additions and 97 deletions.
5 changes: 5 additions & 0 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ func (do *Domain) StatsHandle() *statistics.Handle {
return do.statsHandle
}

// CreateStatsHandle is used only for test.
func (do *Domain) CreateStatsHandle(ctx context.Context) {
do.statsHandle = statistics.NewHandle(ctx)
}

// UpdateTableStatsLoop creates a goroutine loads stats info and updates stats info in a loop. It
// should be called only once in BootstrapSession.
func (do *Domain) UpdateTableStatsLoop(ctx context.Context) error {
Expand Down
32 changes: 16 additions & 16 deletions executor/explain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ func (s *testSuite) TestExplain(c *C) {
{
"select * from t1 left join t2 on t1.c2 = t2.c1 where t1.c1 > 1",
[]string{
"TableScan_8", "TableScan_10", "HashLeftJoin_7",
"TableScan_7", "TableScan_10", "HashLeftJoin_9",
},
[]string{
"HashLeftJoin_7", "HashLeftJoin_7", "",
"HashLeftJoin_9", "HashLeftJoin_9", "",
},
[]string{
`{
Expand Down Expand Up @@ -213,7 +213,7 @@ func (s *testSuite) TestExplain(c *C) {
"leftCond": null,
"rightCond": null,
"otherCond": null,
"leftPlan": "TableScan_8",
"leftPlan": "TableScan_7",
"rightPlan": "TableScan_10"
}`,
},
Expand Down Expand Up @@ -282,10 +282,10 @@ func (s *testSuite) TestExplain(c *C) {
{
"select count(b.c2) from t1 a, t2 b where a.c1 = b.c2 group by a.c1",
[]string{
"TableScan_11", "TableScan_12", "HashAgg_13", "HashLeftJoin_10", "Projection_9",
"TableScan_16", "TableScan_10", "HashAgg_11", "HashLeftJoin_15", "Projection_9",
},
[]string{
"HashLeftJoin_10", "HashAgg_13", "HashLeftJoin_10", "Projection_9", "",
"HashLeftJoin_15", "HashAgg_11", "HashLeftJoin_15", "Projection_9", "",
},
[]string{`{
"db": "test",
Expand Down Expand Up @@ -327,7 +327,7 @@ func (s *testSuite) TestExplain(c *C) {
"GroupByItems": [
"[b.c2]"
],
"child": "TableScan_12"
"child": "TableScan_10"
}`,
`{
"eqCond": [
Expand All @@ -336,14 +336,14 @@ func (s *testSuite) TestExplain(c *C) {
"leftCond": null,
"rightCond": null,
"otherCond": null,
"leftPlan": "TableScan_11",
"rightPlan": "HashAgg_13"
"leftPlan": "TableScan_16",
"rightPlan": "HashAgg_11"
}`,
`{
"exprs": [
"cast(join_agg_0)"
],
"child": "HashLeftJoin_10"
"child": "HashLeftJoin_15"
}`,
},
},
Expand Down Expand Up @@ -619,8 +619,8 @@ func (s *testSuite) TestExplain(c *C) {
},
{
"select s.c1 from t2 s left outer join t2 t on s.c2 = t.c2 limit 10",
[]string{"TableScan_7", "Limit_8", "TableScan_9", "HashLeftJoin_6", "Limit_10", "Projection_4"},
[]string{"Limit_8", "HashLeftJoin_6", "HashLeftJoin_6", "Limit_10", "Projection_4", ""},
[]string{"TableScan_6", "Limit_7", "TableScan_10", "HashLeftJoin_9", "Limit_11", "Projection_4"},
[]string{"Limit_7", "HashLeftJoin_9", "HashLeftJoin_9", "Limit_11", "Projection_4", ""},
[]string{
`{
"db": "test",
Expand All @@ -637,7 +637,7 @@ func (s *testSuite) TestExplain(c *C) {
`{
"limit": 10,
"offset": 0,
"child": "TableScan_7"
"child": "TableScan_6"
}`,
`{
"db": "test",
Expand All @@ -658,19 +658,19 @@ func (s *testSuite) TestExplain(c *C) {
"leftCond": null,
"rightCond": null,
"otherCond": null,
"leftPlan": "Limit_8",
"rightPlan": "TableScan_9"
"leftPlan": "Limit_7",
"rightPlan": "TableScan_10"
}`,
`{
"limit": 10,
"offset": 0,
"child": "HashLeftJoin_6"
"child": "HashLeftJoin_9"
}`,
`{
"exprs": [
"s.c1"
],
"child": "Limit_10"
"child": "Limit_11"
}`,
},
},
Expand Down
28 changes: 28 additions & 0 deletions plan/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/testleak"
Expand Down Expand Up @@ -362,10 +363,37 @@ func mockContext() context.Context {
client: &mockClient{},
}
do := &domain.Domain{}
do.CreateStatsHandle(ctx)
sessionctx.BindDomain(ctx, do)
return ctx
}

func mockStatsTable(tbl *model.TableInfo, rowCount int64) *statistics.Table {
statsTbl := &statistics.Table{
TableID: tbl.ID,
Count: rowCount,
Columns: make(map[int64]*statistics.Column, len(tbl.Columns)),
Indices: make(map[int64]*statistics.Index, len(tbl.Indices)),
}
return statsTbl
}

// mockStatsHistogram will create a statistics.Histogram, of which the data is uniform distribution.
func mockStatsHistogram(id int64, values []types.Datum, repeat int64) *statistics.Histogram {
ndv := len(values)
histogram := &statistics.Histogram{
ID: id,
NDV: int64(ndv),
Buckets: make([]statistics.Bucket, ndv),
}
for i := 0; i < ndv; i++ {
histogram.Buckets[i].Repeats = repeat
histogram.Buckets[i].Count = repeat * int64(i+1)
histogram.Buckets[i].Value = values[i]
}
return histogram
}

func (s *testPlanSuite) TestPredicatePushDown(c *C) {
defer testleak.AfterTest(c)()
tests := []struct {
Expand Down
47 changes: 31 additions & 16 deletions plan/match_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func (ts *PhysicalTableScan) matchProperty(prop *requiredProperty, infos ...*phy
newTS := ts.Copy().(*PhysicalTableScan)
newTS.addLimit(prop.limit)
p := newTS.tryToAddUnionScan(newTS)
return enforceProperty(prop, &physicalPlanInfo{p: p, cost: cost, count: infos[0].count})
return enforceProperty(prop, &physicalPlanInfo{
p: p,
cost: cost,
count: infos[0].count,
reliable: infos[0].reliable})
}
if len(prop.props) == 1 && ts.pkCol != nil && ts.pkCol.Equal(prop.props[0].col, ts.ctx) {
sortedTS := ts.Copy().(*PhysicalTableScan)
Expand All @@ -44,9 +48,10 @@ func (ts *PhysicalTableScan) matchProperty(prop *requiredProperty, infos ...*phy
}
p := sortedTS.tryToAddUnionScan(sortedTS)
return enforceProperty(&requiredProperty{limit: prop.limit}, &physicalPlanInfo{
p: p,
cost: cost,
count: infos[0].count})
p: p,
cost: cost,
count: infos[0].count,
reliable: infos[0].reliable})
}
if prop.limit != nil {
sortedTS := ts.Copy().(*PhysicalTableScan)
Expand All @@ -59,9 +64,10 @@ func (ts *PhysicalTableScan) matchProperty(prop *requiredProperty, infos ...*phy
sortedTS.KeepOrder = true
p := sortedTS.tryToAddUnionScan(sortedTS)
return enforceProperty(prop, &physicalPlanInfo{
p: p,
cost: cost,
count: infos[0].count})
p: p,
cost: cost,
count: infos[0].count,
reliable: infos[0].reliable})
}
return &physicalPlanInfo{p: nil, cost: math.MaxFloat64, count: infos[0].count}
}
Expand Down Expand Up @@ -109,7 +115,11 @@ func (is *PhysicalIndexScan) matchProperty(prop *requiredProperty, infos ...*phy
}
if len(prop.props) == 0 {
p := is.tryToAddUnionScan(is)
return enforceProperty(&requiredProperty{limit: prop.limit}, &physicalPlanInfo{p: p, cost: cost, count: infos[0].count})
return enforceProperty(&requiredProperty{limit: prop.limit}, &physicalPlanInfo{
p: p,
cost: cost,
count: infos[0].count,
reliable: infos[0].reliable})
}
matchedIdx := 0
matchedList := make([]bool, len(prop.props))
Expand Down Expand Up @@ -141,9 +151,10 @@ func (is *PhysicalIndexScan) matchProperty(prop *requiredProperty, infos ...*phy
sortedIS.addLimit(prop.limit)
p := sortedIS.tryToAddUnionScan(sortedIS)
return enforceProperty(&requiredProperty{limit: prop.limit}, &physicalPlanInfo{
p: p,
cost: sortedCost,
count: infos[0].count})
p: p,
cost: sortedCost,
count: infos[0].count,
reliable: infos[0].reliable})
}
}
if prop.limit != nil {
Expand All @@ -157,9 +168,10 @@ func (is *PhysicalIndexScan) matchProperty(prop *requiredProperty, infos ...*phy
sortedIS.OutOfOrder = true
p := sortedIS.tryToAddUnionScan(sortedIS)
return enforceProperty(prop, &physicalPlanInfo{
p: p,
cost: cost,
count: infos[0].count})
p: p,
cost: cost,
count: infos[0].count,
reliable: infos[0].reliable})
}
return &physicalPlanInfo{p: nil, cost: math.MaxFloat64, count: infos[0].count}
}
Expand Down Expand Up @@ -223,13 +235,15 @@ func (p *Union) matchProperty(_ *requiredProperty, childPlanInfo ...*physicalPla
children := make([]Plan, 0, len(childPlanInfo))
cost := float64(0)
count := float64(0)
reliable := true
for _, res := range childPlanInfo {
children = append(children, res.p)
cost += res.cost
count += res.count
reliable = reliable && res.reliable
}
np.SetChildren(children...)
return &physicalPlanInfo{p: np, cost: cost, count: count}
return &physicalPlanInfo{p: np, cost: cost, count: count, reliable: reliable}
}

// matchProperty implements PhysicalPlan matchProperty interface.
Expand All @@ -254,6 +268,7 @@ func (p *PhysicalUnionScan) matchProperty(prop *requiredProperty, childPlanInfo
res.p = np
if limit != nil {
res = addPlanToResponse(limit, res)
res.reliable = true
}
return res
}
Expand All @@ -262,7 +277,7 @@ func (p *PhysicalUnionScan) matchProperty(prop *requiredProperty, childPlanInfo
func (p *Projection) matchProperty(_ *requiredProperty, childPlanInfo ...*physicalPlanInfo) *physicalPlanInfo {
np := p.Copy()
np.SetChildren(childPlanInfo[0].p)
return &physicalPlanInfo{p: np, cost: childPlanInfo[0].cost}
return &physicalPlanInfo{p: np, cost: childPlanInfo[0].cost, reliable: childPlanInfo[0].reliable}
}

// matchProperty implements PhysicalPlan matchProperty interface.
Expand Down
Loading

0 comments on commit 93f4780

Please sign in to comment.