From 68b76d12884c46bccd33bcab4cb91b97631a1649 Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Fri, 26 Jul 2019 15:11:57 +0800 Subject: [PATCH] add test and fix built-in function castIntAsReal --- expression/builtin_cast.go | 2 +- expression/integration_test.go | 23 +++++++++++++++++++++++ go.sum | 8 -------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/expression/builtin_cast.go b/expression/builtin_cast.go index 1b8205c009118..366c48def1b80 100644 --- a/expression/builtin_cast.go +++ b/expression/builtin_cast.go @@ -458,7 +458,7 @@ func (b *builtinCastIntAsRealSig) evalReal(row chunk.Row) (res float64, isNull b if isNull || err != nil { return res, isNull, err } - if !mysql.HasUnsignedFlag(b.tp.Flag) { + if !mysql.HasUnsignedFlag(b.tp.Flag) && !mysql.HasUnsignedFlag(b.args[0].GetType().Flag) { res = float64(val) } else if b.inUnion && val < 0 { res = 0 diff --git a/expression/integration_test.go b/expression/integration_test.go index e07ffe91fc2ac..59edab6569914 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -2330,6 +2330,29 @@ func (s *testIntegrationSuite) TestBuiltin(c *C) { result = tk.MustQuery("select cast(1 as signed int)") result.Check(testkit.Rows("1")) + // test cast as double + result = tk.MustQuery("select cast(1 as double)") + result.Check(testkit.Rows("1")) + result = tk.MustQuery("select cast(1.1 as double)") + result.Check(testkit.Rows("1.1")) + result = tk.MustQuery("select cast(1.1 as double)") + result.Check(testkit.Rows("1.1")) + result = tk.MustQuery("select cast('123.321' as double)") + result.Check(testkit.Rows("123.321")) + result = tk.MustQuery("select cast(-1 as double)") + result.Check(testkit.Rows("-1")) + result = tk.MustQuery("select cast(null as double)") + result.Check(testkit.Rows("")) + /* There is something wrong in showing float number. + The testkit gets the format 'f' (-ddd.dddd, no exponent), + but we need the format 'e' (-d.dddde±dd, a decimal exponent). + + result = tk.MustQuery("select cast(12345678901234567890 as double)") + result.Check(testkit.Rows("1.2345678901234567e19")) + result = tk.MustQuery("select cast(cast(-1 as unsigned) as double)") + result.Check(testkit.Rows("1.8446744073709552e19")) + */ + // test cast time as decimal overflow tk.MustExec("drop table if exists t1") tk.MustExec("create table t1(s1 time);") diff --git a/go.sum b/go.sum index 73cb6e7ed7912..da1a87c8df089 100644 --- a/go.sum +++ b/go.sum @@ -165,16 +165,8 @@ github.com/pingcap/kvproto v0.0.0-20190703131923-d9830856b531/go.mod h1:QMdbTAXC github.com/pingcap/log v0.0.0-20190214045112-b37da76f67a7/go.mod h1:xsfkWVaFVV5B8e1K9seWfyJWFrIhbtUTAD8NV1Pq3+w= github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 h1:t2OQTpPJnrPDGlvA+3FwJptMTt6MEPdzK1Wt99oaefQ= github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596/go.mod h1:WpHUKhNZ18v116SvGrmjkA9CBhYmuUTKL+p8JC9ANEw= -<<<<<<< HEAD -<<<<<<< HEAD -github.com/pingcap/parser v0.0.0-20190726023712-ca2f45b420fd h1:vFLYlcLyvWrIN+3Y9uPExiisDjeRCPh1Vic0vnkxBMs= -github.com/pingcap/parser v0.0.0-20190726023712-ca2f45b420fd/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= -======= ->>>>>>> support cast as double -======= github.com/pingcap/parser v0.0.0-20190726045944-46d1b3d054b2 h1:uPLL91Kf8Sh86ht/ijNj6iUWw7JBArTcJtyn8aR+uDs= github.com/pingcap/parser v0.0.0-20190726045944-46d1b3d054b2/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= ->>>>>>> update mod github.com/pingcap/pd v0.0.0-20190712044914-75a1f9f3062b h1:oS9PftxQqgcRouKhhdaB52tXhVLEP7Ng3Qqsd6Z18iY= github.com/pingcap/pd v0.0.0-20190712044914-75a1f9f3062b/go.mod h1:3DlDlFT7EF64A1bmb/tulZb6wbPSagm5G4p1AlhaEDs= github.com/pingcap/tidb-tools v2.1.3-0.20190321065848-1e8b48f5c168+incompatible h1:MkWCxgZpJBgY2f4HtwWMMFzSBb3+JPzeJgF3VrXE/bU=