From 2ea2b8c40ad331f201f602c01844d256370a3f72 Mon Sep 17 00:00:00 2001 From: pingcap-github-bot Date: Fri, 13 Mar 2020 13:57:24 +0800 Subject: [PATCH] infoschema, planner: fix wrong `table_names` in statement summary (#15228) (#15231) --- 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 caa6ded05fab8..3e11bd30f1592 100644 --- a/infoschema/perfschema/tables_test.go +++ b/infoschema/perfschema/tables_test.go @@ -107,14 +107,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 50861087f67b5..94d5fca0997ea 100644 --- a/planner/core/point_get_plan.go +++ b/planner/core/point_get_plan.go @@ -308,7 +308,7 @@ func newPointGetPlan(ctx sessionctx.Context, dbName string, schema *expression.S TblInfo: tbl, 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 7d48b064694de..b63986976fe55 100644 --- a/util/stmtsummary/statement_summary.go +++ b/util/stmtsummary/statement_summary.go @@ -501,6 +501,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))