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
sre-bot authored Mar 13, 2020
1 parent b6c97b7 commit 2ea2b8c
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 @@ -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("<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 @@ -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
}

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 @@ -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))
Expand Down

0 comments on commit 2ea2b8c

Please sign in to comment.