Skip to content

Commit

Permalink
*: fix a bug caused by the wrong collation setting which leads to the…
Browse files Browse the repository at this point in the history
… wrong result of collation function (#17116) (#17231)

Signed-off-by: sre-bot <[email protected]>
  • Loading branch information
sre-bot authored Jul 28, 2020
1 parent 9c66a42 commit 949fa1a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 51 deletions.
2 changes: 1 addition & 1 deletion executor/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (e *SetExecutor) setCharset(cs, co string) error {
return errors.Trace(err)
}
}
return errors.Trace(sessionVars.SetSystemVar(variable.CollationConnection, co))
return sessionVars.SetSystemVar(variable.CollationConnection, co)
}

func (e *SetExecutor) getVarValue(v *expression.VarAssignment, sysVar *variable.SysVar) (value types.Datum, err error) {
Expand Down
1 change: 1 addition & 0 deletions expression/builtin_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ func (c *collationFunctionClass) getFunction(ctx sessionctx.Context, args []Expr
if err != nil {
return nil, err
}
bf.tp.Charset, bf.tp.Collate = ctx.GetSessionVars().GetCharsetInfo()
sig := &builtinCollationSig{bf}
return sig, nil
}
Expand Down
46 changes: 3 additions & 43 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6481,50 +6481,10 @@ func (s *testIntegrationSuite) TestIssue16697(c *C) {
}
}

func (s *testIntegrationSerialSuite) TestIssue17176(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustGetErrMsg("create table t(a enum('a', 'a ')) charset utf8 collate utf8_bin;", "[types:1291]Column 'a' has duplicated value 'a ' in ENUM")
tk.MustGetErrMsg("create table t(a enum('a', 'Á')) charset utf8 collate utf8_general_ci;", "[types:1291]Column 'a' has duplicated value 'Á' in ENUM")
tk.MustGetErrMsg("create table t(a enum('a', 'a ')) charset utf8mb4 collate utf8mb4_bin;", "[types:1291]Column 'a' has duplicated value 'a ' in ENUM")
tk.MustExec("create table t(a enum('a', 'A')) charset utf8 collate utf8_bin;")
tk.MustExec("drop table t;")
tk.MustExec("create table t3(a enum('a', 'A')) charset utf8mb4 collate utf8mb4_bin;")
}

func (s *testIntegrationSuite) TestIndexedVirtualGeneratedColumnTruncate(c *C) {
func (s *testIntegrationSuite) TestIssue17115(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(a int, b tinyint as(a+100) unique key)")
tk.MustExec("insert ignore into t values(200, default)")
tk.MustExec("update t set a=1 where a=200")
tk.MustExec("admin check table t")
tk.MustExec("delete from t")
tk.MustExec("insert ignore into t values(200, default)")
tk.MustExec("admin check table t")
tk.MustExec("insert ignore into t values(200, default) on duplicate key update a=100")
tk.MustExec("admin check table t")
tk.MustExec("delete from t")
tk.MustExec("admin check table t")

tk.MustExec("begin")
tk.MustExec("insert ignore into t values(200, default)")
tk.MustExec("update t set a=1 where a=200")
tk.MustExec("admin check table t")
tk.MustExec("delete from t")
tk.MustExec("insert ignore into t values(200, default)")
tk.MustExec("admin check table t")
tk.MustExec("insert ignore into t values(200, default) on duplicate key update a=100")
tk.MustExec("admin check table t")
tk.MustExec("delete from t")
tk.MustExec("admin check table t")
tk.MustExec("commit")
tk.MustExec("admin check table t")
tk.MustQuery("select collation(user());").Check(testkit.Rows("utf8mb4_bin"))
tk.MustQuery("select collation(compress('abc'));").Check(testkit.Rows("binary"))
}

func (s *testIntegrationSuite) TestIssue17287(c *C) {
Expand Down
7 changes: 1 addition & 6 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,7 @@ func (s *session) SetCollation(coID int) error {
for _, v := range variable.SetNamesVariables {
terror.Log(s.sessionVars.SetSystemVar(v, cs))
}
err = s.sessionVars.SetSystemVar(variable.CollationConnection, co)
if err != nil {
// Some clients may use the unsupported collations, such as utf8mb4_0900_ai_ci, We shouldn't return error or use the ERROR level log.
logutil.BgLogger().Warn(err.Error())
}
return nil
return s.sessionVars.SetSystemVar(variable.CollationConnection, co)
}

func (s *session) PreparedPlanCache() *kvcache.SimpleLRUCache {
Expand Down
21 changes: 20 additions & 1 deletion sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/auth"
"github.com/pingcap/parser/charset"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
pumpcli "github.com/pingcap/tidb-tools/tidb-binlog/pump_client"
Expand Down Expand Up @@ -1262,7 +1263,25 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
s.MetricSchemaRangeDuration = tidbOptInt64(val, DefTiDBMetricSchemaRangeDuration)
case CollationConnection, CollationDatabase, CollationServer:
if _, err := collate.GetCollationByName(val); err != nil {
return errors.Trace(err)
var ok bool
var charsetVal string
var err2 error
if name == CollationConnection {
charsetVal, ok = s.systems[CharacterSetConnection]
} else if name == CollationDatabase {
charsetVal, ok = s.systems[CharsetDatabase]
} else {
// CollationServer
charsetVal, ok = s.systems[CharacterSetServer]
}
if !ok {
return err
}
val, err2 = charset.GetDefaultCollation(charsetVal)
if err2 != nil {
return err2
}
logutil.BgLogger().Warn(err.Error())
}
case TiDBSlowLogThreshold:
atomic.StoreUint64(&config.GetGlobalConfig().Log.SlowThreshold, uint64(tidbOptInt64(val, logutil.DefaultSlowThreshold)))
Expand Down

0 comments on commit 949fa1a

Please sign in to comment.