diff --git a/pkg/sessionctx/variable/sysvar_test.go b/pkg/sessionctx/variable/sysvar_test.go index e71dc91dd0bf8..d7844291b6585 100644 --- a/pkg/sessionctx/variable/sysvar_test.go +++ b/pkg/sessionctx/variable/sysvar_test.go @@ -1605,3 +1605,69 @@ func TestTiDBLowResTSOUpdateInterval(t *testing.T) { require.NoError(t, err) require.Equal(t, "1000", val) } + +func TestSetEnableColumnTrackingAndPersistAnalyzeOptions(t *testing.T) { + vars := NewSessionVars(nil) + mock := NewMockGlobalAccessor4Tests() + mock.SessionVars = vars + vars.GlobalVarsAccessor = mock + + // Test EnableColumnTracking + val, err := mock.GetGlobalSysVar(TiDBEnableColumnTracking) + require.NoError(t, err) + require.Equal(t, Off, val) + err = mock.SetGlobalSysVar(context.Background(), TiDBEnableColumnTracking, On) + require.NoError(t, err) + val, err = mock.GetGlobalSysVar(TiDBEnableColumnTracking) + require.NoError(t, err) + require.Equal(t, On, val) + // Reset back. + err = mock.SetGlobalSysVar(context.Background(), TiDBEnableColumnTracking, Off) + require.NoError(t, err) + + // Test PersistAnalyzeOptions + val, err = mock.GetGlobalSysVar(TiDBPersistAnalyzeOptions) + require.NoError(t, err) + require.Equal(t, On, val) + err = mock.SetGlobalSysVar(context.Background(), TiDBPersistAnalyzeOptions, Off) + require.NoError(t, err) + val, err = mock.GetGlobalSysVar(TiDBPersistAnalyzeOptions) + require.NoError(t, err) + require.Equal(t, Off, val) + // Reset back + err = mock.SetGlobalSysVar(context.Background(), TiDBPersistAnalyzeOptions, On) + require.NoError(t, err) + + // Set EnableColumnTracking to true when PersistAnalyzeOptions is false + // Set to false first. + err = mock.SetGlobalSysVar(context.Background(), TiDBEnableColumnTracking, Off) + require.NoError(t, err) + err = mock.SetGlobalSysVar(context.Background(), TiDBPersistAnalyzeOptions, Off) + require.NoError(t, err) + val, err = mock.GetGlobalSysVar(TiDBPersistAnalyzeOptions) + require.NoError(t, err) + require.Equal(t, Off, val) + err = mock.SetGlobalSysVar(context.Background(), TiDBEnableColumnTracking, On) + require.Error(t, err, "enable column tracking requires to persist analyze options") + val, err = mock.GetGlobalSysVar(TiDBEnableColumnTracking) + require.NoError(t, err) + require.Equal(t, Off, val) + + // Set PersistAnalyzeOptions to false when EnableColumnTracking is true + // Set to true first. + err = mock.SetGlobalSysVar(context.Background(), TiDBPersistAnalyzeOptions, On) + require.NoError(t, err) + val, err = mock.GetGlobalSysVar(TiDBPersistAnalyzeOptions) + require.NoError(t, err) + require.Equal(t, On, val) + err = mock.SetGlobalSysVar(context.Background(), TiDBEnableColumnTracking, On) + require.NoError(t, err) + val, err = mock.GetGlobalSysVar(TiDBEnableColumnTracking) + require.NoError(t, err) + require.Equal(t, On, val) + err = mock.SetGlobalSysVar(context.Background(), TiDBPersistAnalyzeOptions, Off) + require.Error(t, err, "persist analyze options requires to enable column tracking") + val, err = mock.GetGlobalSysVar(TiDBPersistAnalyzeOptions) + require.NoError(t, err) + require.Equal(t, On, val) +}