diff --git a/executor/analyzetest/analyze_test.go b/executor/analyzetest/analyze_test.go index b9bfc153fa33c..9274fd62b423a 100644 --- a/executor/analyzetest/analyze_test.go +++ b/executor/analyzetest/analyze_test.go @@ -910,6 +910,8 @@ func TestAnalyzeSamplingWorkPanic(t *testing.T) { } func TestSmallTableAnalyzeV2(t *testing.T) { + failpoint.Enable("github.com/pingcap/tidb/planner/core/forceDynamicPrune", `return(true)`) + defer failpoint.Disable("github.com/pingcap/tidb/planner/core/forceDynamicPrune") store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -935,6 +937,20 @@ create table small_table_inject_pd_with_partition( "Note 1105 Analyze use auto adjusted sample rate 1.000000 for table test.small_table_inject_pd_with_partition's partition p1", "Note 1105 Analyze use auto adjusted sample rate 1.000000 for table test.small_table_inject_pd_with_partition's partition p2", )) + rows := [][]interface{}{ + {"global", "a"}, + {"p0", "a"}, + {"p1", "a"}, + {"p2", "a"}, + } + tk.MustQuery("show column_stats_usage where db_name = 'test' and table_name = 'small_table_inject_pd_with_partition' and last_analyzed_at is not null").Sort().CheckAt([]int{2, 3}, rows) + rows = [][]interface{}{ + {"global", "0", "3"}, + {"p0", "0", "1"}, + {"p1", "0", "1"}, + {"p2", "0", "1"}, + } + tk.MustQuery("show stats_meta where db_name = 'test' and table_name = 'small_table_inject_pd_with_partition'").Sort().CheckAt([]int{2, 4, 5}, rows) require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/calcSampleRateByStorageCount")) } diff --git a/executor/partition_table_test.go b/executor/partition_table_test.go index 00b2173421bff..1f70c5bc04630 100644 --- a/executor/partition_table_test.go +++ b/executor/partition_table_test.go @@ -33,6 +33,36 @@ import ( "github.com/stretchr/testify/require" ) +func TestSetPartitionPruneMode(t *testing.T) { + store := testkit.CreateMockStore(t) + + tkInit := testkit.NewTestKit(t, store) + tkInit.MustExec(`set @@session.tidb_partition_prune_mode = DEFAULT`) + tkInit.MustQuery("show warnings").Check(testkit.Rows()) + tkInit.MustExec(`set @@global.tidb_partition_prune_mode = DEFAULT`) + tkInit.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 Please analyze all partition tables again for consistency between partition and global stats")) + tk := testkit.NewTestKit(t, store) + tk.MustQuery("select @@global.tidb_partition_prune_mode").Check(testkit.Rows("dynamic")) + tk.MustQuery("select @@session.tidb_partition_prune_mode").Check(testkit.Rows("dynamic")) + tk.MustExec(`set @@session.tidb_partition_prune_mode = "static"`) + tk.MustQuery("show warnings").Check(testkit.Rows()) + tk.MustExec(`set @@global.tidb_partition_prune_mode = "static"`) + tk.MustQuery("show warnings").Check(testkit.Rows()) + tk2 := testkit.NewTestKit(t, store) + tk2.MustQuery("select @@session.tidb_partition_prune_mode").Check(testkit.Rows("static")) + tk2.MustQuery("show warnings").Check(testkit.Rows()) + tk2.MustQuery("select @@global.tidb_partition_prune_mode").Check(testkit.Rows("static")) + tk2.MustExec(`set @@session.tidb_partition_prune_mode = "dynamic"`) + tk2.MustQuery("show warnings").Sort().Check(testkit.Rows( + `Warning 1105 Please analyze all partition tables again for consistency between partition and global stats`, + `Warning 1105 Please avoid setting partition prune mode to dynamic at session level and set partition prune mode to dynamic at global level`)) + tk2.MustExec(`set @@global.tidb_partition_prune_mode = "dynamic"`) + tk2.MustQuery("show warnings").Check(testkit.Rows(`Warning 1105 Please analyze all partition tables again for consistency between partition and global stats`)) + tk3 := testkit.NewTestKit(t, store) + tk3.MustQuery("select @@global.tidb_partition_prune_mode").Check(testkit.Rows("dynamic")) + tk3.MustQuery("select @@session.tidb_partition_prune_mode").Check(testkit.Rows("dynamic")) +} + func TestFourReader(t *testing.T) { failpoint.Enable("github.com/pingcap/tidb/planner/core/forceDynamicPrune", `return(true)`) defer failpoint.Disable("github.com/pingcap/tidb/planner/core/forceDynamicPrune") diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index 4e005817636ab..e848acb4be481 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -1543,7 +1543,6 @@ func TestVariablesInfo(t *testing.T) { "tidb_enable_collect_execution_info ON OFF", // for test stability "tidb_enable_mutation_checker OFF ON", // for new installs "tidb_mem_oom_action CANCEL LOG", // always changed for tests - "tidb_partition_prune_mode static dynamic", // for new installs "tidb_row_format_version 1 2", // for new installs "tidb_txn_assertion_level OFF FAST", // for new installs "timestamp 0 123456789", // always dynamic diff --git a/planner/core/rule_result_reorder_test.go b/planner/core/rule_result_reorder_test.go index f2b678c6e758e..e6a248bee4071 100644 --- a/planner/core/rule_result_reorder_test.go +++ b/planner/core/rule_result_reorder_test.go @@ -179,7 +179,9 @@ func TestOrderedResultModeOnPartitionTable(t *testing.T) { partition p1 values less than (200), partition p2 values less than (300), partition p3 values less than (400))`) - tk.MustQuery("select @@tidb_partition_prune_mode").Check(testkit.Rows("static")) + tk.MustExec(`analyze table thash`) + tk.MustExec(`analyze table trange`) + tk.MustQuery("select @@tidb_partition_prune_mode").Check(testkit.Rows("dynamic")) runTestData(t, tk, "TestOrderedResultModeOnPartitionTable") } diff --git a/planner/core/testdata/ordered_result_mode_suite_out.json b/planner/core/testdata/ordered_result_mode_suite_out.json index aa185878595f6..1d724eae8eca5 100644 --- a/planner/core/testdata/ordered_result_mode_suite_out.json +++ b/planner/core/testdata/ordered_result_mode_suite_out.json @@ -439,44 +439,30 @@ "Cases": [ { "Plan": [ - "Sort_10 4.00 root test.thash.a", - "└─PartitionUnion_12 4.00 root ", - " ├─Batch_Point_Get_13 2.00 root table:thash handle:[1 200], keep order:false, desc:false", - " └─Batch_Point_Get_14 2.00 root table:thash handle:[1 200], keep order:false, desc:false" + "Sort_6 2.00 root test.thash.a", + "└─TableReader_9 2.00 root partition:p0,p1 data:TableRangeScan_8", + " └─TableRangeScan_8 2.00 cop[tikv] table:thash range:[1,1], [200,200], keep order:false, stats:pseudo" ] }, { "Plan": [ - "Sort_12 400.00 root test.thash.a", - "└─PartitionUnion_14 400.00 root ", - " ├─TableReader_16 100.00 root data:TableRangeScan_15", - " │ └─TableRangeScan_15 100.00 cop[tikv] table:thash, partition:p0 range:[50,150], keep order:false, stats:pseudo", - " ├─TableReader_18 100.00 root data:TableRangeScan_17", - " │ └─TableRangeScan_17 100.00 cop[tikv] table:thash, partition:p1 range:[50,150], keep order:false, stats:pseudo", - " ├─TableReader_20 100.00 root data:TableRangeScan_19", - " │ └─TableRangeScan_19 100.00 cop[tikv] table:thash, partition:p2 range:[50,150], keep order:false, stats:pseudo", - " └─TableReader_22 100.00 root data:TableRangeScan_21", - " └─TableRangeScan_21 100.00 cop[tikv] table:thash, partition:p3 range:[50,150], keep order:false, stats:pseudo" + "Sort_6 100.00 root test.thash.a", + "└─TableReader_9 100.00 root partition:all data:TableRangeScan_8", + " └─TableRangeScan_8 100.00 cop[tikv] table:thash range:[50,150], keep order:false, stats:pseudo" ] }, { "Plan": [ - "Sort_10 4.00 root test.trange.a", - "└─PartitionUnion_12 4.00 root ", - " ├─TableReader_14 2.00 root data:TableRangeScan_13", - " │ └─TableRangeScan_13 2.00 cop[tikv] table:trange, partition:p0 range:[1,1], [200,200], keep order:false, stats:pseudo", - " └─TableReader_16 2.00 root data:TableRangeScan_15", - " └─TableRangeScan_15 2.00 cop[tikv] table:trange, partition:p2 range:[1,1], [200,200], keep order:false, stats:pseudo" + "Sort_6 2.00 root test.trange.a", + "└─TableReader_9 2.00 root partition:p0,p2 data:TableRangeScan_8", + " └─TableRangeScan_8 2.00 cop[tikv] table:trange range:[1,1], [200,200], keep order:false, stats:pseudo" ] }, { "Plan": [ - "Sort_10 200.00 root test.trange.a", - "└─PartitionUnion_12 200.00 root ", - " ├─TableReader_14 100.00 root data:TableRangeScan_13", - " │ └─TableRangeScan_13 100.00 cop[tikv] table:trange, partition:p0 range:[50,150], keep order:false, stats:pseudo", - " └─TableReader_16 100.00 root data:TableRangeScan_15", - " └─TableRangeScan_15 100.00 cop[tikv] table:trange, partition:p1 range:[50,150], keep order:false, stats:pseudo" + "Sort_6 100.00 root test.trange.a", + "└─TableReader_9 100.00 root partition:p0,p1 data:TableRangeScan_8", + " └─TableRangeScan_8 100.00 cop[tikv] table:trange range:[50,150], keep order:false, stats:pseudo" ] } ] diff --git a/session/bootstrap.go b/session/bootstrap.go index d9422cd52aadf..5518fbde9be9a 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -2059,10 +2059,6 @@ func doDMLWorks(s Session) { if config.GetGlobalConfig().Store == "tikv" { vVal = variable.On } - case variable.TiDBPartitionPruneMode: - if inTestSuite() || config.CheckTableBeforeDrop { - vVal = string(variable.Dynamic) - } case variable.TiDBMemOOMAction: if inTestSuite() { vVal = variable.OOMActionLog diff --git a/sessionctx/variable/sysvar_test.go b/sessionctx/variable/sysvar_test.go index 1ca4eca74e029..b3e94767f792e 100644 --- a/sessionctx/variable/sysvar_test.go +++ b/sessionctx/variable/sysvar_test.go @@ -656,6 +656,17 @@ func TestDefaultMemoryDebugModeValue(t *testing.T) { require.Equal(t, val, "0") } +func TestDefaultPartitionPruneMode(t *testing.T) { + vars := NewSessionVars() + mock := NewMockGlobalAccessor4Tests() + mock.SessionVars = vars + vars.GlobalVarsAccessor = mock + val, err := vars.GetSessionOrGlobalSystemVar(TiDBPartitionPruneMode) + require.NoError(t, err) + require.Equal(t, "dynamic", val) + require.Equal(t, "dynamic", DefTiDBPartitionPruneMode) +} + func TestSetTIDBFastDDL(t *testing.T) { vars := NewSessionVars() mock := NewMockGlobalAccessor4Tests() diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index ffffc0e5f0b33..8067f4ffb6ad5 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -966,7 +966,7 @@ const ( DefTiDBEnableTelemetry = true DefTiDBEnableParallelApply = false DefTiDBEnableAmendPessimisticTxn = false - DefTiDBPartitionPruneMode = "static" + DefTiDBPartitionPruneMode = "dynamic" DefTiDBEnableRateLimitAction = true DefTiDBEnableAsyncCommit = false DefTiDBEnable1PC = false