Skip to content

Commit

Permalink
plan: fix bug of stats count (pingcap#5775)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored and winoros committed Feb 6, 2018
1 parent 8cc9e45 commit 4e55470
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2620,8 +2620,8 @@ func (s *testIntegrationSuite) TestCompareBuiltin(c *C) {
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a date)")
result = tk.MustQuery("desc select a = a from t")
result.Check(testkit.Rows("TableScan_4 cop table:t, range:[-inf,+inf], keep order:false 8000",
"TableReader_5 Projection_3 root data:TableScan_4 8000",
result.Check(testkit.Rows("TableScan_4 cop table:t, range:[-inf,+inf], keep order:false 10000",
"TableReader_5 Projection_3 root data:TableScan_4 10000",
"Projection_3 TableReader_5 root eq(test.t.a, test.t.a) 8000"))

// for interval
Expand Down
3 changes: 1 addition & 2 deletions plan/cbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,8 @@ func (s *testAnalyzeSuite) TestOutdatedAnalyze(c *C) {
testKit.MustExec("insert into t select * from t")
h.DumpStatsDeltaToKV()
c.Assert(h.Update(dom.InfoSchema()), IsNil)
// FIXME: The count for table scan is wrong.
testKit.MustQuery("explain select * from t where a <= 5 and b <= 5").Check(testkit.Rows(
"TableScan_5 Selection_6 cop table:t, range:[-inf,+inf], keep order:false 28.799999999999997",
"TableScan_5 Selection_6 cop table:t, range:[-inf,+inf], keep order:false 80",
"Selection_6 TableScan_5 cop le(test.t.a, 5), le(test.t.b, 5) 28.799999999999997",
"TableReader_7 root data:Selection_6 28.799999999999997",
))
Expand Down
3 changes: 3 additions & 0 deletions plan/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ type DataSource struct {
// pushedDownConds are the conditions that will be pushed down to coprocessor.
pushedDownConds []expression.Expression

// statsAfterSelect is the statsInfo for dataSource and selection.
statsAfterSelect *statsInfo

statisticTable *statistics.Table

// availableIndices is used for storing result of avalableIndices function.
Expand Down
6 changes: 3 additions & 3 deletions plan/physical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func (is *PhysicalIndexScan) addPushedDownSelection(copTask *copTask, p *DataSou
}
if tableConds != nil {
copTask.finishIndexPlan()
tableSel := PhysicalSelection{Conditions: tableConds}.init(is.ctx, p.stats.scaleByExpectCnt(expectedCnt))
tableSel := PhysicalSelection{Conditions: tableConds}.init(is.ctx, p.statsAfterSelect.scaleByExpectCnt(expectedCnt))
tableSel.SetChildren(copTask.tablePlan)
copTask.tablePlan = tableSel
copTask.cst += copTask.count() * cpuFactor
Expand Down Expand Up @@ -586,15 +586,15 @@ func (ds *DataSource) convertToTableScan(prop *requiredProp) (task task, err err
}
ts.KeepOrder = true
copTask.keepOrder = true
ts.addPushedDownSelection(copTask, ds.stats.scaleByExpectCnt(prop.expectedCnt))
ts.addPushedDownSelection(copTask, ds.statsAfterSelect.scaleByExpectCnt(prop.expectedCnt))
} else {
expectedCnt := math.MaxFloat64
if prop.isEmpty() {
expectedCnt = prop.expectedCnt
} else {
return invalidTask, nil
}
ts.addPushedDownSelection(copTask, ds.stats.scaleByExpectCnt(expectedCnt))
ts.addPushedDownSelection(copTask, ds.statsAfterSelect.scaleByExpectCnt(expectedCnt))
}
if prop.taskTp == rootTaskType {
task = finishCopTask(task, ds.ctx)
Expand Down
5 changes: 3 additions & 2 deletions plan/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (ds *DataSource) getStatsByFilter(conds expression.CNFExprs) *statsInfo {
profile.cardinality[i] = profile.count * distinctFactor
}
}
ds.stats = profile
selectivity, err := ds.statisticTable.Selectivity(ds.ctx, conds)
if err != nil {
log.Warnf("An error happened: %v, we have to use the default selectivity", err.Error())
Expand All @@ -105,8 +106,8 @@ func (ds *DataSource) deriveStats() *statsInfo {
for i, expr := range ds.pushedDownConds {
ds.pushedDownConds[i] = expression.PushDownNot(expr, false, nil)
}
ds.stats = ds.getStatsByFilter(ds.pushedDownConds)
return ds.stats
ds.statsAfterSelect = ds.getStatsByFilter(ds.pushedDownConds)
return ds.statsAfterSelect
}

func (p *LogicalSelection) deriveStats() *statsInfo {
Expand Down

0 comments on commit 4e55470

Please sign in to comment.