Skip to content

Commit

Permalink
stats: fix shallow copy bugs (pingcap#12691) (pingcap#12817)
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored Oct 18, 2019
1 parent a663cc9 commit c6bbf0f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
4 changes: 3 additions & 1 deletion statistics/cmsketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ func LoadCMSketchWithTopN(exec sqlexec.RestrictedSQLExecutor, tableID, isIndex,
}
topN := make([]*TopNMeta, 0, len(topNRows))
for _, row := range topNRows {
topN = append(topN, &TopNMeta{Data: row.GetBytes(0), Count: row.GetUint64(1)})
data := make([]byte, len(row.GetBytes(0)))
copy(data, row.GetBytes(0))
topN = append(topN, &TopNMeta{Data: data, Count: row.GetUint64(1)})
}
return decodeCMSketch(cms, topN)
}
Expand Down
5 changes: 3 additions & 2 deletions statistics/handle/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, tables Stat
continue
}
id, ndv, nullCount, version, totColSize := row.GetInt64(2), row.GetInt64(3), row.GetInt64(5), row.GetUint64(4), row.GetInt64(7)
lastAnalyzePos := row.GetDatum(11, types.NewFieldType(mysql.TypeBlob))
tbl, _ := h.getTableByPhysicalID(is, table.PhysicalID)
if row.GetInt64(1) > 0 {
var idxInfo *model.IndexInfo
Expand All @@ -109,7 +110,7 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, tables Stat
terror.Log(errors.Trace(err))
}
hist := statistics.NewHistogram(id, ndv, nullCount, version, types.NewFieldType(mysql.TypeBlob), chunk.InitialCapacity, 0)
table.Indices[hist.ID] = &statistics.Index{Histogram: *hist, CMSketch: cms, Info: idxInfo, StatsVer: row.GetInt64(8), Flag: row.GetInt64(10), LastAnalyzePos: row.GetDatum(11, types.NewFieldType(mysql.TypeBlob))}
table.Indices[hist.ID] = &statistics.Index{Histogram: *hist, CMSketch: cms, Info: idxInfo, StatsVer: row.GetInt64(8), Flag: row.GetInt64(10), LastAnalyzePos: *lastAnalyzePos.Copy()}
} else {
var colInfo *model.ColumnInfo
for _, col := range tbl.Meta().Columns {
Expand All @@ -130,7 +131,7 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, tables Stat
Count: nullCount,
IsHandle: tbl.Meta().PKIsHandle && mysql.HasPriKeyFlag(colInfo.Flag),
Flag: row.GetInt64(10),
LastAnalyzePos: row.GetDatum(11, types.NewFieldType(mysql.TypeBlob)),
LastAnalyzePos: *lastAnalyzePos.Copy(),
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func (h *Handle) Clear() {
}
h.feedback = h.feedback[:0]
h.mu.ctx.GetSessionVars().InitChunkSize = 1
h.mu.ctx.GetSessionVars().MaxChunkSize = 32
h.mu.ctx.GetSessionVars().MaxChunkSize = 1
h.mu.ctx.GetSessionVars().ProjectionConcurrency = 0
h.listHead = &SessionStatsCollector{mapper: make(tableDeltaMap), rateMap: make(errorRateDeltaMap)}
h.globalMap = make(tableDeltaMap)
h.mu.rateMap = make(errorRateDeltaMap)
Expand Down Expand Up @@ -353,6 +354,7 @@ func (h *Handle) indexStatsFromStorage(row chunk.Row, table *statistics.Table, t
idx := table.Indices[histID]
errorRate := statistics.ErrorRate{}
flag := row.GetInt64(8)
lastAnalyzePos := row.GetDatum(10, types.NewFieldType(mysql.TypeBlob))
if statistics.IsAnalyzed(flag) {
h.mu.Lock()
h.mu.rateMap.clear(table.PhysicalID, histID, true)
Expand All @@ -373,7 +375,7 @@ func (h *Handle) indexStatsFromStorage(row chunk.Row, table *statistics.Table, t
if err != nil {
return errors.Trace(err)
}
idx = &statistics.Index{Histogram: *hg, CMSketch: cms, Info: idxInfo, ErrorRate: errorRate, StatsVer: row.GetInt64(7), Flag: flag, LastAnalyzePos: row.GetDatum(10, types.NewFieldType(mysql.TypeBlob))}
idx = &statistics.Index{Histogram: *hg, CMSketch: cms, Info: idxInfo, ErrorRate: errorRate, StatsVer: row.GetInt64(7), Flag: flag, LastAnalyzePos: *lastAnalyzePos.Copy()}
}
break
}
Expand All @@ -392,6 +394,7 @@ func (h *Handle) columnStatsFromStorage(row chunk.Row, table *statistics.Table,
nullCount := row.GetInt64(5)
totColSize := row.GetInt64(6)
correlation := row.GetFloat64(9)
lastAnalyzePos := row.GetDatum(10, types.NewFieldType(mysql.TypeBlob))
col := table.Columns[histID]
errorRate := statistics.ErrorRate{}
flag := row.GetInt64(8)
Expand Down Expand Up @@ -429,7 +432,7 @@ func (h *Handle) columnStatsFromStorage(row chunk.Row, table *statistics.Table,
ErrorRate: errorRate,
IsHandle: tableInfo.PKIsHandle && mysql.HasPriKeyFlag(colInfo.Flag),
Flag: flag,
LastAnalyzePos: row.GetDatum(10, types.NewFieldType(mysql.TypeBlob)),
LastAnalyzePos: *lastAnalyzePos.Copy(),
}
col.Histogram.Correlation = correlation
break
Expand All @@ -452,7 +455,7 @@ func (h *Handle) columnStatsFromStorage(row chunk.Row, table *statistics.Table,
ErrorRate: errorRate,
IsHandle: tableInfo.PKIsHandle && mysql.HasPriKeyFlag(colInfo.Flag),
Flag: flag,
LastAnalyzePos: row.GetDatum(10, types.NewFieldType(mysql.TypeBlob)),
LastAnalyzePos: *lastAnalyzePos.Copy(),
}
break
}
Expand Down
6 changes: 5 additions & 1 deletion statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (s *testStatsSuite) TestInitStats(c *C) {
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
testKit.MustExec("create table t(a int, b int, c int, primary key(a), key idx(b))")
testKit.MustExec("insert into t values (1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5),(6,6,6)")
testKit.MustExec("insert into t values (1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5),(6,7,8)")
testKit.MustExec("analyze table t")
h := s.do.StatsHandle()
is := s.do.InfoSchema()
Expand All @@ -384,6 +384,10 @@ func (s *testStatsSuite) TestInitStats(c *C) {
h.Clear()
c.Assert(h.InitStats(is), IsNil)
table0 := h.GetTableStats(tbl.Meta())
cols := table0.Columns
c.Assert(cols[1].LastAnalyzePos.GetBytes()[0], Equals, uint8(0x36))
c.Assert(cols[2].LastAnalyzePos.GetBytes()[0], Equals, uint8(0x37))
c.Assert(cols[3].LastAnalyzePos.GetBytes()[0], Equals, uint8(0x38))
h.Clear()
c.Assert(h.Update(is), IsNil)
table1 := h.GetTableStats(tbl.Meta())
Expand Down

0 comments on commit c6bbf0f

Please sign in to comment.