Skip to content

Commit

Permalink
planner: update some UTs to cost model2 (#38893)
Browse files Browse the repository at this point in the history
ref #35240
  • Loading branch information
qw4990 authored Nov 5, 2022
1 parent 114d8c1 commit d323b8d
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 161 deletions.
10 changes: 10 additions & 0 deletions planner/core/enforce_mpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestSetVariables(t *testing.T) {
tk := testkit.NewTestKit(t, store)

// test value limit of tidb_opt_tiflash_concurrency_factor
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("set @@tidb_opt_tiflash_concurrency_factor = 0")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_opt_tiflash_concurrency_factor value: '0'"))
tk.MustQuery(`select @@tidb_opt_tiflash_concurrency_factor`).Check(testkit.Rows("1"))
Expand All @@ -52,6 +53,7 @@ func TestRowSizeInMPP(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a varchar(10), b varchar(20), c varchar(256))")
tk.MustExec("insert into t values (space(10), space(20), space(256))")
Expand Down Expand Up @@ -89,6 +91,7 @@ func TestEnforceMPP(t *testing.T) {

// test query
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int)")
tk.MustExec("create index idx on t(a)")
Expand Down Expand Up @@ -154,6 +157,7 @@ func TestEnforceMPPWarning1(t *testing.T) {

// test query
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int as (a+1), c enum('xx', 'yy'), d bit(1))")
tk.MustExec("create index idx on t(a)")
Expand Down Expand Up @@ -224,6 +228,7 @@ func TestEnforceMPPWarning2(t *testing.T) {

// test query
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE t (a int, b char(20)) PARTITION BY HASH(a)")

Expand Down Expand Up @@ -275,6 +280,7 @@ func TestEnforceMPPWarning3(t *testing.T) {

// test query
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE t (a int, b char(20))")

Expand Down Expand Up @@ -335,6 +341,7 @@ func TestEnforceMPPWarning4(t *testing.T) {

// test table
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE t(a int primary key)")
tk.MustExec("drop table if exists s")
Expand Down Expand Up @@ -388,6 +395,7 @@ func TestMPP2PhaseAggPushDown(t *testing.T) {

// test table
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists c")
tk.MustExec("drop table if exists o")
tk.MustExec("create table c(c_id bigint)")
Expand Down Expand Up @@ -441,6 +449,7 @@ func TestMPPSkewedGroupDistinctRewrite(t *testing.T) {

// test table
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b bigint not null, c bigint, d date, e varchar(20))")

Expand Down Expand Up @@ -492,6 +501,7 @@ func TestMPPSingleDistinct3Stage(t *testing.T) {

// test table
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b bigint not null, c bigint, d date, e varchar(20) collate utf8mb4_general_ci)")

Expand Down
15 changes: 15 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestShowSubquery(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a varchar(10), b int, c int)")
tk.MustQuery("show columns from t where true").Check(testkit.Rows(
Expand Down Expand Up @@ -123,6 +124,7 @@ func TestAggPushDownLeftJoin(t *testing.T) {
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists customer")
tk.MustExec("create table customer (C_CUSTKEY bigint(20) NOT NULL, C_NAME varchar(25) NOT NULL, " +
"C_ADDRESS varchar(25) NOT NULL, PRIMARY KEY (`C_CUSTKEY`) /*T![clustered_index] CLUSTERED */)")
Expand Down Expand Up @@ -165,6 +167,7 @@ func TestPushLimitDownIndexLookUpReader(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("set @@session.tidb_executor_concurrency = 4;")
tk.MustExec("set @@session.tidb_hash_join_concurrency = 5;")
tk.MustExec("set @@session.tidb_distsql_scan_concurrency = 15;")
Expand Down Expand Up @@ -195,6 +198,7 @@ func TestAggColumnPrune(t *testing.T) {
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values(1),(2)")
Expand All @@ -219,6 +223,7 @@ func TestIsFromUnixtimeNullRejective(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a bigint, b bigint);`)
var input []string
Expand Down Expand Up @@ -376,6 +381,7 @@ func TestSimplifyOuterJoinWithCast(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int not null, b datetime default null)")
Expand Down Expand Up @@ -1567,6 +1573,7 @@ func TestMaxMinEliminate(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int primary key)")
Expand Down Expand Up @@ -1606,6 +1613,7 @@ func TestIndexJoinUniqueCompositeIndex(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2")
tk.Session().GetSessionVars().EnableClusteredIndex = variable.ClusteredIndexDefModeIntOnly
Expand Down Expand Up @@ -1764,6 +1772,7 @@ func TestSubqueryWithTopN(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int)")
Expand Down Expand Up @@ -2127,6 +2136,7 @@ func TestIndexJoinInnerIndexNDV(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int not null, b int not null, c int not null)")
tk.MustExec("create table t2(a int not null, b int not null, c int not null, index idx1(a,b), index idx2(c))")
Expand Down Expand Up @@ -4283,6 +4293,7 @@ func TestPushDownProjectionForTiFlashCoprocessor(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b real, i int, id int, value decimal(6,3), name char(128), d decimal(6,3), s char(128), t datetime, c bigint as ((a+1)) virtual, e real as ((b+a)))")
tk.MustExec("analyze table t")
Expand Down Expand Up @@ -4323,6 +4334,7 @@ func TestPushDownProjectionForTiFlash(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (id int, value decimal(6,3), name char(128))")
tk.MustExec("analyze table t")
Expand Down Expand Up @@ -4363,6 +4375,7 @@ func TestPushDownSelectionForMPP(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (id int, value decimal(6,3), name char(128))")
tk.MustExec("analyze table t")
Expand Down Expand Up @@ -4405,6 +4418,7 @@ func TestPushDownProjectionForMPP(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (id int, value decimal(6,3), name char(128))")
tk.MustExec("analyze table t")
Expand Down Expand Up @@ -4447,6 +4461,7 @@ func TestReorderSimplifiedOuterJoins(t *testing.T) {
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("drop table if exists t1,t2,t3")
tk.MustExec("create table t1 (pk char(32) primary key nonclustered, col1 char(32), col2 varchar(40), col3 char(32), key (col1), key (col3), key (col2,col3), key (col1,col3))")
tk.MustExec("create table t2 (pk char(32) primary key nonclustered, col1 varchar(100))")
Expand Down
1 change: 1 addition & 0 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ func TestMPPHints(t *testing.T) {
store := testkit.CreateMockStore(t, withMockTiFlash(2))
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("create table t (a int, b int, c int)")
tk.MustExec("alter table t set tiflash replica 1")
tk.MustExec("set @@session.tidb_allow_mpp=ON")
Expand Down
10 changes: 6 additions & 4 deletions planner/core/plan_cost_ver2.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ func netCostVer2(option *PlanCostOption, rows, rowSize float64, netFactor costVe
func filterCostVer2(option *PlanCostOption, rows float64, filters []expression.Expression, cpuFactor costVer2Factor) costVer2 {
numFuncs := numFunctions(filters)
return newCostVer2(option, cpuFactor,
rows*float64(numFuncs)*cpuFactor.Value,
rows*numFuncs*cpuFactor.Value,
"cpu(%v*filters(%v)*%v)", rows, numFuncs, cpuFactor)
}

Expand All @@ -727,15 +727,17 @@ func aggCostVer2(option *PlanCostOption, rows float64, aggFuncs []*aggregation.A
func groupCostVer2(option *PlanCostOption, rows float64, groupItems []expression.Expression, cpuFactor costVer2Factor) costVer2 {
numFuncs := numFunctions(groupItems)
return newCostVer2(option, cpuFactor,
rows*float64(numFuncs)*cpuFactor.Value,
rows*numFuncs*cpuFactor.Value,
"group(%v*cols(%v)*%v)", rows, numFuncs, cpuFactor)
}

func numFunctions(exprs []expression.Expression) int {
num := 0
func numFunctions(exprs []expression.Expression) float64 {
num := 0.0
for _, e := range exprs {
if _, ok := e.(*expression.ScalarFunction); ok {
num++
} else { // Column and Constant
num += 0.01 // an empirical value
}
}
return num
Expand Down
Loading

0 comments on commit d323b8d

Please sign in to comment.