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

*: add trace support for subquery #11182

Merged
merged 14 commits into from
Jul 24, 2019
4 changes: 2 additions & 2 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ func (a *ExecStmt) IsReadOnly(vars *variable.SessionVars) bool {

// RebuildPlan rebuilds current execute statement plan.
// It returns the current information schema version that 'a' is using.
func (a *ExecStmt) RebuildPlan() (int64, error) {
func (a *ExecStmt) RebuildPlan(ctx context.Context) (int64, error) {
is := GetInfoSchema(a.Ctx)
a.InfoSchema = is
if err := plannercore.Preprocess(a.Ctx, a.StmtNode, is, plannercore.InTxnRetry); err != nil {
return 0, err
}
p, err := planner.Optimize(a.Ctx, a.StmtNode, is)
p, err := planner.Optimize(ctx, a.Ctx, a.StmtNode, is)
if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion executor/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c *Compiler) compile(ctx context.Context, stmtNode ast.StmtNode, skipBind
return nil, err
}

finalPlan, err := planner.Optimize(c.Ctx, stmtNode, infoSchema)
finalPlan, err := planner.Optimize(ctx, c.Ctx, stmtNode, infoSchema)
if err != nil {
return nil, err
}
Expand Down
8 changes: 6 additions & 2 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,13 +810,17 @@ func init() {
// While doing optimization in the plan package, we need to execute uncorrelated subquery,
// but the plan package cannot import the executor package because of the dependency cycle.
// So we assign a function implemented in the executor package to the plan package to avoid the dependency cycle.
plannercore.EvalSubquery = func(p plannercore.PhysicalPlan, is infoschema.InfoSchema, sctx sessionctx.Context) (rows [][]types.Datum, err error) {
plannercore.EvalSubquery = func(ctx context.Context, p plannercore.PhysicalPlan, is infoschema.InfoSchema, sctx sessionctx.Context) (rows [][]types.Datum, err error) {
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
span1 := span.Tracer().StartSpan("executor.EvalSubQuery", opentracing.ChildOf(span.Context()))
defer span1.Finish()
}

e := &executorBuilder{is: is, ctx: sctx}
exec := e.build(p)
if e.err != nil {
return rows, err
}
ctx := context.TODO()
err = exec.Open(ctx)
defer terror.Call(exec.Close)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2022,7 +2022,7 @@ func (s *testSuiteP1) TestIsPointGet(c *C) {
c.Check(err, IsNil)
err = plannercore.Preprocess(ctx, stmtNode, infoSchema)
c.Check(err, IsNil)
p, err := planner.Optimize(ctx, stmtNode, infoSchema)
p, err := planner.Optimize(context.TODO(), ctx, stmtNode, infoSchema)
c.Check(err, IsNil)
ret, err := executor.IsPointGetWithPKOrUniqueKeyByAutoCommit(ctx, p)
c.Assert(err, IsNil)
Expand Down
3 changes: 2 additions & 1 deletion executor/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package executor_test

import (
"context"
"fmt"

. "github.com/pingcap/check"
Expand Down Expand Up @@ -63,7 +64,7 @@ func (s *testSuite4) TestStmtLabel(c *C) {
is := executor.GetInfoSchema(tk.Se)
err = plannercore.Preprocess(tk.Se.(sessionctx.Context), stmtNode, is)
c.Assert(err, IsNil)
_, err = planner.Optimize(tk.Se, stmtNode, is)
_, err = planner.Optimize(context.TODO(), tk.Se, stmtNode, is)
c.Assert(err, IsNil)
c.Assert(executor.GetStmtLabel(stmtNode), Equals, tt.label)
}
Expand Down
16 changes: 8 additions & 8 deletions executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (e *PrepareExec) Next(ctx context.Context, req *chunk.Chunk) error {
param.InExecute = false
}
var p plannercore.Plan
p, err = plannercore.BuildLogicalPlan(e.ctx, stmt, e.is)
p, err = plannercore.BuildLogicalPlan(ctx, e.ctx, stmt, e.is)
if err != nil {
return err
}
Expand Down Expand Up @@ -266,14 +266,14 @@ func (e *DeallocateExec) Next(ctx context.Context, req *chunk.Chunk) error {
}

// CompileExecutePreparedStmt compiles a session Execute command to a stmt.Statement.
func CompileExecutePreparedStmt(ctx sessionctx.Context, ID uint32, args []types.Datum) (sqlexec.Statement, error) {
func CompileExecutePreparedStmt(ctx context.Context, sctx sessionctx.Context, ID uint32, args []types.Datum) (sqlexec.Statement, error) {
execStmt := &ast.ExecuteStmt{ExecID: ID}
if err := ResetContextOfStmt(ctx, execStmt); err != nil {
if err := ResetContextOfStmt(sctx, execStmt); err != nil {
return nil, err
}
execStmt.BinaryArgs = args
is := GetInfoSchema(ctx)
execPlan, err := planner.Optimize(ctx, execStmt, is)
is := GetInfoSchema(sctx)
execPlan, err := planner.Optimize(ctx, sctx, execStmt, is)
if err != nil {
return nil, err
}
Expand All @@ -282,11 +282,11 @@ func CompileExecutePreparedStmt(ctx sessionctx.Context, ID uint32, args []types.
InfoSchema: is,
Plan: execPlan,
StmtNode: execStmt,
Ctx: ctx,
Ctx: sctx,
}
if prepared, ok := ctx.GetSessionVars().PreparedStmts[ID]; ok {
if prepared, ok := sctx.GetSessionVars().PreparedStmts[ID]; ok {
stmt.Text = prepared.Stmt.Text()
ctx.GetSessionVars().StmtCtx.OriginalSQL = stmt.Text
sctx.GetSessionVars().StmtCtx.OriginalSQL = stmt.Text
}
return stmt, nil
}
Expand Down
4 changes: 2 additions & 2 deletions executor/seqtest/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ func (s *seqTestSuite) TestPrepared(c *C) {
tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows())

// Check that ast.Statement created by executor.CompileExecutePreparedStmt has query text.
stmt, err := executor.CompileExecutePreparedStmt(tk.Se, stmtID, []types.Datum{types.NewDatum(1)})
stmt, err := executor.CompileExecutePreparedStmt(context.TODO(), tk.Se, stmtID, []types.Datum{types.NewDatum(1)})
c.Assert(err, IsNil)
c.Assert(stmt.OriginText(), Equals, query)

// Check that rebuild plan works.
tk.Se.PrepareTxnCtx(ctx)
_, err = stmt.RebuildPlan()
_, err = stmt.RebuildPlan(ctx)
c.Assert(err, IsNil)
rs, err = stmt.Exec(ctx)
c.Assert(err, IsNil)
Expand Down
1 change: 1 addition & 0 deletions expression/builtin_miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (b *builtinSleepSig) evalInt(row chunk.Row) (int64, bool, error) {
if err != nil {
return 0, isNull, err
}

sessVars := b.ctx.GetSessionVars()
if isNull {
if sessVars.StrictSQLMode {
Expand Down
2 changes: 1 addition & 1 deletion expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
)

// EvalAstExpr evaluates ast expression directly.
var EvalAstExpr func(ctx sessionctx.Context, expr ast.ExprNode) (types.Datum, error)
var EvalAstExpr func(sctx sessionctx.Context, expr ast.ExprNode) (types.Datum, error)

// Expression represents all scalar expression in SQL.
type Expression interface {
Expand Down
17 changes: 9 additions & 8 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3906,24 +3906,25 @@ func (s *testIntegrationSuite) TestFilterExtractFromDNF(c *C) {
},
}

ctx := context.Background()
for _, tt := range tests {
sql := "select * from t where " + tt.exprStr
ctx := tk.Se.(sessionctx.Context)
sc := ctx.GetSessionVars().StmtCtx
stmts, err := session.Parse(ctx, sql)
sctx := tk.Se.(sessionctx.Context)
sc := sctx.GetSessionVars().StmtCtx
stmts, err := session.Parse(sctx, sql)
c.Assert(err, IsNil, Commentf("error %v, for expr %s", err, tt.exprStr))
c.Assert(stmts, HasLen, 1)
is := domain.GetDomain(ctx).InfoSchema()
err = plannercore.Preprocess(ctx, stmts[0], is)
is := domain.GetDomain(sctx).InfoSchema()
err = plannercore.Preprocess(sctx, stmts[0], is)
c.Assert(err, IsNil, Commentf("error %v, for resolve name, expr %s", err, tt.exprStr))
p, err := plannercore.BuildLogicalPlan(ctx, stmts[0], is)
p, err := plannercore.BuildLogicalPlan(ctx, sctx, stmts[0], is)
c.Assert(err, IsNil, Commentf("error %v, for build plan, expr %s", err, tt.exprStr))
selection := p.(plannercore.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)
conds := make([]expression.Expression, 0, len(selection.Conditions))
for _, cond := range selection.Conditions {
conds = append(conds, expression.PushDownNot(ctx, cond, false))
conds = append(conds, expression.PushDownNot(sctx, cond, false))
}
afterFunc := expression.ExtractFiltersFromDNFs(ctx, conds)
afterFunc := expression.ExtractFiltersFromDNFs(sctx, conds)
sort.Slice(afterFunc, func(i, j int) bool {
return bytes.Compare(afterFunc[i].HashCode(sc), afterFunc[j].HashCode(sc)) < 0
})
Expand Down
9 changes: 5 additions & 4 deletions expression/typeinfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ func (s *testInferTypeSuite) TestInferType(c *C) {
tests = append(tests, s.createTestCase4JSONFuncs()...)
tests = append(tests, s.createTestCase4MiscellaneousFunc()...)

ctx := context.Background()
for _, tt := range tests {
ctx := testKit.Se.(sessionctx.Context)
sctx := testKit.Se.(sessionctx.Context)
sql := "select " + tt.sql + " from t"
comment := Commentf("for %s", sql)
stmt, err := s.ParseOneStmt(sql, "", "")
Expand All @@ -137,10 +138,10 @@ func (s *testInferTypeSuite) TestInferType(c *C) {
err = se.NewTxn(context.Background())
c.Assert(err, IsNil)

is := domain.GetDomain(ctx).InfoSchema()
err = plannercore.Preprocess(ctx, stmt, is)
is := domain.GetDomain(sctx).InfoSchema()
err = plannercore.Preprocess(sctx, stmt, is)
c.Assert(err, IsNil, comment)
p, err := plannercore.BuildLogicalPlan(ctx, stmt, is)
p, err := plannercore.BuildLogicalPlan(ctx, sctx, stmt, is)
c.Assert(err, IsNil, comment)
tp := p.Schema().Columns[0].RetType

Expand Down
7 changes: 4 additions & 3 deletions planner/cascades/optimize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package cascades

import (
"context"
"math"
"testing"

Expand Down Expand Up @@ -54,7 +55,7 @@ func (s *testCascadesSuite) TearDownSuite(c *C) {
func (s *testCascadesSuite) TestImplGroupZeroCost(c *C) {
stmt, err := s.ParseOneStmt("select t1.a, t2.a from t as t1 left join t as t2 on t1.a = t2.a where t1.a < 1.0", "", "")
c.Assert(err, IsNil)
p, err := plannercore.BuildLogicalPlan(s.sctx, stmt, s.is)
p, err := plannercore.BuildLogicalPlan(context.Background(), s.sctx, stmt, s.is)
c.Assert(err, IsNil)
logic, ok := p.(plannercore.LogicalPlan)
c.Assert(ok, IsTrue)
Expand All @@ -70,7 +71,7 @@ func (s *testCascadesSuite) TestImplGroupZeroCost(c *C) {
func (s *testCascadesSuite) TestInitGroupSchema(c *C) {
stmt, err := s.ParseOneStmt("select a from t", "", "")
c.Assert(err, IsNil)
p, err := plannercore.BuildLogicalPlan(s.sctx, stmt, s.is)
p, err := plannercore.BuildLogicalPlan(context.Background(), s.sctx, stmt, s.is)
c.Assert(err, IsNil)
logic, ok := p.(plannercore.LogicalPlan)
c.Assert(ok, IsTrue)
Expand All @@ -84,7 +85,7 @@ func (s *testCascadesSuite) TestInitGroupSchema(c *C) {
func (s *testCascadesSuite) TestFillGroupStats(c *C) {
stmt, err := s.ParseOneStmt("select * from t t1 join t t2 on t1.a = t2.a", "", "")
c.Assert(err, IsNil)
p, err := plannercore.BuildLogicalPlan(s.sctx, stmt, s.is)
p, err := plannercore.BuildLogicalPlan(context.Background(), s.sctx, stmt, s.is)
c.Assert(err, IsNil)
logic, ok := p.(plannercore.LogicalPlan)
c.Assert(ok, IsTrue)
Expand Down
11 changes: 6 additions & 5 deletions planner/core/cbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package core_test

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -380,7 +381,7 @@ func (s *testAnalyzeSuite) TestIndexRead(c *C) {
is := domain.GetDomain(ctx).InfoSchema()
err = core.Preprocess(ctx, stmt, is)
c.Assert(err, IsNil)
p, err := planner.Optimize(ctx, stmt, is)
p, err := planner.Optimize(context.TODO(), ctx, stmt, is)
c.Assert(err, IsNil)
c.Assert(core.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
Expand Down Expand Up @@ -430,7 +431,7 @@ func (s *testAnalyzeSuite) TestEmptyTable(c *C) {
is := domain.GetDomain(ctx).InfoSchema()
err = core.Preprocess(ctx, stmt, is)
c.Assert(err, IsNil)
p, err := planner.Optimize(ctx, stmt, is)
p, err := planner.Optimize(context.TODO(), ctx, stmt, is)
c.Assert(err, IsNil)
c.Assert(core.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
Expand Down Expand Up @@ -546,7 +547,7 @@ func (s *testAnalyzeSuite) TestAnalyze(c *C) {
is := domain.GetDomain(ctx).InfoSchema()
err = core.Preprocess(ctx, stmt, is)
c.Assert(err, IsNil)
p, err := planner.Optimize(ctx, stmt, is)
p, err := planner.Optimize(context.TODO(), ctx, stmt, is)
c.Assert(err, IsNil)
c.Assert(core.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
Expand Down Expand Up @@ -623,7 +624,7 @@ func (s *testAnalyzeSuite) TestPreparedNullParam(c *C) {
is := domain.GetDomain(ctx).InfoSchema()
err = core.Preprocess(ctx, stmt, is, core.InPrepare)
c.Assert(err, IsNil)
p, err := planner.Optimize(ctx, stmt, is)
p, err := planner.Optimize(context.TODO(), ctx, stmt, is)
c.Assert(err, IsNil)

c.Assert(core.ToString(p), Equals, best, Commentf("for %s", sql))
Expand Down Expand Up @@ -879,7 +880,7 @@ func BenchmarkOptimize(b *testing.B) {
b.Run(tt.sql, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := planner.Optimize(ctx, stmt, is)
_, err := planner.Optimize(context.TODO(), ctx, stmt, is)
c.Assert(err, IsNil)
}
b.ReportAllocs()
Expand Down
19 changes: 10 additions & 9 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package core

import (
"bytes"
"context"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -177,8 +178,8 @@ type Execute struct {
}

// OptimizePreparedPlan optimizes the prepared statement.
func (e *Execute) OptimizePreparedPlan(ctx sessionctx.Context, is infoschema.InfoSchema) error {
vars := ctx.GetSessionVars()
func (e *Execute) OptimizePreparedPlan(ctx context.Context, sctx sessionctx.Context, is infoschema.InfoSchema) error {
vars := sctx.GetSessionVars()
if e.Name != "" {
e.ExecID = vars.PreparedStmtNameToID[e.Name]
}
Expand Down Expand Up @@ -220,13 +221,13 @@ func (e *Execute) OptimizePreparedPlan(ctx sessionctx.Context, is infoschema.Inf
if prepared.SchemaVersion != is.SchemaMetaVersion() {
// If the schema version has changed we need to preprocess it again,
// if this time it failed, the real reason for the error is schema changed.
err := Preprocess(ctx, prepared.Stmt, is, InPrepare)
err := Preprocess(sctx, prepared.Stmt, is, InPrepare)
if err != nil {
return ErrSchemaChanged.GenWithStack("Schema change caused error: %s", err.Error())
}
prepared.SchemaVersion = is.SchemaMetaVersion()
}
p, err := e.getPhysicalPlan(ctx, is, prepared)
p, err := e.getPhysicalPlan(ctx, sctx, is, prepared)
if err != nil {
return err
}
Expand All @@ -235,13 +236,13 @@ func (e *Execute) OptimizePreparedPlan(ctx sessionctx.Context, is infoschema.Inf
return nil
}

func (e *Execute) getPhysicalPlan(ctx sessionctx.Context, is infoschema.InfoSchema, prepared *ast.Prepared) (Plan, error) {
func (e *Execute) getPhysicalPlan(ctx context.Context, sctx sessionctx.Context, is infoschema.InfoSchema, prepared *ast.Prepared) (Plan, error) {
var cacheKey kvcache.Key
sessionVars := ctx.GetSessionVars()
sessionVars := sctx.GetSessionVars()
sessionVars.StmtCtx.UseCache = prepared.UseCache
if prepared.UseCache {
cacheKey = NewPSTMTPlanCacheKey(sessionVars, e.ExecID, prepared.SchemaVersion)
if cacheValue, exists := ctx.PreparedPlanCache().Get(cacheKey); exists {
if cacheValue, exists := sctx.PreparedPlanCache().Get(cacheKey); exists {
if metrics.ResettablePlanCacheCounterFortTest {
metrics.PlanCacheCounter.WithLabelValues("prepare").Inc()
} else {
Expand All @@ -255,13 +256,13 @@ func (e *Execute) getPhysicalPlan(ctx sessionctx.Context, is infoschema.InfoSche
return plan, nil
}
}
p, err := OptimizeAstNode(ctx, prepared.Stmt, is)
p, err := OptimizeAstNode(ctx, sctx, prepared.Stmt, is)
if err != nil {
return nil, err
}
_, isTableDual := p.(*PhysicalTableDual)
if !isTableDual && prepared.UseCache {
ctx.PreparedPlanCache().Put(cacheKey, NewPSTMTPlanCacheValue(p))
sctx.PreparedPlanCache().Put(cacheKey, NewPSTMTPlanCacheValue(p))
}
return p, err
}
Expand Down
Loading