@@ -19,12 +19,10 @@ import (
19
19
"math"
20
20
"time"
21
21
22
- "github.com/pingcap/errors"
23
22
"github.com/pingcap/tidb/pkg/errctx"
24
23
exprctx "github.com/pingcap/tidb/pkg/expression/context"
25
24
"github.com/pingcap/tidb/pkg/expression/contextopt"
26
25
infoschema "github.com/pingcap/tidb/pkg/infoschema/context"
27
- "github.com/pingcap/tidb/pkg/parser/ast"
28
26
"github.com/pingcap/tidb/pkg/parser/auth"
29
27
"github.com/pingcap/tidb/pkg/parser/model"
30
28
"github.com/pingcap/tidb/pkg/parser/mysql"
@@ -34,10 +32,8 @@ import (
34
32
"github.com/pingcap/tidb/pkg/sessionctx/variable"
35
33
"github.com/pingcap/tidb/pkg/types"
36
34
"github.com/pingcap/tidb/pkg/util"
37
- "github.com/pingcap/tidb/pkg/util/chunk"
38
35
"github.com/pingcap/tidb/pkg/util/intest"
39
36
"github.com/pingcap/tidb/pkg/util/logutil"
40
- "github.com/pingcap/tidb/pkg/util/sqlexec"
41
37
"github.com/tikv/client-go/v2/oracle"
42
38
"go.uber.org/zap"
43
39
)
@@ -51,92 +47,104 @@ var _ exprctx.BuildContext = struct {
51
47
52
48
// ExprCtxExtendedImpl extends the sessionctx.Context to implement `expression.BuildContext`
53
49
type ExprCtxExtendedImpl struct {
54
- sctx sessionctx.Context
55
- props contextopt.OptionalEvalPropProviders
50
+ * SessionEvalContext
56
51
}
57
52
58
53
// NewExprExtendedImpl creates a new ExprCtxExtendedImpl.
59
54
func NewExprExtendedImpl (sctx sessionctx.Context ) * ExprCtxExtendedImpl {
60
- impl := & ExprCtxExtendedImpl {sctx : sctx }
55
+ return & ExprCtxExtendedImpl {
56
+ SessionEvalContext : NewSessionEvalContext (sctx ),
57
+ }
58
+ }
59
+
60
+ // SessionEvalContext implements the `expression.EvalContext` interface to provide evaluation context in session.
61
+ type SessionEvalContext struct {
62
+ sctx sessionctx.Context
63
+ props contextopt.OptionalEvalPropProviders
64
+ }
65
+
66
+ // NewSessionEvalContext creates a new SessionEvalContext.
67
+ func NewSessionEvalContext (sctx sessionctx.Context ) * SessionEvalContext {
68
+ ctx := & SessionEvalContext {sctx : sctx }
61
69
// set all optional properties
62
- impl .setOptionalProp (currentUserProp (sctx ))
63
- impl .setOptionalProp (contextopt .NewSessionVarsProvider (sctx ))
64
- impl .setOptionalProp (infoSchemaProp (sctx ))
65
- impl .setOptionalProp (contextopt .KVStorePropProvider (sctx .GetStore ))
66
- impl .setOptionalProp (sqlExecutorProp (sctx ))
67
- impl .setOptionalProp (sequenceOperatorProp (sctx ))
68
- impl .setOptionalProp (contextopt .NewAdvisoryLockPropProvider (sctx ))
69
- impl .setOptionalProp (contextopt .DDLOwnerInfoProvider (sctx .IsDDLOwner ))
70
+ ctx .setOptionalProp (currentUserProp (sctx ))
71
+ ctx .setOptionalProp (contextopt .NewSessionVarsProvider (sctx ))
72
+ ctx .setOptionalProp (infoSchemaProp (sctx ))
73
+ ctx .setOptionalProp (contextopt .KVStorePropProvider (sctx .GetStore ))
74
+ ctx .setOptionalProp (sqlExecutorProp (sctx ))
75
+ ctx .setOptionalProp (sequenceOperatorProp (sctx ))
76
+ ctx .setOptionalProp (contextopt .NewAdvisoryLockPropProvider (sctx ))
77
+ ctx .setOptionalProp (contextopt .DDLOwnerInfoProvider (sctx .IsDDLOwner ))
70
78
// When EvalContext is created from a session, it should contain all the optional properties.
71
- intest .Assert (impl .props .PropKeySet ().IsFull ())
72
- return impl
79
+ intest .Assert (ctx .props .PropKeySet ().IsFull ())
80
+ return ctx
73
81
}
74
82
75
- func (ctx * ExprCtxExtendedImpl ) setOptionalProp (prop exprctx.OptionalEvalPropProvider ) {
83
+ func (ctx * SessionEvalContext ) setOptionalProp (prop exprctx.OptionalEvalPropProvider ) {
76
84
intest .AssertFunc (func () bool {
77
85
return ! ctx .props .Contains (prop .Desc ().Key ())
78
86
})
79
87
ctx .props .Add (prop )
80
88
}
81
89
82
90
// CtxID returns the context id.
83
- func (ctx * ExprCtxExtendedImpl ) CtxID () uint64 {
91
+ func (ctx * SessionEvalContext ) CtxID () uint64 {
84
92
return ctx .sctx .GetSessionVars ().StmtCtx .CtxID ()
85
93
}
86
94
87
95
// SQLMode returns the sql mode
88
- func (ctx * ExprCtxExtendedImpl ) SQLMode () mysql.SQLMode {
96
+ func (ctx * SessionEvalContext ) SQLMode () mysql.SQLMode {
89
97
return ctx .sctx .GetSessionVars ().SQLMode
90
98
}
91
99
92
100
// TypeCtx returns the types.Context
93
- func (ctx * ExprCtxExtendedImpl ) TypeCtx () types.Context {
101
+ func (ctx * SessionEvalContext ) TypeCtx () types.Context {
94
102
return ctx .sctx .GetSessionVars ().StmtCtx .TypeCtx ()
95
103
}
96
104
97
105
// ErrCtx returns the errctx.Context
98
- func (ctx * ExprCtxExtendedImpl ) ErrCtx () errctx.Context {
106
+ func (ctx * SessionEvalContext ) ErrCtx () errctx.Context {
99
107
return ctx .sctx .GetSessionVars ().StmtCtx .ErrCtx ()
100
108
}
101
109
102
110
// Location returns the timezone info
103
- func (ctx * ExprCtxExtendedImpl ) Location () * time.Location {
111
+ func (ctx * SessionEvalContext ) Location () * time.Location {
104
112
tc := ctx .TypeCtx ()
105
113
return tc .Location ()
106
114
}
107
115
108
116
// AppendWarning append warnings to the context.
109
- func (ctx * ExprCtxExtendedImpl ) AppendWarning (err error ) {
117
+ func (ctx * SessionEvalContext ) AppendWarning (err error ) {
110
118
ctx .sctx .GetSessionVars ().StmtCtx .AppendWarning (err )
111
119
}
112
120
113
121
// WarningCount gets warning count.
114
- func (ctx * ExprCtxExtendedImpl ) WarningCount () int {
122
+ func (ctx * SessionEvalContext ) WarningCount () int {
115
123
return int (ctx .sctx .GetSessionVars ().StmtCtx .WarningCount ())
116
124
}
117
125
118
126
// TruncateWarnings truncates warnings begin from start and returns the truncated warnings.
119
- func (ctx * ExprCtxExtendedImpl ) TruncateWarnings (start int ) []stmtctx.SQLWarn {
127
+ func (ctx * SessionEvalContext ) TruncateWarnings (start int ) []stmtctx.SQLWarn {
120
128
return ctx .sctx .GetSessionVars ().StmtCtx .TruncateWarnings (start )
121
129
}
122
130
123
131
// CurrentDB returns the current database name
124
- func (ctx * ExprCtxExtendedImpl ) CurrentDB () string {
132
+ func (ctx * SessionEvalContext ) CurrentDB () string {
125
133
return ctx .sctx .GetSessionVars ().CurrentDB
126
134
}
127
135
128
136
// CurrentTime returns the current time
129
- func (ctx * ExprCtxExtendedImpl ) CurrentTime () (time.Time , error ) {
137
+ func (ctx * SessionEvalContext ) CurrentTime () (time.Time , error ) {
130
138
return getStmtTimestamp (ctx .sctx )
131
139
}
132
140
133
141
// GetMaxAllowedPacket returns the value of the 'max_allowed_packet' system variable.
134
- func (ctx * ExprCtxExtendedImpl ) GetMaxAllowedPacket () uint64 {
142
+ func (ctx * SessionEvalContext ) GetMaxAllowedPacket () uint64 {
135
143
return ctx .sctx .GetSessionVars ().MaxAllowedPacket
136
144
}
137
145
138
146
// GetDefaultWeekFormatMode returns the value of the 'default_week_format' system variable.
139
- func (ctx * ExprCtxExtendedImpl ) GetDefaultWeekFormatMode () string {
147
+ func (ctx * SessionEvalContext ) GetDefaultWeekFormatMode () string {
140
148
mode , ok := ctx .sctx .GetSessionVars ().GetSystemVar (variable .DefaultWeekFormat )
141
149
if ! ok || mode == "" {
142
150
return "0"
@@ -145,22 +153,22 @@ func (ctx *ExprCtxExtendedImpl) GetDefaultWeekFormatMode() string {
145
153
}
146
154
147
155
// GetDivPrecisionIncrement returns the specified value of DivPrecisionIncrement.
148
- func (ctx * ExprCtxExtendedImpl ) GetDivPrecisionIncrement () int {
156
+ func (ctx * SessionEvalContext ) GetDivPrecisionIncrement () int {
149
157
return ctx .sctx .GetSessionVars ().GetDivPrecisionIncrement ()
150
158
}
151
159
152
160
// GetOptionalPropSet gets the optional property set from context
153
- func (ctx * ExprCtxExtendedImpl ) GetOptionalPropSet () exprctx.OptionalEvalPropKeySet {
161
+ func (ctx * SessionEvalContext ) GetOptionalPropSet () exprctx.OptionalEvalPropKeySet {
154
162
return ctx .props .PropKeySet ()
155
163
}
156
164
157
165
// GetOptionalPropProvider gets the optional property provider by key
158
- func (ctx * ExprCtxExtendedImpl ) GetOptionalPropProvider (key exprctx.OptionalEvalPropKey ) (exprctx.OptionalEvalPropProvider , bool ) {
166
+ func (ctx * SessionEvalContext ) GetOptionalPropProvider (key exprctx.OptionalEvalPropKey ) (exprctx.OptionalEvalPropProvider , bool ) {
159
167
return ctx .props .Get (key )
160
168
}
161
169
162
170
// RequestVerification verifies user privilege
163
- func (ctx * ExprCtxExtendedImpl ) RequestVerification (db , table , column string , priv mysql.PrivilegeType ) bool {
171
+ func (ctx * SessionEvalContext ) RequestVerification (db , table , column string , priv mysql.PrivilegeType ) bool {
164
172
checker := privilege .GetPrivilegeManager (ctx .sctx )
165
173
if checker == nil {
166
174
return true
@@ -169,7 +177,7 @@ func (ctx *ExprCtxExtendedImpl) RequestVerification(db, table, column string, pr
169
177
}
170
178
171
179
// RequestDynamicVerification verifies user privilege for a DYNAMIC privilege.
172
- func (ctx * ExprCtxExtendedImpl ) RequestDynamicVerification (privName string , grantable bool ) bool {
180
+ func (ctx * SessionEvalContext ) RequestDynamicVerification (privName string , grantable bool ) bool {
173
181
checker := privilege .GetPrivilegeManager (ctx .sctx )
174
182
if checker == nil {
175
183
return true
@@ -223,27 +231,9 @@ func infoSchemaProp(sctx sessionctx.Context) contextopt.InfoSchemaPropProvider {
223
231
}
224
232
}
225
233
226
- type sqlExecutor struct {
227
- exec sqlexec.RestrictedSQLExecutor
228
- }
229
-
230
- // NewSQLExecutor creates a new SQLExecutor.
231
- func NewSQLExecutor (sctx sessionctx.Context ) (contextopt.SQLExecutor , error ) {
232
- if e , ok := sctx .(sqlexec.RestrictedSQLExecutor ); ok {
233
- return & sqlExecutor {exec : e }, nil
234
- }
235
- return nil , errors .Errorf ("'%T' cannot be casted to sqlexec.RestrictedSQLExecutor" , sctx )
236
- }
237
-
238
- func (e * sqlExecutor ) ExecRestrictedSQL (
239
- ctx context.Context , sql string , args ... any ,
240
- ) ([]chunk.Row , []* ast.ResultField , error ) {
241
- return e .exec .ExecRestrictedSQL (ctx , nil , sql , args ... )
242
- }
243
-
244
234
func sqlExecutorProp (sctx sessionctx.Context ) contextopt.SQLExecutorPropProvider {
245
235
return func () (contextopt.SQLExecutor , error ) {
246
- return NewSQLExecutor ( sctx )
236
+ return sctx . GetRestrictedSQLExecutor (), nil
247
237
}
248
238
}
249
239
0 commit comments