Skip to content

Commit

Permalink
plan: fix a bug when expectedCnt and count after filter are all 0. (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros authored and coocood committed Mar 8, 2018
1 parent d3186dc commit 73900c4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
11 changes: 10 additions & 1 deletion plan/cbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (s *testAnalyzeSuite) TestIndexRead(c *C) {
store.Close()
}()
testKit.MustExec("use test")
testKit.MustExec("drop table if exists t")
testKit.MustExec("drop table if exists t, t1")
testKit.MustExec("create table t (a int primary key, b int, c varchar(200), d datetime DEFAULT CURRENT_TIMESTAMP, e int, ts timestamp DEFAULT CURRENT_TIMESTAMP)")
testKit.MustExec("create index b on t (b)")
testKit.MustExec("create index d on t (d)")
Expand All @@ -128,7 +128,12 @@ func (s *testAnalyzeSuite) TestIndexRead(c *C) {
for i := 0; i < 100; i++ {
testKit.MustExec(constructInsertSQL(i, 100))
}
testKit.MustExec("create table t1 (a int, b int, index idx(a), index idxx(b))")
for i := 1; i < 16; i++ {
testKit.MustExec(fmt.Sprintf("insert into t1 values(%v, %v)", i, i))
}
testKit.MustExec("analyze table t")
testKit.MustExec("analyze table t1")
tests := []struct {
sql string
best string
Expand Down Expand Up @@ -208,6 +213,10 @@ func (s *testAnalyzeSuite) TestIndexRead(c *C) {
sql: "select * from t where ts < '1991-09-05'",
best: "IndexLookUp(Index(t.ts)[[-inf,1991-09-05 00:00:00)], Table(t))",
},
{
sql: "select sum(a) from t1 use index(idx) where a = 3 and b = 100000 group by a limit 1",
best: "IndexLookUp(Index(t1.idx)[[3,3]], Table(t1)->Sel([eq(test.t1.b, 100000)]))->StreamAgg->Limit",
},
}
for _, tt := range tests {
ctx := testKit.Se.(sessionctx.Context)
Expand Down
3 changes: 3 additions & 0 deletions plan/gen_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,9 @@ func (la *LogicalAggregation) getStreamAggs(prop *requiredProp) []PhysicalPlan {
desc: prop.desc,
expectedCnt: prop.expectedCnt * la.inputCount / la.stats.count,
}
if childProp.expectedCnt < prop.expectedCnt {
childProp.expectedCnt = prop.expectedCnt
}
if !prop.isPrefix(childProp) {
continue
}
Expand Down
10 changes: 8 additions & 2 deletions plan/physical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ func (ds *DataSource) convertToIndexScan(prop *requiredProp, idx *model.IndexInf
}
}
}
if matchProperty && prop.expectedCnt < math.MaxFloat64 {
// Only use expectedCnt when it's smaller than the count we calculated.
// e.g. IndexScan(count1)->After Filter(count2). The `ds.statsAfterSelect.count` is count2. count1 is the one we need to calculate
// If expectedCnt and count2 are both zero and we go into the below `if` block, the count1 will be set to zero though it's shouldn't be.
if matchProperty && prop.expectedCnt < ds.statsAfterSelect.count {
selectivity, err := statsTbl.Selectivity(ds.ctx, is.filterCondition)
if err != nil {
log.Warnf("An error happened: %v, we have to use the default selectivity", err.Error())
Expand Down Expand Up @@ -572,7 +575,10 @@ func (ds *DataSource) convertToTableScan(prop *requiredProp) (task task, err err
}
task = copTask
matchProperty := len(prop.cols) == 1 && pkCol != nil && prop.cols[0].Equal(nil, pkCol)
if matchProperty && prop.expectedCnt < math.MaxFloat64 {
// Only use expectedCnt when it's smaller than the count we calculated.
// e.g. IndexScan(count1)->After Filter(count2). The `ds.statsAfterSelect.count` is count2. count1 is the one we need to calculate
// If expectedCnt and count2 are both zero and we go into the below `if` block, the count1 will be set to zero though it's shouldn't be.
if matchProperty && prop.expectedCnt < ds.statsAfterSelect.count {
selectivity, err := statsTbl.Selectivity(ds.ctx, ts.filterCondition)
if err != nil {
log.Warnf("An error happened: %v, we have to use the default selectivity", err.Error())
Expand Down

0 comments on commit 73900c4

Please sign in to comment.