@@ -1192,3 +1192,53 @@ func TestIgnoreRealtimeStats(t *testing.T) {
1192
1192
testKit .MustExec ("set @@tidb_opt_objective = 'determinate'" )
1193
1193
testKit .MustQuery ("explain select * from t where a = 1 and b > 2" ).Check (testkit .Rows (analyzedPlan ... ))
1194
1194
}
1195
+
1196
+ func TestEstimationForUnknownValuesAfterModify (t * testing.T ) {
1197
+ store , dom := testkit .CreateMockStoreAndDomain (t )
1198
+ testKit := testkit .NewTestKit (t , store )
1199
+ testKit .MustExec ("use test" )
1200
+ testKit .MustExec ("drop table if exists t" )
1201
+ testKit .MustExec ("create table t(a int, key idx(a))" )
1202
+ testKit .MustExec ("set @@tidb_analyze_version=2" )
1203
+ testKit .MustExec ("set @@global.tidb_enable_auto_analyze='OFF'" )
1204
+ for i := 1 ; i <= 10 ; i ++ {
1205
+ testKit .MustExec (fmt .Sprintf ("insert into t values (%d)" , i ))
1206
+ testKit .MustExec (fmt .Sprintf ("insert into t values (%d)" , i ))
1207
+ testKit .MustExec (fmt .Sprintf ("insert into t values (%d)" , i ))
1208
+ testKit .MustExec (fmt .Sprintf ("insert into t values (%d)" , i ))
1209
+ testKit .MustExec (fmt .Sprintf ("insert into t values (%d)" , i ))
1210
+ testKit .MustExec (fmt .Sprintf ("insert into t select a from t where a = %d" , i ))
1211
+ }
1212
+ testKit .MustExec ("analyze table t" )
1213
+ h := dom .StatsHandle ()
1214
+ require .Nil (t , h .DumpStatsDeltaToKV (true ))
1215
+
1216
+ table , err := dom .InfoSchema ().TableByName (model .NewCIStr ("test" ), model .NewCIStr ("t" ))
1217
+ require .NoError (t , err )
1218
+ statsTbl := h .GetTableStats (table .Meta ())
1219
+
1220
+ // Search for a found value == 10.0
1221
+ sctx := mock .NewContext ()
1222
+ col := statsTbl .Columns [1 ]
1223
+ count , err := cardinality .GetColumnRowCount (sctx , col , getRange (5 , 5 ), statsTbl .RealtimeCount , statsTbl .ModifyCount , false )
1224
+ require .NoError (t , err )
1225
+ require .Equal (t , 10.0 , count )
1226
+
1227
+ // Search for a not found value with zero modifyCount. Defaults to count == 1.0
1228
+ count , err = cardinality .GetColumnRowCount (sctx , col , getRange (11 , 11 ), statsTbl .RealtimeCount , statsTbl .ModifyCount , false )
1229
+ require .NoError (t , err )
1230
+ require .Equal (t , 1.0 , count )
1231
+
1232
+ // Add another 200 rows to the table
1233
+ testKit .MustExec ("insert into t select a+10 from t" )
1234
+ testKit .MustExec ("insert into t select a+10 from t where a <= 10" )
1235
+ require .Nil (t , h .DumpStatsDeltaToKV (true ))
1236
+ require .Nil (t , h .Update (dom .InfoSchema ()))
1237
+ statsTblnew := h .GetTableStats (table .Meta ())
1238
+
1239
+ // Search for a not found value based upon statistics - count should be >= 10 and <=40
1240
+ count , err = cardinality .GetColumnRowCount (sctx , col , getRange (15 , 15 ), statsTblnew .RealtimeCount , statsTblnew .ModifyCount , false )
1241
+ require .NoError (t , err )
1242
+ require .Truef (t , count < 41 , "expected: between 10 to 40, got: %v" , count )
1243
+ require .Truef (t , count > 9 , "expected: between 10 to 40, got: %v" , count )
1244
+ }
0 commit comments