Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: parse mysql table partition to metadata (#11162)

h3n4l167fd17275b1
diff --git a/backend/plugin/parser/mysql/differ.go b/backend/plugin/parser/mysql/differ.go
index 94a12b5678..636ff97479 100644
--- a/backend/plugin/parser/mysql/differ.go
+++ b/backend/plugin/parser/mysql/differ.go
@@ -2053,6 +2053,7 @@ func (t *mysqlTransformer) EnterPartitionClause(ctx *mysql.PartitionClauseContex
identifierList[i] = fmt.Sprintf("`%s`", identifier)
}
}
+ expr = strings.Join(identifierList, ",")
} else {
bitExprText := partitionTypeDefCtx.GetParser().GetTokenStream().GetTextFromRuleContext(partitionTypeDefCtx.BitExpr())
bitExprFields := strings.Split(bitExprText, ",")
@@ -2084,26 +2085,29 @@ func (t *mysqlTransformer) EnterPartitionClause(ctx *mysql.PartitionClauseContex
switch tp {
case storepb.TablePartitionMetadata_RANGE_COLUMNS, storepb.TablePartitionMetadata_RANGE:
- if partitionDefinition.PartitionValueItemListParen() == nil {
- t.err = errors.New("COLUMNS partition but no partition value item in LESS THAN clause")
+ if partitionDefinition.LESS_SYMBOL() == nil {
+ t.err = errors.New("RANGE partition but no LESS THAN clause")
return
}
- itemsText := partitionDefinition.PartitionValueItemListParen().GetParser().GetTokenStream().GetTextFromInterval(
- antlr.NewInterval(
- partitionDefinition.PartitionValueItemListParen().OPEN_PAR_SYMBOL().GetSymbol().GetTokenIndex()+1,
- partitionDefinition.PartitionValueItemListParen().CLOSE_PAR_SYMBOL().GetSymbol().GetTokenIndex()-1,
- ),
- )
- itemsTextFields := strings.Split(itemsText, ",")
- for i, itemsTextField := range itemsTextFields {
- itemsTextField := strings.TrimSpace(itemsTextField)
- if strings.HasPrefix(itemsTextField, "`") && strings.HasSuffix(itemsTextField, "`") {
- itemsTextField = itemsTextField[1 : len(itemsTextField)-1]
+ if partitionDefinition.PartitionValueItemListParen() != nil {
+ itemsText := partitionDefinition.PartitionValueItemListParen().GetParser().GetTokenStream().GetTextFromInterval(
+ antlr.NewInterval(
+ partitionDefinition.PartitionValueItemListParen().OPEN_PAR_SYMBOL().GetSymbol().GetTokenIndex()+1,
+ partitionDefinition.PartitionValueItemListParen().CLOSE_PAR_SYMBOL().GetSymbol().GetTokenIndex()-1,
+ ),
+ )
+ itemsTextFields := strings.Split(itemsText, ",")
+ for i, itemsTextField := range itemsTextFields {
+ itemsTextField := strings.TrimSpace(itemsTextField)
+ if strings.HasPrefix(itemsTextField, "`") && strings.HasSuffix(itemsTextField, "`") {
+ itemsTextField = itemsTextField[1 : len(itemsTextField)-1]
+ }
+ itemsTextFields[i] = itemsTextField
}
- itemsTextFields[i] = itemsTextField
+ partitionDef.value = strings.Join(itemsTextFields, ",")
+ } else {
+ partitionDef.value = "MAXVALUE"
}
-
- partitionDef.value = strings.Join(itemsTextFields, ",")
case storepb.TablePartitionMetadata_LIST_COLUMNS, storepb.TablePartitionMetadata_LIST:
if partitionDefinition.PartitionValuesIn() == nil {
t.err = errors.New("COLUMNS partition but no partition value item in IN clause")
diff --git a/backend/plugin/schema/mysql/parse_to_metadata.go b/backend/plugin/schema/mysql/parse_to_metadata.go
index 4c92286d80..2fa0198c42 100644
--- a/backend/plugin/schema/mysql/parse_to_metadata.go
+++ b/backend/plugin/schema/mysql/parse_to_metadata.go
@@ -2,6 +2,7 @@ package mysql
import (
"fmt"
+ "strconv"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -308,3 +309,237 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
}
}
}
+
+func (t *mysqlTransformer) EnterPartitionClause(ctx *mysql.PartitionClauseContext) {
+ if t.err != nil {
+ return
+ }
+ if _, parentIsCreateTable := ctx.GetParent().(*mysql.CreateTableContext); !parentIsCreateTable {
+ return
+ }
+ if t.currentTable == "" {
+ return
+ }
+ table := t.state.schemas[""].tables[t.currentTable]
+ if table == nil {
+ return
+ }
+
+ parititonInfo := partitionInfo{}
+
+ iTypeDefCtx := ctx.PartitionTypeDef()
+ if iTypeDefCtx != nil {
+ switch typeDefCtx := iTypeDefCtx.(type) {
+ case *mysql.PartitionDefKeyContext:
+ parititonInfo.tp = storepb.TablePartitionMetadata_KEY
+ if typeDefCtx.LINEAR_SYMBOL() != nil {
+ parititonInfo.tp = storepb.TablePartitionMetadata_LINEAR_KEY
+ }
+ // TODO(zp): handle the key algorithm
+ if typeDefCtx.IdentifierList() != nil {
+ identifiers := extractIdentifierList(typeDefCtx.IdentifierList())
+ for i, identifier := range identifiers {
+ identifier := strings.TrimSpace(identifier)
+ if !strings.HasPrefix(identifier, "`") || !strings.HasSuffix(identifier, "`") {
+ identifiers[i] = fmt.Sprintf("`%s`", identifier)
+ }
+ }
+ parititonInfo.expr = strings.Join(identifiers, ",")
+ }
+ case *mysql.PartitionDefHashContext:
+ parititonInfo.tp = storepb.TablePartitionMetadata_HASH
+ if typeDefCtx.LINEAR_SYMBOL() != nil {
+ parititonInfo.tp = storepb.TablePartitionMetadata_LINEAR_HASH
+ }
+ bitExprText := typeDefCtx.GetParser().GetTokenStream().GetTextFromRuleContext(typeDefCtx.BitExpr())
+ bitExprFields := strings.Split(bitExprText, ",")
+ for i, bitExprField := range bitExprFields {
+ bitExprField := strings.TrimSpace(bitExprField)
+ if !strings.HasPrefix(bitExprField, "`") || !strings.HasSuffix(bitExprField, "`") {
+ bitExprFields[i] = fmt.Sprintf("`%s`", bitExprField)
+ }
+ }
+ parititonInfo.expr = strings.Join(bitExprFields, ",")
+ case *mysql.PartitionDefRangeListContext:
+ if typeDefCtx.RANGE_SYMBOL() != nil {
+ parititonInfo.tp = storepb.TablePartitionMetadata_RANGE
+ } else {
+ parititonInfo.tp = storepb.TablePartitionMetadata_LIST
+ }
+ if typeDefCtx.COLUMNS_SYMBOL() != nil {
+ if parititonInfo.tp == storepb.TablePartitionMetadata_RANGE {
+ parititonInfo.tp = storepb.TablePartitionMetadata_RANGE_COLUMNS
+ } else {
+ parititonInfo.tp = storepb.TablePartitionMetadata_LIST_COLUMNS
+ }
+
+ identifierList := extractIdentifierList(typeDefCtx.IdentifierList())
+ for i, identifier := range identifierList {
+ identifier := strings.TrimSpace(identifier)
+ if !strings.HasPrefix(identifier, "`") || !strings.HasSuffix(identifier, "`") {
+ identifierList[i] = fmt.Sprintf("`%s`", identifier)
+ }
+ }
+ parititonInfo.expr = strings.Join(identifierList, ",")
+ } else {
+ bitExprText := typeDefCtx.GetParser().GetTokenStream().GetTextFromRuleContext(typeDefCtx.BitExpr())
+ bitExprFields := strings.Split(bitExprText, ",")
+ for i, bitExprField := range bitExprFields {
+ bitExprField := strings.TrimSpace(bitExprField)
+ if !strings.HasPrefix(bitExprField, "`") || !strings.HasSuffix(bitExprField, "`") {
+ bitExprFields[i] = fmt.Sprintf("`%s`", bitExprField)
+ }
+ }
+ parititonInfo.expr = strings.Join(bitExprFields, ",")
+ }
+ default:
+ t.err = errors.New("unknown partition type")
+ return
+ }
+ }
+
+ if n := ctx.Real_ulong_number(); n != nil {
+ number, err := strconv.ParseInt(n.GetText(), 10, 64)
+ if err != nil {
+ t.err = errors.Wrap(err, "failed to parse partition number")
+ return
+ }
+ parititonInfo.useDefault = int(number)
+ }
+
+ var subInfo *partitionInfo
+ if subPartitionCtx := ctx.SubPartitions(); subPartitionCtx != nil {
+ subInfo = new(partitionInfo)
+ if subPartitionCtx.HASH_SYMBOL() != nil {
+ subInfo.tp = storepb.TablePartitionMetadata_HASH
+ if subPartitionCtx.LINEAR_SYMBOL() != nil {
+ subInfo.tp = storepb.TablePartitionMetadata_LINEAR_HASH
+ }
+ if bitExprCtx := subPartitionCtx.BitExpr(); bitExprCtx != nil {
+ bitExprText := bitExprCtx.GetParser().GetTokenStream().GetTextFromRuleContext(bitExprCtx)
+ bitExprFields := strings.Split(bitExprText, ",")
+ for i, bitExprField := range bitExprFields {
+ bitExprField := strings.TrimSpace(bitExprField)
+ if !strings.HasPrefix(bitExprField, "`") || !strings.HasSuffix(bitExprField, "`") {
+ bitExprFields[i] = fmt.Sprintf("`%s`", bitExprField)
+ }
+ }
+ subInfo.expr = strings.Join(bitExprFields, ",")
+ }
+ } else if subPartitionCtx.KEY_SYMBOL() != nil {
+ subInfo.tp = storepb.TablePartitionMetadata_KEY
+ if subPartitionCtx.LINEAR_SYMBOL() != nil {
+ subInfo.tp = storepb.TablePartitionMetadata_LINEAR_KEY
+ }
+ if identifierListParensCtx := subPartitionCtx.IdentifierListWithParentheses(); identifierListParensCtx != nil {
+ identifiers := extractIdentifierList(identifierListParensCtx.IdentifierList())
+ for i, identifier := range identifiers {
+ identifier := strings.TrimSpace(identifier)
+ if !strings.HasPrefix(identifier, "`") || !strings.HasSuffix(identifier, "`") {
+ identifiers[i] = fmt.Sprintf("`%s`", identifier)
+ }
+ }
+ subInfo.expr = strings.Join(identifiers, ",")
+ }
+ }
+
+ if n := subPartitionCtx.Real_ulong_number(); n != nil {
+ number, err := strconv.ParseInt(n.GetText(), 10, 64)
+ if err != nil {
+ t.err = errors.Wrap(err, "failed to parse sub partition number")
+ return
+ }
+ subInfo.useDefault = int(number)
+ }
+ }
+
+ partitionDefinitions := make(map[string]*partitionDefinition)
+ allPartDefs := ctx.PartitionDefinitions().AllPartitionDefinition()
+ for i, partDef := range allPartDefs {
+ pd := &partitionDefinition{
+ id: i + 1,
+ name: mysqlparser.NormalizeMySQLIdentifier(partDef.Identifier()),
+ }
+ switch parititonInfo.tp {
+ case storepb.TablePartitionMetadata_RANGE_COLUMNS, storepb.TablePartitionMetadata_RANGE:
+ if partDef.LESS_SYMBOL() == nil {
+ t.err = errors.New("RANGE partition but no LESS THAN clause")
+ return
+ }
+ if partDef.PartitionValueItemListParen() != nil {
+ itemsText := partDef.PartitionValueItemListParen().GetParser().GetTokenStream().GetTextFromInterval(
+ antlr.NewInterval(
+ partDef.PartitionValueItemListParen().OPEN_PAR_SYMBOL().GetSymbol().GetTokenIndex()+1,
+ partDef.PartitionValueItemListParen().CLOSE_PAR_SYMBOL().GetSymbol().GetTokenIndex()-1,
+ ),
+ )
+ itemsTextFields := strings.Split(itemsText, ",")
+ for i, itemsTextField := range itemsTextFields {
+ itemsTextField := strings.TrimSpace(itemsTextField)
+ if strings.HasPrefix(itemsTextField, "`") && strings.HasSuffix(itemsTextField, "`") {
+ itemsTextField = itemsTextField[1 : len(itemsTextField)-1]
+ }
+ itemsTextFields[i] = itemsTextField
+ }
+ pd.value = strings.Join(itemsTextFields, ",")
+ } else {
+ pd.value = "MAXVALUE"
+ }
+ case storepb.TablePartitionMetadata_LIST_COLUMNS, storepb.TablePartitionMetadata_LIST:
+ if partDef.PartitionValuesIn() == nil {
+ t.err = errors.New("COLUMNS partition but no partition value item in IN clause")
+ return
+ }
+ var itemsText string
+ if partDef.PartitionValuesIn().OPEN_PAR_SYMBOL() != nil {
+ itemsText = partDef.PartitionValuesIn().GetParser().GetTokenStream().GetTextFromInterval(
+ antlr.NewInterval(
+ partDef.PartitionValuesIn().OPEN_PAR_SYMBOL().GetSymbol().GetTokenIndex()+1,
+ partDef.PartitionValuesIn().CLOSE_PAR_SYMBOL().GetSymbol().GetTokenIndex()-1,
+ ),
+ )
+ } else {
+ itemsText = partDef.PartitionValuesIn().GetParser().GetTokenStream().GetTextFromRuleContext(partDef.PartitionValuesIn().PartitionValueItemListParen(0))
+ }
+
+ itemsTextFields := strings.Split(itemsText, ",")
+ for i, itemsTextField := range itemsTextFields {
+ itemsTextField := strings.TrimSpace(itemsTextField)
+ if strings.HasPrefix(itemsTextField, "`") && strings.HasSuffix(itemsTextField, "`") {
+ itemsTextField = itemsTextField[1 : len(itemsTextField)-1]
+ }
+ itemsTextFields[i] = itemsTextField
+ }
+ pd.value = strings.Join(itemsTextFields, ",")
+ case storepb.TablePartitionMetadata_HASH, storepb.TablePartitionMetadata_LINEAR_HASH, storepb.TablePartitionMetadata_KEY, storepb.TablePartitionMetadata_LINEAR_KEY:
+ default:
+ t.err = errors.New("unknown partition type")
+ return
+ }
+
+ if subInfo != nil {
+ allSubpartitions := partDef.AllSubpartitionDefinition()
+ if subInfo.tp == storepb.TablePartitionMetadata_TYPE_UNSPECIFIED && len(allSubpartitions) > 0 {
+ t.err = errors.New("specify subpartition definition but no subpartition type specified")
+ return
+ }
+ subPartitionDefinitions := make(map[string]*partitionDefinition)
+ for i, sub := range allSubpartitions {
+ subpd := &partitionDefinition{
+ id: i + 1,
+ name: mysqlparser.NormalizeMySQLTextOrIdentifier(sub.TextOrIdentifier()),
+ }
+ subPartitionDefinitions[subpd.name] = subpd
+ }
+ pd.subPartitions = subPartitionDefinitions
+ }
+
+ partitionDefinitions[pd.name] = pd
+ }
+
+ table.partitionStateV2 = &partitionStateV2{
+ info: parititonInfo,
+ subInfo: subInfo,
+ partitions: partitionDefinitions,
+ }
+}
diff --git a/backend/plugin/schema/mysql/state.go b/backend/plugin/schema/mysql/state.go
index 5acfd95f22..6ac5ab0dcc 100644
--- a/backend/plugin/schema/mysql/state.go
+++ b/backend/plugin/schema/mysql/state.go
@@ -3,7 +3,9 @@ package mysql
import (
"fmt"
"io"
+ "log/slog"
"sort"
+ "strconv"
"strings"
"google.golang.org/protobuf/types/known/wrapperspb"
@@ -109,6 +111,9 @@ type tableState struct {
engine string
collation string
partitionStateWrapper *partitionStateWrapper
+ // TODO(zp): more flexible struct, use in parseToMetadata.
+ // Migrate to use it.
+ partitionStateV2 *partitionStateV2
}
func (t *tableState) toString(buf io.StringWriter) error {
@@ -277,6 +282,11 @@ func (t *tableState) convertToTableMetadata() *storepb.TableMetadata {
fks = append(fks, fk.convertToForeignKeyMetadata())
}
+ var partitions []*storepb.TablePartitionMetadata
+ if t.partitionStateV2 != nil {
+ partitions = t.partitionStateV2.convertToPartitionMetadata()
+ }
+
return &storepb.TableMetadata{
Name: t.name,
Columns: columns,
@@ -285,6 +295,7 @@ func (t *tableState) convertToTableMetadata() *storepb.TableMetadata {
Comment: t.comment,
Engine: t.engine,
Collation: t.collation,
+ Partitions: partitions,
}
}
@@ -500,6 +511,165 @@ func (i *indexState) toString(buf io.StringWriter) error {
return nil
}
+type partitionStateV2 struct {
+ info partitionInfo
+ subInfo *partitionInfo
+ partitions map[string]*partitionDefinition
+}
+
+type partitionInfo struct {
+ tp storepb.TablePartitionMetadata_Type
+ useDefault int
+ expr string
+}
+
+type partitionDefinition struct {
+ id int
+ name string
+ value string
+ subPartitions map[string]*partitionDefinition
+}
+
+func (p *partitionStateV2) isValid() error {
+ if p.info.useDefault == 0 && len(p.partitions) == 0 {
+ return errors.New("empty partition list")
+ }
+
+ if p.info.useDefault != 0 && len(p.partitions) > 0 {
+ return errors.New("specify partitions clause and use default partition are not allowed at the same time")
+ }
+
+ if p.info.tp == storepb.TablePartitionMetadata_TYPE_UNSPECIFIED {
+ return errors.New("invalid partition type")
+ }
+
+ if p.subInfo != nil {
+ if p.subInfo.tp == storepb.TablePartitionMetadata_TYPE_UNSPECIFIED {
+ return errors.New("invalid subpartition type")
+ }
+
+ anySpecificPartition := len(p.partitions) > 0
+ if anySpecificPartition {
+ var anySpecificSubpartition bool
+ for _, partition := range p.partitions {
+ if len(partition.subPartitions) > 0 {
+ anySpecificSubpartition = true
+ break
+ }
+ }
+
+ if anySpecificSubpartition && p.subInfo.useDefault != 0 {
+ return errors.New("specify subpartitions clause and use default subpartition are not allowed at the same time")
+ }
+ }
+ }
+
+ return nil
+}
+
+func (p *partitionStateV2) convertToPartitionMetadata() []*storepb.TablePartitionMetadata {
+ // There are `useDefault` fields in partitionInfo structure, which may use with empty parititions fields.
+ // For example, `PARTITION BY HASH (id) PARTITIONS 4 SUBPARTITION BY KEY(id) SUBPARTITIONS 5;` may cause the `partitions` field to be empty.
+ // To be compatible with our protobuf definition, which requires at least one partition, we need to generate the default partition name. And, value
+ // is not important, because only the KEY and HASH partition type support PARTITIONS clause.
+ // TODO(parser-group): recheck the MySQL behavior of generating the default partition name and value.
+ if err := p.isValid(); err != nil {
+ slog.Warn(err.Error())
+ return nil
+ }
+ var partitions []*storepb.TablePartitionMetadata
+ if p.info.useDefault != 0 {
+ generator := newPartitionDefaultNameGenerator("")
+ for i := 0; i < p.info.useDefault; i++ {
+ partitions = append(partitions, &storepb.TablePartitionMetadata{
+ Name: generator.next(),
+ Type: p.info.tp,
+ Expression: p.info.expr,
+ Value: "",
+ UseDefault: strconv.Itoa(p.info.useDefault),
+ })
+ }
+ // The reason of we do not consider subUseDefault in this case is that MySQL does not support
+ // subpartitions for HASH and KEY partitioning, and they are the only partitioning types that support
+ // the PARTITIONS clause.
+ } else {
+ sortedPartitions := make([]*partitionDefinition, 0, len(p.partitions))
+ for _, partition := range p.partitions {
+ sortedPartitions = append(sortedPartitions, partition)
+ }
+ sort.Slice(sortedPartitions, func(i, j int) bool {
+ return sortedPartitions[i].id < sortedPartitions[j].id
+ })
+ for _, partition := range sortedPartitions {
+ partitionMetadata := &storepb.TablePartitionMetadata{
+ Name: partition.name,
+ Type: p.info.tp,
+ Expression: p.info.expr,
+ Value: partition.value,
+ }
+ if p.subInfo != nil {
+ if p.subInfo.useDefault != 0 {
+ subUseDefault := strconv.Itoa(p.subInfo.useDefault)
+ generator := newPartitionDefaultNameGenerator(partition.name)
+ for i := 0; i < p.subInfo.useDefault; i++ {
+ partitionMetadata.Subpartitions = append(partitionMetadata.Subpartitions, &storepb.TablePartitionMetadata{
+ Name: generator.next(),
+ Type: p.subInfo.tp,
+ Expression: p.subInfo.expr,
+ UseDefault: subUseDefault,
+ Value: "",
+ })
+ }
+ } else {
+ sortedSubpartitions := make([]*partitionDefinition, 0, len(partition.subPartitions))
+ for _, subPartition := range partition.subPartitions {
+ sortedSubpartitions = append(sortedSubpartitions, subPartition)
+ }
+ sort.Slice(sortedSubpartitions, func(i, j int) bool {
+ return sortedSubpartitions[i].id < sortedSubpartitions[j].id
+ })
+ for _, subPartition := range sortedSubpartitions {
+ partitionMetadata.Subpartitions = append(partitionMetadata.Subpartitions, &storepb.TablePartitionMetadata{
+ Name: subPartition.name,
+ Type: p.subInfo.tp,
+ Expression: p.subInfo.expr,
+ Value: subPartition.value,
+ })
+ }
+ }
+ }
+ partitions = append(partitions, partitionMetadata)
+ }
+ }
+
+ return partitions
+}
+
+// partitionDefaultNameGenerator is the name generator of MySQL partition, which use the default clause.
+// The behavior of this generator should be compatible with MySQL.
+// - If do not specify the `parentName`, the default partition name series is "p0", "p1", "p2", ...
+// - Otherwise, the default partition name series is "parentNamesp0", "parentNamesp1", "parentNamesp2", ...
+type partitionDefaultNameGenerator struct {
+ parentName string
+ count int
+}
+
+func newPartitionDefaultNameGenerator(parentName string) *partitionDefaultNameGenerator {
+ return &partitionDefaultNameGenerator{
+ parentName: parentName,
+ count: -1,
+ }
+}
+
+func (g *partitionDefaultNameGenerator) next() string {
+ g.count++
+
+ if g.parentName == "" {
+ return fmt.Sprintf("p%d", g.count)
+ }
+ return fmt.Sprintf("%ssp%d", g.parentName, g.count)
+}
+
// Currently, our storepb.TablePartitionMetadata is too redundant, we need to convert it to a more compact format.
// In the future, we should update the storepb.TablePartitionMetadata to a more compact format.
type partitionStateWrapper struct {
@@ -510,7 +680,7 @@ type partitionStateWrapper struct {
func (p *partitionStateWrapper) hasSubpartitions() (bool, storepb.TablePartitionMetadata_Type, string) {
for _, partition := range p.partitions {
- if partition.subPartition != nil && len(partition.subPartition.partitions) > 0 {
+ if partition.subPartition != nil && partition.subPartition.tp != storepb.TablePartitionMetadata_TYPE_UNSPECIFIED {
return true, partition.subPartition.tp, partition.subPartition.expr
}
}
@@ -525,30 +695,6 @@ type partitionState struct {
subPartition *partitionStateWrapper
}
-// nolint
-func (p *partitionStateWrapper) convertToPartitionMetadata() []*storepb.TablePartitionMetadata {
- partitions := make([]*storepb.TablePartitionMetadata, 0, len(p.partitions))
- partitionStates := make([]*partitionState, 0, len(p.partitions))
- for _, partition := range p.partitions {
- partitionStates = append(partitionStates, partition)
- }
- sort.Slice(partitionStates, func(i, j int) bool {
- return partitionStates[i].id < partitionStates[j].id
- })
-
- for _, partition := range partitionStates {
- partitions = append(partitions, &storepb.TablePartitionMetadata{
- Name: partition.name,
- Type: p.tp,
- Expression: p.expr,
- Value: partition.value,
- Subpartitions: partition.subPartition.convertToPartitionMetadata(),
- })
- }
-
- return partitions
-}
-
func convertToPartitionStateWrapper(partitions []*storepb.TablePartitionMetadata) *partitionStateWrapper {
if len(partitions) == 0 {
return nil
diff --git a/backend/plugin/schema/mysql/testdata/parse_to_metadata.yaml b/backend/plugin/schema/mysql/testdata/parse_to_metadata.yaml
index 5571e1cd2c..9a9d6cf7ff 100644
--- a/backend/plugin/schema/mysql/testdata/parse_to_metadata.yaml
+++ b/backend/plugin/schema/mysql/testdata/parse_to_metadata.yaml
@@ -270,3 +270,223 @@
}
]
}
+- schema: |-
+ CREATE TABLE `range_partition` (
+ `id` INT,
+ `name` VARCHAR(45) NOT NULL,
+ PRIMARY KEY (`id`, `name`)
+ )
+ PARTITION BY RANGE COLUMNS(id, name)
+ SUBPARTITION BY HASH(id) (
+ PARTITION p0 VALUES LESS THAN (10, 'a') (
+ SUBPARTITION p0s0,
+ SUBPARTITION p0s1
+ ),
+ PARTITION p1 VALUES LESS THAN (20, 'b') (
+ SUBPARTITION p1s0,
+ SUBPARTITION p1s1
+ ),
+ PARTITION p2 VALUES LESS THAN (MAXVALUE, MAXVALUE) (
+ SUBPARTITION p2s0,
+ SUBPARTITION p2s1
+ )
+ );
+ metadata: |-
+ {
+ "schemas": [
+ {
+ "tables": [
+ {
+ "name": "range_partition",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "defaultNull": true,
+ "nullable": true,
+ "type": "INT"
+ },
+ {
+ "name": "name",
+ "position": 2,
+ "type": "VARCHAR(45)"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PRIMARY",
+ "expressions": [
+ "id",
+ "name"
+ ],
+ "keyLength": [
+ "-1",
+ "-1"
+ ],
+ "unique": true,
+ "primary": true,
+ "visible": true
+ }
+ ],
+ "partitions": [
+ {
+ "name": "p0",
+ "type": "RANGE_COLUMNS",
+ "expression": "`id`,`name`",
+ "value": "10,'a'",
+ "subpartitions": [
+ {
+ "name": "p0s0",
+ "type": "HASH",
+ "expression": "`id`"
+ },
+ {
+ "name": "p0s1",
+ "type": "HASH",
+ "expression": "`id`"
+ }
+ ]
+ },
+ {
+ "name": "p1",
+ "type": "RANGE_COLUMNS",
+ "expression": "`id`,`name`",
+ "value": "20,'b'",
+ "subpartitions": [
+ {
+ "name": "p1s0",
+ "type": "HASH",
+ "expression": "`id`"
+ },
+ {
+ "name": "p1s1",
+ "type": "HASH",
+ "expression": "`id`"
+ }
+ ]
+ },
+ {
+ "name": "p2",
+ "type": "RANGE_COLUMNS",
+ "expression": "`id`,`name`",
+ "value": "MAXVALUE,MAXVALUE",
+ "subpartitions": [
+ {
+ "name": "p2s0",
+ "type": "HASH",
+ "expression": "`id`"
+ },
+ {
+ "name": "p2s1",
+ "type": "HASH",
+ "expression": "`id`"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+- schema: |-
+ CREATE TABLE `list_partition` (
+ `id` INT,
+ `name` VARCHAR(45) NOT NULL
+ )
+ PARTITION BY LIST COLUMNS(id, name)
+ SUBPARTITION BY KEY(id)
+ SUBPARTITIONS 2
+ (
+ PARTITION p0 VALUES IN (1, 'a'),
+ PARTITION p1 VALUES IN (2, 'b'),
+ PARTITION p2 VALUES IN (3, 'c')
+ );
+ metadata: |-
+ {
+ "schemas": [
+ {
+ "tables": [
+ {
+ "name": "list_partition",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "defaultNull": true,
+ "nullable": true,
+ "type": "INT"
+ },
+ {
+ "name": "name",
+ "position": 2,
+ "type": "VARCHAR(45)"
+ }
+ ],
+ "partitions": [
+ {
+ "name": "p0",
+ "type": "LIST_COLUMNS",
+ "expression": "`id`,`name`",
+ "value": "(1,'a')",
+ "subpartitions": [
+ {
+ "name": "p0sp0",
+ "type": "KEY",
+ "expression": "`id`",
+ "useDefault": "2"
+ },
+ {
+ "name": "p0sp1",
+ "type": "KEY",
+ "expression": "`id`",
+ "useDefault": "2"
+ }
+ ]
+ },
+ {
+ "name": "p1",
+ "type": "LIST_COLUMNS",
+ "expression": "`id`,`name`",
+ "value": "(2,'b')",
+ "subpartitions": [
+ {
+ "name": "p1sp0",
+ "type": "KEY",
+ "expression": "`id`",
+ "useDefault": "2"
+ },
+ {
+ "name": "p1sp1",
+ "type": "KEY",
+ "expression": "`id`",
+ "useDefault": "2"
+ }
+ ]
+ },
+ {
+ "name": "p2",
+ "type": "LIST_COLUMNS",
+ "expression": "`id`,`name`",
+ "value": "(3,'c')",
+ "subpartitions": [
+ {
+ "name": "p2sp0",
+ "type": "KEY",
+ "expression": "`id`",
+ "useDefault": "2"
+ },
+ {
+ "name": "p2sp1",
+ "type": "KEY",
+ "expression": "`id`",
+ "useDefault": "2"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }