Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: year type should have an unsigned flag #7542

Merged
merged 7 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2117,24 +2117,29 @@ 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) {
func (s *testDBSuite) TestZeroFillCreateTable(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));")
s.tk.MustExec("create table abc(y year, z tinyint(10) zerofill, 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
var yearCol, zCol *model.ColumnInfo
for _, col := range tbl.Meta().Columns {
if col.Name.String() == "y" {
yearCol = col
break
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this removal affect test-case's behavior?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No.

}
if col.Name.String() == "z" {
zCol = col
}
}
c.Assert(yearCol, NotNil)
c.Assert(yearCol.Tp, Equals, mysql.TypeYear)
c.Assert(mysql.HasUnsignedFlag(yearCol.Flag), IsFalse)
c.Assert(mysql.HasUnsignedFlag(yearCol.Flag), IsTrue)

c.Assert(zCol, NotNil)
c.Assert(mysql.HasUnsignedFlag(zCol.Flag), IsTrue)
}

func (s *testDBSuite) TestCheckColumnDefaultValue(c *C) {
Expand Down
6 changes: 6 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ func columnDefToCol(ctx sessionctx.Context, offset int, colDef *ast.ColumnDef, o
col.Flag &= ^mysql.BinaryFlag
col.Flag |= mysql.ZerofillFlag
}
// If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column.
// See https://dev.mysql.com/doc/refman/5.7/en/numeric-type-overview.html for more details.
// But some types like bit and year, won't show its unsigned flag in `show create table`.
if mysql.HasZerofillFlag(col.Flag) {
col.Flag |= mysql.UnsignedFlag
}
err := checkPriKeyConstraint(col, hasDefaultValue, hasNullFlag, outPriKeyConstraint)
if err != nil {
return nil, nil, errors.Trace(err)
Expand Down
12 changes: 11 additions & 1 deletion executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,23 @@ func (s *testSuite) TestShow(c *C) {

// 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.MustExec(`create table t(y year unsigned signed zerofill zerofill, 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"))

// Test show create table with zerofill flag
tk.MustExec(`drop table if exists t`)
tk.MustExec(`create table t(id int primary key, val tinyint(10) zerofill);`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a year type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

year with zerofill?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not allowed, according to #7542 (comment) @shenli

tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|",
"t CREATE TABLE `t` (\n"+
" `id` int(11) NOT NULL,\n"+
" `val` tinyint(10) UNSIGNED ZEROFILL DEFAULT NULL,\n"+
" PRIMARY KEY (`id`)\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"))
}

func (s *testSuite) TestShowVisibility(c *C) {
Expand Down
2 changes: 1 addition & 1 deletion expression/typeinfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,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.ZerofillFlag, 4, 0},
{"c_year ", mysql.TypeYear, charset.CharsetBin, mysql.UnsignedFlag | 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},
Expand Down
2 changes: 1 addition & 1 deletion parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -6321,7 +6321,7 @@ DateAndTimeType:
}
$$ = x
}
| "YEAR" OptFieldLen
| "YEAR" OptFieldLen FieldOpts
{
x := types.NewFieldType(mysql.TypeYear)
x.Flen = $2.(int)
Expand Down
1 change: 1 addition & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1819,6 +1819,7 @@ func (s *testParserSuite) TestType(c *C) {

// for year
{"create table t (y year(4), y1 year)", true},
{"create table t (y year(4) unsigned zerofill zerofill, y1 year signed unsigned zerofill)", true},

// for national
{"create table t (c1 national char(2), c2 national varchar(2))", true},
Expand Down
3 changes: 3 additions & 0 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ func (c *Column) GetTypeDesc() string {
if mysql.HasUnsignedFlag(c.Flag) && c.Tp != mysql.TypeBit && c.Tp != mysql.TypeYear {
desc += " UNSIGNED"
}
if mysql.HasZerofillFlag(c.Flag) && c.Tp != mysql.TypeYear {
desc += " ZEROFILL"
}
return desc
}

Expand Down
2 changes: 1 addition & 1 deletion table/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (t *testTableSuite) TestString(c *C) {
col.Collate = mysql.DefaultCollationName
col.Flag |= mysql.ZerofillFlag | mysql.UnsignedFlag | mysql.BinaryFlag | mysql.AutoIncrementFlag | mysql.NotNullFlag

c.Assert(col.GetTypeDesc(), Equals, "tinyint(2) UNSIGNED")
c.Assert(col.GetTypeDesc(), Equals, "tinyint(2) UNSIGNED ZEROFILL")
col.ToInfo()
tbInfo := &model.TableInfo{}
c.Assert(col.IsPKHandleColumn(tbInfo), Equals, false)
Expand Down