From eefb4bca44b96640cd1f7dc4e71984567f081578 Mon Sep 17 00:00:00 2001 From: Shenghui Wu <793703860@qq.com> Date: Tue, 23 Nov 2021 17:03:51 +0800 Subject: [PATCH] cherry pick #29563 to release-5.0 Signed-off-by: ti-srebot --- expression/builtin_cast.go | 3 +++ expression/integration_test.go | 13 +++++++++++++ expression/typeinfer_test.go | 10 ++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/expression/builtin_cast.go b/expression/builtin_cast.go index 7fac3ee46f4cc..c8973f34a784b 100644 --- a/expression/builtin_cast.go +++ b/expression/builtin_cast.go @@ -290,6 +290,9 @@ func (c *castAsStringFunctionClass) getFunction(ctx sessionctx.Context, args []E argTp := args[0].GetType().EvalType() switch argTp { case types.ETInt: + if bf.tp.Flen == types.UnspecifiedLength { + bf.tp.Flen = args[0].GetType().Flen + } sig = &builtinCastIntAsStringSig{bf} sig.setPbCode(tipb.ScalarFuncSig_CastIntAsString) case types.ETReal: diff --git a/expression/integration_test.go b/expression/integration_test.go index f9557b1c574cd..952a22044d495 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -9274,3 +9274,16 @@ func (s *testIntegrationSuite) TestConstPropNullFunctions(c *C) { tk.MustExec("insert into t2 values (0, 'c', null), (1, null, 0.1), (3, 'b', 0.01), (2, 'q', 0.12), (null, 'a', -0.1), (null, null, null)") tk.MustQuery("select * from t2 where t2.i2=((select count(1) from t1 where t1.i1=t2.i2))").Check(testkit.Rows("1 0.1")) } + +func (s *testIntegrationSuite) TestIssue29513(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustQuery("select '123' union select cast(45678 as char);").Sort().Check(testkit.Rows("123", "45678")) + tk.MustQuery("select '123' union select cast(45678 as char(2));").Sort().Check(testkit.Rows("123", "45")) + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int);") + tk.MustExec("insert into t values(45678);") + tk.MustQuery("select '123' union select cast(a as char) from t;").Sort().Check(testkit.Rows("123", "45678")) + tk.MustQuery("select '123' union select cast(a as char(2)) from t;").Sort().Check(testkit.Rows("123", "45")) +} diff --git a/expression/typeinfer_test.go b/expression/typeinfer_test.go index 10e146d6189ab..fc6f0a0b3f1a3 100644 --- a/expression/typeinfer_test.go +++ b/expression/typeinfer_test.go @@ -181,9 +181,9 @@ func (s *testInferTypeSuite) createTestCase4Constants() []typeInferTestCase { func (s *testInferTypeSuite) createTestCase4Cast() []typeInferTestCase { return []typeInferTestCase{ - {"CAST(c_int_d AS BINARY)", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag, -1, -1}, // TODO: Flen should be 11. + {"CAST(c_int_d AS BINARY)", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag, 11, -1}, {"CAST(c_int_d AS BINARY(5))", mysql.TypeString, charset.CharsetBin, mysql.BinaryFlag, 5, -1}, - {"CAST(c_int_d AS CHAR)", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, -1, -1}, // TODO: Flen should be 11. + {"CAST(c_int_d AS CHAR)", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, 11, -1}, {"CAST(c_int_d AS CHAR(5))", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, 5, -1}, {"CAST(c_int_d AS DATE)", mysql.TypeDate, charset.CharsetBin, mysql.BinaryFlag, 10, 0}, {"CAST(c_int_d AS DATETIME)", mysql.TypeDatetime, charset.CharsetBin, mysql.BinaryFlag, 19, 0}, @@ -406,9 +406,15 @@ func (s *testInferTypeSuite) createTestCase4StrFuncs() []typeInferTestCase { {"reverse(c_int_d )", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, 20, types.UnspecifiedLength}, {"reverse(c_bigint_d )", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, 20, types.UnspecifiedLength}, +<<<<<<< HEAD {"reverse(c_float_d )", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, -1, types.UnspecifiedLength}, {"reverse(c_double_d )", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, -1, types.UnspecifiedLength}, {"reverse(c_decimal )", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, 8, types.UnspecifiedLength}, +======= + {"reverse(c_float_d )", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, types.UnspecifiedLength, types.UnspecifiedLength}, + {"reverse(c_double_d )", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, types.UnspecifiedLength, types.UnspecifiedLength}, + {"reverse(c_decimal )", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, 9, types.UnspecifiedLength}, +>>>>>>> 8858592f7... expression: fix wrong flen for CastAsString funtion (#29563) {"reverse(c_char )", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, 20, types.UnspecifiedLength}, {"reverse(c_varchar )", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, 20, types.UnspecifiedLength}, {"reverse(c_text_d )", mysql.TypeVarString, charset.CharsetUTF8MB4, 0, 65535, types.UnspecifiedLength},