Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infoschema, planner: fix wrong table_names in statement summary #15228

Merged
merged 2 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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