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

partition: Show partition reformat #29945

Merged
merged 9 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (s *testIntegrationSuite3) TestCreateTableWithPartition(c *C) {
partition p0 values less than (to_seconds('2004-01-01')),
partition p1 values less than (to_seconds('2005-01-01')));`)
tk.MustQuery("show create table t26").Check(
testkit.Rows("t26 CREATE TABLE `t26` (\n `a` date DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\nPARTITION BY RANGE ( TO_SECONDS(`a`) ) (\n PARTITION `p0` VALUES LESS THAN (63240134400),\n PARTITION `p1` VALUES LESS THAN (63271756800)\n)"))
testkit.Rows("t26 CREATE TABLE `t26` (\n `a` date DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\nPARTITION BY RANGE (TO_SECONDS(`a`))\n(PARTITION `p0` VALUES LESS THAN (63240134400),\n PARTITION `p1` VALUES LESS THAN (63271756800))"))
tk.MustExec(`create table t27 (a bigint unsigned not null)
partition by range(a) (
partition p0 values less than (10),
Expand Down
3 changes: 3 additions & 0 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@ func buildHashPartitionDefinitions(_ sessionctx.Context, defs []*ast.PartitionDe
def := defs[i]
definitions[i].Name = def.Name
definitions[i].Comment, _ = def.Comment()
if err := setPartitionPlacementFromOptions(&definitions[i], def.Options); err != nil {
return nil, err
}
}
}
return definitions, nil
Expand Down
212 changes: 94 additions & 118 deletions ddl/placement_policy_test.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions executor/seqtest/seq_executor_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ func TestShow(t *testing.T) {
tk.MustQuery("show create table t").Check(testutil.RowsWithSep("|",
"t CREATE TABLE `t` (\n"+
" `a` int(11) DEFAULT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_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)",
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_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))",
))

tk.MustExec(`drop table if exists t`)
Expand Down Expand Up @@ -529,8 +529,8 @@ func TestShow(t *testing.T) {
tk.MustQuery("show create table t").Check(testutil.RowsWithSep("|",
"t CREATE TABLE `t` (\n"+
" `a` int(11) DEFAULT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"+"\nPARTITION BY HASH( `a` )\nPARTITIONS 4",
))
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n"+
"PARTITION BY HASH (`a`) PARTITIONS 4"))

// Test show create table compression type.
tk.MustExec(`drop table if exists t1`)
Expand Down
108 changes: 51 additions & 57 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -1232,58 +1232,54 @@ func appendPartitionInfo(partitionInfo *model.PartitionInfo, buf *bytes.Buffer,
if partitionInfo == nil {
return
}
// Since MySQL 5.1/5.5 is very old and TiDB aims for 5.7/8.0 compatibility, we will not
// include the /*!50100 or /*!50500 comments for TiDB.
// This also solves the issue with comments within comments that would happen for
// PLACEMENT POLICY options.
if partitionInfo.Type == model.PartitionTypeHash {
fmt.Fprintf(buf, "\nPARTITION BY HASH( %s )", partitionInfo.Expr)
fmt.Fprintf(buf, "\nPARTITIONS %d", partitionInfo.Num)
return
defaultPartitionDefinitions := true
for i, def := range partitionInfo.Definitions {
if def.Name.O != fmt.Sprintf("p%d", i) {
defaultPartitionDefinitions = false
break
}
if len(def.Comment) > 0 || def.DirectPlacementOpts != nil || def.PlacementPolicyRef != nil {
defaultPartitionDefinitions = false
break
}
}

if defaultPartitionDefinitions {
fmt.Fprintf(buf, "\nPARTITION BY HASH (%s) PARTITIONS %d", partitionInfo.Expr, partitionInfo.Num)
return
}
}
// this if statement takes care of range columns case
if partitionInfo.Columns != nil && partitionInfo.Type == model.PartitionTypeRange {
buf.WriteString("\nPARTITION BY RANGE COLUMNS(")
// this if statement takes care of lists/range columns case
if partitionInfo.Columns != nil {
// partitionInfo.Type == model.PartitionTypeRange || partitionInfo.Type == model.PartitionTypeList
// Notice that MySQL uses two spaces between LIST and COLUMNS...
fmt.Fprintf(buf, "\nPARTITION BY %s COLUMNS(", partitionInfo.Type.String())
for i, col := range partitionInfo.Columns {
buf.WriteString(col.L)
buf.WriteString(stringutil.Escape(col.O, sqlMode))
if i < len(partitionInfo.Columns)-1 {
buf.WriteString(",")
}
}
buf.WriteString(") (\n")
} else if partitionInfo.Type == model.PartitionTypeList {
if len(partitionInfo.Columns) == 0 {
fmt.Fprintf(buf, "\nPARTITION BY %s (%s) (\n", partitionInfo.Type.String(), partitionInfo.Expr)
} else {
colsName := ""
for _, col := range partitionInfo.Columns {
if len(colsName) > 0 {
colsName += ","
}
colsName += col.L
}
fmt.Fprintf(buf, "\nPARTITION BY LIST COLUMNS(%s) (\n", colsName)
}
buf.WriteString(")\n(")
} else {
fmt.Fprintf(buf, "\nPARTITION BY %s ( %s ) (\n", partitionInfo.Type.String(), partitionInfo.Expr)
fmt.Fprintf(buf, "\nPARTITION BY %s (%s)\n(", partitionInfo.Type.String(), partitionInfo.Expr)
}
if partitionInfo.Type == model.PartitionTypeRange {
for i, def := range partitionInfo.Definitions {
lessThans := strings.Join(def.LessThan, ",")
fmt.Fprintf(buf, " PARTITION `%s` VALUES LESS THAN (%s)", def.Name, lessThans)
if def.DirectPlacementOpts != nil {
// add direct placement info here
appendDirectPlacementInfo(def.DirectPlacementOpts, buf)
}
if def.PlacementPolicyRef != nil {
// add placement ref info here
fmt.Fprintf(buf, " /*T![placement] PLACEMENT POLICY=%s */", stringutil.Escape(def.PlacementPolicyRef.Name.O, sqlMode))
}
if i < len(partitionInfo.Definitions)-1 {
buf.WriteString(",\n")
} else {
buf.WriteString("\n")
}

for i, def := range partitionInfo.Definitions {
if i > 0 {
fmt.Fprintf(buf, ",\n ")
}
buf.WriteString(")")
} else if partitionInfo.Type == model.PartitionTypeList {
for i, def := range partitionInfo.Definitions {
fmt.Fprintf(buf, "PARTITION %s", stringutil.Escape(def.Name.O, sqlMode))
// PartitionTypeHash does not have any VALUES definition
if partitionInfo.Type == model.PartitionTypeRange {
lessThans := strings.Join(def.LessThan, ",")
fmt.Fprintf(buf, " VALUES LESS THAN (%s)", lessThans)
} else if partitionInfo.Type == model.PartitionTypeList {
values := bytes.NewBuffer(nil)
for j, inValues := range def.InValues {
if j > 0 {
Expand All @@ -1297,23 +1293,21 @@ func appendPartitionInfo(partitionInfo *model.PartitionInfo, buf *bytes.Buffer,
values.WriteString(strings.Join(inValues, ","))
}
}
fmt.Fprintf(buf, " PARTITION `%s` VALUES IN (%s)", def.Name, values.String())
if def.DirectPlacementOpts != nil {
// add direct placement info here
appendDirectPlacementInfo(def.DirectPlacementOpts, buf)
}
if def.PlacementPolicyRef != nil {
// add placement ref info here
fmt.Fprintf(buf, " /*T![placement] PLACEMENT POLICY=%s */", stringutil.Escape(def.PlacementPolicyRef.Name.O, sqlMode))
}
if i < len(partitionInfo.Definitions)-1 {
buf.WriteString(",\n")
} else {
buf.WriteString("\n")
}
fmt.Fprintf(buf, " VALUES IN (%s)", values.String())
}
if len(def.Comment) > 0 {
buf.WriteString(fmt.Sprintf(" COMMENT '%s'", format.OutputFormat(def.Comment)))
}
if def.DirectPlacementOpts != nil {
// add direct placement info here
appendDirectPlacementInfo(def.DirectPlacementOpts, buf)
}
if def.PlacementPolicyRef != nil {
// add placement ref info here
fmt.Fprintf(buf, " /*T![placement] PLACEMENT POLICY=%s */", stringutil.Escape(def.PlacementPolicyRef.Name.O, sqlMode))
}
buf.WriteString(")")
}
buf.WriteString(")")
}

// ConstructResultOfShowCreateDatabase constructs the result for show create database.
Expand Down
Loading