Skip to content

Commit

Permalink
Fix parsing of PARTITION BY KEY (vitessio#10958) (vitessio#932)
Browse files Browse the repository at this point in the history
* Fix parsing of `PARTITION BY KEY`

The `PARTITION BY KEY` syntax allows for passing in an empty column list
which signifies that the primary key is used for partitioning.

This needs to be added to the parser to support this case.

See also https://dev.mysql.com/doc/refman/8.0/en/partitioning-key.html

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>

* Simplify parser

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
  • Loading branch information
dbussink authored Aug 9, 2022
1 parent 10d01b5 commit b0a2d11
Show file tree
Hide file tree
Showing 5 changed files with 5,590 additions and 5,556 deletions.
12 changes: 10 additions & 2 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,11 @@ func (node *PartitionOption) Format(buf *TrackedBuffer) {
if node.KeyAlgorithm != 0 {
buf.astPrintf(node, " algorithm = %d", node.KeyAlgorithm)
}
buf.astPrintf(node, " %v", node.ColList)
if len(node.ColList) == 0 {
buf.literal(" ()")
} else {
buf.astPrintf(node, " %v", node.ColList)
}
case RangeType, ListType:
buf.astPrintf(node, " %s", node.Type.ToString())
if node.Expr != nil {
Expand Down Expand Up @@ -611,7 +615,11 @@ func (node *SubPartition) Format(buf *TrackedBuffer) {
if node.KeyAlgorithm != 0 {
buf.astPrintf(node, " algorithm = %d", node.KeyAlgorithm)
}
buf.astPrintf(node, " %v", node.ColList)
if len(node.ColList) == 0 {
buf.literal(" ()")
} else {
buf.astPrintf(node, " %v", node.ColList)
}
}

if node.SubPartitions != -1 {
Expand Down
16 changes: 12 additions & 4 deletions go/vt/sqlparser/ast_format_fast.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,9 @@ var (
}, {
input: "create table t (id int) partition by key (id) partitions 2",
output: "create table t (\n\tid int\n)\npartition by key (id) partitions 2",
}, {
input: "create table t (id int, primary key(id)) partition by key () partitions 2",
output: "create table t (\n\tid int,\n\tprimary key (id)\n)\npartition by key () partitions 2",
}, {
input: "create table t (id int) partition by key algorithm = 1 (id)",
output: "create table t (\n\tid int\n)\npartition by key algorithm = 1 (id)",
Expand Down
Loading

0 comments on commit b0a2d11

Please sign in to comment.