Skip to content

Commit e43e548

Browse files
author
Lingyu Song
authored
infoschema: fix bug of column size of set and enum type (#7347)
1 parent 396b242 commit e43e548

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

infoschema/tables.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,28 @@ func dataForColumnsInTable(schema *model.DBInfo, tbl *model.TableInfo) [][]types
797797
if colLen == types.UnspecifiedLength {
798798
colLen = defaultFlen
799799
}
800+
if col.Tp == mysql.TypeSet {
801+
// Example: In MySQL set('a','bc','def','ghij') has length 13, because
802+
// len('a')+len('bc')+len('def')+len('ghij')+len(ThreeComma)=13
803+
// Reference link: https://bugs.mysql.com/bug.php?id=22613
804+
colLen = 0
805+
for _, ele := range col.Elems {
806+
colLen += len(ele)
807+
}
808+
if len(col.Elems) != 0 {
809+
colLen += (len(col.Elems) - 1)
810+
}
811+
} else if col.Tp == mysql.TypeEnum {
812+
// Example: In MySQL enum('a', 'ab', 'cdef') has length 4, because
813+
// the longest string in the enum is 'cdef'
814+
// Reference link: https://bugs.mysql.com/bug.php?id=22613
815+
colLen = 0
816+
for _, ele := range col.Elems {
817+
if len(ele) > colLen {
818+
colLen = len(ele)
819+
}
820+
}
821+
}
800822
if decimal == types.UnspecifiedLength {
801823
decimal = defaultDecimal
802824
}

infoschema/tables_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ func (s *testSuite) TestDataForTableRowsCountField(c *C) {
7777
tk.MustExec("create user xxx")
7878
tk.MustExec("flush privileges")
7979

80+
// Test for length of enum and set
81+
tk.MustExec("drop table if exists t")
82+
tk.MustExec("create table t ( s set('a','bc','def','ghij') default NULL, e1 enum('a', 'ab', 'cdef'), s2 SET('1','2','3','4','1585','ONE','TWO','Y','N','THREE'))")
83+
tk.MustQuery("select column_name, character_maximum_length from information_schema.columns where table_schema=Database() and table_name = 't' and column_name = 's'").Check(
84+
testkit.Rows("s 13"))
85+
tk.MustQuery("select column_name, character_maximum_length from information_schema.columns where table_schema=Database() and table_name = 't' and column_name = 's2'").Check(
86+
testkit.Rows("s2 30"))
87+
tk.MustQuery("select column_name, character_maximum_length from information_schema.columns where table_schema=Database() and table_name = 't' and column_name = 'e1'").Check(
88+
testkit.Rows("e1 4"))
89+
8090
tk1 := testkit.NewTestKit(c, store)
8191
tk1.MustExec("use test")
8292
c.Assert(tk1.Se.Auth(&auth.UserIdentity{

0 commit comments

Comments
 (0)