Skip to content

Commit 564ae2e

Browse files
committed
planner,executor,variable: warn and deprecate analyze v1 usage
1 parent c6bccde commit 564ae2e

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

pkg/executor/test/analyzetest/analyze_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,21 @@ func TestAnalyzeIndex(t *testing.T) {
544544
require.Greater(t, len(tk.MustQuery("show stats_buckets where table_name = 't1' and column_name = 'k' and is_index = 1").Rows()), 1)
545545
}
546546

547+
func TestAnalyzeV1DeprecationWarning(t *testing.T) {
548+
store := testkit.CreateMockStore(t)
549+
550+
tk := testkit.NewTestKit(t, store)
551+
tk.MustExec("use test")
552+
tk.MustExec("drop table if exists t")
553+
tk.MustExec("create table t (a int, index idx(a))")
554+
tk.MustExec("insert into t values (1), (2), (3)")
555+
tk.MustExec("set @@tidb_analyze_version=1")
556+
tk.MustExec("analyze table t")
557+
tk.MustQuery("show warnings").Check(testkit.Rows(
558+
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release.",
559+
))
560+
}
561+
547562
func TestIssue20874(t *testing.T) {
548563
store := testkit.CreateMockStore(t)
549564

@@ -2598,10 +2613,9 @@ PARTITION BY RANGE ( a ) (
25982613

25992614
// analyze partition with index and with options are allowed under dynamic V1
26002615
tk.MustExec("analyze table t partition p0 with 1 topn, 3 buckets")
2601-
rows := tk.MustQuery("show warnings").Rows()
2602-
require.Len(t, rows, 0)
2616+
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release."))
26032617
tk.MustExec("analyze table t partition p1 with 1 topn, 3 buckets")
2604-
tk.MustQuery("show warnings").Sort().Check(testkit.Rows())
2618+
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release."))
26052619
tk.MustQuery("select * from t where a > 1 and b > 1 and c > 1 and d > 1")
26062620
require.NoError(t, h.LoadNeededHistograms(dom.InfoSchema()))
26072621
tbl := h.GetPhysicalTableStats(tableInfo.ID, tableInfo)
@@ -2610,7 +2624,7 @@ PARTITION BY RANGE ( a ) (
26102624
require.Equal(t, 3, len(tbl.GetCol(tableInfo.Columns[3].ID).Buckets))
26112625

26122626
tk.MustExec("analyze table t partition p1 index idx with 1 topn, 2 buckets")
2613-
tk.MustQuery("show warnings").Sort().Check(testkit.Rows())
2627+
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release."))
26142628
tbl = h.GetPhysicalTableStats(tableInfo.ID, tableInfo)
26152629
require.Greater(t, tbl.Version, lastVersion)
26162630
require.Equal(t, 2, len(tbl.GetIdx(tableInfo.Indices[0].ID).Buckets))

pkg/planner/core/planbuilder.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2911,8 +2911,15 @@ func pickColumnList(astColChoice pmodel.ColumnChoice, astColList []*model.Column
29112911
return tblSavedColChoice, tblSavedColList
29122912
}
29132913

2914+
func appendAnalyzeV1DeprecationWarning(sctx base.PlanContext, version int) {
2915+
if version == statistics.Version1 {
2916+
sctx.GetSessionVars().StmtCtx.AppendWarning(variable.ErrWarnDeprecatedSyntaxSimpleMsg.FastGenByArgs("ANALYZE with tidb_analyze_version=1"))
2917+
}
2918+
}
2919+
29142920
// buildAnalyzeTable constructs analyze tasks for each table.
29152921
func (b *PlanBuilder) buildAnalyzeTable(as *ast.AnalyzeTableStmt, opts map[ast.AnalyzeOptionType]uint64, version int) (base.Plan, error) {
2922+
appendAnalyzeV1DeprecationWarning(b.ctx, version)
29162923
p := &Analyze{Opts: opts}
29172924
p.OptionsMap = make(map[int64]V2AnalyzeOptions)
29182925
usePersistedOptions := variable.PersistAnalyzeOptions.Load()
@@ -3008,6 +3015,7 @@ func (b *PlanBuilder) buildAnalyzeIndex(as *ast.AnalyzeTableStmt, opts map[ast.A
30083015
if !versionIsSame {
30093016
b.ctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackError("The analyze version from the session is not compatible with the existing statistics of the table. Use the existing version instead"))
30103017
}
3018+
appendAnalyzeV1DeprecationWarning(b.ctx, version)
30113019
if version == statistics.Version2 {
30123020
return b.buildAnalyzeTable(as, opts, version)
30133021
}
@@ -3064,6 +3072,7 @@ func (b *PlanBuilder) buildAnalyzeAllIndex(as *ast.AnalyzeTableStmt, opts map[as
30643072
if !versionIsSame {
30653073
b.ctx.GetSessionVars().StmtCtx.AppendWarning(errors.NewNoStackErrorf("The analyze version from the session is not compatible with the existing statistics of the table. Use the existing version instead"))
30663074
}
3075+
appendAnalyzeV1DeprecationWarning(b.ctx, version)
30673076
if version == statistics.Version2 {
30683077
return b.buildAnalyzeTable(as, opts, version)
30693078
}

pkg/sessionctx/variable/sysvar.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,10 +2537,17 @@ var defaultSysVars = []*SysVar{
25372537
s.GuaranteeLinearizability = TiDBOptOn(val)
25382538
return nil
25392539
}},
2540-
{Scope: ScopeGlobal | ScopeSession, Name: TiDBAnalyzeVersion, Value: strconv.Itoa(DefTiDBAnalyzeVersion), Type: TypeInt, MinValue: 1, MaxValue: 2, SetSession: func(s *SessionVars, val string) error {
2541-
s.AnalyzeVersion = tidbOptPositiveInt32(val, DefTiDBAnalyzeVersion)
2542-
return nil
2543-
}},
2540+
{Scope: ScopeGlobal | ScopeSession, Name: TiDBAnalyzeVersion, Value: strconv.Itoa(DefTiDBAnalyzeVersion), Type: TypeInt, MinValue: 1, MaxValue: 2,
2541+
Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
2542+
if normalizedValue == "1" {
2543+
vars.StmtCtx.AppendWarning(errWarnDeprecatedSyntax.FastGenByArgs("tidb_analyze_version=1", "tidb_analyze_version=2"))
2544+
}
2545+
return normalizedValue, nil
2546+
},
2547+
SetSession: func(s *SessionVars, val string) error {
2548+
s.AnalyzeVersion = tidbOptPositiveInt32(val, DefTiDBAnalyzeVersion)
2549+
return nil
2550+
}},
25442551
{Scope: ScopeGlobal | ScopeSession, Name: TiDBHashJoinVersion, Value: DefTiDBHashJoinVersion, Type: TypeStr,
25452552
Validation: func(_ *SessionVars, normalizedValue string, originalValue string, _ ScopeFlag) (string, error) {
25462553
lowerValue := strings.ToLower(normalizedValue)

pkg/sessionctx/variable/varsutil_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,18 @@ func TestVarsutil(t *testing.T) {
421421
v.StmtCtx.TruncateWarnings(0)
422422
require.Len(t, v.StmtCtx.GetWarnings(), 0)
423423

424+
err = v.SetSystemVar(TiDBAnalyzeVersion, "1")
425+
require.NoError(t, err)
426+
warn := v.StmtCtx.GetWarnings()[0]
427+
require.Error(t, warn.Err)
428+
require.Equal(t, "[variable:1287]'tidb_analyze_version=1' is deprecated and will be removed in a future release. Please use tidb_analyze_version=2 instead", warn.Err.Error())
429+
430+
v.StmtCtx.TruncateWarnings(0)
431+
require.Len(t, v.StmtCtx.GetWarnings(), 0)
432+
424433
err = v.SetSystemVar(TiDBAnalyzeVersion, "4")
425434
require.NoError(t, err) // converts to max value
426-
warn := v.StmtCtx.GetWarnings()[0]
435+
warn = v.StmtCtx.GetWarnings()[0]
427436
require.Error(t, warn.Err)
428437
require.Contains(t, warn.Err.Error(), "Truncated incorrect tidb_analyze_version value")
429438

0 commit comments

Comments
 (0)