Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: support trace plan target='estimation' statement #30491

Merged
merged 16 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from 13 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: 7 additions & 5 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,11 +978,13 @@ func (b *executorBuilder) buildDDL(v *plannercore.DDL) Executor {
// at build().
func (b *executorBuilder) buildTrace(v *plannercore.Trace) Executor {
t := &TraceExec{
baseExecutor: newBaseExecutor(b.ctx, v.Schema(), v.ID()),
stmtNode: v.StmtNode,
builder: b,
format: v.Format,
optimizerTrace: v.OptimizerTrace,
baseExecutor: newBaseExecutor(b.ctx, v.Schema(), v.ID()),
stmtNode: v.StmtNode,
builder: b,
format: v.Format,

optimizerTrace: v.OptimizerTrace,
optimizerTraceTarget: v.OptimizerTraceTarget,
}
if t.format == plannercore.TraceFormatLog && !t.optimizerTrace {
return &SortExec{
Expand Down
91 changes: 62 additions & 29 deletions executor/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"path/filepath"
"sort"
"strings"
"time"

"github.com/opentracing/basictracer-go"
Expand Down Expand Up @@ -58,8 +59,10 @@ type TraceExec struct {

builder *executorBuilder
format string

// optimizerTrace indicates 'trace plan statement'
optimizerTrace bool
optimizerTrace bool
optimizerTraceTarget string
}

// Next executes real query and collects span later.
Expand Down Expand Up @@ -93,44 +96,74 @@ func (e *TraceExec) Next(ctx context.Context, req *chunk.Chunk) error {
}

func (e *TraceExec) nextOptimizerPlanTrace(ctx context.Context, se sessionctx.Context, req *chunk.Chunk) error {
zf, fileName, err := generateOptimizerTraceFile()
if err != nil {
return err
}
zw := zip.NewWriter(zf)
defer func() {
err := zw.Close()
if err != nil {
logutil.BgLogger().Warn("Closing zip writer failed", zap.Error(err))
}
err = zf.Close()
if err != nil {
logutil.BgLogger().Warn("Closing zip file failed", zap.Error(err))
}
}()
traceZW, err := zw.Create("trace.json")
if err != nil {
return errors.AddStack(err)
}
stmtCtx := se.GetSessionVars().StmtCtx
origin := stmtCtx.EnableOptimizeTrace
stmtCtx.EnableOptimizeTrace = true

time-and-fate marked this conversation as resolved.
Show resolved Hide resolved
var option *bool
var dumpToFile bool
if e.optimizerTraceTarget == core.TracePlanTargetEstimation {
option = &stmtCtx.EnableOptimizerCETrace
dumpToFile = false
} else {
option = &stmtCtx.EnableOptimizeTrace
dumpToFile = true
}

origin := *option
*option = true
defer func() {
stmtCtx.EnableOptimizeTrace = origin
*option = origin
}()
_, _, err = core.OptimizeAstNode(ctx, se, e.stmtNode, se.GetInfoSchema().(infoschema.InfoSchema))

_, _, err := core.OptimizeAstNode(ctx, se, e.stmtNode, se.GetInfoSchema().(infoschema.InfoSchema))
if err != nil {
return err
}
res, err := json.Marshal(se.GetSessionVars().StmtCtx.LogicalOptimizeTrace)
if err != nil {
return errors.AddStack(err)

var traceResult interface{}
if e.optimizerTraceTarget == core.TracePlanTargetEstimation {
traceResult = stmtCtx.OptimizerCETrace
} else {
traceResult = stmtCtx.LogicalOptimizeTrace
}
_, err = traceZW.Write(res)

writer := strings.Builder{}
jsonEncoder := json.NewEncoder(&writer)
// If we do not set this to false, ">", "<", "&"... will be escaped to "\u003c","\u003e", "\u0026"...
jsonEncoder.SetEscapeHTML(false)
err = jsonEncoder.Encode(traceResult)
if err != nil {
return errors.AddStack(err)
}
req.AppendString(0, fileName)
res := []byte(writer.String())

if dumpToFile {
zf, fileName, err := generateOptimizerTraceFile()
if err != nil {
return err
}
zw := zip.NewWriter(zf)
defer func() {
err := zw.Close()
if err != nil {
logutil.BgLogger().Warn("Closing zip writer failed", zap.Error(err))
}
err = zf.Close()
if err != nil {
logutil.BgLogger().Warn("Closing zip file failed", zap.Error(err))
}
}()
traceZW, err := zw.Create("trace.json")
if err != nil {
return errors.AddStack(err)
}
_, err = traceZW.Write(res)
if err != nil {
return errors.AddStack(err)
}
req.AppendString(0, fileName)
} else {
req.AppendBytes(0, res)
}
e.exhausted = true
return nil
}
Expand Down
14 changes: 11 additions & 3 deletions parser/ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,24 @@ func (n *AuthOption) Restore(ctx *format.RestoreCtx) error {
type TraceStmt struct {
stmtNode

Stmt StmtNode
Format string
TracePlan bool
Stmt StmtNode
Format string

TracePlan bool
TracePlanTarget string
}

// Restore implements Node interface.
func (n *TraceStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("TRACE ")
if n.TracePlan {
ctx.WriteKeyWord("PLAN ")
if n.TracePlanTarget != "" {
ctx.WriteKeyWord("TARGET")
ctx.WritePlain(" = ")
ctx.WriteString(n.TracePlanTarget)
ctx.WritePlain(" ")
}
} else if n.Format != "row" {
ctx.WriteKeyWord("FORMAT")
ctx.WritePlain(" = ")
Expand Down
1 change: 1 addition & 0 deletions parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ var tokenMap = map[string]int{
"SWITCHES": switchesSym,
"SYSTEM": system,
"SYSTEM_TIME": systemTime,
"TARGET": target,
"TABLE_CHECKSUM": tableChecksum,
"TABLE": tableKwd,
"TABLES": tables,
Expand Down
Loading