Skip to content

Commit

Permalink
planner: fix space requirement when binding from history (#41140) (#4…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot committed Feb 14, 2023
1 parent 1b9e79c commit ba22f43
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
28 changes: 28 additions & 0 deletions infoschema/cluster_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,3 +852,31 @@ func TestMDLView(t *testing.T) {

wg.Wait()
}

func TestCreateBindingForPrepareToken(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil))

tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b time, c varchar(5))")

//some builtin functions listed in https://dev.mysql.com/doc/refman/8.0/en/function-resolution.html
cases := []string{
"select std(a) from t",
"select cast(a as decimal(10, 2)) from t",
"select bit_or(a) from t",
"select min(a) from t",
"select max(a) from t",
"select substr(c, 1, 2) from t",
}

for _, sql := range cases {
prep := fmt.Sprintf("prepare stmt from '%s'", sql)
tk.MustExec(prep)
tk.MustExec("execute stmt")
planDigest := tk.MustQuery(fmt.Sprintf("select plan_digest from information_schema.statements_summary where query_sample_text = '%s'", sql)).Rows()
tk.MustExec(fmt.Sprintf("create binding from history using plan digest '%s'", planDigest[0][0]))
}
}
6 changes: 4 additions & 2 deletions util/stmtsummary/statement_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,12 @@ func getBindableStmtByPlanDigest(ssbd *stmtSummaryByDigest, planDigest string) *
Collation: ssElement.collation,
Users: ssElement.authUsers,
}
// If it is SQL command prepare / execute, the ssElement.sampleSQL is `execute ...`, we should get the original select query.
// If it is SQL command prepare / execute, we should remove the arguments
// If it is binary protocol prepare / execute, ssbd.normalizedSQL should be same as ssElement.sampleSQL.
if ssElement.prepared {
stmt.Query = ssbd.normalizedSQL
if idx := strings.LastIndex(stmt.Query, "[arguments:"); idx != -1 {
stmt.Query = stmt.Query[:idx]
}
}
return stmt
}
Expand Down

0 comments on commit ba22f43

Please sign in to comment.