@@ -421,11 +421,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
421
421
hist := statistics .HistogramFromProto (jsonIdx .Histogram )
422
422
hist .ID , hist .NullCount , hist .LastUpdateVersion , hist .Correlation = idxInfo .ID , jsonIdx .NullCount , jsonIdx .LastUpdateVersion , jsonIdx .Correlation
423
423
cm , topN := statistics .CMSketchAndTopNFromProto (jsonIdx .CMSketch )
424
- // If the statistics is loaded from a JSON without stats version,
425
- // we set it to 1.
426
- statsVer := int64 (statistics .Version1 )
424
+ statsVer := int64 (statistics .Version0 )
427
425
if jsonIdx .StatsVer != nil {
428
426
statsVer = * jsonIdx .StatsVer
427
+ } else if jsonIdx .Histogram .Ndv > 0 || jsonIdx .NullCount > 0 {
428
+ // If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
429
+ // we set it to 1.
430
+ statsVer = int64 (statistics .Version1 )
429
431
}
430
432
idx := & statistics.Index {
431
433
Histogram : * hist ,
@@ -465,11 +467,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
465
467
cm , topN := statistics .CMSketchAndTopNFromProto (jsonCol .CMSketch )
466
468
fms := statistics .FMSketchFromProto (jsonCol .FMSketch )
467
469
hist .ID , hist .NullCount , hist .LastUpdateVersion , hist .TotColSize , hist .Correlation = colInfo .ID , jsonCol .NullCount , jsonCol .LastUpdateVersion , jsonCol .TotColSize , jsonCol .Correlation
468
- // If the statistics is loaded from a JSON without stats version,
469
- // we set it to 1.
470
- statsVer := int64 (statistics .Version1 )
470
+ statsVer := int64 (statistics .Version0 )
471
471
if jsonCol .StatsVer != nil {
472
472
statsVer = * jsonCol .StatsVer
473
+ } else if jsonCol .Histogram .Ndv > 0 || jsonCol .NullCount > 0 {
474
+ // If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
475
+ // we set it to 1.
476
+ statsVer = int64 (statistics .Version1 )
473
477
}
474
478
col := & statistics.Column {
475
479
PhysicalID : physicalID ,
0 commit comments