Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#55991
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
winoros authored and ti-chi-bot committed Sep 10, 2024
1 parent 92acec0 commit a06e889
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 4 deletions.
4 changes: 0 additions & 4 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,10 +1184,6 @@ func createSessionFunc(store kv.Storage) pools.Factory {
if err != nil {
return nil, errors.Trace(err)
}
err = se.sessionVars.SetSystemVar(variable.TiDBEnableWindowFunction, variable.BoolToOnOff(variable.DefEnableWindowFunction))
if err != nil {
return nil, errors.Trace(err)
}
err = se.sessionVars.SetSystemVar(variable.TiDBConstraintCheckInPlacePessimistic, variable.On)
if err != nil {
return nil, errors.Trace(err)
Expand Down
1 change: 1 addition & 0 deletions pkg/sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,7 @@ func NewSessionVars(hctx HookContext) *SessionVars {
DefaultCollationForUTF8MB4: mysql.DefaultCollationName,
GroupConcatMaxLen: DefGroupConcatMaxLen,
EnableRedactLog: DefTiDBRedactLog,
EnableWindowFunction: DefEnableWindowFunction,
}
vars.status.Store(uint32(mysql.ServerStatusAutocommit))
vars.StmtCtx.ResourceGroupName = resourcegroup.DefaultResourceGroupName
Expand Down
182 changes: 182 additions & 0 deletions pkg/sessionctx/variable/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1605,3 +1605,185 @@ func TestTiDBLowResTSOUpdateInterval(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "1000", val)
}
<<<<<<< HEAD
=======

func TestTiDBSchemaCacheSize(t *testing.T) {
vars := NewSessionVars(nil)
mock := NewMockGlobalAccessor4Tests()
mock.SessionVars = vars
vars.GlobalVarsAccessor = mock
var (
mb uint64 = 1 << 20
err error
val string
maxValue uint64 = math.MaxInt64
)
// Test tidb_schema_cache_size
schemaCacheSize := GetSysVar(TiDBSchemaCacheSize)
// Check default value
require.Equal(t, schemaCacheSize.Value, strconv.Itoa(DefTiDBSchemaCacheSize))

// MinValue is 512 MB
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, strconv.FormatUint(100*mb, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, "512MB", val)

// MaxValue is 9223372036854775807
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, strconv.FormatUint(maxValue, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, strconv.FormatUint(maxValue, 10), val)

// test MinValue-1
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, strconv.FormatUint(100*mb-1, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, "512MB", val)

// test MaxValue+1
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, strconv.FormatUint(maxValue+1, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, strconv.FormatUint(maxValue, 10), val)

// Test Normal Value
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, strconv.FormatUint(1024*mb, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, strconv.FormatUint(1024*mb, 10), val)

// Test Close
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, strconv.FormatUint(0, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, "0", val)

// Test byteSize format
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "1234567890123")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, SchemaCacheSize.Load(), uint64(1234567890123))
require.Equal(t, "1234567890123", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "10KB")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, SchemaCacheSize.Load(), uint64(512<<20))
require.Equal(t, "512MB", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "12345678KB")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, SchemaCacheSize.Load(), uint64(12345678<<10))
require.Equal(t, "12345678KB", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "700MB")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, SchemaCacheSize.Load(), uint64(700<<20))
require.Equal(t, "700MB", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "20GB")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, SchemaCacheSize.Load(), uint64(20<<30))
require.Equal(t, "20GB", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "2TB")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, SchemaCacheSize.Load(), uint64(2<<40))
require.Equal(t, "2TB", val)

// Test error
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "123aaa123")
require.Error(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "700MBaa")
require.Error(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "a700MB")
require.Error(t, err)
}

func TestEnableWindowFunction(t *testing.T) {
vars := NewSessionVars(nil)
require.Equal(t, vars.EnableWindowFunction, DefEnableWindowFunction)
require.NoError(t, vars.SetSystemVar(TiDBEnableWindowFunction, "on"))
require.Equal(t, vars.EnableWindowFunction, true)
require.NoError(t, vars.SetSystemVar(TiDBEnableWindowFunction, "0"))
require.Equal(t, vars.EnableWindowFunction, false)
require.NoError(t, vars.SetSystemVar(TiDBEnableWindowFunction, "1"))
require.Equal(t, vars.EnableWindowFunction, true)
}

func TestTiDBAutoAnalyzeConcurrencyValidation(t *testing.T) {
vars := NewSessionVars(nil)

tests := []struct {
name string
autoAnalyze bool
autoAnalyzePriority bool
input string
expectError bool
}{
{
name: "Both enabled, valid input",
autoAnalyze: true,
autoAnalyzePriority: true,
input: "10",
expectError: false,
},
{
name: "Auto analyze disabled",
autoAnalyze: false,
autoAnalyzePriority: true,
input: "10",
expectError: true,
},
{
name: "Auto analyze priority queue disabled",
autoAnalyze: true,
autoAnalyzePriority: false,
input: "10",
expectError: true,
},
{
name: "Both disabled",
autoAnalyze: false,
autoAnalyzePriority: false,
input: "10",
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
RunAutoAnalyze.Store(tt.autoAnalyze)
EnableAutoAnalyzePriorityQueue.Store(tt.autoAnalyzePriority)

sysVar := GetSysVar(TiDBAutoAnalyzeConcurrency)
require.NotNil(t, sysVar)

_, err := sysVar.Validate(vars, tt.input, ScopeGlobal)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
>>>>>>> 2c30f865e32 (session: set EnableWindowFunction for all SessionVars (#55991))

0 comments on commit a06e889

Please sign in to comment.