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

*: fix a bug caused by the wrong collation setting which leads to the wrong result of collation function #17116

Merged
merged 3 commits into from
May 15, 2020
Merged
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
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
6 changes: 6 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6420,6 +6420,12 @@ func (s *testIntegrationSuite) TestIssue17098(c *C) {
tk.MustQuery("select collation(t1.a) from t1 union select collation(t2.a) from t2;").Check(testkit.Rows("utf8mb4_bin"))
}

func (s *testIntegrationSuite) TestIssue17115(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustQuery("select collation(user());").Check(testkit.Rows("utf8mb4_bin"))
tk.MustQuery("select collation(compress('abc'));").Check(testkit.Rows("binary"))
}

func (s *testIntegrationSuite) TestIndexedVirtualGeneratedColumnTruncate(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
7 changes: 1 addition & 6 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,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 @@ -1245,7 +1246,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 {
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
return errors.Trace(err)
var ok bool
var charsetVal string
var err2 error
if name == CollationConnection {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a switch?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double switch makes the code complicated.
I think it's fine since there are only three branches. If four, then I will use switch

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