From ee5bbd0de1e4a62e684b745f9269911393885179 Mon Sep 17 00:00:00 2001 From: Shen Li Date: Sun, 11 Oct 2015 15:53:35 +0800 Subject: [PATCH] *: Check float length in parser Fix https://github.com/pingcap/tidb/issues/312 --- parser/parser.y | 10 ++++++++++ parser/parser_test.go | 4 ++++ stmt/stmts/create_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/parser/parser.y b/parser/parser.y index 41cbc0bf427a5..3af473054521f 100644 --- a/parser/parser.y +++ b/parser/parser.y @@ -3681,6 +3681,16 @@ NumericType: fopt := $2.(*coldef.FloatOpt) x := types.NewFieldType($1.(byte)) x.Flen = fopt.Flen + if x.Tp == mysql.TypeFloat { + // Fix issue #312 + if x.Flen > 53 { + yylex.(*lexer).errf("Float len(%d) should not be greater than 53", x.Flen) + return 1 + } + if x.Flen > 24 { + x.Tp = mysql.TypeDouble + } + } x.Decimal =fopt.Decimal for _, o := range $3.([]*field.Opt) { if o.IsUnsigned { diff --git a/parser/parser_test.go b/parser/parser_test.go index 0a7abcbf1f3db..7b84fe89d9b83 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -533,6 +533,10 @@ func (s *testParserSuite) TestParser0(c *C) { {`select * from t as a`, true}, {"select 1 full, 1 row, 1 abs", true}, {"select * from t full, t1 row, t2 abs", true}, + + // For https://github.com/pingcap/tidb/issues/312 + {`create table t (c float(53));`, true}, + {`create table t (c float(54));`, false}, } for _, t := range table { diff --git a/stmt/stmts/create_test.go b/stmt/stmts/create_test.go index fdffd3c71e1fa..da035e18863e9 100644 --- a/stmt/stmts/create_test.go +++ b/stmt/stmts/create_test.go @@ -103,6 +103,44 @@ func (s *testStmtSuite) TestCreateTable(c *C) { // Test "if not exist" mustExec(c, s.testDB, "CREATE TABLE if not exists test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") + + // Testcase for https://github.com/pingcap/tidb/issues/312 + mustExec(c, s.testDB, `create table issue312_1 (c float(24));`) + mustExec(c, s.testDB, `create table issue312_2 (c float(25));`) + tx = mustBegin(c, s.testDB) + rows, err := tx.Query(`desc issue312_1`) + c.Assert(err, IsNil) + for rows.Next() { + var ( + c1 string + c2 string + c3 string + c4 string + c5 string + c6 string + ) + rows.Scan(&c1, &c2, &c3, &c4, &c5, &c6) + c.Assert(c2, Equals, "float") + } + rows.Close() + mustCommit(c, tx) + tx = mustBegin(c, s.testDB) + rows, err = tx.Query(`desc issue312_2`) + c.Assert(err, IsNil) + for rows.Next() { + var ( + c1 string + c2 string + c3 string + c4 string + c5 string + c6 string + ) + rows.Scan(&c1, &c2, &c3, &c4, &c5, &c6) + c.Assert(c2, Equals, "double") + } + rows.Close() + mustCommit(c, tx) } func (s *testStmtSuite) TestCreateIndex(c *C) {