@@ -272,11 +272,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
272
272
hist := statistics .HistogramFromProto (jsonIdx .Histogram )
273
273
hist .ID , hist .NullCount , hist .LastUpdateVersion , hist .Correlation = idxInfo .ID , jsonIdx .NullCount , jsonIdx .LastUpdateVersion , jsonIdx .Correlation
274
274
cm , topN := statistics .CMSketchAndTopNFromProto (jsonIdx .CMSketch )
275
- // If the statistics is loaded from a JSON without stats version,
276
- // we set it to 1.
277
- statsVer := int64 (statistics .Version1 )
275
+ statsVer := int64 (statistics .Version0 )
278
276
if jsonIdx .StatsVer != nil {
279
277
statsVer = * jsonIdx .StatsVer
278
+ } else if jsonIdx .Histogram .Ndv > 0 || jsonIdx .NullCount > 0 {
279
+ // If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
280
+ // we set it to 1.
281
+ statsVer = int64 (statistics .Version1 )
280
282
}
281
283
idx := & statistics.Index {
282
284
Histogram : * hist ,
@@ -303,11 +305,13 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
303
305
cm , topN := statistics .CMSketchAndTopNFromProto (jsonCol .CMSketch )
304
306
fms := statistics .FMSketchFromProto (jsonCol .FMSketch )
305
307
hist .ID , hist .NullCount , hist .LastUpdateVersion , hist .TotColSize , hist .Correlation = colInfo .ID , jsonCol .NullCount , jsonCol .LastUpdateVersion , jsonCol .TotColSize , jsonCol .Correlation
306
- // If the statistics is loaded from a JSON without stats version,
307
- // we set it to 1.
308
- statsVer := int64 (statistics .Version1 )
308
+ statsVer := int64 (statistics .Version0 )
309
309
if jsonCol .StatsVer != nil {
310
310
statsVer = * jsonCol .StatsVer
311
+ } else if jsonCol .Histogram .Ndv > 0 || jsonCol .NullCount > 0 {
312
+ // If the statistics are collected without setting stats version(which happens in v4.0 and earlier versions),
313
+ // we set it to 1.
314
+ statsVer = int64 (statistics .Version1 )
311
315
}
312
316
col := & statistics.Column {
313
317
PhysicalID : physicalID ,
0 commit comments