@@ -276,11 +276,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
276
276
hist := statistics .HistogramFromProto (jsonIdx .Histogram )
277
277
hist .ID , hist .NullCount , hist .LastUpdateVersion , hist .Correlation = idxInfo .ID , jsonIdx .NullCount , jsonIdx .LastUpdateVersion , jsonIdx .Correlation
278
278
cm , topN := statistics .CMSketchAndTopNFromProto (jsonIdx .CMSketch )
279
- // If the statistics is loaded from a JSON without stats version,
280
- // we set it to 1.
281
- statsVer := int64 (statistics .Version1 )
279
+ statsVer := int64 (statistics .Version0 )
282
280
if jsonIdx .StatsVer != nil {
283
281
statsVer = * jsonIdx .StatsVer
282
+ } else if jsonIdx .Histogram .Ndv > 0 || jsonIdx .NullCount > 0 {
283
+ // If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
284
+ // we set it to 1.
285
+ statsVer = int64 (statistics .Version1 )
284
286
}
285
287
idx := & statistics.Index {
286
288
Histogram : * hist ,
@@ -320,11 +322,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
320
322
cm , topN := statistics .CMSketchAndTopNFromProto (jsonCol .CMSketch )
321
323
fms := statistics .FMSketchFromProto (jsonCol .FMSketch )
322
324
hist .ID , hist .NullCount , hist .LastUpdateVersion , hist .TotColSize , hist .Correlation = colInfo .ID , jsonCol .NullCount , jsonCol .LastUpdateVersion , jsonCol .TotColSize , jsonCol .Correlation
323
- // If the statistics is loaded from a JSON without stats version,
324
- // we set it to 1.
325
- statsVer := int64 (statistics .Version1 )
325
+ statsVer := int64 (statistics .Version0 )
326
326
if jsonCol .StatsVer != nil {
327
327
statsVer = * jsonCol .StatsVer
328
+ } else if jsonCol .Histogram .Ndv > 0 || jsonCol .NullCount > 0 {
329
+ // If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
330
+ // we set it to 1.
331
+ statsVer = int64 (statistics .Version1 )
328
332
}
329
333
col := & statistics.Column {
330
334
PhysicalID : physicalID ,
0 commit comments