Skip to content

Commit

Permalink
planner: show binding information in explain format = 'verbose' (#26930)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reminiscent authored Aug 5, 2021
1 parent 3cda7d0 commit c08de09
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
7 changes: 3 additions & 4 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2036,10 +2036,9 @@ func (s *testSuite) TestExplainShowBindSQL(c *C) {
"select * from `test` . `t` SELECT * FROM `test`.`t` USE INDEX (`a`)",
))

tk.MustExec("explain select * from t")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 Using the bindSQL: SELECT * FROM `test`.`t` USE INDEX (`a`)"))
tk.MustExec("explain analyze select * from t")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 Using the bindSQL: SELECT * FROM `test`.`t` USE INDEX (`a`)"))
tk.MustExec("explain format = 'verbose' select * from t")
tk.MustQuery("show warnings").Check(testkit.Rows("Note 1105 Using the bindSQL: SELECT * FROM `test`.`t` USE INDEX (`a`)"))
// explain analyze do not support verbose yet.
}

func (s *testSuite) TestDMLIndexHintBind(c *C) {
Expand Down
1 change: 1 addition & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,7 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
if explainStmt, ok := s.(*ast.ExplainStmt); ok {
sc.InExplainStmt = true
sc.IgnoreExplainIDSuffix = (strings.ToLower(explainStmt.Format) == types.ExplainFormatBrief)
sc.InVerboseExplain = strings.ToLower(explainStmt.Format) == types.ExplainFormatVerbose
s = explainStmt.Stmt
}
if _, ok := s.(*ast.ExplainForStmt); ok {
Expand Down
4 changes: 2 additions & 2 deletions planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ func Optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
if err := setFoundInBinding(sctx, true); err != nil {
logutil.BgLogger().Warn("set tidb_found_in_binding failed", zap.Error(err))
}
if _, ok := stmtNode.(*ast.ExplainStmt); ok {
sessVars.StmtCtx.AppendWarning(errors.Errorf("Using the bindSQL: %v", chosenBinding.BindSQL))
if sessVars.StmtCtx.InVerboseExplain {
sessVars.StmtCtx.AppendNote(errors.Errorf("Using the bindSQL: %v", chosenBinding.BindSQL))
}
}
// Restore the hint to avoid changing the stmt node.
Expand Down
3 changes: 3 additions & 0 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ type StatementContext struct {
DiskTracker disk.Tracker
LogOnExceed [2]memory.LogOnExceed
}

// InVerboseExplain indicates the statement is "explain format='verbose' ...".
InVerboseExplain bool
}

// StmtHints are SessionVars related sql hints.
Expand Down

0 comments on commit c08de09

Please sign in to comment.