Skip to content

Commit

Permalink
stats: fix panic caused by empty histogram (pingcap#7912)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx committed Oct 17, 2018
1 parent 1ca8e99 commit 0710f87
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ func MergeHistograms(sc *stmtctx.StatementContext, lh *Histogram, rh *Histogram,
}

func (hg *Histogram) outOfRange(val types.Datum) bool {
if hg.Bounds == nil {
if hg.Len() == 0 {
return true
}
len := hg.Bounds.NumRows()
Expand Down
12 changes: 12 additions & 0 deletions statistics/selectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,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 0710f87

Please sign in to comment.