Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 5b68206

Browse files
authored
Merge pull request #859 from carlosms/fix-async-index
Do not cancel context for async queries on success
2 parents 68b087c + 6059987 commit 5b68206

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

engine.go

+12
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ func (e *Engine) Query(
153153
return analyzed.Schema(), iter, nil
154154
}
155155

156+
// Async returns true if the query is async. If there are any errors with the
157+
// query it returns false
158+
func (e *Engine) Async(ctx *sql.Context, query string) bool {
159+
parsed, err := parse.Parse(ctx, query)
160+
if err != nil {
161+
return false
162+
}
163+
164+
asyncNode, ok := parsed.(sql.AsyncNode)
165+
return ok && asyncNode.IsAsync()
166+
}
167+
156168
// AddDatabase adds the given database to the catalog.
157169
func (e *Engine) AddDatabase(db sql.Database) {
158170
e.Catalog.AddDatabase(db)

server/handler.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,13 @@ func (h *Handler) ComQuery(
113113
callback func(*sqltypes.Result) error,
114114
) (err error) {
115115
ctx := h.sm.NewContextWithQuery(c, query)
116-
newCtx, cancel := context.WithCancel(ctx)
117-
defer cancel()
118-
ctx = ctx.WithContext(newCtx)
116+
117+
if !h.e.Async(ctx, query) {
118+
newCtx, cancel := context.WithCancel(ctx)
119+
ctx = ctx.WithContext(newCtx)
120+
121+
defer cancel()
122+
}
119123

120124
handled, err := h.handleKill(c, query)
121125
if err != nil {

sql/core.go

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ type OpaqueNode interface {
120120
Opaque() bool
121121
}
122122

123+
// AsyncNode is a node that can be executed asynchronously.
124+
type AsyncNode interface {
125+
// IsAsync reports whether the node is async or not.
126+
IsAsync() bool
127+
}
128+
123129
// Expressioner is a node that contains expressions.
124130
type Expressioner interface {
125131
// Expressions returns the list of expressions contained by the node.

sql/plan/create_index.go

+5
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ func (c *CreateIndex) WithChildren(children ...sql.Node) (sql.Node, error) {
282282
return &nc, nil
283283
}
284284

285+
// IsAsync implements the AsyncNode interface.
286+
func (c *CreateIndex) IsAsync() bool {
287+
return c.Async
288+
}
289+
285290
// getColumnsAndPrepareExpressions extracts the unique columns required by all
286291
// those expressions and fixes the indexes of the GetFields in the expressions
287292
// to match a row with only the returned columns in that same order.

0 commit comments

Comments
 (0)