Skip to content

Commit

Permalink
executor: Fix DEFAULT output in SHOW CREATE TABLE (pingcap#4427)
Browse files Browse the repository at this point in the history
  • Loading branch information
breezewish authored and coocood committed Sep 5, 2017
1 parent 3f773ba commit bc0efef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
11 changes: 10 additions & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package executor

import (
"bytes"
"encoding/binary"
"fmt"
"sort"
"strings"
Expand Down Expand Up @@ -429,7 +430,15 @@ func (e *ShowExec) fetchShowCreateTable() error {
case "CURRENT_TIMESTAMP":
buf.WriteString(" DEFAULT CURRENT_TIMESTAMP")
default:
buf.WriteString(fmt.Sprintf(" DEFAULT '%v'", col.DefaultValue))
defaultValStr := fmt.Sprintf("%v", col.DefaultValue)
if col.Tp == mysql.TypeBit {
bytes := make([]byte, 8)
copy(bytes[8-len(defaultValStr):8], []byte(defaultValStr))
intValue := binary.BigEndian.Uint64(bytes)
buf.WriteString(fmt.Sprintf(" DEFAULT b'%b'", intValue))
} else {
buf.WriteString(fmt.Sprintf(" DEFAULT '%s'", format.OutputFormat(defaultValStr)))
}
}
}
if mysql.HasOnUpdateNowFlag(col.Flag) {
Expand Down
36 changes: 36 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (s *testSuite) TestShow(c *C) {
}()
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

testSQL := `drop table if exists show_test`
tk.MustExec(testSQL)
testSQL = `create table SHOW_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int comment "c1_comment", c2 int, c3 int default 1) ENGINE=InnoDB AUTO_INCREMENT=28934 DEFAULT CHARSET=utf8 COMMENT "table_comment";`
Expand Down Expand Up @@ -148,6 +149,41 @@ func (s *testSuite) TestShow(c *C) {
" `id` int(11) DEFAULT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=4",
))

// for issue https://github.com/pingcap/tidb/issues/4425
tk.MustExec("drop table if exists show_test")
testSQL = `create table show_test(
a varchar(10) DEFAULT 'abc\ndef'
);`
tk.MustExec(testSQL)
testSQL = "show create table show_test;"
result = tk.MustQuery(testSQL)
c.Check(result.Rows(), HasLen, 1)
row = result.Rows()[0]
expectedRow = []interface{}{
"show_test", "CREATE TABLE `show_test` (\n `a` varchar(10) DEFAULT 'abc\\ndef'\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"}
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}

// for issue https://github.com/pingcap/tidb/issues/4426
tk.MustExec("drop table if exists show_test")
testSQL = `create table show_test(
a bit(1),
b bit(32) DEFAULT 0b0,
c bit(1) DEFAULT 0b1,
d bit(10) DEFAULT 0b1010
);`
tk.MustExec(testSQL)
testSQL = "show create table show_test;"
result = tk.MustQuery(testSQL)
c.Check(result.Rows(), HasLen, 1)
row = result.Rows()[0]
expectedRow = []interface{}{
"show_test", "CREATE TABLE `show_test` (\n `a` bit(1) DEFAULT NULL,\n `b` bit(32) DEFAULT b'0',\n `c` bit(1) DEFAULT b'1',\n `d` bit(10) DEFAULT b'1010'\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"}
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}
}

func (s *testSuite) TestShowVisibility(c *C) {
Expand Down

0 comments on commit bc0efef

Please sign in to comment.