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

planner: fix the issue that the optimizer caches wrong TableDual plans under binary protocol #34709

Merged
merged 8 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
23 changes: 18 additions & 5 deletions planner/core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package core

import (
"bytes"
"math"
"sync/atomic"
"time"
Expand Down Expand Up @@ -194,25 +195,37 @@ type PlanCacheValue struct {
Plan Plan
OutPutNames []*types.FieldName
TblInfo2UnionScan map[*model.TableInfo]bool
UserVarTypes FieldSlice
TxtVarTypes FieldSlice // variable types under text protocol
BinVarTypes []byte // variable types under binary protocol
IsBinProto bool // whether this plan is under binary protocol
BindSQL string
}

func (v *PlanCacheValue) varTypesUnchanged(binVarTps []byte, txtVarTps []*types.FieldType) bool {
if v.IsBinProto {
return bytes.Equal(v.BinVarTypes, binVarTps)
}
return v.TxtVarTypes.CheckTypesCompatibility4PC(txtVarTps)
}

// NewPlanCacheValue creates a SQLCacheValue.
func NewPlanCacheValue(plan Plan, names []*types.FieldName, srcMap map[*model.TableInfo]bool, userVarTps []*types.FieldType, bindSQL string) *PlanCacheValue {
func NewPlanCacheValue(plan Plan, names []*types.FieldName, srcMap map[*model.TableInfo]bool,
isBinProto bool, binVarTypes []byte, txtVarTps []*types.FieldType, bindSQL string) *PlanCacheValue {
dstMap := make(map[*model.TableInfo]bool)
for k, v := range srcMap {
dstMap[k] = v
}
userVarTypes := make([]types.FieldType, len(userVarTps))
for i, tp := range userVarTps {
userVarTypes := make([]types.FieldType, len(txtVarTps))
for i, tp := range txtVarTps {
userVarTypes[i] = *tp
}
return &PlanCacheValue{
Plan: plan,
OutPutNames: names,
TblInfo2UnionScan: dstMap,
UserVarTypes: userVarTypes,
TxtVarTypes: userVarTypes,
BinVarTypes: binVarTypes,
IsBinProto: isBinProto,
BindSQL: bindSQL,
}
}
Expand Down
37 changes: 25 additions & 12 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ type Execute struct {
baseSchemaProducer

Name string
UsingVars []expression.Expression
PrepareParams []types.Datum
UsingVars []expression.Expression // only used for text protocol
PrepareParams []types.Datum // only used for binary protocol
ExecID uint32
// Deprecated: SnapshotTS now is only used for asserting after refactoring stale read, it will be removed later.
SnapshotTS uint64
Expand Down Expand Up @@ -456,15 +456,28 @@ func (e *Execute) getPhysicalPlan(ctx context.Context, sctx sessionctx.Context,
return err
}
}
tps := make([]*types.FieldType, len(e.UsingVars))
varsNum := len(e.UsingVars)
for i, param := range e.UsingVars {
name := param.(*expression.ScalarFunction).GetArgs()[0].String()
tps[i] = sctx.GetSessionVars().UserVarTypes[name]
if tps[i] == nil {
tps[i] = types.NewFieldType(mysql.TypeNull)

var varsNum int
var binVarTypes []byte
var txtVarTypes []*types.FieldType
isBinProtocol := len(e.PrepareParams) > 0
if isBinProtocol { // binary protocol
varsNum = len(e.PrepareParams)
for _, param := range e.PrepareParams {
binVarTypes = append(binVarTypes, param.Kind())
}
} else { // txt protocol
varsNum = len(e.UsingVars)
for _, param := range e.UsingVars {
name := param.(*expression.ScalarFunction).GetArgs()[0].String()
tp := sctx.GetSessionVars().UserVarTypes[name]
if tp == nil {
tp = types.NewFieldType(mysql.TypeNull)
}
txtVarTypes = append(txtVarTypes, tp)
}
}

if prepared.CachedPlan != nil {
// Rewriting the expression in the select.where condition will convert its
// type from "paramMarker" to "Constant".When Point Select queries are executed,
Expand Down Expand Up @@ -505,7 +518,7 @@ func (e *Execute) getPhysicalPlan(ctx context.Context, sctx sessionctx.Context,
sctx.PreparedPlanCache().Delete(cacheKey)
break
}
if !cachedVal.UserVarTypes.CheckTypesCompatibility4PC(tps) {
if !cachedVal.varTypesUnchanged(binVarTypes, txtVarTypes) {
continue
}
planValid := true
Expand Down Expand Up @@ -576,13 +589,13 @@ REBUILD:
}
sessVars.IsolationReadEngines[kv.TiFlash] = struct{}{}
}
cached := NewPlanCacheValue(p, names, stmtCtx.TblInfo2UnionScan, tps, sessVars.StmtCtx.BindSQL)
cached := NewPlanCacheValue(p, names, stmtCtx.TblInfo2UnionScan, isBinProtocol, binVarTypes, txtVarTypes, sessVars.StmtCtx.BindSQL)
preparedStmt.NormalizedPlan, preparedStmt.PlanDigest = NormalizePlan(p)
stmtCtx.SetPlanDigest(preparedStmt.NormalizedPlan, preparedStmt.PlanDigest)
if cacheVals, exists := sctx.PreparedPlanCache().Get(cacheKey); exists {
hitVal := false
for i, cacheVal := range cacheVals.([]*PlanCacheValue) {
if cacheVal.UserVarTypes.CheckTypesCompatibility4PC(tps) {
if cacheVal.varTypesUnchanged(binVarTypes, txtVarTypes) {
hitVal = true
cacheVals.([]*PlanCacheValue)[i] = cached
break
Expand Down