diff --git a/expression/collation.go b/expression/collation.go index c449f1fb7a6a0..71d03113e9486 100644 --- a/expression/collation.go +++ b/expression/collation.go @@ -114,13 +114,13 @@ var ( // collationPriority is the priority when infer the result collation, the priority of collation a > b iff collationPriority[a] > collationPriority[b] collationPriority = map[string]int{ - charset.CollationASCII: 0, - charset.CollationLatin1: 1, - "utf8_general_ci": 2, - charset.CollationUTF8: 3, - "utf8mb4_general_ci": 4, - charset.CollationUTF8MB4: 5, - charset.CollationBin: 6, + charset.CollationASCII: 1, + charset.CollationLatin1: 2, + "utf8_general_ci": 3, + charset.CollationUTF8: 4, + "utf8mb4_general_ci": 5, + charset.CollationUTF8MB4: 6, + charset.CollationBin: 7, } // CollationStrictness indicates the strictness of comparison of the collation. The unequal order in a weak collation also holds in a strict collation. @@ -171,7 +171,7 @@ func deriveCoercibilityForColumn(c *Column) Coercibility { // DeriveCollationFromExprs derives collation information from these expressions. func DeriveCollationFromExprs(ctx sessionctx.Context, exprs ...Expression) (dstCharset, dstCollation string) { curCoer := CoercibilityCoercible - curCollationPriority := -1 + curCollationPriority := 0 dstCharset, dstCollation = charset.GetDefaultCharsetAndCollate() if ctx != nil && ctx.GetSessionVars() != nil { dstCharset, dstCollation = ctx.GetSessionVars().GetCharsetInfo() @@ -189,20 +189,17 @@ func DeriveCollationFromExprs(ctx sessionctx.Context, exprs ...Expression) (dstC coer := e.Coercibility() ft := e.GetType() - collationPriority, ok := collationPriority[strings.ToLower(ft.Collate)] - if !ok { - collationPriority = -1 - } + priority := collationPriority[strings.ToLower(ft.Collate)] if coer != curCoer { if coer < curCoer { - curCoer, curCollationPriority, dstCharset, dstCollation = coer, collationPriority, ft.Charset, ft.Collate + curCoer, curCollationPriority, dstCharset, dstCollation = coer, priority, ft.Charset, ft.Collate } continue } - if !ok || collationPriority <= curCollationPriority { + if priority <= curCollationPriority { continue } - curCollationPriority, dstCharset, dstCollation = collationPriority, ft.Charset, ft.Collate + curCollationPriority, dstCharset, dstCollation = priority, ft.Charset, ft.Collate } if !hasStrArg { dstCharset, dstCollation = charset.CharsetBin, charset.CollationBin