From b58a977289b6963e17cee0dfd7738e0a62f9e2a4 Mon Sep 17 00:00:00 2001 From: Lingyu Song Date: Wed, 12 Sep 2018 10:29:07 +0800 Subject: [PATCH] type: make decimal default precision visible in `show create table` (#7667) --- executor/show_test.go | 31 +++++++++++++++++++++++++++++++ types/field_type.go | 9 +-------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/executor/show_test.go b/executor/show_test.go index 4527afd03bb1..a3f8e4ac7604 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -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) diff --git a/types/field_type.go b/types/field_type.go index 29d3a3fd1c9d..996fc9afed85 100644 --- a/types/field_type.go +++ b/types/field_type.go @@ -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. @@ -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)