Skip to content

Commit

Permalink
add test and fix built-in function castIntAsReal
Browse files Browse the repository at this point in the history
  • Loading branch information
wshwsh12 committed Jul 26, 2019
1 parent 28a1244 commit 68b76d1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<nil>"))
/* 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);")
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 68b76d1

Please sign in to comment.