Skip to content

Commit

Permalink
infoschema, planner: fix wrong table_names in statement summary (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
djshow832 authored Mar 9, 2020
1 parent 55b3e11 commit bb329c5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
18 changes: 16 additions & 2 deletions infoschema/perfschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<nil>"))

// Test SELECT.
const failpointName = "github.com/pingcap/tidb/planner/core/mockPlanRowCount"
c.Assert(failpoint.Enable(failpointName, "return(100)"), IsNil)
Expand Down
2 changes: 1 addition & 1 deletion planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions util/stmtsummary/statement_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit bb329c5

Please sign in to comment.