From 675f85481283b3820f900ef88de9eb86bae08dbc Mon Sep 17 00:00:00 2001 From: xiongjiwei Date: Mon, 26 Apr 2021 22:41:56 +0800 Subject: [PATCH] cherry pick #24297 to release-5.0 Signed-off-by: ti-srebot --- expression/builtin_cast.go | 6 +++++- expression/integration_test.go | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/expression/builtin_cast.go b/expression/builtin_cast.go index 9f55331f2fd46..f9fd1e512b4c3 100644 --- a/expression/builtin_cast.go +++ b/expression/builtin_cast.go @@ -1906,7 +1906,11 @@ func WrapWithCastAsString(ctx sessionctx.Context, expr Expression) Expression { argLen = -1 } tp := types.NewFieldType(mysql.TypeVarString) - tp.Charset, tp.Collate = expr.CharsetAndCollation(ctx) + if expr.Coercibility() == CoercibilityExplicit { + tp.Charset, tp.Collate = expr.CharsetAndCollation(ctx) + } else { + tp.Charset, tp.Collate = ctx.GetSessionVars().GetCharsetInfo() + } tp.Flen, tp.Decimal = argLen, types.UnspecifiedLength return BuildCastFunction(ctx, expr, tp) } diff --git a/expression/integration_test.go b/expression/integration_test.go index 8bbd6c530ea78..561365e786385 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -7744,6 +7744,7 @@ func (s *testIntegrationSerialSuite) TestIssue19116(c *C) { defer collate.SetNewCollationEnabledForTest(false) tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") 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")) @@ -7754,6 +7755,12 @@ func (s *testIntegrationSerialSuite) TestIssue19116(c *C) { 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")) + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a datetime)") + tk.MustExec("insert into t values ('2020-02-02')") + tk.MustQuery("select collation(concat(unix_timestamp(a))) from t;").Check(testkit.Rows("utf8mb4_general_ci")) + tk.MustQuery("select coercibility(concat(unix_timestamp(a))) from t;").Check(testkit.Rows("4")) } // issues 14448, 19383, 17734