From bb329c56c99614d76cf16d94a08e73b8ac15ff8e Mon Sep 17 00:00:00 2001 From: djshow832 <873581766@qq.com> Date: Mon, 9 Mar 2020 17:54:16 +0800 Subject: [PATCH] infoschema, planner: fix wrong `table_names` in statement summary (#15228) --- infoschema/perfschema/tables_test.go | 18 ++++++++++++++++-- planner/core/point_get_plan.go | 2 +- util/stmtsummary/statement_summary.go | 4 ++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/infoschema/perfschema/tables_test.go b/infoschema/perfschema/tables_test.go index cc4a9e57a7e40..5b5bc1ab0587d 100644 --- a/infoschema/perfschema/tables_test.go +++ b/infoschema/perfschema/tables_test.go @@ -117,14 +117,28 @@ func (s *testTableSuite) TestStmtSummaryTable(c *C) { tk.MustExec("create table p(a int primary key, b int)") for i := 1; i < 3; i++ { tk.MustQuery("select b from p where a=1") - expectedResult := fmt.Sprintf("%d \tPoint_Get_1\troot\t1\ttable:p, handle:1", i) + expectedResult := fmt.Sprintf("%d \tPoint_Get_1\troot\t1\ttable:p, handle:1 %s", i, "test.p") // Also make sure that the plan digest is not empty - tk.MustQuery(`select exec_count, plan + tk.MustQuery(`select exec_count, plan, table_names from performance_schema.events_statements_summary_by_digest where digest_text like 'select b from p%' and plan_digest != ''`, ).Check(testkit.Rows(expectedResult)) } + // Point get another database. + tk.MustQuery("select variable_value from mysql.tidb where variable_name = 'system_tz'") + tk.MustQuery(`select table_names + from performance_schema.events_statements_summary_by_digest + where digest_text like 'select variable_value%' and schema_name='test'`, + ).Check(testkit.Rows("mysql.tidb")) + + // Test `create database`. + tk.MustExec("create database if not exists test") + tk.MustQuery(`select table_names + from performance_schema.events_statements_summary_by_digest + where digest_text like 'create database%' and schema_name='test'`, + ).Check(testkit.Rows("")) + // Test SELECT. const failpointName = "github.com/pingcap/tidb/planner/core/mockPlanRowCount" c.Assert(failpoint.Enable(failpointName, "return(100)"), IsNil) diff --git a/planner/core/point_get_plan.go b/planner/core/point_get_plan.go index 195c4be391a14..3d875ec5f6261 100644 --- a/planner/core/point_get_plan.go +++ b/planner/core/point_get_plan.go @@ -735,7 +735,7 @@ func newPointGetPlan(ctx sessionctx.Context, dbName string, schema *expression.S outputNames: names, LockWaitTime: ctx.GetSessionVars().LockWaitTimeout, } - ctx.GetSessionVars().StmtCtx.Tables = []stmtctx.TableEntry{{DB: ctx.GetSessionVars().CurrentDB, Table: tbl.Name.L}} + ctx.GetSessionVars().StmtCtx.Tables = []stmtctx.TableEntry{{DB: dbName, Table: tbl.Name.L}} return p } diff --git a/util/stmtsummary/statement_summary.go b/util/stmtsummary/statement_summary.go index 0248388180162..00ffe03a0c76a 100644 --- a/util/stmtsummary/statement_summary.go +++ b/util/stmtsummary/statement_summary.go @@ -500,6 +500,10 @@ func (ssbd *stmtSummaryByDigest) init(sei *StmtExecInfo, beginTime int64, interv // Use "," to separate table names to support FIND_IN_SET. var buffer bytes.Buffer for i, value := range sei.StmtCtx.Tables { + // In `create database` statement, DB name is not empty but table name is empty. + if len(value.Table) == 0 { + continue + } buffer.WriteString(strings.ToLower(value.DB)) buffer.WriteString(".") buffer.WriteString(strings.ToLower(value.Table))