Skip to content

Commit

Permalink
sessionctx/variable: fix range check for @@timestamp (#37249)
Browse files Browse the repository at this point in the history
close #31585
  • Loading branch information
bb7133 authored Aug 23, 2022
1 parent cfd4ddd commit 2c22fff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var defaultSysVars = []*SysVar{
{Scope: ScopeNone, Name: TiDBAllowFunctionForExpressionIndex, ReadOnly: true, Value: collectAllowFuncName4ExpressionIndex()},

/* The system variables below have SESSION scope */
{Scope: ScopeSession, Name: Timestamp, Value: DefTimestamp, MinValue: 0, MaxValue: 2147483647, Type: TypeFloat, GetSession: func(s *SessionVars) (string, error) {
{Scope: ScopeSession, Name: Timestamp, Value: DefTimestamp, MinValue: 0, MaxValue: math.MaxInt32, Type: TypeFloat, GetSession: func(s *SessionVars) (string, error) {
if timestamp, ok := s.systems[Timestamp]; ok && timestamp != DefTimestamp {
return timestamp, nil
}
Expand All @@ -86,6 +86,12 @@ var defaultSysVars = []*SysVar{
}, GetStateValue: func(s *SessionVars) (string, bool, error) {
timestamp, ok := s.systems[Timestamp]
return timestamp, ok && timestamp != DefTimestamp, nil
}, Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
val := tidbOptFloat64(originalValue, DefTimestampFloat)
if val > math.MaxInt32 {
return originalValue, ErrWrongValueForVar.GenWithStackByArgs(Timestamp, originalValue)
}
return normalizedValue, nil
}},
{Scope: ScopeSession, Name: WarningCount, Value: "0", ReadOnly: true, GetSession: func(s *SessionVars) (string, error) {
return strconv.Itoa(s.SysWarningCount), nil
Expand Down
18 changes: 18 additions & 0 deletions sessionctx/variable/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,24 @@ func TestTimestamp(t *testing.T) {
require.NoError(t, err)
require.NotEqual(t, "", val)
require.NotEqual(t, "10", val)

// Test validating a value that less than the minimum one.
sv := GetSysVar(Timestamp)
_, err = sv.Validate(vars, "-5", ScopeSession)
require.NoError(t, err)
warn := vars.StmtCtx.GetWarnings()[0].Err
require.Equal(t, "[variable:1292]Truncated incorrect timestamp value: '-5'", warn.Error())

// Test validating values that larger than the maximum one.
_, err = sv.Validate(vars, "3147483698", ScopeSession)
require.Equal(t, "[variable:1231]Variable 'timestamp' can't be set to the value of '3147483698'", err.Error())

_, err = sv.Validate(vars, "2147483648", ScopeSession)
require.Equal(t, "[variable:1231]Variable 'timestamp' can't be set to the value of '2147483648'", err.Error())

// Test validating the maximum value.
_, err = sv.Validate(vars, "2147483647", ScopeSession)
require.NoError(t, err)
}

func TestIdentity(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ const (
DefTiDBRegardNULLAsPoint = true
DefEnablePlacementCheck = true
DefTimestamp = "0"
DefTimestampFloat = 0.0
DefTiDBEnableStmtSummary = true
DefTiDBStmtSummaryInternalQuery = false
DefTiDBStmtSummaryRefreshInterval = 1800
Expand Down

0 comments on commit 2c22fff

Please sign in to comment.