Skip to content

Commit

Permalink
expression: change ExprCtxExtendedImpl to SessionExprContext (#52839
Browse files Browse the repository at this point in the history
)

close #52838
  • Loading branch information
lcwangchao authored Apr 29, 2024
1 parent ff4784e commit 0f0418f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 57 deletions.
49 changes: 22 additions & 27 deletions pkg/expression/contextsession/sessionctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,100 +39,95 @@ import (
"go.uber.org/zap"
)

// sessionctx.Context + *ExprCtxExtendedImpl should implement
// `expression.BuildContext` and `expression.AggFuncBuildContext`
// Only used to assert `ExprCtxExtendedImpl` should implement all methods not in `sessionctx.Context`
var _ exprctx.ExprContext = struct {
sessionctx.Context
*ExprCtxExtendedImpl
}{}

// ExprCtxExtendedImpl extends the sessionctx.Context to implement `expression.BuildContext`
type ExprCtxExtendedImpl struct {
// SessionExprContext should implement the `ExprContext` interface.
var _ exprctx.ExprContext = &SessionExprContext{}

// SessionExprContext implements `ExprContext`
type SessionExprContext struct {
sctx sessionctx.Context
*SessionEvalContext
}

// NewExprExtendedImpl creates a new ExprCtxExtendedImpl.
func NewExprExtendedImpl(sctx sessionctx.Context) *ExprCtxExtendedImpl {
return &ExprCtxExtendedImpl{
// NewSessionExprContext creates a new SessionExprContext.
func NewSessionExprContext(sctx sessionctx.Context) *SessionExprContext {
return &SessionExprContext{
sctx: sctx,
SessionEvalContext: NewSessionEvalContext(sctx),
}
}

// GetEvalCtx returns the EvalContext.
func (ctx *ExprCtxExtendedImpl) GetEvalCtx() exprctx.EvalContext {
func (ctx *SessionExprContext) GetEvalCtx() exprctx.EvalContext {
return ctx.SessionEvalContext
}

// GetCharsetInfo gets charset and collation for current context.
func (ctx *ExprCtxExtendedImpl) GetCharsetInfo() (string, string) {
func (ctx *SessionExprContext) GetCharsetInfo() (string, string) {
return ctx.sctx.GetSessionVars().GetCharsetInfo()
}

// GetDefaultCollationForUTF8MB4 returns the default collation of UTF8MB4.
func (ctx *ExprCtxExtendedImpl) GetDefaultCollationForUTF8MB4() string {
func (ctx *SessionExprContext) GetDefaultCollationForUTF8MB4() string {
return ctx.sctx.GetSessionVars().DefaultCollationForUTF8MB4
}

// GetBlockEncryptionMode returns the variable block_encryption_mode
func (ctx *ExprCtxExtendedImpl) GetBlockEncryptionMode() string {
func (ctx *SessionExprContext) GetBlockEncryptionMode() string {
blockMode, _ := ctx.sctx.GetSessionVars().GetSystemVar(variable.BlockEncryptionMode)
return blockMode
}

// GetSysdateIsNow returns a bool to determine whether Sysdate is an alias of Now function.
// It is the value of variable `tidb_sysdate_is_now`.
func (ctx *ExprCtxExtendedImpl) GetSysdateIsNow() bool {
func (ctx *SessionExprContext) GetSysdateIsNow() bool {
return ctx.sctx.GetSessionVars().SysdateIsNow
}

// GetNoopFuncsMode returns the noop function mode: OFF/ON/WARN values as 0/1/2.
func (ctx *ExprCtxExtendedImpl) GetNoopFuncsMode() int {
func (ctx *SessionExprContext) GetNoopFuncsMode() int {
return ctx.sctx.GetSessionVars().NoopFuncsMode
}

// Rng is used to generate random values.
func (ctx *ExprCtxExtendedImpl) Rng() *mathutil.MysqlRng {
func (ctx *SessionExprContext) Rng() *mathutil.MysqlRng {
return ctx.sctx.GetSessionVars().Rng
}

// IsUseCache indicates whether to cache the build expression in plan cache.
// If SetSkipPlanCache is invoked, it should return false.
func (ctx *ExprCtxExtendedImpl) IsUseCache() bool {
func (ctx *SessionExprContext) IsUseCache() bool {
return ctx.sctx.GetSessionVars().StmtCtx.UseCache()
}

// SetSkipPlanCache sets to skip the plan cache and records the reason.
func (ctx *ExprCtxExtendedImpl) SetSkipPlanCache(reason string) {
func (ctx *SessionExprContext) SetSkipPlanCache(reason string) {
ctx.sctx.GetSessionVars().StmtCtx.SetSkipPlanCache(reason)
}

// AllocPlanColumnID allocates column id for plan.
func (ctx *ExprCtxExtendedImpl) AllocPlanColumnID() int64 {
func (ctx *SessionExprContext) AllocPlanColumnID() int64 {
return ctx.sctx.GetSessionVars().AllocPlanColumnID()
}

// IsInNullRejectCheck returns whether the expression is in null reject check.
func (ctx *ExprCtxExtendedImpl) IsInNullRejectCheck() bool {
func (ctx *SessionExprContext) IsInNullRejectCheck() bool {
return false
}

// GetWindowingUseHighPrecision determines whether to compute window operations without loss of precision.
// see https://dev.mysql.com/doc/refman/8.0/en/window-function-optimization.html for more details.
func (ctx *ExprCtxExtendedImpl) GetWindowingUseHighPrecision() bool {
func (ctx *SessionExprContext) GetWindowingUseHighPrecision() bool {
return ctx.sctx.GetSessionVars().WindowingUseHighPrecision
}

// GetGroupConcatMaxLen returns the value of the 'group_concat_max_len' system variable.
func (ctx *ExprCtxExtendedImpl) GetGroupConcatMaxLen() uint64 {
func (ctx *SessionExprContext) GetGroupConcatMaxLen() uint64 {
return ctx.sctx.GetSessionVars().GroupConcatMaxLen
}

// ConnectionID indicates the connection ID of the current session.
// If the context is not in a session, it should return 0.
func (ctx *ExprCtxExtendedImpl) ConnectionID() uint64 {
func (ctx *SessionExprContext) ConnectionID() uint64 {
return ctx.sctx.GetSessionVars().ConnectionID
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/expression/contextsession/sessionctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func TestSessionEvalContextOptProps(t *testing.T) {

func TestSessionBuildContext(t *testing.T) {
ctx := mock.NewContext()
impl := contextsession.NewExprExtendedImpl(ctx)
impl := contextsession.NewSessionExprContext(ctx)
evalCtx, ok := impl.GetEvalCtx().(*contextsession.SessionEvalContext)
require.True(t, ok)
require.Same(t, evalCtx, impl.SessionEvalContext)
Expand Down
12 changes: 2 additions & 10 deletions pkg/lightning/backend/kv/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,6 @@ type planCtxImpl struct {
*planctximpl.PlanCtxExtendedImpl
}

type exprCtxImpl struct {
*Session
*exprctximpl.ExprCtxExtendedImpl
}

// Session is a trimmed down Session type which only wraps our own trimmed-down
// transaction type and provides the session variables to the TiDB library
// optimized for Lightning.
Expand All @@ -300,7 +295,7 @@ type Session struct {
planctx.EmptyPlanContextExtended
txn transaction
Vars *variable.SessionVars
exprCtx *exprCtxImpl
exprCtx *exprctximpl.SessionExprContext
planctx *planCtxImpl
tblctx *tbctximpl.TableContextImpl
// currently, we only set `CommonAddRecordCtx`
Expand Down Expand Up @@ -363,10 +358,7 @@ func NewSession(options *encode.SessionOptions, logger log.Logger) *Session {
}
vars.TxnCtx = nil
s.Vars = vars
s.exprCtx = &exprCtxImpl{
Session: s,
ExprCtxExtendedImpl: exprctximpl.NewExprExtendedImpl(s),
}
s.exprCtx = exprctximpl.NewSessionExprContext(s)
s.planctx = &planCtxImpl{
Session: s,
PlanCtxExtendedImpl: planctximpl.NewPlanCtxExtendedImpl(s),
Expand Down
1 change: 1 addition & 0 deletions pkg/session/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ go_test(
"//pkg/domain",
"//pkg/executor",
"//pkg/expression",
"//pkg/expression/contextsession",
"//pkg/kv",
"//pkg/meta",
"//pkg/parser",
Expand Down
3 changes: 2 additions & 1 deletion pkg/session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pingcap/tidb/pkg/bindinfo"
"github.com/pingcap/tidb/pkg/ddl"
"github.com/pingcap/tidb/pkg/domain"
"github.com/pingcap/tidb/pkg/expression/contextsession"
"github.com/pingcap/tidb/pkg/meta"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/auth"
Expand Down Expand Up @@ -153,7 +154,7 @@ func TestBootstrapWithError(t *testing.T) {
store: store,
sessionVars: variable.NewSessionVars(nil),
}
se.exprctx = newExpressionContextImpl(se)
se.exprctx = contextsession.NewSessionExprContext(se)
se.pctx = newPlanContextImpl(se)
se.tblctx = tbctximpl.NewTableContextImpl(se, se.exprctx)
globalVarsAccessor := variable.NewMockGlobalAccessor4Tests()
Expand Down
13 changes: 0 additions & 13 deletions pkg/session/contextimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package session

import (
exprctximpl "github.com/pingcap/tidb/pkg/expression/contextsession"
planctx "github.com/pingcap/tidb/pkg/planner/context"
planctximpl "github.com/pingcap/tidb/pkg/planner/contextimpl"
)
Expand All @@ -37,15 +36,3 @@ func newPlanContextImpl(s *session) *planContextImpl {
PlanCtxExtendedImpl: planctximpl.NewPlanCtxExtendedImpl(s),
}
}

type expressionContextImpl struct {
*session
*exprctximpl.ExprCtxExtendedImpl
}

func newExpressionContextImpl(s *session) *expressionContextImpl {
return &expressionContextImpl{
session: s,
ExprCtxExtendedImpl: exprctximpl.NewExprExtendedImpl(s),
}
}
7 changes: 4 additions & 3 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
"github.com/pingcap/tidb/pkg/executor"
"github.com/pingcap/tidb/pkg/expression"
exprctx "github.com/pingcap/tidb/pkg/expression/context"
"github.com/pingcap/tidb/pkg/expression/contextsession"
"github.com/pingcap/tidb/pkg/extension"
"github.com/pingcap/tidb/pkg/extension/extensionimpl"
"github.com/pingcap/tidb/pkg/infoschema"
Expand Down Expand Up @@ -183,7 +184,7 @@ type session struct {
sessionManager util.SessionManager

pctx *planContextImpl
exprctx *expressionContextImpl
exprctx *contextsession.SessionExprContext
tblctx *tbctximpl.TableContextImpl

statsCollector *usage.SessionStatsItem
Expand Down Expand Up @@ -3629,7 +3630,7 @@ func createSessionWithOpt(store kv.Storage, opt *Opt) (*session, error) {
sessionStatesHandlers: make(map[sessionstates.SessionStateType]sessionctx.SessionStatesHandler),
}
s.sessionVars = variable.NewSessionVars(s)
s.exprctx = newExpressionContextImpl(s)
s.exprctx = contextsession.NewSessionExprContext(s)
s.pctx = newPlanContextImpl(s)
s.tblctx = tbctximpl.NewTableContextImpl(s, s.exprctx)

Expand Down Expand Up @@ -3692,7 +3693,7 @@ func CreateSessionWithDomain(store kv.Storage, dom *domain.Domain) (*session, er
stmtStats: stmtstats.CreateStatementStats(),
sessionStatesHandlers: make(map[sessionstates.SessionStateType]sessionctx.SessionStatesHandler),
}
s.exprctx = newExpressionContextImpl(s)
s.exprctx = contextsession.NewSessionExprContext(s)
s.pctx = newPlanContextImpl(s)
s.tblctx = tbctximpl.NewTableContextImpl(s, s.exprctx)
s.mu.values = make(map[fmt.Stringer]any)
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/mock/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var (
// Context represents mocked sessionctx.Context.
type Context struct {
planctx.EmptyPlanContextExtended
*exprctximpl.ExprCtxExtendedImpl
*exprctximpl.SessionExprContext
txn wrapTxn // mock global variable
Store kv.Storage // mock global variable
ctx context.Context
Expand Down Expand Up @@ -617,7 +617,7 @@ func NewContext() *Context {
}
vars := variable.NewSessionVars(sctx)
sctx.sessionVars = vars
sctx.ExprCtxExtendedImpl = exprctximpl.NewExprExtendedImpl(sctx)
sctx.SessionExprContext = exprctximpl.NewSessionExprContext(sctx)
sctx.tblctx = tbctximpl.NewTableContextImpl(sctx, sctx)
vars.InitChunkSize = 2
vars.MaxChunkSize = 32
Expand Down

0 comments on commit 0f0418f

Please sign in to comment.