From 7010794f2416838d5ba3810d077e32029a2bc981 Mon Sep 17 00:00:00 2001 From: Reminiscent Date: Thu, 5 Aug 2021 17:17:20 +0800 Subject: [PATCH] planner: show binding in explain format = 'verbose' --- bindinfo/bind_test.go | 7 +++---- executor/executor.go | 1 + planner/optimize.go | 4 ++-- sessionctx/stmtctx/stmtctx.go | 3 +++ 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/bindinfo/bind_test.go b/bindinfo/bind_test.go index a1e53c7f696ac..1e2281ee20438 100644 --- a/bindinfo/bind_test.go +++ b/bindinfo/bind_test.go @@ -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) { diff --git a/executor/executor.go b/executor/executor.go index 8a408911915cd..12cab3e4a8866 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -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 { diff --git a/planner/optimize.go b/planner/optimize.go index 80225befe1b42..5d1453848f51c 100644 --- a/planner/optimize.go +++ b/planner/optimize.go @@ -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. diff --git a/sessionctx/stmtctx/stmtctx.go b/sessionctx/stmtctx/stmtctx.go index 97565705bedc4..88b24b5ae1fa6 100644 --- a/sessionctx/stmtctx/stmtctx.go +++ b/sessionctx/stmtctx/stmtctx.go @@ -182,6 +182,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.