Skip to content

Commit

Permalink
stats: fix panic when log detailed stats info (#7588)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored Sep 3, 2018
1 parent e0b98a3 commit aa9f46a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 7 additions & 3 deletions statistics/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,16 +844,20 @@ func logForIndex(prefix string, t *Table, idx *Index, ranges []*ranger.Range, ac
}
colName := idx.Info.Columns[rangePosition].Name.L
// prefer index stats over column stats
if idxHist := t.indexStartWithColumnForDebugLog(colName); idxHist != nil {
if idxHist := t.indexStartWithColumnForDebugLog(colName); idxHist != nil && idxHist.Histogram.Len() > 0 {
rangeString := logForIndexRange(idxHist, &rang, -1, factor)
log.Debugf("%s index: %s, actual: %d, equality: %s, expected equality: %d, %s", prefix, idx.Info.Name.O,
actual[i], equalityString, equalityCount, rangeString)
} else if colHist := t.columnByNameForDebugLog(colName); colHist != nil {
} else if colHist := t.columnByNameForDebugLog(colName); colHist != nil && colHist.Histogram.Len() > 0 {
rangeString := colRangeToStr(colHist, &rang, -1, factor)
log.Debugf("%s index: %s, actual: %d, equality: %s, expected equality: %d, %s", prefix, idx.Info.Name.O,
actual[i], equalityString, equalityCount, rangeString)
} else {
return
count, err := getPseudoRowCountByColumnRanges(sc, float64(t.Count), []*ranger.Range{&rang}, 0)
if err == nil {
log.Debugf("%s index: %s, actual: %d, equality: %s, expected equality: %d, range: %s, pseudo count: %.0f", prefix, idx.Info.Name.O,
actual[i], equalityString, equalityCount, rang.String(), count)
}
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions statistics/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,23 +869,26 @@ func (s *testStatsUpdateSuite) TestLogDetailedInfo(c *C) {
oriMinError := statistics.MinLogErrorRate
oriLevel := log.GetLevel()
oriBucketNum := executor.GetMaxBucketSizeForTest()
oriLease := s.do.StatsHandle().Lease
defer func() {
statistics.FeedbackProbability = oriProbability
statistics.MinLogScanCount = oriMinLogCount
statistics.MinLogErrorRate = oriMinError
executor.SetMaxBucketSizeForTest(oriBucketNum)
s.do.StatsHandle().Lease = oriLease
log.SetLevel(oriLevel)
}()
executor.SetMaxBucketSizeForTest(4)
statistics.FeedbackProbability = 1
statistics.MinLogScanCount = 0
statistics.MinLogErrorRate = 0
s.do.StatsHandle().Lease = 1

testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
testKit.MustExec("create table t (a bigint(64), b bigint(64), primary key(a), index idx(b), index idx_ba(b,a))")
testKit.MustExec("create table t (a bigint(64), b bigint(64), c bigint(64), primary key(a), index idx(b), index idx_ba(b,a), index idx_bc(b,c))")
for i := 0; i < 20; i++ {
testKit.MustExec(fmt.Sprintf("insert into t values (%d, %d)", i, i))
testKit.MustExec(fmt.Sprintf("insert into t values (%d, %d, %d)", i, i, i))
}
testKit.MustExec("analyze table t")
tests := []struct {
Expand All @@ -906,6 +909,10 @@ func (s *testStatsUpdateSuite) TestLogDetailedInfo(c *C) {
sql: "select b from t use index(idx_ba) where b = 1 and a <= 5",
result: "[stats-feedback] test.t, index: idx_ba, actual: 1, equality: 1, expected equality: 1, range: [-inf,6], actual: -1, expected: 6, buckets: {num: 8 lower_bound: 0 upper_bound: 7 repeats: 1}",
},
{
sql: "select b from t use index(idx_bc) where b = 1 and c <= 5",
result: "[stats-feedback] test.t, index: idx_bc, actual: 1, equality: 1, expected equality: 1, range: [-inf,6], pseudo count: 7",
},
{
sql: "select b from t use index(idx_ba) where b = 1",
result: "[stats-feedback] test.t, index: idx_ba, value: 1, actual: 1, expected: 1",
Expand Down

0 comments on commit aa9f46a

Please sign in to comment.