diff --git a/expression/builtin_cast.go b/expression/builtin_cast.go index ae4641b6b53c2..2d03f40c97bed 100644 --- a/expression/builtin_cast.go +++ b/expression/builtin_cast.go @@ -28,7 +28,6 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/parser/ast" - "github.com/pingcap/parser/charset" "github.com/pingcap/parser/model" "github.com/pingcap/parser/mysql" "github.com/pingcap/parser/terror" @@ -1900,7 +1899,7 @@ func WrapWithCastAsString(ctx sessionctx.Context, expr Expression) Expression { argLen = -1 } tp := types.NewFieldType(mysql.TypeVarString) - tp.Charset, tp.Collate = charset.GetDefaultCharsetAndCollate() + tp.Charset, tp.Collate = expr.CharsetAndCollation(ctx) tp.Flen, tp.Decimal = argLen, types.UnspecifiedLength return BuildCastFunction(ctx, expr, tp) } diff --git a/expression/collation.go b/expression/collation.go index 4163c670513fd..492f7788a6941 100644 --- a/expression/collation.go +++ b/expression/collation.go @@ -151,12 +151,18 @@ func deriveCoercibilityForScarlarFunc(sf *ScalarFunction) Coercibility { if sf.RetType.EvalType() != types.ETString { return CoercibilityNumeric } - coer := CoercibilityCoercible + coer := CoercibilityIgnorable for _, arg := range sf.GetArgs() { if arg.Coercibility() < coer { coer = arg.Coercibility() } } + + // it is weird if a ScalarFunction is CoercibilityNumeric but return string type + if coer == CoercibilityNumeric { + return CoercibilityCoercible + } + return coer } diff --git a/expression/integration_test.go b/expression/integration_test.go index a55e2c2e567a1..a8d6fb3d1e0a7 100755 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -7674,7 +7674,14 @@ func (s *testIntegrationSerialSuite) TestIssue19116(c *C) { defer collate.SetNewCollationEnabledForTest(false) tk := testkit.NewTestKit(c, s.store) + tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci;") + tk.MustQuery("select collation(concat(1 collate `binary`));").Check(testkit.Rows("binary")) + tk.MustQuery("select coercibility(concat(1 collate `binary`));").Check(testkit.Rows("0")) tk.MustQuery("select collation(concat(NULL,NULL));").Check(testkit.Rows("binary")) + tk.MustQuery("select coercibility(concat(NULL,NULL));").Check(testkit.Rows("6")) + tk.MustQuery("select collation(concat(1,1));").Check(testkit.Rows("utf8mb4_general_ci")) tk.MustQuery("select coercibility(concat(1,1));").Check(testkit.Rows("4")) + tk.MustQuery("select collation(1);").Check(testkit.Rows("binary")) tk.MustQuery("select coercibility(1);").Check(testkit.Rows("5")) + tk.MustQuery("select coercibility(1=1);").Check(testkit.Rows("5")) } diff --git a/expression/simple_rewriter.go b/expression/simple_rewriter.go index e28df4eacf143..4f73911dcaa4e 100644 --- a/expression/simple_rewriter.go +++ b/expression/simple_rewriter.go @@ -297,6 +297,7 @@ func (sr *simpleRewriter) Leave(originInNode ast.Node) (retNode ast.Node, ok boo arg.GetType().Collate = v.Collate } sr.stack[len(sr.stack)-1].SetCoercibility(CoercibilityExplicit) + sr.stack[len(sr.stack)-1].SetCharsetAndCollation(arg.GetType().Charset, arg.GetType().Collate) default: sr.err = errors.Errorf("UnknownType: %T", v) return retNode, false diff --git a/planner/core/expression_rewriter.go b/planner/core/expression_rewriter.go index f7bfb4783b32e..cd9e1c82c7471 100644 --- a/planner/core/expression_rewriter.go +++ b/planner/core/expression_rewriter.go @@ -1120,6 +1120,7 @@ func (er *expressionRewriter) Leave(originInNode ast.Node) (retNode ast.Node, ok arg.GetType().Collate = v.Collate } er.ctxStack[len(er.ctxStack)-1].SetCoercibility(expression.CoercibilityExplicit) + er.ctxStack[len(er.ctxStack)-1].SetCharsetAndCollation(arg.GetType().Charset, arg.GetType().Collate) default: er.err = errors.Errorf("UnknownType: %T", v) return retNode, false