From b11f780bd8b9ec24af2eb1527c926fada7c6435c Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 26 Jun 2024 17:54:49 +0800 Subject: [PATCH] test: add TestAnalyzeTableWithTiDBPersistAnalyzeOptionsEnabled Signed-off-by: hi-rustin --- .../handle/usage/predicate_column_test.go | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/pkg/statistics/handle/usage/predicate_column_test.go b/pkg/statistics/handle/usage/predicate_column_test.go index e437300ed8376..c63792a4ad5ee 100644 --- a/pkg/statistics/handle/usage/predicate_column_test.go +++ b/pkg/statistics/handle/usage/predicate_column_test.go @@ -98,3 +98,47 @@ func TestAnalyzeTableWithPredicateColumns(t *testing.T) { testkit.Rows("t analyze table all columns with 256 buckets, 100 topn, 1 samplerate"), ) } + +func TestAnalyzeTableWithTiDBPersistAnalyzeOptionsEnabled(t *testing.T) { + store, dom := testkit.CreateMockStoreAndDomain(t) + tk := testkit.NewTestKit(t, store) + + // Check tidb_persist_analyze_options first. + tk.MustQuery("select @@tidb_persist_analyze_options").Check(testkit.Rows("1")) + // Set tidb_analyze_column_options to PREDICATE. + tk.MustExec("set global tidb_analyze_column_options='PREDICATE'") + // Create table and select data with predicate. + tk.MustExec("use test") + tk.MustExec("create table t (a int, b int, c int)") + tk.MustExec("insert into t values (1, 1, 1), (2, 2, 2), (3, 3, 3)") + tk.MustQuery("select * from t where a > 1").Check(testkit.Rows("2 2 2", "3 3 3")) + + // Dump the statistics usage. + h := dom.StatsHandle() + err := h.DumpColStatsUsageToKV() + require.NoError(t, err) + + // Analyze table with specified columns. + tk.MustExec("analyze table t columns a, b") + tk.MustQuery("select table_name, job_info from mysql.analyze_jobs order by id desc limit 1").Check( + testkit.Rows("t analyze table columns a, b with 256 buckets, 100 topn, 1 samplerate"), + ) + + // Analyze again, it should use the same options. + tk.MustExec("analyze table t") + tk.MustQuery("select table_name, job_info from mysql.analyze_jobs order by id desc limit 1").Check( + testkit.Rows("t analyze table columns a, b with 256 buckets, 100 topn, 1 samplerate"), + ) + + // Analyze table with ALL syntax. + tk.MustExec("analyze table t all columns") + tk.MustQuery("select table_name, job_info from mysql.analyze_jobs order by id desc limit 1").Check( + testkit.Rows("t analyze table all columns with 256 buckets, 100 topn, 1 samplerate"), + ) + + // Analyze again, it should use the same options. + tk.MustExec("analyze table t") + tk.MustQuery("select table_name, job_info from mysql.analyze_jobs order by id desc limit 1").Check( + testkit.Rows("t analyze table all columns with 256 buckets, 100 topn, 1 samplerate"), + ) +}