Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#49760
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
AilinKid authored and ti-chi-bot committed Dec 25, 2023
1 parent 4d53073 commit 91fb5d3
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 32 deletions.
4 changes: 2 additions & 2 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1212,10 +1212,10 @@ func (sc *StatementContext) RecordRangeFallback(rangeMaxSize int64) {
// If range fallback happens, it means ether the query is unreasonable(for example, several long IN lists) or tidb_opt_range_max_size is too small
// and the generated plan is probably suboptimal. In that case we don't put it into plan cache.
if sc.UseCache {
sc.SetSkipPlanCache(errors.Errorf("in-list is too long"))
sc.SetSkipPlanCache(errors.NewNoStackError("in-list is too long"))
}
if !sc.RangeFallback {
sc.AppendWarning(errors.Errorf("Memory capacity of %v bytes for 'tidb_opt_range_max_size' exceeded when building ranges. Less accurate ranges such as full range are chosen", rangeMaxSize))
sc.AppendWarning(errors.NewNoStackErrorf("Memory capacity of %v bytes for 'tidb_opt_range_max_size' exceeded when building ranges. Less accurate ranges such as full range are chosen", rangeMaxSize))
sc.RangeFallback = true
}
}
Expand Down
118 changes: 118 additions & 0 deletions sessionctx/stmtctx/stmtctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,121 @@ func TestStmtHintsClone(t *testing.T) {
}
require.Equal(t, hints, *hints.Clone())
}
<<<<<<< HEAD:sessionctx/stmtctx/stmtctx_test.go
=======

func TestNewStmtCtx(t *testing.T) {
sc := stmtctx.NewStmtCtx()
require.Equal(t, types.DefaultStmtFlags, sc.TypeFlags())
require.Same(t, time.UTC, sc.TimeZone())
require.Same(t, time.UTC, sc.TimeZone())
sc.AppendWarning(errors.NewNoStackError("err1"))
warnings := sc.GetWarnings()
require.Equal(t, 1, len(warnings))
require.Equal(t, stmtctx.WarnLevelWarning, warnings[0].Level)
require.Equal(t, "err1", warnings[0].Err.Error())

tz := time.FixedZone("UTC+1", 2*60*60)
sc = stmtctx.NewStmtCtxWithTimeZone(tz)
require.Equal(t, types.DefaultStmtFlags, sc.TypeFlags())
require.Same(t, tz, sc.TimeZone())
require.Same(t, tz, sc.TimeZone())
sc.AppendWarning(errors.NewNoStackError("err2"))
warnings = sc.GetWarnings()
require.Equal(t, 1, len(warnings))
require.Equal(t, stmtctx.WarnLevelWarning, warnings[0].Level)
require.Equal(t, "err2", warnings[0].Err.Error())
}

func TestSetStmtCtxTimeZone(t *testing.T) {
sc := stmtctx.NewStmtCtx()
require.Same(t, time.UTC, sc.TimeZone())
tz := time.FixedZone("UTC+1", 2*60*60)
sc.SetTimeZone(tz)
require.Same(t, tz, sc.TimeZone())
}

func TestSetStmtCtxTypeFlags(t *testing.T) {
sc := stmtctx.NewStmtCtx()
require.Equal(t, types.DefaultStmtFlags, sc.TypeFlags())

sc.SetTypeFlags(types.FlagAllowNegativeToUnsigned | types.FlagSkipASCIICheck)
require.Equal(t, types.FlagAllowNegativeToUnsigned|types.FlagSkipASCIICheck, sc.TypeFlags())
require.Equal(t, sc.TypeFlags(), sc.TypeFlags())

sc.SetTypeFlags(types.FlagSkipASCIICheck | types.FlagSkipUTF8Check | types.FlagTruncateAsWarning)
require.Equal(t, types.FlagSkipASCIICheck|types.FlagSkipUTF8Check|types.FlagTruncateAsWarning, sc.TypeFlags())
require.Equal(t, sc.TypeFlags(), sc.TypeFlags())
}

func TestResetStmtCtx(t *testing.T) {
sc := stmtctx.NewStmtCtx()
require.Equal(t, types.DefaultStmtFlags, sc.TypeFlags())

tz := time.FixedZone("UTC+1", 2*60*60)
sc.SetTimeZone(tz)
sc.SetTypeFlags(types.FlagAllowNegativeToUnsigned | types.FlagSkipASCIICheck)
sc.AppendWarning(errors.NewNoStackError("err1"))
sc.InRestrictedSQL = true
sc.StmtType = "Insert"

require.Same(t, tz, sc.TimeZone())
require.Equal(t, types.FlagAllowNegativeToUnsigned|types.FlagSkipASCIICheck, sc.TypeFlags())
require.Equal(t, 1, len(sc.GetWarnings()))

sc.Reset()
require.Same(t, time.UTC, sc.TimeZone())
require.Same(t, time.UTC, sc.TimeZone())
require.Equal(t, types.DefaultStmtFlags, sc.TypeFlags())
require.Equal(t, types.DefaultStmtFlags, sc.TypeFlags())
require.False(t, sc.InRestrictedSQL)
require.Empty(t, sc.StmtType)
require.Equal(t, 0, len(sc.GetWarnings()))
sc.AppendWarning(errors.NewNoStackError("err2"))
warnings := sc.GetWarnings()
require.Equal(t, 1, len(warnings))
require.Equal(t, stmtctx.WarnLevelWarning, warnings[0].Level)
require.Equal(t, "err2", warnings[0].Err.Error())
}

func TestStmtCtxID(t *testing.T) {
sc := stmtctx.NewStmtCtx()
currentID := sc.CtxID()

cases := []struct {
fn func() *stmtctx.StatementContext
}{
{func() *stmtctx.StatementContext { return stmtctx.NewStmtCtx() }},
{func() *stmtctx.StatementContext { return stmtctx.NewStmtCtxWithTimeZone(time.Local) }},
{func() *stmtctx.StatementContext {
sc.Reset()
return sc
}},
}

for _, c := range cases {
ctxID := c.fn().CtxID()
require.Greater(t, ctxID, currentID)
currentID = ctxID
}
}

func TestErrCtx(t *testing.T) {
sc := stmtctx.NewStmtCtx()
// the default errCtx
err := types.ErrTruncated
require.Error(t, sc.HandleError(err))

// reset the types flags will re-initialize the error flag
sc.SetTypeFlags(types.DefaultStmtFlags | types.FlagTruncateAsWarning)
require.NoError(t, sc.HandleError(err))
}

func BenchmarkErrCtx(b *testing.B) {
sc := stmtctx.NewStmtCtx()

for i := 0; i < b.N; i++ {
sc.ErrCtx()
}
}
>>>>>>> 116456ddfb1 (sessionctx: refactor sessionctx pkg's warning and note generation (#49760)):pkg/sessionctx/stmtctx/stmtctx_test.go
8 changes: 5 additions & 3 deletions sessionctx/variable/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package variable

import (
"math"

"github.com/pingcap/errors"
)

// The following sysVars are noops.
Expand Down Expand Up @@ -197,11 +199,11 @@ var noopSysVars = []*SysVar{
{Scope: ScopeGlobal | ScopeSession, Name: SQLAutoIsNull, Value: Off, Type: TypeBool, IsHintUpdatable: true, Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
// checkSQLAutoIsNull requires TiDBEnableNoopFuncs != OFF for the same scope otherwise an error will be returned.
// See also https://github.com/pingcap/tidb/issues/28230
errMsg := ErrFunctionsNoopImpl.GenWithStackByArgs("sql_auto_is_null")
errMsg := ErrFunctionsNoopImpl.FastGenByArgs("sql_auto_is_null")
if TiDBOptOn(normalizedValue) {
if scope == ScopeSession && vars.NoopFuncsMode != OnInt {
if vars.NoopFuncsMode == OffInt {
return Off, errMsg
return Off, errors.Trace(errMsg)
}
vars.StmtCtx.AppendWarning(errMsg)
}
Expand All @@ -211,7 +213,7 @@ var noopSysVars = []*SysVar{
return originalValue, errUnknownSystemVariable.GenWithStackByArgs(TiDBEnableNoopFuncs)
}
if val == Off {
return Off, errMsg
return Off, errors.Trace(errMsg)
}
if val == Warn {
vars.StmtCtx.AppendWarning(errMsg)
Expand Down
4 changes: 2 additions & 2 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1672,9 +1672,9 @@ func (s *SessionVars) RaiseWarningWhenMPPEnforced(warning string) {
return
}
if s.StmtCtx.InExplainStmt {
s.StmtCtx.AppendWarning(errors.New(warning))
s.StmtCtx.AppendWarning(errors.NewNoStackError(warning))
} else {
s.StmtCtx.AppendExtraWarning(errors.New(warning))
s.StmtCtx.AppendExtraWarning(errors.NewNoStackError(warning))
}
}

Expand Down
Loading

0 comments on commit 91fb5d3

Please sign in to comment.