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) #11458

Merged
merged 4 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,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 @@ -819,13 +819,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 @@ -1923,7 +1923,7 @@ func (s *testSuite) 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
2 changes: 1 addition & 1 deletion executor/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (e *GrantExec) Next(ctx context.Context, req *chunk.Chunk) error {
}
user := fmt.Sprintf(`('%s', '%s', '%s')`, user.User.Hostname, user.User.Username, pwd)
sql := fmt.Sprintf(`INSERT INTO %s.%s (Host, User, Password) VALUES %s;`, mysql.SystemDB, mysql.UserTable, user)
_, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.TODO(), sql)
_, err := e.ctx.(sqlexec.SQLExecutor).Execute(ctx, sql)
if err != nil {
return err
}
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 @@ -179,7 +179,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 @@ -265,17 +265,17 @@ 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 ...interface{}) (sqlexec.Statement, error) {
func CompileExecutePreparedStmt(ctx context.Context, sctx sessionctx.Context, ID uint32, args ...interface{}) (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.UsingVars = make([]ast.ExprNode, len(args))
for i, val := range args {
execStmt.UsingVars[i] = ast.NewValueExpr(val)
}
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 @@ -284,11 +284,11 @@ func CompileExecutePreparedStmt(ctx sessionctx.Context, ID uint32, args ...inter
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 @@ -130,13 +130,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, 1)
stmt, err := executor.CompileExecutePreparedStmt(context.TODO(), tk.Se, stmtID, 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
10 changes: 5 additions & 5 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (e *ShowExec) Next(ctx context.Context, req *chunk.Chunk) error {
req.GrowAndReset(e.maxChunkSize)
if e.result == nil {
e.result = newFirstChunk(e)
err := e.fetchAll()
err := e.fetchAll(ctx)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -109,14 +109,14 @@ func (e *ShowExec) Next(ctx context.Context, req *chunk.Chunk) error {
return nil
}

func (e *ShowExec) fetchAll() error {
func (e *ShowExec) fetchAll(ctx context.Context) error {
switch e.Tp {
case ast.ShowCharset:
return e.fetchShowCharset()
case ast.ShowCollation:
return e.fetchShowCollation()
case ast.ShowColumns:
return e.fetchShowColumns()
return e.fetchShowColumns(ctx)
case ast.ShowCreateTable:
return e.fetchShowCreateTable()
case ast.ShowCreateUser:
Expand Down Expand Up @@ -367,7 +367,7 @@ func createOptions(tb *model.TableInfo) string {
return ""
}

func (e *ShowExec) fetchShowColumns() error {
func (e *ShowExec) fetchShowColumns(ctx context.Context) error {
tb, err := e.getTable()

if err != nil {
Expand All @@ -384,7 +384,7 @@ func (e *ShowExec) fetchShowColumns() error {
// Because view's undertable's column could change or recreate, so view's column type may change overtime.
// To avoid this situation we need to generate a logical plan and extract current column types from Schema.
planBuilder := plannercore.NewPlanBuilder(e.ctx, e.is)
viewLogicalPlan, err := planBuilder.BuildDataSourceFromView(e.DBName, tb.Meta())
viewLogicalPlan, err := planBuilder.BuildDataSourceFromView(ctx, e.DBName, tb.Meta())
if err != nil {
return err
}
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 @@ -3864,24 +3864,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 @@ -126,8 +126,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 @@ -136,10 +137,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
Loading