Skip to content

Commit

Permalink
planner: fallback to static plan for partition table if having no glo…
Browse files Browse the repository at this point in the history
…bal stats in dynamic prune mode (#37573)

ref #37535
  • Loading branch information
Yisaer authored Sep 8, 2022
1 parent 5f0c580 commit 7eb7ca9
Show file tree
Hide file tree
Showing 34 changed files with 229 additions and 35 deletions.
1 change: 1 addition & 0 deletions cmd/explaintest/r/generated_columns.result
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ PARTITION p3 VALUES LESS THAN (4),
PARTITION p4 VALUES LESS THAN (5),
PARTITION p5 VALUES LESS THAN (6),
PARTITION max VALUES LESS THAN MAXVALUE);
analyze table sgc3;
EXPLAIN format = 'brief' SELECT * FROM sgc3 WHERE a <= 1;
id estRows task access object operator info
TableReader 3323.33 root partition:p0,p1 data:Selection
Expand Down
15 changes: 8 additions & 7 deletions cmd/explaintest/r/select.result
Original file line number Diff line number Diff line change
Expand Up @@ -381,19 +381,20 @@ PartitionUnion_8 20000.00 root
└─TableReader_12 10000.00 root data:TableFullScan_11
└─TableFullScan_11 10000.00 cop[tikv] table:th, partition:p2 keep order:false, stats:pseudo
set @@session.tidb_partition_prune_mode = 'dynamic';
analyze table th;
desc select * from th where a=-2;
id estRows task access object operator info
TableReader_7 10.00 root partition:p2 data:Selection_6
└─Selection_6 10.00 cop[tikv] eq(test.th.a, -2)
└─TableFullScan_5 10000.00 cop[tikv] table:th keep order:false, stats:pseudo
TableReader_7 1.00 root partition:p2 data:Selection_6
└─Selection_6 1.00 cop[tikv] eq(test.th.a, -2)
└─TableFullScan_5 17.00 cop[tikv] table:th keep order:false
desc select * from th;
id estRows task access object operator info
TableReader_5 10000.00 root partition:all data:TableFullScan_4
└─TableFullScan_4 10000.00 cop[tikv] table:th keep order:false, stats:pseudo
TableReader_5 17.00 root partition:all data:TableFullScan_4
└─TableFullScan_4 17.00 cop[tikv] table:th keep order:false
desc select * from th partition (p2,p1);
id estRows task access object operator info
TableReader_5 10000.00 root partition:p1,p2 data:TableFullScan_4
└─TableFullScan_4 10000.00 cop[tikv] table:th keep order:false, stats:pseudo
TableReader_5 17.00 root partition:p1,p2 data:TableFullScan_4
└─TableFullScan_4 17.00 cop[tikv] table:th keep order:false
set @@session.tidb_partition_prune_mode = DEFAULT;
drop table if exists t;
create table t(a int, b int);
Expand Down
2 changes: 2 additions & 0 deletions cmd/explaintest/t/generated_columns.test
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ PARTITION p4 VALUES LESS THAN (5),
PARTITION p5 VALUES LESS THAN (6),
PARTITION max VALUES LESS THAN MAXVALUE);

analyze table sgc3;

EXPLAIN format = 'brief' SELECT * FROM sgc3 WHERE a <= 1;
EXPLAIN format = 'brief' SELECT * FROM sgc3 WHERE a < 7;

Expand Down
1 change: 1 addition & 0 deletions cmd/explaintest/t/select.test
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ desc select * from th where a=-2;
desc select * from th;
desc select * from th partition (p2,p1);
set @@session.tidb_partition_prune_mode = 'dynamic';
analyze table th;
desc select * from th where a=-2;
desc select * from th;
desc select * from th partition (p2,p1);
Expand Down
5 changes: 5 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,8 @@ create table log_message_1 (
}

func TestPartitionRangeColumnsCollate(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)
tk.MustExec("create schema PartitionRangeColumnsCollate")
Expand Down Expand Up @@ -3569,6 +3571,9 @@ func TestPartitionListWithTimeType(t *testing.T) {
}

func TestPartitionListWithNewCollation(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)
tk.MustExec("use test;")
Expand Down
4 changes: 3 additions & 1 deletion executor/analyzetest/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2833,6 +2833,8 @@ PARTITION BY RANGE ( a ) (
}

func TestAnalyzePartitionStaticToDynamic(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, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
originalVal := tk.MustQuery("select @@tidb_persist_analyze_options").Rows()[0][0].(string)
Expand Down Expand Up @@ -2901,7 +2903,7 @@ PARTITION BY RANGE ( a ) (
tk.MustQuery("select * from t where a > 1 and b > 1 and c > 1 and d > 1")
require.NoError(t, h.LoadNeededHistograms())
tbl := h.GetTableStats(tableInfo)
require.Equal(t, 0, len(tbl.Columns))
require.Equal(t, 4, len(tbl.Columns))

// ignore both p0's 3 buckets, persisted-partition-options' 1 bucket, just use table-level 2 buckets
tk.MustExec("analyze table t partition p0")
Expand Down
18 changes: 9 additions & 9 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3300,7 +3300,7 @@ func (b *executorBuilder) buildTableReader(v *plannercore.PhysicalTableReader) E
sctx := b.ctx.GetSessionVars().StmtCtx
sctx.TableIDs = append(sctx.TableIDs, ts.Table.ID)

if !b.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if !b.ctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
return ret
}
// When isPartition is set, it means the union rewriting is done, so a partition reader is preferred.
Expand Down Expand Up @@ -3504,7 +3504,7 @@ func buildNoRangeIndexReader(b *executorBuilder, v *plannercore.PhysicalIndexRea
e.feedback = statistics.NewQueryFeedback(0, nil, 0, is.Desc)
} else {
tblID := e.physicalTableID
if b.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if b.ctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
tblID = e.table.Meta().ID
}
e.feedback = statistics.NewQueryFeedback(tblID, is.Hist, int64(is.StatsCount()), is.Desc)
Expand Down Expand Up @@ -3547,7 +3547,7 @@ func (b *executorBuilder) buildIndexReader(v *plannercore.PhysicalIndexReader) E
sctx := b.ctx.GetSessionVars().StmtCtx
sctx.IndexNames = append(sctx.IndexNames, is.Table.Name.O+":"+is.Index.Name.O)

if !b.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if !b.ctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
return ret
}
// When isPartition is set, it means the union rewriting is done, so a partition reader is preferred.
Expand Down Expand Up @@ -3723,7 +3723,7 @@ func (b *executorBuilder) buildIndexLookUpReader(v *plannercore.PhysicalIndexLoo
sctx.IndexNames = append(sctx.IndexNames, is.Table.Name.O+":"+is.Index.Name.O)
sctx.TableIDs = append(sctx.TableIDs, ts.Table.ID)

if !b.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if !b.ctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
return ret
}

Expand Down Expand Up @@ -3860,7 +3860,7 @@ func (b *executorBuilder) buildIndexMergeReader(v *plannercore.PhysicalIndexMerg
sctx.TableIDs = append(sctx.TableIDs, ts.Table.ID)
executorCounterIndexMergeReaderExecutor.Inc()

if !b.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if !b.ctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
return ret
}

Expand Down Expand Up @@ -3978,7 +3978,7 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte
return nil, err
}
tbInfo := e.table.Meta()
if tbInfo.GetPartitionInfo() == nil || !builder.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if tbInfo.GetPartitionInfo() == nil || !builder.ctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
if v.IsCommonHandle {
kvRanges, err := buildKvRangesForIndexJoin(e.ctx, getPhysicalTableID(e.table), -1, lookUpContents, indexRanges, keyOff2IdxOff, cwc, memTracker, interruptSignal)
if err != nil {
Expand Down Expand Up @@ -4226,7 +4226,7 @@ func (builder *dataReaderBuilder) buildIndexReaderForIndexJoin(ctx context.Conte
return nil, err
}
tbInfo := e.table.Meta()
if tbInfo.GetPartitionInfo() == nil || !builder.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if tbInfo.GetPartitionInfo() == nil || !builder.ctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
kvRanges, err := buildKvRangesForIndexJoin(e.ctx, e.physicalTableID, e.index.ID, lookUpContents, indexRanges, keyOff2IdxOff, cwc, memoryTracker, interruptSignal)
if err != nil {
return nil, err
Expand Down Expand Up @@ -4273,7 +4273,7 @@ func (builder *dataReaderBuilder) buildIndexLookUpReaderForIndexJoin(ctx context
}

tbInfo := e.table.Meta()
if tbInfo.GetPartitionInfo() == nil || !builder.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if tbInfo.GetPartitionInfo() == nil || !builder.ctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
e.kvRanges, err = buildKvRangesForIndexJoin(e.ctx, getPhysicalTableID(e.table), e.index.ID, lookUpContents, indexRanges, keyOff2IdxOff, cwc, memTracker, interruptSignal)
if err != nil {
return nil, err
Expand Down Expand Up @@ -4859,7 +4859,7 @@ func getPhysicalTableID(t table.Table) int64 {
}

func getFeedbackStatsTableID(ctx sessionctx.Context, t table.Table) int64 {
if p, ok := t.(table.PhysicalTable); ok && !ctx.GetSessionVars().UseDynamicPartitionPrune() {
if p, ok := t.(table.PhysicalTable); ok && !ctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
return p.GetPhysicalID()
}
return t.Meta().ID
Expand Down
2 changes: 2 additions & 0 deletions executor/distsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ func TestPushLimitDownIndexLookUpReader(t *testing.T) {
}

func TestPartitionTableIndexLookUpReader(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)

Expand Down
7 changes: 7 additions & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,13 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
sc.OptimizerCETrace = nil
sc.StatsLoadStatus = make(map[model.TableItemID]string)
sc.IsSyncStatsFailed = false
// Firstly we assume that UseDynamicPruneMode can be enabled according session variable, then we will check other conditions
// in PlanBuilder.buildDataSource
if ctx.GetSessionVars().IsDynamicPartitionPruneEnabled() {
sc.UseDynamicPruneMode = true
} else {
sc.UseDynamicPruneMode = false
}

sc.SysdateIsNow = ctx.GetSessionVars().SysdateIsNow

Expand Down
2 changes: 2 additions & 0 deletions executor/executor_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ func TestFix31537(t *testing.T) {
}

func TestIssue30382(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)
tk.MustExec("use test")
Expand Down
3 changes: 3 additions & 0 deletions executor/index_lookup_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"
"testing"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -391,6 +392,8 @@ func TestIssue24547(t *testing.T) {
}

func TestIssue27138(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)
Expand Down
26 changes: 26 additions & 0 deletions executor/partition_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/infoschema"
Expand All @@ -33,6 +34,8 @@ import (
)

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")
store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)
Expand Down Expand Up @@ -529,6 +532,8 @@ func TestView(t *testing.T) {
}

func TestDirectReadingwithIndexJoin(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)
Expand Down Expand Up @@ -641,6 +646,8 @@ func TestDirectReadingwithIndexJoin(t *testing.T) {
}

func TestDynamicPruningUnderIndexJoin(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)
Expand Down Expand Up @@ -943,6 +950,8 @@ func TestGlobalStatsAndSQLBinding(t *testing.T) {
}

func TestPartitionTableWithDifferentJoin(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)
Expand Down Expand Up @@ -1731,6 +1740,8 @@ func TestDynamicPruneModeWithExpression(t *testing.T) {
}

func TestAddDropPartitions(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)
Expand Down Expand Up @@ -1764,6 +1775,8 @@ func TestAddDropPartitions(t *testing.T) {
}

func TestMPPQueryExplainInfo(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)
Expand Down Expand Up @@ -1793,6 +1806,8 @@ func TestMPPQueryExplainInfo(t *testing.T) {
}

func TestPartitionPruningInTransaction(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)
Expand Down Expand Up @@ -2026,6 +2041,8 @@ func TestSubqueries(t *testing.T) {
}

func TestSplitRegion(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)
Expand Down Expand Up @@ -2062,6 +2079,9 @@ func TestSplitRegion(t *testing.T) {
}

func TestParallelApply(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)
Expand Down Expand Up @@ -2569,6 +2589,8 @@ func TestDirectReadingWithAgg(t *testing.T) {
}

func TestDynamicModeByDefault(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)
Expand Down Expand Up @@ -2764,6 +2786,8 @@ func TestIssue25309(t *testing.T) {
}

func TestGlobalIndexScan(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)
Expand All @@ -2784,6 +2808,8 @@ partition p2 values less than (10))`)
}

func TestGlobalIndexDoubleRead(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)
Expand Down
10 changes: 5 additions & 5 deletions executor/show_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (e *ShowExec) fetchShowStatsMeta() error {
for _, db := range dbs {
for _, tbl := range db.Tables {
pi := tbl.GetPartitionInfo()
if pi == nil || e.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if pi == nil || e.ctx.GetSessionVars().IsDynamicPartitionPruneEnabled() {
partitionName := ""
if pi != nil {
partitionName = "global"
Expand Down Expand Up @@ -150,7 +150,7 @@ func (e *ShowExec) fetchShowStatsHistogram() error {
for _, db := range dbs {
for _, tbl := range db.Tables {
pi := tbl.GetPartitionInfo()
if pi == nil || e.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if pi == nil || e.ctx.GetSessionVars().IsDynamicPartitionPruneEnabled() {
partitionName := ""
if pi != nil {
partitionName = "global"
Expand Down Expand Up @@ -224,7 +224,7 @@ func (e *ShowExec) fetchShowStatsBuckets() error {
for _, db := range dbs {
for _, tbl := range db.Tables {
pi := tbl.GetPartitionInfo()
if pi == nil || e.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if pi == nil || e.ctx.GetSessionVars().IsDynamicPartitionPruneEnabled() {
partitionName := ""
if pi != nil {
partitionName = "global"
Expand Down Expand Up @@ -283,7 +283,7 @@ func (e *ShowExec) fetchShowStatsTopN() error {
for _, db := range dbs {
for _, tbl := range db.Tables {
pi := tbl.GetPartitionInfo()
if pi == nil || e.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if pi == nil || e.ctx.GetSessionVars().IsDynamicPartitionPruneEnabled() {
partitionName := ""
if pi != nil {
partitionName = "global"
Expand Down Expand Up @@ -415,7 +415,7 @@ func (e *ShowExec) fetchShowStatsHealthy() {
for _, db := range dbs {
for _, tbl := range db.Tables {
pi := tbl.GetPartitionInfo()
if pi == nil || e.ctx.GetSessionVars().UseDynamicPartitionPrune() {
if pi == nil || e.ctx.GetSessionVars().IsDynamicPartitionPruneEnabled() {
partitionName := ""
if pi != nil {
partitionName = "global"
Expand Down
2 changes: 2 additions & 0 deletions executor/tiflashtest/tiflash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,8 @@ func TestTiFlashPartitionTableReader(t *testing.T) {
}

func TestPartitionTable(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, withMockTiFlash(2))
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand Down
Loading

0 comments on commit 7eb7ca9

Please sign in to comment.