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

Do not cancel context for async queries on success #859

Merged
merged 1 commit into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ func (e *Engine) Query(
return analyzed.Schema(), iter, nil
}

// Async returns true if the query is async. If there are any errors with the
// query it returns false
func (e *Engine) Async(ctx *sql.Context, query string) bool {
parsed, err := parse.Parse(ctx, query)
if err != nil {
return false
}

asyncNode, ok := parsed.(sql.AsyncNode)
return ok && asyncNode.IsAsync()
}

// AddDatabase adds the given database to the catalog.
func (e *Engine) AddDatabase(db sql.Database) {
e.Catalog.AddDatabase(db)
Expand Down
10 changes: 7 additions & 3 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ func (h *Handler) ComQuery(
callback func(*sqltypes.Result) error,
) (err error) {
ctx := h.sm.NewContextWithQuery(c, query)
newCtx, cancel := context.WithCancel(ctx)
defer cancel()
ctx = ctx.WithContext(newCtx)

if !h.e.Async(ctx, query) {
newCtx, cancel := context.WithCancel(ctx)
ctx = ctx.WithContext(newCtx)

defer cancel()
}

handled, err := h.handleKill(c, query)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions sql/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ type OpaqueNode interface {
Opaque() bool
}

// AsyncNode is a node that can be executed asynchronously.
type AsyncNode interface {
// IsAsync reports whether the node is async or not.
IsAsync() bool
}

// Expressioner is a node that contains expressions.
type Expressioner interface {
// Expressions returns the list of expressions contained by the node.
Expand Down
5 changes: 5 additions & 0 deletions sql/plan/create_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ func (c *CreateIndex) WithChildren(children ...sql.Node) (sql.Node, error) {
return &nc, nil
}

// IsAsync implements the AsyncNode interface.
func (c *CreateIndex) IsAsync() bool {
return c.Async
}

// getColumnsAndPrepareExpressions extracts the unique columns required by all
// those expressions and fixes the indexes of the GetFields in the expressions
// to match a row with only the returned columns in that same order.
Expand Down