Skip to content

Commit

Permalink
Merge pull request pingcap#1 from spongedu/cdb_fix_partition_algorithm
Browse files Browse the repository at this point in the history
parser, ast: make parser happy for PARTITION BY [LINEAR] KEY ALGORITH…
  • Loading branch information
junhuaqin authored May 8, 2021
2 parents 27dba85 + 0f785f3 commit 38c7db1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
Binary file added ast/.ddl_test.go.swp
Binary file not shown.
13 changes: 13 additions & 0 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package ast

import (
"fmt"

"github.com/pingcap/errors"
"github.com/pingcap/parser/auth"
"github.com/pingcap/parser/format"
Expand Down Expand Up @@ -3129,6 +3131,13 @@ type PartitionMethod struct {

// Num is the number of (sub)partitions required by the method.
Num uint64

// KeyAlgorithm is the optional hash algorithm type for `PARTITION BY [LINEAR] KEY` syntax.
KeyAlgorithm *PartitionKeyAlgorithm
}

type PartitionKeyAlgorithm struct {
Type uint64
}

// Restore implements the Node interface
Expand All @@ -3138,6 +3147,10 @@ func (n *PartitionMethod) Restore(ctx *format.RestoreCtx) error {
}
ctx.WriteKeyWord(n.Tp.String())

if n.KeyAlgorithm != nil {
ctx.WritePlain(fmt.Sprintf(" ALGORITHM = %d", n.KeyAlgorithm.Type))
}

switch {
case n.Tp == model.PartitionTypeSystemTime:
if n.Expr != nil && n.Unit != TimeUnitInvalid {
Expand Down
24 changes: 21 additions & 3 deletions parser.go

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

24 changes: 19 additions & 5 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -3286,10 +3286,13 @@ PartitionOpt:
SubPartitionMethod:
LinearOpt "KEY" PartitionKeyAlgorithmOpt '(' ColumnNameListOpt ')'
{
keyAlgorithm, _ := $3.(*ast.PartitionKeyAlgorithm)

$$ = &ast.PartitionMethod{
Tp: model.PartitionTypeKey,
Linear: len($1) != 0,
ColumnNames: $5.([]*ast.ColumnName),
Tp: model.PartitionTypeKey,
Linear: len($1) != 0,
ColumnNames: $5.([]*ast.ColumnName),
KeyAlgorithm: keyAlgorithm,
}
}
| LinearOpt "HASH" '(' Expression ')'
Expand All @@ -3303,9 +3306,20 @@ SubPartitionMethod:

PartitionKeyAlgorithmOpt:
/* empty */
{}
{
$$ = nil
}
| "ALGORITHM" eq NUM
{}
{
tp := getUint64FromNUM($3)
if tp != 1 && tp != 2 {
yylex.AppendError(ErrSyntax)
return 1
}
$$ = &ast.PartitionKeyAlgorithm{
Type: tp,
}
}

PartitionMethod:
SubPartitionMethod
Expand Down
12 changes: 12 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5148,3 +5148,15 @@ func (s *testParserSuite) TestBRIE(c *C) {

s.RunTest(c, table)
}

// For `PARTITION BY [LINEAR] KEY ALGORITHM` syntax
func (s *testParserSuite) TestPartitionKeyAlgorithm(c *C) {
table := []testCase{
{"CREATE TABLE t492 (c1 integer ,c2 integer) PARTITION BY LINEAR KEY ALGORITHM = 1 (c1,c2) PARTITIONS 4", true, "CREATE TABLE `t492` (`c1` INT,`c2` INT) PARTITION BY LINEAR KEY ALGORITHM = 1 (`c1`,`c2`) PARTITIONS 4"},
{"CREATE TABLE t492 (c1 integer ,c2 integer) PARTITION BY LINEAR KEY ALGORITHM = -1 (c1,c2) PARTITIONS 4", false, ""},
{"CREATE TABLE t492 (c1 integer ,c2 integer) PARTITION BY LINEAR KEY ALGORITHM = 0 (c1,c2) PARTITIONS 4", false, ""},
{"CREATE TABLE t492 (c1 integer ,c2 integer) PARTITION BY LINEAR KEY ALGORITHM = 3 (c1,c2) PARTITIONS 4", false, ""},
}

s.RunTest(c, table)
}

0 comments on commit 38c7db1

Please sign in to comment.