Skip to content

Commit 1c370d4

Browse files
committed
Added interpretation awareness
1 parent ecbe045 commit 1c370d4

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

processlist.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ func (pl *ProcessList) BeginQuery(
113113
ctx *sql.Context,
114114
query string,
115115
) (*sql.Context, error) {
116+
if ctx.IsInterpreted() {
117+
return ctx, nil
118+
}
116119
pl.mu.Lock()
117120
defer pl.mu.Unlock()
118121

@@ -144,6 +147,9 @@ func (pl *ProcessList) BeginQuery(
144147
}
145148

146149
func (pl *ProcessList) EndQuery(ctx *sql.Context) {
150+
if ctx.IsInterpreted() {
151+
return
152+
}
147153
pl.mu.Lock()
148154
defer pl.mu.Unlock()
149155
id := ctx.Session.ID()
@@ -177,6 +183,9 @@ func (pl *ProcessList) EndQuery(ctx *sql.Context) {
177183
// bracketed with EndOperation(). Should certainly be used for any
178184
// Handler callbacks which may access the database, like Prepare.
179185
func (pl *ProcessList) BeginOperation(ctx *sql.Context) (*sql.Context, error) {
186+
if ctx.IsInterpreted() {
187+
return ctx, nil
188+
}
180189
pl.mu.Lock()
181190
defer pl.mu.Unlock()
182191
id := ctx.Session.ID()
@@ -193,6 +202,9 @@ func (pl *ProcessList) BeginOperation(ctx *sql.Context) (*sql.Context, error) {
193202
}
194203

195204
func (pl *ProcessList) EndOperation(ctx *sql.Context) {
205+
if ctx.IsInterpreted() {
206+
return
207+
}
196208
pl.mu.Lock()
197209
defer pl.mu.Unlock()
198210
id := ctx.Session.ID()

sql/rowexec/transaction_iters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type TransactionCommittingIter struct {
7878

7979
func AddTransactionCommittingIter(ctx *sql.Context, qFlags *sql.QueryFlags, iter sql.RowIter) (sql.RowIter, error) {
8080
// TODO: This is a bit of a hack. Need to figure out better relationship between new transaction node and warnings.
81-
if qFlags != nil && qFlags.IsSet(sql.QFlagShowWarnings) {
81+
if (qFlags != nil && qFlags.IsSet(sql.QFlagShowWarnings)) || ctx.IsInterpreted() {
8282
return iter, nil
8383
}
8484

sql/session.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ type Context struct {
255255
queryTime time.Time
256256
tracer trace.Tracer
257257
rootSpan trace.Span
258+
interpreted bool
258259
Version AnalyzerVersion
259260
}
260261

@@ -328,6 +329,17 @@ func RunWithNowFunc(nowFunc func() time.Time, fn func() error) error {
328329
return fn()
329330
}
330331

332+
// RunInterpreted modifies the context such that all calls to Context.IsInterpreted will return `true`. It is safe to
333+
// recursively call this.
334+
func RunInterpreted[T any](ctx *Context, f func(ctx *Context) (T, error)) (T, error) {
335+
current := ctx.interpreted
336+
ctx.interpreted = true
337+
defer func() {
338+
ctx.interpreted = current
339+
}()
340+
return f(ctx)
341+
}
342+
331343
func swapNowFunc(newNowFunc func() time.Time) func() time.Time {
332344
ctxNowFuncMutex.Lock()
333345
defer ctxNowFuncMutex.Unlock()
@@ -388,6 +400,13 @@ func (c *Context) ApplyOpts(opts ...ContextOption) {
388400
// NewEmptyContext returns a default context with default values.
389401
func NewEmptyContext() *Context { return NewContext(context.TODO()) }
390402

403+
// IsInterpreted returns `true` when this is being called from within RunInterpreted. In such cases, GMS will choose to
404+
// handle logic differently, as running from within an interpreted function requires different considerations than
405+
// running in a standard environment.
406+
func (c *Context) IsInterpreted() bool {
407+
return c.interpreted
408+
}
409+
391410
// Pid returns the process id associated with this context.
392411
func (c *Context) Pid() uint64 {
393412
if c == nil {

0 commit comments

Comments
 (0)