Skip to content

Commit

Permalink
planner: consider connection collation when reusing cached plans (pin…
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored Sep 19, 2023
1 parent 20f1555 commit ca57ed1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 62 deletions.
1 change: 0 additions & 1 deletion planner/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ go_test(
"plan_cache_lru_test.go",
"plan_cache_param_test.go",
"plan_cache_test.go",
"plan_cache_utils_test.go",
"plan_cacheable_checker_test.go",
"plan_cost_detail_test.go",
"plan_cost_ver1_test.go",
Expand Down
12 changes: 12 additions & 0 deletions planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2755,6 +2755,18 @@ func TestIssue46159(t *testing.T) {
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 1105 skip plan-cache: plan rebuild failed, rebuild to get an unsafe range"))
}

func TestIssue47008(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`set @@time_zone='UTC';`)
tk.MustExec(`set @@collation_connection='utf8_general_ci';`)
tk.MustExec(`prepare s from 'select DATE_FORMAT("2020-01-01","%W") = "wednesday"';`)
tk.MustQuery(`execute s;`).Check(testkit.Rows(`1`))
tk.MustExec(`set @@collation_connection='utf8_bin';`)
tk.MustQuery(`execute s;`).Check(testkit.Rows(`0`))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows(`0`))
}

func TestBuiltinFuncFlen(t *testing.T) {
// same as TestIssue45378 and TestIssue45253
store := testkit.CreateMockStore(t)
Expand Down
13 changes: 9 additions & 4 deletions planner/core/plan_cache_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,11 @@ type planCacheKey struct {
isolationReadEngines map[kv.StoreType]struct{}
selectLimit uint64
bindSQL string
connCollation string
inRestrictedSQL bool
restrictedReadOnly bool
TiDBSuperReadOnly bool
ExprBlacklistTS int64 // expr-pushdown-blacklist can affect query optimization, so we need to consider it in plan cache.
exprBlacklistTS int64 // expr-pushdown-blacklist can affect query optimization, so we need to consider it in plan cache.

memoryUsage int64 // Do not include in hash
hash []byte
Expand Down Expand Up @@ -248,10 +249,11 @@ func (key *planCacheKey) Hash() []byte {
}
key.hash = codec.EncodeInt(key.hash, int64(key.selectLimit))
key.hash = append(key.hash, hack.Slice(key.bindSQL)...)
key.hash = append(key.hash, hack.Slice(key.connCollation)...)
key.hash = append(key.hash, hack.Slice(strconv.FormatBool(key.inRestrictedSQL))...)
key.hash = append(key.hash, hack.Slice(strconv.FormatBool(key.restrictedReadOnly))...)
key.hash = append(key.hash, hack.Slice(strconv.FormatBool(key.TiDBSuperReadOnly))...)
key.hash = codec.EncodeInt(key.hash, key.ExprBlacklistTS)
key.hash = codec.EncodeInt(key.hash, key.exprBlacklistTS)
}
return key.hash
}
Expand All @@ -267,7 +269,7 @@ func (key *planCacheKey) MemoryUsage() (sum int64) {
if key.memoryUsage > 0 {
return key.memoryUsage
}
sum = emptyPlanCacheKeySize + int64(len(key.database)+len(key.stmtText)+len(key.bindSQL)) +
sum = emptyPlanCacheKeySize + int64(len(key.database)+len(key.stmtText)+len(key.bindSQL)+len(key.connCollation)) +
int64(len(key.isolationReadEngines))*size.SizeOfUint8 + int64(cap(key.hash))
key.memoryUsage = sum
return
Expand Down Expand Up @@ -307,6 +309,8 @@ func NewPlanCacheKey(sessionVars *variable.SessionVars, stmtText, stmtDB string,
if sessionVars.TimeZone != nil {
_, timezoneOffset = time.Now().In(sessionVars.TimeZone).Zone()
}
_, connCollation := sessionVars.GetCharsetInfo()

key := &planCacheKey{
database: stmtDB,
connID: sessionVars.ConnectionID,
Expand All @@ -318,10 +322,11 @@ func NewPlanCacheKey(sessionVars *variable.SessionVars, stmtText, stmtDB string,
isolationReadEngines: make(map[kv.StoreType]struct{}),
selectLimit: sessionVars.SelectLimit,
bindSQL: bindSQL,
connCollation: connCollation,
inRestrictedSQL: sessionVars.InRestrictedSQL,
restrictedReadOnly: variable.RestrictedReadOnly.Load(),
TiDBSuperReadOnly: variable.VarTiDBSuperReadOnly.Load(),
ExprBlacklistTS: exprBlacklistTS,
exprBlacklistTS: exprBlacklistTS,
}
for k, v := range sessionVars.IsolationReadEngines {
key.isolationReadEngines[k] = v
Expand Down
57 changes: 0 additions & 57 deletions planner/core/plan_cache_utils_test.go

This file was deleted.

0 comments on commit ca57ed1

Please sign in to comment.