Skip to content

Commit

Permalink
test: add case for analyzing with predicate
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Jun 26, 2024
1 parent 1165798 commit 990c0d2
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions pkg/statistics/handle/usage/predicate_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ func TestCleanupPredicateColumns(t *testing.T) {
tk.MustExec("use test")
tk.MustExec("create table t (a int, b int)")
tk.MustExec("insert into t values (1, 1), (2, 2), (3, 3)")
// Enable column tracking.
tk.MustExec("set global tidb_enable_column_tracking = 1")
tk.MustQuery("select * from t where a > 1").Check(testkit.Rows("2 2", "3 3"))
tk.MustQuery("select * from t where b > 1").Check(testkit.Rows("2 2", "3 3"))

Expand All @@ -54,3 +52,49 @@ func TestCleanupPredicateColumns(t *testing.T) {
require.NoError(t, err)
require.Len(t, columns, 1)
}

func TestAnalyzeTableWithPredicateColumns(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)

// 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)

// Set tidb_analyze_default_column_choice to PREDICATE.
tk.MustExec("set global tidb_analyze_default_column_choice='PREDICATE'")

// Analyze table and check analyze jobs.
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 with 256 buckets, 100 topn, 1 samplerate"),
)

// More columns.
tk.MustQuery("select * from t where b > 1").Check(testkit.Rows("2 2 2", "3 3 3"))

// Dump the statistics usage.
err = h.DumpColStatsUsageToKV()
require.NoError(t, err)

// Analyze again.
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"),
)

// Set tidb_analyze_default_column_choice to ALL.
tk.MustExec("set global tidb_analyze_default_column_choice='ALL'")
// Analyze again.
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"),
)
}

0 comments on commit 990c0d2

Please sign in to comment.