@@ -289,6 +289,56 @@ func TestEstimationForUnknownValues(t *testing.T) {
289
289
require .Equal (t , 0.0 , count )
290
290
}
291
291
292
+ func TestEstimationForUnknownValuesAfterModify (t * testing.T ) {
293
+ store , dom := testkit .CreateMockStoreAndDomain (t )
294
+ testKit := testkit .NewTestKit (t , store )
295
+ testKit .MustExec ("use test" )
296
+ testKit .MustExec ("drop table if exists t" )
297
+ testKit .MustExec ("create table t(a int, key idx(a))" )
298
+ testKit .MustExec ("set @@tidb_analyze_version=2" )
299
+ testKit .MustExec ("set @@global.tidb_enable_auto_analyze='OFF'" )
300
+ for i := 1 ; i <= 10 ; i ++ {
301
+ testKit .MustExec (fmt .Sprintf ("insert into t values (%d)" , i ))
302
+ testKit .MustExec (fmt .Sprintf ("insert into t values (%d)" , i ))
303
+ testKit .MustExec (fmt .Sprintf ("insert into t values (%d)" , i ))
304
+ testKit .MustExec (fmt .Sprintf ("insert into t values (%d)" , i ))
305
+ testKit .MustExec (fmt .Sprintf ("insert into t values (%d)" , i ))
306
+ testKit .MustExec (fmt .Sprintf ("insert into t select a from t where a = %d" , i ))
307
+ }
308
+ testKit .MustExec ("analyze table t" )
309
+ h := dom .StatsHandle ()
310
+ require .Nil (t , h .DumpStatsDeltaToKV (true ))
311
+
312
+ table , err := dom .InfoSchema ().TableByName (context .Background (), pmodel .NewCIStr ("test" ), pmodel .NewCIStr ("t" ))
313
+ require .NoError (t , err )
314
+ statsTbl := h .GetTableStats (table .Meta ())
315
+
316
+ // Search for a found value == 10.0
317
+ sctx := mock .NewContext ()
318
+ col := statsTbl .GetCol (table .Meta ().Columns [0 ].ID )
319
+ count , err := cardinality .GetColumnRowCount (sctx , col , getRange (5 , 5 ), statsTbl .RealtimeCount , statsTbl .ModifyCount , false )
320
+ require .NoError (t , err )
321
+ require .Equal (t , 10.0 , count )
322
+
323
+ // Search for a not found value with zero modifyCount. Defaults to count == 1.0
324
+ count , err = cardinality .GetColumnRowCount (sctx , col , getRange (11 , 11 ), statsTbl .RealtimeCount , statsTbl .ModifyCount , false )
325
+ require .NoError (t , err )
326
+ require .Equal (t , 1.0 , count )
327
+
328
+ // Add another 200 rows to the table
329
+ testKit .MustExec ("insert into t select a+10 from t" )
330
+ testKit .MustExec ("insert into t select a+10 from t where a <= 10" )
331
+ require .Nil (t , h .DumpStatsDeltaToKV (true ))
332
+ require .Nil (t , h .Update (context .Background (), dom .InfoSchema ()))
333
+ statsTblnew := h .GetTableStats (table .Meta ())
334
+
335
+ // Search for a not found value based upon statistics - count should be >= 10 and <=40
336
+ count , err = cardinality .GetColumnRowCount (sctx , col , getRange (15 , 15 ), statsTblnew .RealtimeCount , statsTblnew .ModifyCount , false )
337
+ require .NoError (t , err )
338
+ require .Truef (t , count < 41 , "expected: between 10 to 40, got: %v" , count )
339
+ require .Truef (t , count > 9 , "expected: between 10 to 40, got: %v" , count )
340
+ }
341
+
292
342
func TestEstimationUniqueKeyEqualConds (t * testing.T ) {
293
343
store , dom := testkit .CreateMockStoreAndDomain (t )
294
344
testKit := testkit .NewTestKit (t , store )
0 commit comments