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

planner: fix wrong collation when rewrite in condition #30492

Merged
merged 13 commits into from
Dec 21, 2021
2 changes: 2 additions & 0 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ func (c *castAsStringFunctionClass) getFunction(ctx sessionctx.Context, args []E
if err != nil {
return nil, err
}

bf.args[0] = HandleBinaryLiteral(ctx, args[0], &ExprCollation{Charset: c.tp.Charset, Collation: c.tp.Collate}, c.funcName)
wjhuang2016 marked this conversation as resolved.
Show resolved Hide resolved
bf.tp = c.tp
if args[0].GetType().Hybrid() || IsBinaryLiteral(args[0]) {
sig = &builtinCastStringAsStringSig{bf}
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_convert_charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func HandleBinaryLiteral(ctx sessionctx.Context, expr Expression, ec *ExprCollat
ast.Left, ast.Right, ast.Repeat, ast.Trim, ast.LTrim, ast.RTrim, ast.Substr, ast.SubstringIndex, ast.Replace,
ast.Substring, ast.Mid, ast.Translate, ast.InsertFunc, ast.Lpad, ast.Rpad, ast.Elt, ast.ExportSet, ast.MakeSet,
ast.FindInSet, ast.Regexp, ast.Field, ast.Locate, ast.Instr, ast.Position, ast.GE, ast.LE, ast.GT, ast.LT, ast.EQ,
ast.NE, ast.NullEQ, ast.Strcmp, ast.If, ast.Ifnull, ast.Like, ast.In, ast.DateFormat, ast.TimeFormat:
ast.NE, ast.NullEQ, ast.Strcmp, ast.If, ast.Ifnull, ast.Like, ast.In, ast.DateFormat, ast.TimeFormat, ast.Cast:
wjhuang2016 marked this conversation as resolved.
Show resolved Hide resolved
if ec.Charset == charset.CharsetBin && expr.GetType().Charset != charset.CharsetBin {
return BuildToBinaryFunction(ctx, expr)
} else if ec.Charset != charset.CharsetBin && expr.GetType().Charset == charset.CharsetBin {
Expand Down
5 changes: 5 additions & 0 deletions expression/integration_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ func TestCollationBasic(t *testing.T) {
tk.MustQuery("select * from t1 where col1 >= 0xc484 and col1 <= 0xc3b3;").Check(testkit.Rows("Ȇ"))

tk.MustQuery("select collation(IF('a' < 'B' collate utf8mb4_general_ci, 'smaller', 'greater' collate utf8mb4_unicode_ci));").Check(testkit.Rows("utf8mb4_unicode_ci"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(10))")
tk.MustExec("insert into t values ('a')")
tk.MustQuery("select * from t where a in ('b' collate utf8mb4_general_ci, 'A', 3)").Check(testkit.Rows("a"))
}

func TestWeightString(t *testing.T) {
Expand Down
58 changes: 58 additions & 0 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,12 @@ func (er *expressionRewriter) inToExpression(lLen int, not bool, tp *types.Field
if allSameType && l == 1 && lLen > 1 {
function = er.notToExpression(not, ast.In, tp, er.ctxStack[stkLen-lLen-1:]...)
} else {
// If we rewrite IN to EQ, we need to decide what's the collation EQ uses.
coll := er.deriveCollationForIn(l, lLen, stkLen, args)
if er.err != nil {
return
}
er.castCollationForIn(l, lLen, stkLen, coll)
eqFunctions := make([]expression.Expression, 0, lLen)
for i := stkLen - lLen; i < stkLen; i++ {
expr, err := er.constructBinaryOpFunction(args[0], er.ctxStack[i], ast.EQ)
Expand All @@ -1504,6 +1510,58 @@ func (er *expressionRewriter) inToExpression(lLen int, not bool, tp *types.Field
er.ctxStackAppend(function, types.EmptyName)
}

// deriveCollationForIn derive collation for in expression.
func (er *expressionRewriter) deriveCollationForIn(l int, lLen int, stkLen int, args []expression.Expression) []*expression.ExprCollation {
coll := make([]*expression.ExprCollation, 0, l)
if l == 1 {
// a in (x, y, z) => coll[0]
coll2, err := expression.CheckAndDeriveCollationFromExprs(er.sctx, "IN", types.ETInt, args...)
er.err = err
if er.err != nil {
return nil
}
coll = append(coll, coll2)
} else {
// (a, b, c) in ((x1, x2, x3), (y1, y2, y3), (z1, z2, z3)) => coll[0], coll[1], coll[2]
for i := 0; i < lLen; i++ {
args := make([]expression.Expression, 0, lLen)
for j := stkLen - lLen - 1; j < stkLen; j++ {
rowFunc, _ := er.ctxStack[j].(*expression.ScalarFunction)
args = append(args, rowFunc.GetArgs()[i])
}
coll2, err := expression.CheckAndDeriveCollationFromExprs(er.sctx, "IN", types.ETInt, args...)
er.err = err
if er.err != nil {
return nil
}
coll = append(coll, coll2)
}
}
return coll
}

func (er *expressionRewriter) castCollationForIn(l int, lLen int, stkLen int, coll []*expression.ExprCollation) {
for i := stkLen - lLen; i < stkLen; i++ {
if l == 1 && er.ctxStack[i].GetType().EvalType() == types.ETString {
tp := er.ctxStack[i].GetType().Clone()
tp.Charset, tp.Collate = coll[0].Charset, coll[0].Collation
er.ctxStack[i] = expression.BuildCastFunction(er.sctx, er.ctxStack[i], tp)
er.ctxStack[i].SetCoercibility(expression.CoercibilityExplicit)
} else {
rowFunc, _ := er.ctxStack[i].(*expression.ScalarFunction)
for j := 0; j < lLen; j++ {
if er.ctxStack[i].GetType().EvalType() != types.ETString {
continue
}
tp := rowFunc.GetArgs()[j].GetType().Clone()
tp.Charset, tp.Collate = coll[j].Charset, coll[j].Collation
rowFunc.GetArgs()[j] = expression.BuildCastFunction(er.sctx, rowFunc.GetArgs()[j], tp)
rowFunc.GetArgs()[j].SetCoercibility(expression.CoercibilityExplicit)
}
}
}
}

func (er *expressionRewriter) caseToExpression(v *ast.CaseExpr) {
stkLen := len(er.ctxStack)
argsLen := 2 * len(v.WhenClauses)
Expand Down