@@ -1578,3 +1578,56 @@ func TestRiskEqSkewRatio(t *testing.T) {
1578
1578
// Reset global variable to default.
1579
1579
testKit .MustExec ("set @@global.tidb_opt_risk_eq_skew_ratio = default" )
1580
1580
}
1581
+
1582
+ func TestRiskRangeSkewRatio (t * testing.T ) {
1583
+ store , dom := testkit .CreateMockStoreAndDomain (t )
1584
+ testKit := testkit .NewTestKit (t , store )
1585
+
1586
+ testKit .MustExec ("use test" )
1587
+ testKit .MustExec ("drop table if exists t" )
1588
+ testKit .MustExec ("create table t(a int, index idx(a))" )
1589
+ is := dom .InfoSchema ()
1590
+ tb , err := is .TableByName (context .Background (), ast .NewCIStr ("test" ), ast .NewCIStr ("t" ))
1591
+ require .NoError (t , err )
1592
+ tblInfo := tb .Meta ()
1593
+
1594
+ // Insert enough rows to produce skewed distribution.
1595
+ testKit .MustExec ("insert into t values (1), (1), (1), (1), (2), (2), (3), (4), (5), (5)" )
1596
+ // Do not collect topn and only collect 1 bucket to ensure later queries will be within a bucket.
1597
+ testKit .MustExec (`analyze table t with 0 topn, 1 buckets` )
1598
+ h := dom .StatsHandle ()
1599
+ require .NoError (t , h .DumpStatsDeltaToKV (true ))
1600
+
1601
+ sctx := testKit .Session ()
1602
+ idxID := tblInfo .Indices [0 ].ID
1603
+ statsTbl := h .GetTableStats (tb .Meta ())
1604
+ // Search for the range from 2 to 3, since there is only one bucket it will be a query within
1605
+ // a bucket.
1606
+ testKit .MustExec ("set @@session.tidb_opt_risk_range_skew_ratio = 0" )
1607
+ count , _ , err := cardinality .GetRowCountByIndexRanges (sctx .GetPlanCtx (), & statsTbl .HistColl , idxID , getRange (2 , 3 ))
1608
+ require .NoError (t , err )
1609
+ testKit .MustExec ("set @@session.tidb_opt_risk_range_skew_ratio = 0.5" )
1610
+ count2 , _ , err2 := cardinality .GetRowCountByIndexRanges (sctx .GetPlanCtx (), & statsTbl .HistColl , idxID , getRange (2 , 3 ))
1611
+ require .NoError (t , err2 )
1612
+ // Result of count2 should be larger than count because the risk ratio is higher
1613
+ require .Less (t , count , count2 )
1614
+ testKit .MustExec ("set @@session.tidb_opt_risk_range_skew_ratio = 1" )
1615
+ count3 , _ , err3 := cardinality .GetRowCountByIndexRanges (sctx .GetPlanCtx (), & statsTbl .HistColl , idxID , getRange (2 , 3 ))
1616
+ require .NoError (t , err3 )
1617
+ // Result of count3 should be larger because the risk ratio is higher
1618
+ require .Less (t , count2 , count3 )
1619
+ // Repeat the prior test by setting the global variable instead of the session variable. This should have no effect.
1620
+ testKit .MustExec ("set @@global.tidb_opt_risk_range_skew_ratio = 0.5" )
1621
+ count4 , _ , err4 := cardinality .GetRowCountByIndexRanges (sctx .GetPlanCtx (), & statsTbl .HistColl , idxID , getRange (2 , 3 ))
1622
+ require .NoError (t , err4 )
1623
+ require .Less (t , count2 , count4 )
1624
+ // Repeat the prior test by setting the session variable to the default. Count4 should inherit the global
1625
+ // variable and be less than count3.
1626
+ testKit .MustExec ("set @@session.tidb_opt_risk_range_skew_ratio = default" )
1627
+ count4 , _ , err4 = cardinality .GetRowCountByIndexRanges (sctx .GetPlanCtx (), & statsTbl .HistColl , idxID , getRange (2 , 3 ))
1628
+ require .NoError (t , err4 )
1629
+ require .Less (t , count4 , count3 )
1630
+ // Reset global variable to default.
1631
+ testKit .MustExec ("set @@global.tidb_opt_risk_range_skew_ratio = default" )
1632
+ testKit .MustExec ("set @@session.tidb_opt_risk_range_skew_ratio = default" )
1633
+ }
0 commit comments