diff --git a/executor/show.go b/executor/show.go index a579294453fa1..9f9179f04af3a 100644 --- a/executor/show.go +++ b/executor/show.go @@ -614,6 +614,24 @@ func (e *ShowExec) fetchShowCreateTable() error { buf.WriteString(fmt.Sprintf(" DEFAULT CHARSET=%s COLLATE=%s", charsetName, collate)) } + // add partition info here. + partitionInfo := tb.Meta().Partition + if partitionInfo != nil { + buf.WriteString(fmt.Sprintf("\nPARTITION BY %s ( %s ) (\n", partitionInfo.Type.String(), partitionInfo.Expr)) + for i, def := range partitionInfo.Definitions { + if i < len(partitionInfo.Definitions)-1 { + buf.WriteString(fmt.Sprintf(" PARTITION %s VALUES LESS THAN %s,\n", def.Name, def.LessThan[0])) + } else { + if def.MaxValue { + buf.WriteString(fmt.Sprintf(" PARTITION %s VALUES LESS THAN %s\n", def.Name, "MAXVALUE")) + } else { + buf.WriteString(fmt.Sprintf(" PARTITION %s VALUES LESS THAN %s\n", def.Name, def.LessThan[0])) + } + } + } + buf.WriteString(")") + } + if hasAutoIncID { autoIncID, err := tb.Allocator(e.ctx).NextGlobalAutoID(tb.Meta().ID) if err != nil { diff --git a/executor/show_test.go b/executor/show_test.go index e2c7441e9feb2..7bc0c14d481c7 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -312,7 +312,17 @@ func (s *testSuite) TestShow(c *C) { ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", )) + // Test range partition tk.MustExec(`drop table if exists t`) + tk.MustExec(`CREATE TABLE t (a int) PARTITION BY RANGE(a) ( + PARTITION p0 VALUES LESS THAN (10), + PARTITION p1 VALUES LESS THAN (20), + PARTITION p2 VALUES LESS THAN (MAXVALUE))`) + tk.MustQuery("show create table t").Check(testutil.RowsWithSep("|", + "t CREATE TABLE `t` (\n"+ + " `a` int(11) DEFAULT NULL\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"+"\nPARTITION BY RANGE ( `a` ) (\n PARTITION p0 VALUES LESS THAN 10,\n PARTITION p1 VALUES LESS THAN 20,\n PARTITION p2 VALUES LESS THAN MAXVALUE\n)", + )) } func (s *testSuite) TestShowVisibility(c *C) { diff --git a/model/model.go b/model/model.go index 456d5e99fa63e..f3c6e2f6cf5d7 100644 --- a/model/model.go +++ b/model/model.go @@ -233,6 +233,20 @@ const ( PartitionTypeList PartitionType = 3 ) +func (p PartitionType) String() string { + switch p { + case PartitionTypeRange: + return "RANGE" + case PartitionTypeHash: + return "HASH" + case PartitionTypeList: + return "LIST" + default: + return "" + } + +} + // PartitionInfo provides table partition info. type PartitionInfo struct { Type PartitionType