diff --git a/expression/integration_test.go b/expression/integration_test.go index 44292b944feea..9448546087076 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -2035,10 +2035,14 @@ func (s *testIntegrationSuite) TestBuiltin(c *C) { result.Check(testkit.Rows("")) result = tk.MustQuery(`select cast(cast('2017-01-01 01:01:11.12' as date) as datetime(2));`) result.Check(testkit.Rows("2017-01-01 00:00:00.00")) - result = tk.MustQuery(`select cast(20170118.999 as datetime);`) result.Check(testkit.Rows("2017-01-18 00:00:00")) + tk.MustExec(`create table tb5(a bigint(64) unsigned, b double);`) + tk.MustExec(`insert into tb5 (a, b) values (9223372036854776000, 9223372036854776000);`) + tk.MustExec(`insert into tb5 (a, b) select * from (select cast(a as json) as a1, b from tb5) as t where t.a1 = t.b;`) + tk.MustExec(`drop table tb5;`) + // Test corner cases of cast string as datetime result = tk.MustQuery(`select cast("170102034" as datetime);`) result.Check(testkit.Rows("2017-01-02 03:04:00")) diff --git a/types/convert.go b/types/convert.go index fe0f1aaac93ff..42f2be4860214 100644 --- a/types/convert.go +++ b/types/convert.go @@ -523,8 +523,7 @@ func ConvertJSONToFloat(sc *stmtctx.StatementContext, j json.BinaryJSON) (float6 case json.TypeCodeInt64: return float64(j.GetInt64()), nil case json.TypeCodeUint64: - u, err := ConvertIntToUint(sc, j.GetInt64(), UnsignedUpperBound[mysql.TypeLonglong], mysql.TypeLonglong) - return float64(u), errors.Trace(err) + return float64(j.GetUint64()), nil case json.TypeCodeFloat64: return j.GetFloat64(), nil case json.TypeCodeString: diff --git a/types/convert_test.go b/types/convert_test.go index 84836ac322238..19c8c786d6ce6 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -811,24 +811,26 @@ func (s *testTypeConvertSuite) TestConvertJSONToInt(c *C) { func (s *testTypeConvertSuite) TestConvertJSONToFloat(c *C) { var tests = []struct { - In string + In interface{} Out float64 + ty json.TypeCode }{ - {`{}`, 0}, - {`[]`, 0}, - {`3`, 3}, - {`-3`, -3}, - {`4.5`, 4.5}, - {`true`, 1}, - {`false`, 0}, - {`null`, 0}, - {`"hello"`, 0}, - {`"123.456hello"`, 123.456}, - {`"1234"`, 1234}, + {make(map[string]interface{}, 0), 0, json.TypeCodeObject}, + {make([]interface{}, 0), 0, json.TypeCodeArray}, + {int64(3), 3, json.TypeCodeInt64}, + {int64(-3), -3, json.TypeCodeInt64}, + {uint64(1 << 63), 1 << 63, json.TypeCodeUint64}, + {float64(4.5), 4.5, json.TypeCodeFloat64}, + {true, 1, json.TypeCodeLiteral}, + {false, 0, json.TypeCodeLiteral}, + {nil, 0, json.TypeCodeLiteral}, + {"hello", 0, json.TypeCodeString}, + {"123.456hello", 123.456, json.TypeCodeString}, + {"1234", 1234, json.TypeCodeString}, } for _, tt := range tests { - j, err := json.ParseBinaryFromString(tt.In) - c.Assert(err, IsNil) + j := json.CreateBinary(tt.In) + c.Assert(j.TypeCode, Equals, tt.ty) casted, _ := ConvertJSONToFloat(new(stmtctx.StatementContext), j) c.Assert(casted, Equals, tt.Out) } diff --git a/types/datum_test.go b/types/datum_test.go index a2d7a64739020..d41fb250dd571 100644 --- a/types/datum_test.go +++ b/types/datum_test.go @@ -218,6 +218,7 @@ func (ts *testDatumSuite) TestToJSON(c *C) { {NewStringDatum("[1, 2, 3]"), `[1, 2, 3]`, true}, {NewStringDatum("{}"), `{}`, true}, {mustParseTimeIntoDatum("2011-11-10 11:11:11.111111", mysql.TypeTimestamp, 6), `"2011-11-10 11:11:11.111111"`, true}, + {NewStringDatum(`{"a": "9223372036854775809"}`), `{"a": "9223372036854775809"}`, true}, // can not parse JSON from this string, so error occurs. {NewStringDatum("hello, 世界"), "", false},