Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 3 additions & 4 deletions pkg/executor/test/analyzetest/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2598,10 +2598,9 @@ PARTITION BY RANGE ( a ) (

// analyze partition with index and with options are allowed under dynamic V1
tk.MustExec("analyze table t partition p0 with 1 topn, 3 buckets")
rows := tk.MustQuery("show warnings").Rows()
require.Len(t, rows, 0)
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."))
tk.MustExec("analyze table t partition p1 with 1 topn, 3 buckets")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows())
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."))
tk.MustQuery("select * from t where a > 1 and b > 1 and c > 1 and d > 1")
require.NoError(t, h.LoadNeededHistograms(dom.InfoSchema()))
tbl := h.GetPhysicalTableStats(tableInfo.ID, tableInfo)
Expand All @@ -2610,7 +2609,7 @@ PARTITION BY RANGE ( a ) (
require.Equal(t, 3, len(tbl.GetCol(tableInfo.Columns[3].ID).Buckets))

tk.MustExec("analyze table t partition p1 index idx with 1 topn, 2 buckets")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows())
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."))
tbl = h.GetPhysicalTableStats(tableInfo.ID, tableInfo)
require.Greater(t, tbl.Version, lastVersion)
require.Equal(t, 2, len(tbl.GetIdx(tableInfo.Indices[0].ID).Buckets))
Expand Down
12 changes: 8 additions & 4 deletions pkg/planner/core/casetest/index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,18 @@ func TestAnalyzeVectorIndex(t *testing.T) {
tk.MustExec("analyze table t")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Warning 1105 analyzing vector index is not supported, skip idx",
"Warning 1105 analyzing vector index is not supported, skip idx2"))
"Warning 1105 analyzing vector index is not supported, skip idx2",
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release."))
tk.MustExec("analyze table t index idx")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Warning 1105 analyzing vector index is not supported, skip idx"))
"Warning 1105 analyzing vector index is not supported, skip idx",
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release."))
tk.MustExec("analyze table t index a")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows())
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release."))
tk.MustExec("analyze table t index a, idx, idx2")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Warning 1105 analyzing vector index is not supported, skip idx",
"Warning 1105 analyzing vector index is not supported, skip idx2"))
"Warning 1105 analyzing vector index is not supported, skip idx2",
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release."))
}
12 changes: 8 additions & 4 deletions pkg/planner/core/indexmerge_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,18 @@ func TestAnalyzeVectorIndex(t *testing.T) {
tk.MustExec("analyze table t")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Warning 1105 analyzing vector index is not supported, skip idx",
"Warning 1105 analyzing vector index is not supported, skip idx2"))
"Warning 1105 analyzing vector index is not supported, skip idx2",
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release."))
tk.MustExec("analyze table t index idx")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Warning 1105 analyzing vector index is not supported, skip idx"))
"Warning 1105 analyzing vector index is not supported, skip idx",
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release."))
tk.MustExec("analyze table t index a")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows())
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release."))
tk.MustExec("analyze table t index a, idx, idx2")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
"Warning 1105 analyzing vector index is not supported, skip idx",
"Warning 1105 analyzing vector index is not supported, skip idx2"))
"Warning 1105 analyzing vector index is not supported, skip idx2",
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release."))
}
9 changes: 9 additions & 0 deletions pkg/planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2911,8 +2911,15 @@ func pickColumnList(astColChoice pmodel.ColumnChoice, astColList []*model.Column
return tblSavedColChoice, tblSavedColList
}

func appendAnalyzeV1DeprecationWarning(sctx base.PlanContext, version int) {
if version == statistics.Version1 {
sctx.GetSessionVars().StmtCtx.AppendWarning(variable.ErrWarnDeprecatedSyntaxSimpleMsg.FastGenByArgs("ANALYZE with tidb_analyze_version=1"))
}
}

// buildAnalyzeTable constructs analyze tasks for each table.
func (b *PlanBuilder) buildAnalyzeTable(as *ast.AnalyzeTableStmt, opts map[ast.AnalyzeOptionType]uint64, version int) (base.Plan, error) {
appendAnalyzeV1DeprecationWarning(b.ctx, version)
p := &Analyze{Opts: opts}
p.OptionsMap = make(map[int64]V2AnalyzeOptions)
usePersistedOptions := variable.PersistAnalyzeOptions.Load()
Expand Down Expand Up @@ -3008,6 +3015,7 @@ func (b *PlanBuilder) buildAnalyzeIndex(as *ast.AnalyzeTableStmt, opts map[ast.A
if !versionIsSame {
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"))
}
appendAnalyzeV1DeprecationWarning(b.ctx, version)
if version == statistics.Version2 {
return b.buildAnalyzeTable(as, opts, version)
}
Expand Down Expand Up @@ -3064,6 +3072,7 @@ func (b *PlanBuilder) buildAnalyzeAllIndex(as *ast.AnalyzeTableStmt, opts map[as
if !versionIsSame {
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"))
}
appendAnalyzeV1DeprecationWarning(b.ctx, version)
if version == statistics.Version2 {
return b.buildAnalyzeTable(as, opts, version)
}
Expand Down
15 changes: 11 additions & 4 deletions pkg/sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2537,10 +2537,17 @@ var defaultSysVars = []*SysVar{
s.GuaranteeLinearizability = TiDBOptOn(val)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBAnalyzeVersion, Value: strconv.Itoa(DefTiDBAnalyzeVersion), Type: TypeInt, MinValue: 1, MaxValue: 2, SetSession: func(s *SessionVars, val string) error {
s.AnalyzeVersion = tidbOptPositiveInt32(val, DefTiDBAnalyzeVersion)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBAnalyzeVersion, Value: strconv.Itoa(DefTiDBAnalyzeVersion), Type: TypeInt, MinValue: 1, MaxValue: 2,
Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
if normalizedValue == "1" {
vars.StmtCtx.AppendWarning(errWarnDeprecatedSyntax.FastGenByArgs("tidb_analyze_version=1", "tidb_analyze_version=2"))
}
return normalizedValue, nil
},
SetSession: func(s *SessionVars, val string) error {
s.AnalyzeVersion = tidbOptPositiveInt32(val, DefTiDBAnalyzeVersion)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBHashJoinVersion, Value: DefTiDBHashJoinVersion, Type: TypeStr,
Validation: func(_ *SessionVars, normalizedValue string, originalValue string, _ ScopeFlag) (string, error) {
lowerValue := strings.ToLower(normalizedValue)
Expand Down
11 changes: 10 additions & 1 deletion pkg/sessionctx/variable/varsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,18 @@ func TestVarsutil(t *testing.T) {
v.StmtCtx.TruncateWarnings(0)
require.Len(t, v.StmtCtx.GetWarnings(), 0)

err = v.SetSystemVar(TiDBAnalyzeVersion, "1")
require.NoError(t, err)
warn := v.StmtCtx.GetWarnings()[0]
require.Error(t, warn.Err)
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())

v.StmtCtx.TruncateWarnings(0)
require.Len(t, v.StmtCtx.GetWarnings(), 0)

err = v.SetSystemVar(TiDBAnalyzeVersion, "4")
require.NoError(t, err) // converts to max value
warn := v.StmtCtx.GetWarnings()[0]
warn = v.StmtCtx.GetWarnings()[0]
require.Error(t, warn.Err)
require.Contains(t, warn.Err.Error(), "Truncated incorrect tidb_analyze_version value")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestLockAndUnlockPartitionStats(t *testing.T) {

tk.MustExec("analyze table test.t")
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.",
"Warning 1105 skip analyze locked table: test.t partition (p0)",
))
partitionStats1 := handle.GetPhysicalTableStats(p0Id, tbl)
Expand Down Expand Up @@ -488,6 +489,7 @@ func TestNewPartitionShouldBeLockedIfWholeTableLocked(t *testing.T) {
// Check the new partition is locked.
tk.MustExec("analyze table t partition p2")
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.",
"Warning 1105 skip analyze locked table: test.t partition (p2)",
))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestLockAndUnlockTableStats(t *testing.T) {

tk.MustExec("analyze table test.t")
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.",
"Warning 1105 skip analyze locked table: test.t",
))
tblStats1 := handle.GetPhysicalTableStats(tbl.ID, tbl)
Expand Down Expand Up @@ -102,6 +103,7 @@ func TestLockAndUnlockPartitionedTableStats(t *testing.T) {

tk.MustExec("analyze table test.t")
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.",
"Warning 1105 skip analyze locked tables: test.t partition (p0), test.t partition (p1)",
))

Expand Down Expand Up @@ -214,6 +216,7 @@ func TestLockAndUnlockTablesStats(t *testing.T) {

tk.MustExec("analyze table test.t1, test.t2")
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.",
"Warning 1105 skip analyze locked tables: test.t1, test.t2",
))
Comment on lines 218 to 221
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Find all warning assertions in the test file to check for inconsistencies
rg -n "show warnings.*Check" pkg/statistics/handle/handletest/lockstats/lock_table_stats_test.go -A 3

Repository: pingcap/tidb

Length of output: 1044


🏁 Script executed:

#!/bin/bash
# Check the setup and analyze calls in the first two test functions
sed -n '45,60p' pkg/statistics/handle/handletest/lockstats/lock_table_stats_test.go
sed -n '95,115p' pkg/statistics/handle/handletest/lockstats/lock_table_stats_test.go
# Also check the test function that has the new warning (to see its setup)
sed -n '168,225p' pkg/statistics/handle/handletest/lockstats/lock_table_stats_test.go

Repository: pingcap/tidb

Length of output: 3667


🏁 Script executed:

#!/bin/bash
# Find the setupTestEnvironmentWithTableT function definition
rg -n "func setupTestEnvironmentWithTableT" pkg/statistics/handle/handletest/lockstats/lock_table_stats_test.go -A 30

Repository: pingcap/tidb

Length of output: 699


Other tests using tidb_analyze_version=1 should also expect the deprecation warning.

Tests at lines 54-56 and 104-106 both:

  • Use setupTestEnvironmentWithTableT() which sets @@tidb_analyze_version = 1 (line 372)
  • Execute analyze table commands on locked tables
  • Check the resulting warnings

These tests currently only expect Warning 1105 (skip analyze locked tables), but since they analyze with version 1, they should also expect Warning 1681 (deprecation warning) to match the expectation at lines 216-219. Update these assertions to include the deprecation warning.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/statistics/handle/handletest/lockstats/lock_table_stats_test.go` around
lines 216 - 219, The assertions that check SQL warnings in the tests that call
setupTestEnvironmentWithTableT() must include the deprecation warning 1681 in
addition to the skip-analyze warning 1105; locate the tk.MustQuery("show
warnings").Check(...) calls in lock_table_stats_test.go that follow analyze
table invocations (those using setupTestEnvironmentWithTableT()) and update
their expected rows to include both "Warning 1681 ANALYZE with
tidb_analyze_version=1 is deprecated..." and "Warning 1105 skip analyze locked
tables: ..." in the same order as the other test block (matching the Check used
in the block that already includes both warnings).

tbl1Stats1 := handle.GetPhysicalTableStats(tbl1.Meta().ID, tbl1.Meta())
Expand Down
20 changes: 16 additions & 4 deletions pkg/statistics/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,21 @@ func TestChangeVerTo2Behavior(t *testing.T) {
})
tk.MustExec("set @@session.tidb_analyze_version = 2")
tk.MustExec("analyze table t index idx")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 The analyze version from the session is not compatible with the existing statistics of the table. Use the existing version instead"))
tk.MustQuery("show warnings").Check(testkit.Rows(
"Warning 1105 The analyze version from the session is not compatible with the existing statistics of the table. Use the existing version instead",
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release.",
))
require.NoError(t, h.Update(context.Background(), is))
statsTblT = h.GetPhysicalTableStats(tblT.Meta().ID, tblT.Meta())
statsTblT.ForEachIndexImmutable(func(_ int64, idx *statistics.Index) bool {
require.Equal(t, int64(1), idx.GetStatsVer())
return false
})
tk.MustExec("analyze table t index")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 The analyze version from the session is not compatible with the existing statistics of the table. Use the existing version instead"))
tk.MustQuery("show warnings").Check(testkit.Rows(
"Warning 1105 The analyze version from the session is not compatible with the existing statistics of the table. Use the existing version instead",
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release.",
))
require.NoError(t, h.Update(context.Background(), is))
statsTblT = h.GetPhysicalTableStats(tblT.Meta().ID, tblT.Meta())
statsTblT.ForEachIndexImmutable(func(_ int64, idx *statistics.Index) bool {
Expand Down Expand Up @@ -157,15 +163,21 @@ func TestChangeVerTo2BehaviorWithPersistedOptions(t *testing.T) {
})
tk.MustExec("set @@session.tidb_analyze_version = 2")
tk.MustExec("analyze table t index idx")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 The analyze version from the session is not compatible with the existing statistics of the table. Use the existing version instead"))
tk.MustQuery("show warnings").Check(testkit.Rows(
"Warning 1105 The analyze version from the session is not compatible with the existing statistics of the table. Use the existing version instead",
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release.",
))
require.NoError(t, h.Update(context.Background(), is))
statsTblT = h.GetPhysicalTableStats(tblT.Meta().ID, tblT.Meta())
statsTblT.ForEachIndexImmutable(func(_ int64, idx *statistics.Index) bool {
require.Equal(t, int64(1), idx.GetStatsVer())
return false
})
tk.MustExec("analyze table t index")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 The analyze version from the session is not compatible with the existing statistics of the table. Use the existing version instead"))
tk.MustQuery("show warnings").Check(testkit.Rows(
"Warning 1105 The analyze version from the session is not compatible with the existing statistics of the table. Use the existing version instead",
"Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release.",
))
require.NoError(t, h.Update(context.Background(), is))
statsTblT = h.GetPhysicalTableStats(tblT.Meta().ID, tblT.Meta())
statsTblT.ForEachIndexImmutable(func(_ int64, idx *statistics.Index) bool {
Expand Down
7 changes: 7 additions & 0 deletions tests/integrationtest/r/planner/core/indexmerge_path.result
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@ Level Code Message
Note 1105 Analyze use auto adjusted sample rate 1.000000 for table planner__core__indexmerge_path.t, reason to use this rate is "TiDB assumes that the table is empty, use sample-rate=1"
Warning 1105 The version 2 would collect all statistics not only the selected indexes
set tidb_analyze_version=1;
Level Code Message
Warning 1287 'tidb_analyze_version=1' is deprecated and will be removed in a future release. Please use tidb_analyze_version=2 instead
analyze table t;
Level Code Message
Warning 1105 analyzing multi-valued indexes is not supported, skip idx
Warning 1105 analyzing multi-valued indexes is not supported, skip idx2
Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release.
analyze table t index idx;
Level Code Message
Warning 1105 analyzing multi-valued indexes is not supported, skip idx
Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release.
analyze table t index a;
Level Code Message
Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release.
analyze table t index a, idx, idx2;
Level Code Message
Warning 1105 analyzing multi-valued indexes is not supported, skip idx
Warning 1105 analyzing multi-valued indexes is not supported, skip idx2
Warning 1681 ANALYZE with tidb_analyze_version=1 is deprecated and will be removed in a future release.
drop table if exists t;
create table t(
a int, j0 json, j1 json,
Expand Down