From 33d4db4adc7d2830cb455d3668983e77e9b6ac93 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 4 Jun 2019 12:11:18 +0800 Subject: [PATCH] executor: fix show create table for partitioned table --- executor/show.go | 49 ++++++++++++++++++++------------------ executor/show_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 23 deletions(-) diff --git a/executor/show.go b/executor/show.go index 3d6ee944cb14a..516e72a04b93e 100644 --- a/executor/show.go +++ b/executor/show.go @@ -671,8 +671,33 @@ func (e *ShowExec) fetchShowCreateTable() error { buf.WriteString(fmt.Sprintf(" COMPRESSION='%s'", tb.Meta().Compression)) } + if hasAutoIncID { + autoIncID, err := tb.Allocator(e.ctx).NextGlobalAutoID(tb.Meta().ID) + if err != nil { + return errors.Trace(err) + } + // It's campatible with MySQL. + if autoIncID > 1 { + buf.WriteString(fmt.Sprintf(" AUTO_INCREMENT=%d", autoIncID)) + } + } + + if tb.Meta().ShardRowIDBits > 0 { + buf.WriteString(fmt.Sprintf("/*!90000 SHARD_ROW_ID_BITS=%d */", tb.Meta().ShardRowIDBits)) + } + + if len(tb.Meta().Comment) > 0 { + buf.WriteString(fmt.Sprintf(" COMMENT='%s'", format.OutputFormat(tb.Meta().Comment))) + } + // add partition info here. - partitionInfo := tb.Meta().Partition + appendPartitionInfo(tb.Meta().Partition, &buf) + + e.appendRow([]interface{}{tb.Meta().Name.O, buf.String()}) + return nil +} + +func appendPartitionInfo(partitionInfo *model.PartitionInfo, buf *bytes.Buffer) { if partitionInfo != nil { // this if statement takes care of range columns case if partitionInfo.Columns != nil && partitionInfo.Type == model.PartitionTypeRange { @@ -703,28 +728,6 @@ func (e *ShowExec) fetchShowCreateTable() error { } buf.WriteString(")") } - - if hasAutoIncID { - autoIncID, err := tb.Allocator(e.ctx).NextGlobalAutoID(tb.Meta().ID) - if err != nil { - return errors.Trace(err) - } - // It's campatible with MySQL. - if autoIncID > 1 { - buf.WriteString(fmt.Sprintf(" AUTO_INCREMENT=%d", autoIncID)) - } - } - - if tb.Meta().ShardRowIDBits > 0 { - buf.WriteString(fmt.Sprintf("/*!90000 SHARD_ROW_ID_BITS=%d */", tb.Meta().ShardRowIDBits)) - } - - if len(tb.Meta().Comment) > 0 { - buf.WriteString(fmt.Sprintf(" COMMENT='%s'", format.OutputFormat(tb.Meta().Comment))) - } - - e.appendRow([]interface{}{tb.Meta().Name.O, buf.String()}) - return nil } // fetchShowCreateDatabase composes show create database result. diff --git a/executor/show_test.go b/executor/show_test.go index 39854e0c0bd10..0124d8debf66a 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -748,6 +748,61 @@ func (s *testSuite) TestShowCreateTable(c *C) { " `ch2` varbinary(10) DEFAULT NULL\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", )) + + tk.MustExec("CREATE TABLE `log` (" + + "`LOG_ID` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT," + + "`ROUND_ID` bigint(20) UNSIGNED NOT NULL," + + "`USER_ID` int(10) UNSIGNED NOT NULL," + + "`USER_IP` int(10) UNSIGNED DEFAULT NULL," + + "`END_TIME` datetime NOT NULL," + + "`USER_TYPE` int(11) DEFAULT NULL," + + "`APP_ID` int(11) DEFAULT NULL," + + "PRIMARY KEY (`LOG_ID`,`END_TIME`)," + + "KEY `IDX_EndTime` (`END_TIME`)," + + "KEY `IDX_RoundId` (`ROUND_ID`)," + + "KEY `IDX_UserId_EndTime` (`USER_ID`,`END_TIME`)" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=505488 " + + "PARTITION BY RANGE ( month(`end_time`) ) (" + + "PARTITION p1 VALUES LESS THAN (2)," + + "PARTITION p2 VALUES LESS THAN (3)," + + "PARTITION p3 VALUES LESS THAN (4)," + + "PARTITION p4 VALUES LESS THAN (5)," + + "PARTITION p5 VALUES LESS THAN (6)," + + "PARTITION p6 VALUES LESS THAN (7)," + + "PARTITION p7 VALUES LESS THAN (8)," + + "PARTITION p8 VALUES LESS THAN (9)," + + "PARTITION p9 VALUES LESS THAN (10)," + + "PARTITION p10 VALUES LESS THAN (11)," + + "PARTITION p11 VALUES LESS THAN (12)," + + "PARTITION p12 VALUES LESS THAN (MAXVALUE))") + tk.MustQuery("show create table log").Check(testutil.RowsWithSep("|", + "log CREATE TABLE `log` (\n"+ + " `LOG_ID` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,\n"+ + " `ROUND_ID` bigint(20) UNSIGNED NOT NULL,\n"+ + " `USER_ID` int(10) UNSIGNED NOT NULL,\n"+ + " `USER_IP` int(10) UNSIGNED DEFAULT NULL,\n"+ + " `END_TIME` datetime NOT NULL,\n"+ + " `USER_TYPE` int(11) DEFAULT NULL,\n"+ + " `APP_ID` int(11) DEFAULT NULL,\n"+ + " PRIMARY KEY (`LOG_ID`,`END_TIME`),\n"+ + " KEY `IDX_EndTime` (`END_TIME`),\n"+ + " KEY `IDX_RoundId` (`ROUND_ID`),\n"+ + " KEY `IDX_UserId_EndTime` (`USER_ID`,`END_TIME`)\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=505488\n"+ + "PARTITION BY RANGE ( month(`end_time`) ) (\n"+ + " PARTITION p1 VALUES LESS THAN (2),\n"+ + " PARTITION p2 VALUES LESS THAN (3),\n"+ + " PARTITION p3 VALUES LESS THAN (4),\n"+ + " PARTITION p4 VALUES LESS THAN (5),\n"+ + " PARTITION p5 VALUES LESS THAN (6),\n"+ + " PARTITION p6 VALUES LESS THAN (7),\n"+ + " PARTITION p7 VALUES LESS THAN (8),\n"+ + " PARTITION p8 VALUES LESS THAN (9),\n"+ + " PARTITION p9 VALUES LESS THAN (10),\n"+ + " PARTITION p10 VALUES LESS THAN (11),\n"+ + " PARTITION p11 VALUES LESS THAN (12),\n"+ + " PARTITION p12 VALUES LESS THAN (MAXVALUE)\n"+ + ")")) } func (s *testSuite) TestShowEscape(c *C) {