diff --git a/statistics/histogram.go b/statistics/histogram.go index 2f54c10bd3f7e..39cc6c8659075 100644 --- a/statistics/histogram.go +++ b/statistics/histogram.go @@ -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() diff --git a/statistics/selectivity_test.go b/statistics/selectivity_test.go index de4e6b2618019..17e19857832de 100644 --- a/statistics/selectivity_test.go +++ b/statistics/selectivity_test.go @@ -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) {