Skip to content

Commit

Permalink
stats: fix panic caused by empty histogram (#7912) (#7928)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and zz-jason committed Oct 17, 2018
1 parent 62a6be9 commit c91290f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ func (hg *Histogram) AvgCountPerValue(totalCount int64) float64 {
}

func (hg *Histogram) outOfRange(val types.Datum) bool {
if hg.Bounds == nil {
if hg.Len() == 0 {
return true
}
return chunk.Compare(hg.Bounds.GetRow(0), 0, &val) > 0 ||
Expand Down Expand Up @@ -858,7 +858,7 @@ func (idx *Index) getRowCount(sc *stmtctx.StatementContext, indexRanges []*range
}

func (idx *Index) outOfRange(val types.Datum) bool {
if idx.Bounds == nil {
if idx.Histogram.Len() == 0 {
return true
}
withInLowBoundOrPrefixMatch := chunk.Compare(idx.Bounds.GetRow(0), 0, &val) <= 0 ||
Expand Down
12 changes: 12 additions & 0 deletions statistics/selectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ func (s *testSelectivitySuite) TestEstimationForUnknownValues(c *C) {
count, err = statsTbl.GetRowCountByIndexRanges(sc, idxID, getRange(9, 30))
c.Assert(err, IsNil)
c.Assert(count, Equals, 2.2)

testKit.MustExec("truncate table t")
testKit.MustExec("insert into t values (null, null)")
testKit.MustExec("analyze table t")
table, err = s.dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
statsTbl = h.GetTableStats(table.Meta())

colID = table.Meta().Columns[0].ID
count, err = statsTbl.GetRowCountByColumnRanges(sc, colID, getRange(1, 30))
c.Assert(err, IsNil)
c.Assert(count, Equals, 0.0)
}

func BenchmarkSelectivity(b *testing.B) {
Expand Down

0 comments on commit c91290f

Please sign in to comment.