-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9cb794b
add arguments in execute statement
jackysp 2f0cceb
show in debug and slow query
jackysp 3631775
fix CI
jackysp 96f9a29
address comments
jackysp d0bfa24
Merge branch 'master' into execute_args
jackysp f046a8d
convert prepared params to a datum slice
jackysp 7f4dbf7
get args when needed
jackysp 93fd670
print log when needed
jackysp b2c281f
Merge branch 'execute_args' of github.com:jackysp/tidb into execute_args
jackysp 4635005
fix some more
jackysp 617df56
fix reset statement
jackysp 0466a38
prepare stmts in session var
jackysp bab907e
merge master
jackysp 48f2a6f
merge master
jackysp 3114517
merge master
jackysp 8faac5e
merge master
jackysp 139e065
Merge branch 'master' into execute_args
jackysp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,22 +16,16 @@ package executor | |
import ( | ||
"math" | ||
"sort" | ||
"sync/atomic" | ||
|
||
"fmt" | ||
"github.com/pingcap/tidb/ast" | ||
"github.com/pingcap/tidb/config" | ||
"github.com/pingcap/tidb/expression" | ||
"github.com/pingcap/tidb/infoschema" | ||
"github.com/pingcap/tidb/mysql" | ||
"github.com/pingcap/tidb/parser" | ||
plannercore "github.com/pingcap/tidb/planner/core" | ||
"github.com/pingcap/tidb/sessionctx" | ||
"github.com/pingcap/tidb/sessionctx/stmtctx" | ||
"github.com/pingcap/tidb/sessionctx/variable" | ||
"github.com/pingcap/tidb/types" | ||
"github.com/pingcap/tidb/util/chunk" | ||
"github.com/pingcap/tidb/util/memory" | ||
"github.com/pingcap/tidb/util/sqlexec" | ||
"github.com/pkg/errors" | ||
"golang.org/x/net/context" | ||
|
@@ -152,7 +146,7 @@ func (e *PrepareExec) Next(ctx context.Context, chk *chunk.Chunk) error { | |
for i := 0; i < e.ParamCount; i++ { | ||
sorter.markers[i].Order = i | ||
} | ||
prepared := &plannercore.Prepared{ | ||
prepared := &ast.Prepared{ | ||
Stmt: stmt, | ||
Params: sorter.markers, | ||
SchemaVersion: e.is.SchemaMetaVersion(), | ||
|
@@ -219,9 +213,6 @@ func (e *ExecuteExec) Build() error { | |
return errors.Trace(b.err) | ||
} | ||
e.stmtExec = stmtExec | ||
if err = ResetStmtCtx(e.ctx, e.stmt); err != nil { | ||
return err | ||
} | ||
CountStmtNode(e.stmt, e.ctx.GetSessionVars().InRestrictedSQL) | ||
logExpensiveQuery(e.stmt, e.plan) | ||
return nil | ||
|
@@ -249,6 +240,9 @@ func (e *DeallocateExec) Next(ctx context.Context, chk *chunk.Chunk) error { | |
// CompileExecutePreparedStmt compiles a session Execute command to a stmt.Statement. | ||
func CompileExecutePreparedStmt(ctx sessionctx.Context, ID uint32, args ...interface{}) (ast.Statement, error) { | ||
execStmt := &ast.ExecuteStmt{ExecID: ID} | ||
if err := ResetContextOfStmt(ctx, execStmt); err != nil { | ||
return nil, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to add the error trace? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think no need now. |
||
} | ||
execStmt.UsingVars = make([]ast.ExprNode, len(args)) | ||
for i, val := range args { | ||
execStmt.UsingVars[i] = ast.NewValueExpr(val) | ||
|
@@ -265,110 +259,22 @@ func CompileExecutePreparedStmt(ctx sessionctx.Context, ID uint32, args ...inter | |
StmtNode: execStmt, | ||
Ctx: ctx, | ||
} | ||
if prepared, ok := ctx.GetSessionVars().PreparedStmts[ID].(*plannercore.Prepared); ok { | ||
if prepared, ok := ctx.GetSessionVars().PreparedStmts[ID]; ok { | ||
stmt.Text = prepared.Stmt.Text() | ||
} | ||
return stmt, nil | ||
} | ||
|
||
// ResetStmtCtx resets the StmtContext. | ||
// Before every execution, we must clear statement context. | ||
func ResetStmtCtx(ctx sessionctx.Context, s ast.StmtNode) (err error) { | ||
sessVars := ctx.GetSessionVars() | ||
sc := new(stmtctx.StatementContext) | ||
sc.TimeZone = sessVars.Location() | ||
sc.MemTracker = memory.NewTracker(s.Text(), sessVars.MemQuotaQuery) | ||
switch config.GetGlobalConfig().OOMAction { | ||
case config.OOMActionCancel: | ||
sc.MemTracker.SetActionOnExceed(&memory.PanicOnExceed{}) | ||
case config.OOMActionLog: | ||
sc.MemTracker.SetActionOnExceed(&memory.LogOnExceed{}) | ||
default: | ||
sc.MemTracker.SetActionOnExceed(&memory.LogOnExceed{}) | ||
} | ||
|
||
// TODO: Many same bool variables here. | ||
// We should set only two variables ( | ||
// IgnoreErr and StrictSQLMode) to avoid setting the same bool variables and | ||
// pushing them down to TiKV as flags. | ||
switch stmt := s.(type) { | ||
case *ast.UpdateStmt: | ||
sc.InUpdateOrDeleteStmt = true | ||
sc.DupKeyAsWarning = stmt.IgnoreErr | ||
sc.BadNullAsWarning = !sessVars.StrictSQLMode || stmt.IgnoreErr | ||
sc.TruncateAsWarning = !sessVars.StrictSQLMode || stmt.IgnoreErr | ||
sc.DividedByZeroAsWarning = !sessVars.StrictSQLMode || stmt.IgnoreErr | ||
sc.IgnoreZeroInDate = !sessVars.StrictSQLMode || stmt.IgnoreErr | ||
sc.Priority = stmt.Priority | ||
case *ast.DeleteStmt: | ||
sc.InUpdateOrDeleteStmt = true | ||
sc.DupKeyAsWarning = stmt.IgnoreErr | ||
sc.BadNullAsWarning = !sessVars.StrictSQLMode || stmt.IgnoreErr | ||
sc.TruncateAsWarning = !sessVars.StrictSQLMode || stmt.IgnoreErr | ||
sc.DividedByZeroAsWarning = !sessVars.StrictSQLMode || stmt.IgnoreErr | ||
sc.IgnoreZeroInDate = !sessVars.StrictSQLMode || stmt.IgnoreErr | ||
sc.Priority = stmt.Priority | ||
case *ast.InsertStmt: | ||
sc.InInsertStmt = true | ||
sc.DupKeyAsWarning = stmt.IgnoreErr | ||
sc.BadNullAsWarning = !sessVars.StrictSQLMode || stmt.IgnoreErr | ||
sc.TruncateAsWarning = !sessVars.StrictSQLMode || stmt.IgnoreErr | ||
sc.DividedByZeroAsWarning = !sessVars.StrictSQLMode || stmt.IgnoreErr | ||
sc.IgnoreZeroInDate = !sessVars.StrictSQLMode || stmt.IgnoreErr | ||
sc.Priority = stmt.Priority | ||
case *ast.CreateTableStmt, *ast.AlterTableStmt: | ||
// Make sure the sql_mode is strict when checking column default value. | ||
case *ast.LoadDataStmt: | ||
sc.DupKeyAsWarning = true | ||
sc.BadNullAsWarning = true | ||
sc.TruncateAsWarning = !sessVars.StrictSQLMode | ||
case *ast.SelectStmt: | ||
sc.InSelectStmt = true | ||
|
||
// see https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-strict | ||
// said "For statements such as SELECT that do not change data, invalid values | ||
// generate a warning in strict mode, not an error." | ||
// and https://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html | ||
sc.OverflowAsWarning = true | ||
|
||
// Return warning for truncate error in selection. | ||
sc.TruncateAsWarning = true | ||
sc.IgnoreZeroInDate = true | ||
if opts := stmt.SelectStmtOpts; opts != nil { | ||
sc.Priority = opts.Priority | ||
sc.NotFillCache = !opts.SQLCache | ||
func getPreparedStmt(stmt *ast.ExecuteStmt, vars *variable.SessionVars) (ast.StmtNode, error) { | ||
execID := stmt.ExecID | ||
ok := false | ||
if stmt.Name != "" { | ||
if execID, ok = vars.PreparedStmtNameToID[stmt.Name]; !ok { | ||
return nil, plannercore.ErrStmtNotFound | ||
} | ||
sc.PadCharToFullLength = ctx.GetSessionVars().SQLMode.HasPadCharToFullLengthMode() | ||
case *ast.ShowStmt: | ||
sc.IgnoreTruncate = true | ||
sc.IgnoreZeroInDate = true | ||
if stmt.Tp == ast.ShowWarnings || stmt.Tp == ast.ShowErrors { | ||
sc.InShowWarning = true | ||
sc.SetWarnings(sessVars.StmtCtx.GetWarnings()) | ||
} | ||
default: | ||
sc.IgnoreTruncate = true | ||
sc.IgnoreZeroInDate = true | ||
} | ||
if !sessVars.InRestrictedSQL { | ||
if priority := mysql.PriorityEnum(atomic.LoadInt32(&variable.ForcePriority)); priority != mysql.NoPriority { | ||
sc.Priority = priority | ||
} | ||
} | ||
if sessVars.LastInsertID > 0 { | ||
sessVars.PrevLastInsertID = sessVars.LastInsertID | ||
sessVars.LastInsertID = 0 | ||
} | ||
sessVars.ResetPrevAffectedRows() | ||
err = sessVars.SetSystemVar("warning_count", fmt.Sprintf("%d", sessVars.StmtCtx.NumWarnings(false))) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
err = sessVars.SetSystemVar("error_count", fmt.Sprintf("%d", sessVars.StmtCtx.NumWarnings(true))) | ||
if err != nil { | ||
return errors.Trace(err) | ||
if prepared, ok := vars.PreparedStmts[execID]; ok { | ||
return prepared.Stmt, nil | ||
} | ||
sessVars.InsertID = 0 | ||
sessVars.StmtCtx = sc | ||
return | ||
return nil, plannercore.ErrStmtNotFound | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's moved or changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change at Line 1149.