Skip to content

Commit

Permalink
planner: add protection to avoid setting tot_col_size to negative n…
Browse files Browse the repository at this point in the history
…umbers (#55327) (#55629)

close #55126
  • Loading branch information
ti-chi-bot authored Aug 24, 2024
1 parent ad786db commit 576749e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pkg/statistics/handle/storage/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func SaveTableStatsToStorage(sctx sessionctx.Context,
return 0, err
}
}
if _, err = util.Exec(sctx, "replace into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, flag, correlation) values (%?, %?, %?, %?, %?, %?, %?, %?, %?, %?, %?)",
if _, err = util.Exec(sctx, "replace into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, flag, correlation) values (%?, %?, %?, %?, %?, %?, %?, GREATEST(%?, 0), %?, %?, %?)",
tableID, result.IsIndex, hg.ID, hg.NDV, version, hg.NullCount, cmSketch, hg.TotColSize, results.StatsVer, statistics.AnalyzeFlag, hg.Correlation); err != nil {
return 0, err
}
Expand Down Expand Up @@ -361,7 +361,7 @@ func SaveStatsToStorage(sctx sessionctx.Context,
if isAnalyzed == 1 {
flag = statistics.AnalyzeFlag
}
if _, err = util.Exec(sctx, "replace into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, flag, correlation) values (%?, %?, %?, %?, %?, %?, %?, %?, %?, %?, %?)",
if _, err = util.Exec(sctx, "replace into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, flag, correlation) values (%?, %?, %?, %?, %?, %?, %?, GREATEST(%?, 0), %?, %?, %?)",
tableID, isIndex, hg.ID, hg.NDV, version, hg.NullCount, cmSketch, hg.TotColSize, statsVersion, flag, hg.Correlation); err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/statistics/handle/storage/stats_read_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *statsReadWriter) InsertColStats2KV(physicalID int64, colInfos []*model.
}
} else {
// If this stats exists, we insert histogram meta first, the distinct_count will always be one.
if _, err := util.Exec(sctx, "insert into mysql.stats_histograms (version, table_id, is_index, hist_id, distinct_count, tot_col_size) values (%?, %?, 0, %?, 1, %?)", startTS, physicalID, colInfo.ID, int64(len(value.GetBytes()))*count); err != nil {
if _, err := util.Exec(sctx, "insert into mysql.stats_histograms (version, table_id, is_index, hist_id, distinct_count, tot_col_size) values (%?, %?, 0, %?, 1, GREATEST(%?, 0))", startTS, physicalID, colInfo.ID, int64(len(value.GetBytes()))*count); err != nil {
return err
}
value, err = value.ConvertTo(sctx.GetSessionVars().StmtCtx, types.NewFieldType(mysql.TypeBlob))
Expand Down
2 changes: 1 addition & 1 deletion pkg/statistics/handle/storage/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func DumpTableStatColSizeToKV(sctx sessionctx.Context, id int64, delta variable.
return nil
}
sql := fmt.Sprintf("insert into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, tot_col_size) "+
"values %s on duplicate key update tot_col_size = tot_col_size + values(tot_col_size)", strings.Join(values, ","))
"values %s on duplicate key update tot_col_size = GREATEST(0, tot_col_size + values(tot_col_size))", strings.Join(values, ","))
_, _, err := statsutil.ExecRows(sctx, sql)
return errors.Trace(err)
}
Expand Down

0 comments on commit 576749e

Please sign in to comment.