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

type: make decimal default precision visible in show create table #7667

Merged
merged 8 commits into from Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
10 changes: 2 additions & 8 deletions types/field_type.go
Expand Up @@ -193,7 +193,7 @@ func (ft *FieldType) CompactStr() string {
suffix := ""

defaultFlen, defaultDecimal := mysql.GetDefaultFieldLengthAndDecimal(ft.Tp)
isFlenNotDefault := ft.Flen != defaultFlen && ft.Flen != 0 && ft.Flen != UnspecifiedLength
//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 +227,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