Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sessionctx/variable: fix range check for @@timestamp #37249

Merged
merged 2 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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