Skip to content

Commit

Permalink
parser: support GLOBAL IndexOption (#55259)
Browse files Browse the repository at this point in the history
ref #52994
  • Loading branch information
mjonss authored Aug 9, 2024
1 parent fd3ad81 commit e1f2b77
Show file tree
Hide file tree
Showing 9 changed files with 8,791 additions and 8,410 deletions.
3 changes: 3 additions & 0 deletions pkg/executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,9 @@ func constructResultOfShowCreateTable(ctx sessionctx.Context, dbName *model.CISt
buf.WriteString(" /*T![clustered_index] NONCLUSTERED */")
}
}
if idxInfo.Global {
buf.WriteString(" /*T![global_index] GLOBAL */")
}
if i != len(publicIndices)-1 {
buf.WriteString(",\n")
}
Expand Down
60 changes: 56 additions & 4 deletions pkg/parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@ func (n *ColumnOption) Restore(ctx *format.RestoreCtx) error {
return nil
})
}
if n.StrValue == "Global" {
ctx.WriteKeyWord(" GLOBAL")
}
case ColumnOptionNotNull:
ctx.WriteKeyWord("NOT NULL")
case ColumnOptionAutoIncrement:
Expand All @@ -596,6 +599,9 @@ func (n *ColumnOption) Restore(ctx *format.RestoreCtx) error {
}
case ColumnOptionUniqKey:
ctx.WriteKeyWord("UNIQUE KEY")
if n.StrValue == "Global" {
ctx.WriteKeyWord(" GLOBAL")
}
case ColumnOptionNull:
ctx.WriteKeyWord("NULL")
case ColumnOptionOnUpdate:
Expand Down Expand Up @@ -715,8 +721,10 @@ const (
// | index_type
// | WITH PARSER parser_name
// | COMMENT 'string'
// | GLOBAL
//
// See http://dev.mysql.com/doc/refman/5.7/en/create-table.html
// with the addition of Global Index
type IndexOption struct {
node

Expand All @@ -726,6 +734,22 @@ type IndexOption struct {
ParserName model.CIStr
Visibility IndexVisibility
PrimaryKeyTp model.PrimaryKeyType
Global bool
}

// IsEmpty is true if only default options are given
// and it should not be added to the output
func (n *IndexOption) IsEmpty() bool {
if n.PrimaryKeyTp != model.PrimaryKeyTypeDefault ||
n.KeyBlockSize > 0 ||
n.Tp != model.IndexTypeInvalid ||
len(n.ParserName.O) > 0 ||
n.Comment != "" ||
n.Global ||
n.Visibility != IndexVisibilityDefault {
return false
}
return true
}

// Restore implements Node interface.
Expand Down Expand Up @@ -774,6 +798,17 @@ func (n *IndexOption) Restore(ctx *format.RestoreCtx) error {
hasPrevOption = true
}

if n.Global {
if hasPrevOption {
ctx.WritePlain(" ")
}
_ = ctx.WriteWithSpecialComments(tidb.FeatureIDGlobalIndex, func() error {
ctx.WriteKeyWord("GLOBAL")
return nil
})
hasPrevOption = true
}

if n.Visibility != IndexVisibilityDefault {
if hasPrevOption {
ctx.WritePlain(" ")
Expand Down Expand Up @@ -920,7 +955,7 @@ func (n *Constraint) Restore(ctx *format.RestoreCtx) error {
}
}

if n.Option != nil {
if n.Option != nil && !n.Option.IsEmpty() {
ctx.WritePlain(" ")
if err := n.Option.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while splicing Constraint Option")
Expand Down Expand Up @@ -1823,7 +1858,7 @@ func (n *CreateIndexStmt) Restore(ctx *format.RestoreCtx) error {
}
ctx.WritePlain(")")

if n.IndexOption.Tp != model.IndexTypeInvalid || n.IndexOption.KeyBlockSize > 0 || n.IndexOption.Comment != "" || len(n.IndexOption.ParserName.O) > 0 || n.IndexOption.Visibility != IndexVisibilityDefault {
if n.IndexOption != nil && !n.IndexOption.IsEmpty() {
ctx.WritePlain(" ")
if err := n.IndexOption.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore CreateIndexStmt.IndexOption")
Expand Down Expand Up @@ -4344,8 +4379,9 @@ func (n *PartitionMethod) acceptInPlace(v Visitor) bool {
// PartitionOptions specifies the partition options.
type PartitionOptions struct {
PartitionMethod
Sub *PartitionMethod
Definitions []*PartitionDefinition
Sub *PartitionMethod
Definitions []*PartitionDefinition
UpdateIndexes []*Constraint
}

// Validate checks if the partition is well-formed.
Expand Down Expand Up @@ -4439,6 +4475,22 @@ func (n *PartitionOptions) Restore(ctx *format.RestoreCtx) error {
ctx.WritePlain(")")
}

if len(n.UpdateIndexes) > 0 {
ctx.WritePlain(" UPDATE INDEXES (")
for i, update := range n.UpdateIndexes {
if i > 0 {
ctx.WritePlain(",")
}
ctx.WriteName(update.Name)
if update.Option != nil && update.Option.Global {
ctx.WritePlain(" GLOBAL")
} else {
ctx.WritePlain(" LOCAL")
}
}
ctx.WritePlain(")")
}

return nil
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/parser/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,13 @@ type ExchangePartitionInfo struct {
XXXExchangePartitionFlag bool `json:"exchange_partition_flag"`
}

// UpdateIndexInfo is to carry the entries in the list of indexes in UPDATE INDEXES
// during ALTER TABLE t PARTITION BY ... UPDATE INDEXES (idx_a GLOBAL, idx_b LOCAL...)
type UpdateIndexInfo struct {
IndexName string `json:"index_name"`
Global bool `json:"global"`
}

// PartitionInfo provides table partition info.
type PartitionInfo struct {
Type PartitionType `json:"type"`
Expand Down Expand Up @@ -1210,6 +1217,8 @@ type PartitionInfo struct {
DDLType PartitionType `json:"ddl_type"`
DDLExpr string `json:"ddl_expr"`
DDLColumns []CIStr `json:"ddl_columns"`
// For ActionAlterTablePartitioning, UPDATE INDEXES
DDLUpdateIndexes []UpdateIndexInfo `json:"ddl_update_indexes"`
}

// Clone clones itself.
Expand Down
Loading

0 comments on commit e1f2b77

Please sign in to comment.