Skip to content

Commit

Permalink
cherry pick #24297 to release-5.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <[email protected]>
  • Loading branch information
xiongjiwei authored and ti-srebot committed Apr 26, 2021
1 parent 1145e34 commit 675f854
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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
Expand Down

0 comments on commit 675f854

Please sign in to comment.