Skip to content

Commit

Permalink
type: make decimal default precision visible in show create table (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Lingyu Song authored and jackysp committed Sep 12, 2018
1 parent 27047a0 commit b58a977
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
31 changes: 31 additions & 0 deletions executor/show_test.go
Expand Up @@ -105,6 +105,37 @@ func (s *testSuite) TestShow(c *C) {
c.Check(r, Equals, expectedRow[i])
}

// Issue #7665
tk.MustExec("drop table if exists `decimalschema`")
testSQL = "create table `decimalschema` (`c1` decimal);"
tk.MustExec(testSQL)
testSQL = "show create table decimalschema"
result = tk.MustQuery(testSQL)
c.Check(result.Rows(), HasLen, 1)
row = result.Rows()[0]
expectedRow = []interface{}{
"decimalschema", "CREATE TABLE `decimalschema` (\n" +
" `c1` decimal(11,0) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"}
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}

tk.MustExec("drop table if exists `decimalschema`")
testSQL = "create table `decimalschema` (`c1` decimal(15));"
tk.MustExec(testSQL)
testSQL = "show create table decimalschema"
result = tk.MustQuery(testSQL)
c.Check(result.Rows(), HasLen, 1)
row = result.Rows()[0]
expectedRow = []interface{}{
"decimalschema", "CREATE TABLE `decimalschema` (\n" +
" `c1` decimal(15,0) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"}
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}

testSQL = "SHOW VARIABLES LIKE 'character_set_results';"
result = tk.MustQuery(testSQL)
c.Check(result.Rows(), HasLen, 1)
Expand Down
9 changes: 1 addition & 8 deletions types/field_type.go
Expand Up @@ -193,7 +193,6 @@ func (ft *FieldType) CompactStr() string {
suffix := ""

defaultFlen, defaultDecimal := mysql.GetDefaultFieldLengthAndDecimal(ft.Tp)
isFlenNotDefault := ft.Flen != defaultFlen && ft.Flen != 0 && ft.Flen != UnspecifiedLength
isDecimalNotDefault := ft.Decimal != defaultDecimal && ft.Decimal != 0 && ft.Decimal != UnspecifiedLength

// displayFlen and displayDecimal are flen and decimal values with `-1` substituted with default value.
Expand Down Expand Up @@ -227,13 +226,7 @@ func (ft *FieldType) CompactStr() string {
suffix = fmt.Sprintf("(%d,%d)", displayFlen, displayDecimal)
}
case mysql.TypeNewDecimal:
if isFlenNotDefault || isDecimalNotDefault {
suffix = fmt.Sprintf("(%d", displayFlen)
if isDecimalNotDefault {
suffix += fmt.Sprintf(",%d", displayDecimal)
}
suffix += ")"
}
suffix = fmt.Sprintf("(%d,%d)", displayFlen, displayDecimal)
case mysql.TypeBit, mysql.TypeShort, mysql.TypeTiny, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong, mysql.TypeVarchar, mysql.TypeString, mysql.TypeVarString:
// Flen is always shown.
suffix = fmt.Sprintf("(%d)", displayFlen)
Expand Down

0 comments on commit b58a977

Please sign in to comment.