diff --git a/executor/analyzetest/analyze_test.go b/executor/analyzetest/analyze_test.go index 55f3ad9397be9..2d520703e07d5 100644 --- a/executor/analyzetest/analyze_test.go +++ b/executor/analyzetest/analyze_test.go @@ -2833,7 +2833,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, 4, len(tbl.Columns)) + require.Equal(t, 0, 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") diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 01befb0446908..df903670ab010 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -4473,32 +4473,36 @@ func (b *PlanBuilder) buildDataSource(ctx context.Context, tn *ast.TableName, as if tableInfo.GetPartitionInfo() != nil { // 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 !b.ctx.GetSessionVars().IsDynamicPartitionPruneEnabled() { + b.optFlag = b.optFlag | flagPartitionProcessor + } else { + if !b.ctx.GetSessionVars().StmtCtx.UseDynamicPruneMode { + b.optFlag = b.optFlag | flagPartitionProcessor + } else { + 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 { - b.ctx.GetSessionVars().StmtCtx.AppendWarning( - fmt.Errorf("disable dynamic pruning due to %s has no global stats", tableInfo.Name.String())) + if usePartitionProcessor { + 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())) + } } } - } else { - b.optFlag = b.optFlag | flagPartitionProcessor } pt := tbl.(table.PartitionedTable) // check partition by name.