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

executor: print arguments in execute statement in log files #7684

Merged
merged 17 commits into from
Oct 9, 2018
Merged
1 change: 0 additions & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ func (a *ExecStmt) buildExecutor(ctx sessionctx.Context) (Executor, error) {
if err != nil {
return nil, errors.Trace(err)
}
a.Text = executorExec.stmt.Text()
a.isPreparedStmt = true
a.Plan = executorExec.plan
e = executorExec.stmtExec
Expand Down
18 changes: 15 additions & 3 deletions executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
package executor

import (
"fmt"
"math"
"sort"
"strings"

"fmt"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/expression"
Expand Down Expand Up @@ -245,8 +246,15 @@ func (e *DeallocateExec) Next(ctx context.Context, chk *chunk.Chunk) error {
func CompileExecutePreparedStmt(ctx sessionctx.Context, ID uint32, args ...interface{}) (ast.Statement, error) {
execStmt := &ast.ExecuteStmt{ExecID: ID}
execStmt.UsingVars = make([]ast.ExprNode, len(args))
argStrs := make([]string, 0, len(args))
for i, val := range args {
execStmt.UsingVars[i] = ast.NewValueExpr(val)
expr := ast.NewValueExpr(val)
execStmt.UsingVars[i] = expr
str, err := expr.ToString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI failed because expr is nil

if err != nil {
return nil, err
}
argStrs = append(argStrs, str)
}
is := GetInfoSchema(ctx)
execPlan, err := plan.Optimize(ctx, execStmt, is)
Expand All @@ -261,7 +269,11 @@ func CompileExecutePreparedStmt(ctx sessionctx.Context, ID uint32, args ...inter
Ctx: ctx,
}
if prepared, ok := ctx.GetSessionVars().PreparedStmts[ID].(*plan.Prepared); ok {
stmt.Text = prepared.Stmt.Text()
argInfo := ""
if len(argStrs) > 0 {
argInfo = fmt.Sprintf(" [arguments: %s]", strings.Join(argStrs, ","))
}
stmt.Text = prepared.Stmt.Text() + argInfo
}
return stmt, nil
}
Expand Down
3 changes: 2 additions & 1 deletion executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package executor_test

import (
"fmt"
"math"
"strings"

Expand Down Expand Up @@ -101,7 +102,7 @@ func (s *testSuite) TestPrepared(c *C) {
// Check that ast.Statement created by executor.CompileExecutePreparedStmt has query text.
stmt, err := executor.CompileExecutePreparedStmt(tk.Se, stmtId, 1)
c.Assert(err, IsNil)
c.Assert(stmt.OriginText(), Equals, query)
c.Assert(stmt.OriginText(), Equals, fmt.Sprintf("%s [arguments: %d]", query, 1))

// Check that rebuild plan works.
tk.Se.PrepareTxnCtx(ctx)
Expand Down