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: remove userVarType in session variables #36935

Closed
wants to merge 7 commits into from
Closed
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
1 change: 0 additions & 1 deletion executor/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ func (e *SetExecutor) Next(ctx context.Context, req *chunk.Chunk) error {
sessionVars.UnsetUserVar(name)
} else {
sessionVars.Users[name] = value
sessionVars.UserVarTypes[name] = v.Expr.GetType()
}
sessionVars.UsersLock.Unlock()
continue
Expand Down
13 changes: 4 additions & 9 deletions expression/builtin_other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,6 @@ func TestGetVar(t *testing.T) {
}
for _, kv := range sessionVars {
ctx.GetSessionVars().Users[kv.key] = types.NewDatum(kv.val)
var tp *types.FieldType
if _, ok := kv.val.(types.Time); ok {
tp = types.NewFieldType(mysql.TypeDatetime)
} else {
tp = types.NewFieldType(mysql.TypeVarString)
}
types.DefaultParamTypeForValue(kv.val, tp)
ctx.GetSessionVars().UserVarTypes[kv.key] = tp
}

testCases := []struct {
Expand All @@ -160,9 +152,12 @@ func TestGetVar(t *testing.T) {
{[]interface{}{"h"}, timeDec.String()},
}
for _, tc := range testCases {
tp, ok := ctx.GetSessionVars().UserVarTypes[tc.args[0].(string)]
userVar, ok := ctx.GetSessionVars().Users[tc.args[0].(string)]
tp := new(types.FieldType)
if !ok {
tp = types.NewFieldType(mysql.TypeVarString)
} else {
types.DefaultParamTypeForValue(userVar.GetValue(), tp)
}
fn, err := BuildGetVarFunction(ctx, datumsToConstants(types.MakeDatums(tc.args...))[0], tp)
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1279,17 +1279,17 @@ func (er *expressionRewriter) rewriteVariable(v *ast.VariableExpr) {
// Store the field type of the variable into SessionVars.UserVarTypes.
// Normally we can infer the type from SessionVars.User, but we need SessionVars.UserVarTypes when
// GetVar has not been executed to fill the SessionVars.Users.
sessionVars.UsersLock.Lock()
sessionVars.UserVarTypes[name] = tp
sessionVars.UsersLock.Unlock()
return
}
sessionVars.UsersLock.RLock()
tp, ok := sessionVars.UserVarTypes[name]
userVar, ok := sessionVars.Users[name]
sessionVars.UsersLock.RUnlock()
tp := new(types.FieldType)
if !ok {
tp = types.NewFieldType(mysql.TypeVarString)
tp.SetFlen(mysql.MaxFieldVarCharLength)
} else {
types.DefaultParamTypeForValue(userVar.GetValue(), tp)
}
f, err := er.newFunction(ast.GetVar, tp, expression.DatumToConstant(types.NewStringDatum(name), mysql.TypeString, 0))
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions planner/core/plan_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ func parseParamTypes(sctx sessionctx.Context, params []expression.Expression) (p

// from text protocol, there must be a GetVar function
name := param.(*expression.ScalarFunction).GetArgs()[0].String()
tp := sctx.GetSessionVars().UserVarTypes[name]
if tp == nil {
userVar, ok := sctx.GetSessionVars().Users[name]
tp := new(types.FieldType)
if !ok {
tp = types.NewFieldType(mysql.TypeNull)
} else {
types.DefaultParamTypeForValue(userVar.GetValue(), tp)
}
paramTypes = append(paramTypes, tp)
}
Expand Down
13 changes: 1 addition & 12 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
ptypes "github.com/pingcap/tidb/parser/types"
"github.com/pingcap/tidb/sessionctx/sessionstates"
"github.com/pingcap/tidb/sessionctx/stmtctx"
pumpcli "github.com/pingcap/tidb/tidb-binlog/pump_client"
Expand Down Expand Up @@ -565,7 +564,7 @@ type SessionVars struct {
Users map[string]types.Datum
// UserVarTypes stores the FieldType for user variables, it cannot be inferred from Users when Users have not been set yet.
// It is read/write protected by UsersLock.
UserVarTypes map[string]*types.FieldType
//UserVarTypes map[string]*types.FieldType
// systems variables, don't modify it directly, use GetSystemVar/SetSystemVar method.
systems map[string]string
// stmtVars variables are temporarily set by SET_VAR hint
Expand Down Expand Up @@ -1363,7 +1362,6 @@ func (connInfo *ConnectionInfo) IsSecureTransport() bool {
func NewSessionVars() *SessionVars {
vars := &SessionVars{
Users: make(map[string]types.Datum),
UserVarTypes: make(map[string]*types.FieldType),
systems: make(map[string]string),
stmtVars: make(map[string]string),
PreparedStmts: make(map[uint32]interface{}),
Expand Down Expand Up @@ -1643,7 +1641,6 @@ func (s *SessionVars) SetUserVar(varName string, svalue string, collation string
func (s *SessionVars) UnsetUserVar(varName string) {
varName = strings.ToLower(varName)
delete(s.Users, varName)
delete(s.UserVarTypes, varName)
}

// SetLastInsertID saves the last insert id to the session context.
Expand Down Expand Up @@ -2005,10 +2002,6 @@ func (s *SessionVars) EncodeSessionStates(ctx context.Context, sessionStates *se
for name, userVar := range s.Users {
sessionStates.UserVars[name] = userVar.Clone()
}
sessionStates.UserVarTypes = make(map[string]*ptypes.FieldType, len(s.UserVarTypes))
for name, userVarType := range s.UserVarTypes {
sessionStates.UserVarTypes[name] = userVarType.Clone()
}
s.UsersLock.RUnlock()

// Encode other session contexts.
Expand Down Expand Up @@ -2043,10 +2036,6 @@ func (s *SessionVars) DecodeSessionStates(ctx context.Context, sessionStates *se
for name, userVar := range sessionStates.UserVars {
s.Users[name] = *userVar.Clone()
}
s.UserVarTypes = make(map[string]*ptypes.FieldType, len(sessionStates.UserVarTypes))
for name, userVarType := range sessionStates.UserVarTypes {
s.UserVarTypes[name] = userVarType.Clone()
}
s.UsersLock.Unlock()

// Decode other session contexts.
Expand Down