errctx, stmtctx: add cache to the errctx (#49689)

close pingcap/tidb#49688
This commit is contained in:
YangKeao
2023-12-22 15:05:16 +08:00
committed by GitHub
parent 82a011fdd8
commit e61ee664f5
3 changed files with 21 additions and 5 deletions

View File

@ -41,7 +41,7 @@ go_test(
],
embed = [":stmtctx"],
flaky = True,
shard_count = 11,
shard_count = 12,
deps = [
"//pkg/kv",
"//pkg/sessionctx/variable",

View File

@ -442,6 +442,7 @@ func NewStmtCtxWithTimeZone(tz *time.Location) *StatementContext {
ctxID: stmtCtxIDGenerator.Add(1),
}
sc.typeCtx = types.NewContext(types.DefaultStmtFlags, tz, sc)
sc.initErrCtx()
return sc
}
@ -451,6 +452,7 @@ func (sc *StatementContext) Reset() {
ctxID: stmtCtxIDGenerator.Add(1),
typeCtx: types.NewContext(types.DefaultStmtFlags, time.UTC, sc),
}
sc.initErrCtx()
}
// CtxID returns the context id of the statement
@ -479,9 +481,7 @@ func (sc *StatementContext) TypeCtx() types.Context {
return sc.typeCtx
}
// ErrCtx returns the error context
// TODO: add a cache to the `ErrCtx` if needed, though it's not a big burden to generate `ErrCtx` everytime.
func (sc *StatementContext) ErrCtx() errctx.Context {
func (sc *StatementContext) initErrCtx() {
ctx := errctx.NewContext(sc)
if sc.TypeFlags().IgnoreTruncateErr() {
@ -489,8 +489,12 @@ func (sc *StatementContext) ErrCtx() errctx.Context {
} else if sc.TypeFlags().TruncateAsWarning() {
ctx = ctx.WithErrGroupLevel(errctx.ErrGroupTruncate, errctx.LevelWarn)
}
sc.errCtx = ctx
}
return ctx
// ErrCtx returns the error context
func (sc *StatementContext) ErrCtx() errctx.Context {
return sc.errCtx
}
// TypeFlags returns the type flags
@ -501,6 +505,7 @@ func (sc *StatementContext) TypeFlags() types.Flags {
// SetTypeFlags sets the type flags
func (sc *StatementContext) SetTypeFlags(flags types.Flags) {
sc.typeCtx = sc.typeCtx.WithFlags(flags)
sc.initErrCtx()
}
// HandleTruncate ignores or returns the error based on the TypeContext inside.

View File

@ -408,6 +408,17 @@ func TestStmtCtxID(t *testing.T) {
}
}
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()