Skip to content

Commit 0f0f708

Browse files
authored
statistics: remove dead code (#58412)
ref #57804
1 parent d9749cd commit 0f0f708

File tree

1 file changed

+21
-67
lines changed

1 file changed

+21
-67
lines changed

pkg/statistics/handle/bootstrap.go

Lines changed: 21 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache stats
282282
}
283283
}
284284

285-
// initStatsHistogramsSQLGen generates the SQL to load all stats_histograms records.
285+
// genInitStatsHistogramsSQL generates the SQL to load all stats_histograms records.
286286
// We need to read all the records since we need to do initialization of table.ColAndIdxExistenceMap.
287-
func initStatsHistogramsSQLGen(isPaging bool) string {
287+
func genInitStatsHistogramsSQL(isPaging bool) string {
288288
selectPrefix := "select /*+ ORDER_INDEX(mysql.stats_histograms,tbl) */ HIGH_PRIORITY table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, correlation, flag, last_analyze_pos from mysql.stats_histograms"
289289
orderSuffix := " order by table_id"
290290
if !isPaging {
@@ -294,7 +294,7 @@ func initStatsHistogramsSQLGen(isPaging bool) string {
294294
}
295295

296296
func (h *Handle) initStatsHistogramsLite(ctx context.Context, cache statstypes.StatsCache) error {
297-
sql := initStatsHistogramsSQLGen(false)
297+
sql := genInitStatsHistogramsSQL(false)
298298
rc, err := util.Exec(h.initStatsCtx, sql)
299299
if err != nil {
300300
return errors.Trace(err)
@@ -317,7 +317,7 @@ func (h *Handle) initStatsHistogramsLite(ctx context.Context, cache statstypes.S
317317
}
318318

319319
func (h *Handle) initStatsHistograms(is infoschema.InfoSchema, cache statstypes.StatsCache) error {
320-
sql := initStatsHistogramsSQLGen(false)
320+
sql := genInitStatsHistogramsSQL(false)
321321
rc, err := util.Exec(h.initStatsCtx, sql)
322322
if err != nil {
323323
return errors.Trace(err)
@@ -351,7 +351,7 @@ func (h *Handle) initStatsHistogramsByPaging(is infoschema.InfoSchema, cache sta
351351
}()
352352

353353
sctx := se.(sessionctx.Context)
354-
sql := initStatsHistogramsSQLGen(true)
354+
sql := genInitStatsHistogramsSQL(true)
355355
rc, err := util.Exec(sctx, sql, task.StartTid, task.EndTid)
356356
if err != nil {
357357
return errors.Trace(err)
@@ -430,10 +430,10 @@ func (*Handle) initStatsTopN4Chunk(cache statstypes.StatsCache, iter *chunk.Iter
430430
}
431431
}
432432

433-
// initStatsTopNSQLGen generates the SQL to load all stats_top_n records.
433+
// genInitStatsTopNSQLForIndexes generates the SQL to load all stats_top_n records for indexes.
434434
// We only need to load the indexes' since we only record the existence of columns in ColAndIdxExistenceMap.
435435
// The stats of the column is not loaded during the bootstrap process.
436-
func initStatsTopNSQLGen(isPaging bool) string {
436+
func genInitStatsTopNSQLForIndexes(isPaging bool) string {
437437
selectPrefix := "select /*+ ORDER_INDEX(mysql.stats_top_n,tbl) */ HIGH_PRIORITY table_id, hist_id, value, count from mysql.stats_top_n where is_index = 1"
438438
orderSuffix := " order by table_id"
439439
if !isPaging {
@@ -443,7 +443,7 @@ func initStatsTopNSQLGen(isPaging bool) string {
443443
}
444444

445445
func (h *Handle) initStatsTopN(cache statstypes.StatsCache, totalMemory uint64) error {
446-
sql := initStatsTopNSQLGen(false)
446+
sql := genInitStatsTopNSQLForIndexes(false)
447447
rc, err := util.Exec(h.initStatsCtx, sql)
448448
if err != nil {
449449
return errors.Trace(err)
@@ -476,7 +476,7 @@ func (h *Handle) initStatsTopNByPaging(cache statstypes.StatsCache, task initsta
476476
}
477477
}()
478478
sctx := se.(sessionctx.Context)
479-
sql := initStatsTopNSQLGen(true)
479+
sql := genInitStatsTopNSQLForIndexes(true)
480480
rc, err := util.Exec(sctx, sql, task.StartTid, task.EndTid)
481481
if err != nil {
482482
return errors.Trace(err)
@@ -577,14 +577,13 @@ func (h *Handle) initStatsFMSketch(cache statstypes.StatsCache) error {
577577

578578
func (*Handle) initStatsBuckets4Chunk(cache statstypes.StatsCache, iter *chunk.Iterator4Chunk) {
579579
var table *statistics.Table
580-
unspecifiedLengthTp := types.NewFieldType(mysql.TypeBlob)
581580
var (
582581
hasErr bool
583582
failedTableID int64
584583
failedHistID int64
585584
)
586585
for row := iter.Begin(); row != iter.End(); row = iter.Next() {
587-
tableID, isIndex, histID := row.GetInt64(0), row.GetInt64(1), row.GetInt64(2)
586+
tableID, histID := row.GetInt64(0), row.GetInt64(1)
588587
if table == nil || table.PhysicalID != tableID {
589588
if table != nil {
590589
table.SetAllIndexFullLoadForBootstrap()
@@ -599,58 +598,13 @@ func (*Handle) initStatsBuckets4Chunk(cache statstypes.StatsCache, iter *chunk.I
599598
}
600599
var lower, upper types.Datum
601600
var hist *statistics.Histogram
602-
if isIndex > 0 {
603-
index := table.GetIdx(histID)
604-
if index == nil {
605-
continue
606-
}
607-
hist = &index.Histogram
608-
lower, upper = types.NewBytesDatum(row.GetBytes(5)), types.NewBytesDatum(row.GetBytes(6))
609-
} else {
610-
column := table.GetCol(histID)
611-
if column == nil {
612-
continue
613-
}
614-
if !mysql.HasPriKeyFlag(column.Info.GetFlag()) {
615-
continue
616-
}
617-
hist = &column.Histogram
618-
d := types.NewBytesDatum(row.GetBytes(5))
619-
var err error
620-
if column.Info.FieldType.EvalType() == types.ETString && column.Info.FieldType.GetType() != mysql.TypeEnum && column.Info.FieldType.GetType() != mysql.TypeSet {
621-
// For new collation data, when storing the bounds of the histogram, we store the collate key instead of the
622-
// original value.
623-
// But there's additional conversion logic for new collation data, and the collate key might be longer than
624-
// the FieldType.flen.
625-
// If we use the original FieldType here, there might be errors like "Invalid utf8mb4 character string"
626-
// or "Data too long".
627-
// So we change it to TypeBlob to bypass those logics here.
628-
lower, err = d.ConvertTo(statistics.UTCWithAllowInvalidDateCtx, unspecifiedLengthTp)
629-
} else {
630-
lower, err = d.ConvertTo(statistics.UTCWithAllowInvalidDateCtx, &column.Info.FieldType)
631-
}
632-
if err != nil {
633-
hasErr = true
634-
failedTableID = tableID
635-
failedHistID = histID
636-
table.DelCol(histID)
637-
continue
638-
}
639-
d = types.NewBytesDatum(row.GetBytes(6))
640-
if column.Info.FieldType.EvalType() == types.ETString && column.Info.FieldType.GetType() != mysql.TypeEnum && column.Info.FieldType.GetType() != mysql.TypeSet {
641-
upper, err = d.ConvertTo(statistics.UTCWithAllowInvalidDateCtx, unspecifiedLengthTp)
642-
} else {
643-
upper, err = d.ConvertTo(statistics.UTCWithAllowInvalidDateCtx, &column.Info.FieldType)
644-
}
645-
if err != nil {
646-
hasErr = true
647-
failedTableID = tableID
648-
failedHistID = histID
649-
table.DelCol(histID)
650-
continue
651-
}
601+
index := table.GetIdx(histID)
602+
if index == nil {
603+
continue
652604
}
653-
hist.AppendBucketWithNDV(&lower, &upper, row.GetInt64(3), row.GetInt64(4), row.GetInt64(7))
605+
hist = &index.Histogram
606+
lower, upper = types.NewBytesDatum(row.GetBytes(4) /*lower_bound*/), types.NewBytesDatum(row.GetBytes(5) /*upper_bound*/)
607+
hist.AppendBucketWithNDV(&lower, &upper, row.GetInt64(2) /*count*/, row.GetInt64(3) /*repeats*/, row.GetInt64(6) /*ndv*/)
654608
}
655609
if table != nil {
656610
cache.Put(table.PhysicalID, table) // put this table in the cache because all statstics of the table have been read.
@@ -660,11 +614,11 @@ func (*Handle) initStatsBuckets4Chunk(cache statstypes.StatsCache, iter *chunk.I
660614
}
661615
}
662616

663-
// initStatsBucketsSQLGen generates the SQL to load all stats_top_n records.
617+
// genInitStatsBucketsSQLForIndexes generates the SQL to load all stats_buckets records for indexes.
664618
// We only need to load the indexes' since we only record the existence of columns in ColAndIdxExistenceMap.
665619
// The stats of the column is not loaded during the bootstrap process.
666-
func initStatsBucketsSQLGen(isPaging bool) string {
667-
selectPrefix := "select /*+ ORDER_INDEX(mysql.stats_buckets,tbl) */ HIGH_PRIORITY table_id, is_index, hist_id, count, repeats, lower_bound, upper_bound, ndv from mysql.stats_buckets where is_index=1"
620+
func genInitStatsBucketsSQLForIndexes(isPaging bool) string {
621+
selectPrefix := "select /*+ ORDER_INDEX(mysql.stats_buckets,tbl) */ HIGH_PRIORITY table_id, hist_id, count, repeats, lower_bound, upper_bound, ndv from mysql.stats_buckets where is_index=1"
668622
orderSuffix := " order by table_id"
669623
if !isPaging {
670624
return selectPrefix + orderSuffix
@@ -682,7 +636,7 @@ func (h *Handle) initStatsBuckets(cache statstypes.StatsCache, totalMemory uint6
682636
return errors.Trace(err)
683637
}
684638
} else {
685-
sql := initStatsBucketsSQLGen(false)
639+
sql := genInitStatsBucketsSQLForIndexes(false)
686640
rc, err := util.Exec(h.initStatsCtx, sql)
687641
if err != nil {
688642
return errors.Trace(err)
@@ -721,7 +675,7 @@ func (h *Handle) initStatsBucketsByPaging(cache statstypes.StatsCache, task init
721675
}
722676
}()
723677
sctx := se.(sessionctx.Context)
724-
sql := initStatsBucketsSQLGen(true)
678+
sql := genInitStatsBucketsSQLForIndexes(true)
725679
rc, err := util.Exec(sctx, sql, task.StartTid, task.EndTid)
726680
if err != nil {
727681
return errors.Trace(err)

0 commit comments

Comments
 (0)