Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: fix panic during starting tidb-server if creating global binding for partition table #40402

Merged
merged 7 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions planner/core/integration_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1619,3 +1619,39 @@ func TestPartitionRangeColumnPruning(t *testing.T) {
tk.MustQuery(`select * from t1 where a = 'a' AND c = 'd'`).Check(testkit.Rows("a <nil> d"))
tk.MustExec(`drop table t1`)
}

func TestPartitionProcessorWithUninitializedTable(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(" create table q1(a int, b int, key (a)) partition by range (a) (partition p0 values less than (10), partition p1 values less than (20));")
tk.MustExec(" create table q2(a int, b int, key (a)) partition by range (a) (partition p0 values less than (10), partition p1 values less than (20));")

rows := [][]interface{}{
{"HashJoin"},
{"├─PartitionUnion(Build)"},
{"│ ├─TableReader"},
{"│ │ └─TableFullScan"},
{"│ └─TableReader"},
{"│ └─TableFullScan"},
{"└─PartitionUnion(Probe)"},
{" ├─TableReader"},
{" │ └─TableFullScan"},
{" └─TableReader"},
{" └─TableFullScan"},
}
tk.MustQuery("explain format=brief select * from q1,q2").CheckAt([]int{0}, rows)

tk.MustExec("analyze table q1")
tk.MustQuery("explain format=brief select * from q1,q2").CheckAt([]int{0}, rows)

tk.MustExec("analyze table q2")
rows = [][]interface{}{
{"HashJoin"},
{"├─TableReader(Build)"},
{"│ └─TableFullScan"},
{"└─TableReader(Probe)"},
{" └─TableFullScan"},
}
tk.MustQuery("explain format=brief select * from q1,q2").CheckAt([]int{0}, rows)
}
43 changes: 24 additions & 19 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4471,30 +4471,35 @@ func (b *PlanBuilder) buildDataSource(ctx context.Context, tn *ast.TableName, as
}

if tableInfo.GetPartitionInfo() != nil {
h := domain.GetDomain(b.ctx).StatsHandle()
tblStats := h.GetTableStats(tableInfo)
isDynamicEnabled := b.ctx.GetSessionVars().IsDynamicPartitionPruneEnabled()
globalStatsReady := tblStats.IsInitialized()
// If dynamic partition prune isn't enabled or global stats is not ready, we won't enable dynamic prune mode in query
usePartitionProcessor := !isDynamicEnabled || !globalStatsReady

failpoint.Inject("forceDynamicPrune", func(val failpoint.Value) {
if val.(bool) {
// If `UseDynamicPruneMode` already been false, then we don't need to check whether execute `flagPartitionProcessor`
// otherwise we need to check global stats initialized for each partition table
if b.ctx.GetSessionVars().StmtCtx.UseDynamicPruneMode {
h := domain.GetDomain(b.ctx).StatsHandle()
tblStats := h.GetTableStats(tableInfo)
isDynamicEnabled := b.ctx.GetSessionVars().IsDynamicPartitionPruneEnabled()
globalStatsReady := tblStats.IsInitialized()
// If dynamic partition prune isn't enabled or global stats is not ready, we won't enable dynamic prune mode in query
usePartitionProcessor := !isDynamicEnabled || !globalStatsReady

failpoint.Inject("forceDynamicPrune", func(val failpoint.Value) {
if val.(bool) {
if isDynamicEnabled {
usePartitionProcessor = false
}
}
})

if usePartitionProcessor {
b.optFlag = b.optFlag | flagPartitionProcessor
b.ctx.GetSessionVars().StmtCtx.UseDynamicPruneMode = false
if isDynamicEnabled {
usePartitionProcessor = false
b.ctx.GetSessionVars().StmtCtx.AppendWarning(
fmt.Errorf("disable dynamic pruning due to %s has no global stats", tableInfo.Name.String()))
}
}
})

if usePartitionProcessor {
} else {
b.optFlag = b.optFlag | flagPartitionProcessor
b.ctx.GetSessionVars().StmtCtx.UseDynamicPruneMode = false
if isDynamicEnabled {
b.ctx.GetSessionVars().StmtCtx.AppendWarning(
fmt.Errorf("disable dynamic pruning due to %s has no global stats", tableInfo.Name.String()))
}
}

pt := tbl.(table.PartitionedTable)
// check partition by name.
if len(tn.PartitionNames) > 0 {
Expand Down
7 changes: 6 additions & 1 deletion statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,13 @@ func (h *Handle) GetTableStats(tblInfo *model.TableInfo, opts ...TableStatsOpt)

// GetPartitionStats retrieves the partition stats from cache.
func (h *Handle) GetPartitionStats(tblInfo *model.TableInfo, pid int64, opts ...TableStatsOpt) *statistics.Table {
statsCache := h.statsCache.Load().(statsCache)
var tbl *statistics.Table
if h == nil {
tbl = statistics.PseudoTable(tblInfo)
tbl.PhysicalID = pid
return tbl
}
statsCache := h.statsCache.Load().(statsCache)
var ok bool
option := &tableStatsOption{}
for _, opt := range opts {
Expand Down