From 9b53781f3774024df7ba81c7c934a95f5a50cd30 Mon Sep 17 00:00:00 2001 From: winkyao Date: Mon, 4 Jun 2018 16:02:09 +0800 Subject: [PATCH 1/2] ddl: year type should not has unsigned flag --- ddl/ddl_api.go | 2 +- ddl/ddl_db_test.go | 20 ++++++++++++++++++++ executor/show_test.go | 10 ++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index d18ba6649f637..5e3660a171d9b 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -366,7 +366,7 @@ func columnDefToCol(ctx sessionctx.Context, offset int, colDef *ast.ColumnDef) ( if col.Tp == mysql.TypeYear { // For Year field, it's charset is binary but does not have binary flag. col.Flag &= ^mysql.BinaryFlag - col.Flag |= mysql.UnsignedFlag | mysql.ZerofillFlag + col.Flag |= mysql.ZerofillFlag } err := checkDefaultValue(ctx, col, hasDefaultValue) if err != nil { diff --git a/ddl/ddl_db_test.go b/ddl/ddl_db_test.go index d95e8c903e0f8..10371a4fc3866 100644 --- a/ddl/ddl_db_test.go +++ b/ddl/ddl_db_test.go @@ -1894,6 +1894,26 @@ func (s *testDBSuite) TestRebaseAutoID(c *C) { s.testErrorCode(c, "alter table tidb.test2 add column b int auto_increment key, auto_increment=10;", tmysql.ErrUnknown) } +func (s *testDBSuite) TestYearTypeCreateTable(c *C) { + s.tk = testkit.NewTestKit(c, s.store) + s.tk.MustExec("use test") + s.tk.MustExec("drop table if exists abc;") + s.tk.MustExec("create table abc(y year, x int, primary key(y));") + is := s.dom.InfoSchema() + tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("abc")) + c.Assert(err, IsNil) + var yearCol *model.ColumnInfo + for _, col := range tbl.Meta().Columns { + if col.Name.String() == "y" { + yearCol = col + break + } + } + c.Assert(yearCol, NotNil) + c.Assert(yearCol.Tp, Equals, mysql.TypeYear) + c.Assert(mysql.HasUnsignedFlag(yearCol.Flag), IsFalse) +} + func (s *testDBSuite) TestCharacterSetInColumns(c *C) { s.tk = testkit.NewTestKit(c, s.store) s.tk.MustExec("drop database if exists varchar_test;") diff --git a/executor/show_test.go b/executor/show_test.go index 22bd062c0acd9..46b68577d687c 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -345,6 +345,16 @@ func (s *testSuite) TestShow(c *C) { " `d` int(11) DEFAULT NULL\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"+"\nPARTITION BY RANGE COLUMNS(a,d,c) (\n PARTITION p0 VALUES LESS THAN (5,10,\"ggg\"),\n PARTITION p1 VALUES LESS THAN (10,20,\"mmm\"),\n PARTITION p2 VALUES LESS THAN (15,30,\"sss\"),\n PARTITION p3 VALUES LESS THAN (50,MAXVALUE,MAXVALUE)\n)", )) + + // Test show create table year type + tk.MustExec(`drop table if exists t`) + tk.MustExec(`create table t(y year, x int, primary key(y));`) + tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", + "t CREATE TABLE `t` (\n"+ + " `y` year NOT NULL,\n"+ + " `x` int(11) DEFAULT NULL,\n"+ + " PRIMARY KEY (`y`)\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin")) } func (s *testSuite) TestShowVisibility(c *C) { From f1f0bc6e728ff77b1468160b9222e84db837a4b7 Mon Sep 17 00:00:00 2001 From: winkyao Date: Mon, 4 Jun 2018 16:35:21 +0800 Subject: [PATCH 2/2] fix ci and address comment --- expression/typeinfer_test.go | 2 +- table/column.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/expression/typeinfer_test.go b/expression/typeinfer_test.go index 9bddbc2ccc0a8..3d3b919222685 100644 --- a/expression/typeinfer_test.go +++ b/expression/typeinfer_test.go @@ -204,7 +204,7 @@ func (s *testInferTypeSuite) createTestCase4Cast() []typeInferTestCase { func (s *testInferTypeSuite) createTestCase4Columns() []typeInferTestCase { return []typeInferTestCase{ {"c_bit ", mysql.TypeBit, charset.CharsetBin, mysql.UnsignedFlag, 10, 0}, - {"c_year ", mysql.TypeYear, charset.CharsetBin, mysql.UnsignedFlag | mysql.ZerofillFlag, 4, 0}, + {"c_year ", mysql.TypeYear, charset.CharsetBin, mysql.ZerofillFlag, 4, 0}, {"c_int_d ", mysql.TypeLong, charset.CharsetBin, 0, 11, 0}, {"c_uint_d ", mysql.TypeLong, charset.CharsetBin, mysql.UnsignedFlag, 10, 0}, {"c_bigint_d ", mysql.TypeLonglong, charset.CharsetBin, 0, 20, 0}, diff --git a/table/column.go b/table/column.go index d3344fe4cf17f..af90941b3621a 100644 --- a/table/column.go +++ b/table/column.go @@ -200,7 +200,7 @@ const defaultPrivileges = "select,insert,update,references" // GetTypeDesc gets the description for column type. func (c *Column) GetTypeDesc() string { desc := c.FieldType.CompactStr() - if mysql.HasUnsignedFlag(c.Flag) && c.Tp != mysql.TypeBit { + if mysql.HasUnsignedFlag(c.Flag) && c.Tp != mysql.TypeBit && c.Tp != mysql.TypeYear { desc += " UNSIGNED" } return desc