Skip to content

Commit

Permalink
expression: simplify get priority by collation (#18783)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongjiwei authored Aug 11, 2020
1 parent c5c7bf8 commit ae779e1
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions expression/collation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit ae779e1

Please sign in to comment.