Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stats: fix panic when log detailed stats info #7588

Merged
merged 3 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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