Skip to content

Commit

Permalink
variable: always truncate tidb_session_alias when it is not a valid…
Browse files Browse the repository at this point in the history
… value (#46707)

ref #46071
  • Loading branch information
lcwangchao authored Sep 12, 2023
1 parent dc99d14 commit 063a5b5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
20 changes: 16 additions & 4 deletions session/test/variable/variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,16 +616,28 @@ func TestSessionAlias(t *testing.T) {
tk.MustQuery("select @@tidb_session_alias").Check(testkit.Rows(""))
// normal set
tk.MustExec("set @@tidb_session_alias='alias123'")
tk.MustQuery("show warnings").Check(testkit.Rows())
tk.MustQuery("select @@tidb_session_alias").Check(testkit.Rows("alias123"))
// set a long value
val := "0123456789012345678901234567890123456789012345678901234567890123456789"
tk.MustExec("set @@tidb_session_alias=?", val)
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_session_alias value: '0123456789012345678901234567890123456789012345678901234567890123456789'"))
tk.MustQuery("select @@tidb_session_alias").Check(testkit.Rows(val[:64]))
// an invalid value
err := tk.ExecToErr("set @@tidb_session_alias='abc '")
require.EqualError(t, err, "[variable:1231]Incorrect value for variable @@tidb_session_alias 'abc '")
tk.MustQuery("select @@tidb_session_alias").Check(testkit.Rows(val[:64]))
// set a long value with unicode
val = "中文测试1中文测试2中文测试3中文测试4中文测试5中文测试6中文测试7中文测试8中文测试9中文测试0中文测试a中文测试b中文测试c"
tk.MustExec("set @@tidb_session_alias=?", val)
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_session_alias value: '中文测试1中文测试2中文测试3中文测试4中文测试5中文测试6中文测试7中文测试8中文测试9中文测试0中文测试a中文测试b中文测试c'"))
tk.MustQuery("select @@tidb_session_alias").Check(testkit.Rows("中文测试1中文测试2中文测试3中文测试4中文测试5中文测试6中文测试7中文测试8中文测试9中文测试0中文测试a中文测试b中文测试"))
// end with space
tk.MustExec("set @@tidb_session_alias='abc '")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_session_alias value: 'abc '"))
tk.MustQuery("select @@tidb_session_alias").Check(testkit.Rows("abc"))
// end with space after truncate
tk.MustExec("set @@tidb_session_alias='abc 1'")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_session_alias value: 'abc 1'"))
tk.MustQuery("select @@tidb_session_alias").Check(testkit.Rows("abc"))
// reset to empty
tk.MustExec("set @@tidb_session_alias=''")
tk.MustQuery("show warnings").Check(testkit.Rows())
tk.MustQuery("select @@tidb_session_alias").Check(testkit.Rows(""))
}
18 changes: 14 additions & 4 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2817,13 +2817,23 @@ var defaultSysVars = []*SysVar{
}},
{Scope: ScopeSession, Name: TiDBSessionAlias, Value: "", Type: TypeStr,
Validation: func(s *SessionVars, normalizedValue string, originalValue string, _ ScopeFlag) (string, error) {
if len(normalizedValue) > 64 {
chars := []rune(normalizedValue)
warningAdded := false
if len(chars) > 64 {
s.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(TiDBSessionAlias, originalValue))
normalizedValue = normalizedValue[:64]
warningAdded = true
chars = chars[:64]
normalizedValue = string(chars)
}

if len(normalizedValue) > 0 && util.IsInCorrectIdentifierName(normalizedValue) {
return "", ErrWrongValueForVar.GenWithStack("Incorrect value for variable @@%s '%s'", TiDBSessionAlias, normalizedValue)
// truncate to a valid identifier
for normalizedValue != "" && util.IsInCorrectIdentifierName(normalizedValue) {
if !warningAdded {
s.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(TiDBSessionAlias, originalValue))
warningAdded = true
}
chars = chars[:len(chars)-1]
normalizedValue = string(chars)
}

return normalizedValue, nil
Expand Down

0 comments on commit 063a5b5

Please sign in to comment.