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 3 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
4 changes: 4 additions & 0 deletions types/field_type.go
Expand Up @@ -231,8 +231,12 @@ func (ft *FieldType) CompactStr() string {
suffix = fmt.Sprintf("(%d", displayFlen)
if isDecimalNotDefault {
Copy link
Member

Choose a reason for hiding this comment

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

how about:

if isFlenNotDefault {
	prefix = xxx
	break
}
if isDecimalNotDefault {
	prefix = xxx
	break
}
prefix = xx

suffix += fmt.Sprintf(",%d", displayDecimal)
} else {
suffix += fmt.Sprintf(",%d", 0)
}
suffix += ")"
} else {
suffix = fmt.Sprintf("(%d,0)", defaultFlen)
Copy link
Member

Choose a reason for hiding this comment

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

It would be simpler if calculate the displayFlen and displayDecimal first, then just Sprintf once.

suffix = fmt.Sprintf("(%d,%d)", displayFlen, displayDecimal)

Copy link
Contributor

Choose a reason for hiding this comment

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

When the type is mysql.TypeNewDecimal, why still need to check isFlenNotDefault or isDecimalNotDefault? What about just using suffix = fmt.Sprintf("(%d,%d)", displayFlen, displayDecimal) ?

Copy link
Member

Choose a reason for hiding this comment

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

@winkyao , have you checked it with MySQL?

Copy link
Author

Choose a reason for hiding this comment

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

I think just use suffix = fmt.Sprintf("(%d,%d)", displayFlen, displayDecimal) is okay.

Copy link
Contributor

Choose a reason for hiding this comment

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

When it is a decimal type, mysql show decimal length in show create table anyway. @jackysp

}
Copy link
Member

Choose a reason for hiding this comment

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

how about

if isFlenNotDefault {
} else {
}
if isDecimalNotDefault {
} else {
}

case mysql.TypeBit, mysql.TypeShort, mysql.TypeTiny, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong, mysql.TypeVarchar, mysql.TypeString, mysql.TypeVarString:
// Flen is always shown.
Expand Down