Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
60 changes: 60 additions & 0 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const (
DatabaseOptionCollate
DatabaseOptionEncryption
DatabaseSetTiFlashReplica
DatabaseOptionReadOnly
DatabaseOptionPlacementPolicy = DatabaseOptionType(PlacementOptionPolicy)
)

Expand All @@ -108,6 +109,10 @@ func (n *DatabaseOption) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("ENCRYPTION")
ctx.WritePlain(" = ")
ctx.WriteString(n.Value)
case DatabaseOptionReadOnly:
ctx.WriteKeyWord("READ ONLY")
ctx.WritePlain(" = ")
ctx.WriteKeyWord(n.Value)
case DatabaseOptionPlacementPolicy:
placementOpt := PlacementOption{
Tp: PlacementOptionPolicy,
Expand Down Expand Up @@ -528,6 +533,9 @@ const (
ColumnOptionAutoRandom
ColumnOptionSecondaryEngineAttribute
ColumnOptionSrid
ColumnOptionVisible
ColumnOptionInvisible
ColumnOptionEngineAttribute
)

var (
Expand Down Expand Up @@ -599,6 +607,11 @@ func (n *ColumnOption) Restore(ctx *format.RestoreCtx) error {
if _, ok := n.Expr.(*ColumnNameExpr); ok {
printOuterParentheses = true
}
if _, ok := n.Expr.(*BinaryOperationExpr); ok {
// DEFAULT (expr) with an operator; MySQL requires the
// parentheses.
printOuterParentheses = true
}
if printOuterParentheses {
ctx.WritePlain("(")
}
Expand Down Expand Up @@ -692,6 +705,14 @@ func (n *ColumnOption) Restore(ctx *format.RestoreCtx) error {
case ColumnOptionSrid:
ctx.WriteKeyWord("SRID ")
ctx.WritePlainf("%d", n.UintValue)
case ColumnOptionVisible:
ctx.WriteKeyWord("VISIBLE")
case ColumnOptionInvisible:
ctx.WriteKeyWord("INVISIBLE")
case ColumnOptionEngineAttribute:
ctx.WriteKeyWord("ENGINE_ATTRIBUTE")
ctx.WritePlain(" = ")
ctx.WriteString(n.StrValue)
default:
return errors.New("An error occurred while splicing ColumnOption")
}
Expand Down Expand Up @@ -754,6 +775,7 @@ type IndexOption struct {
PrimaryKeyTp PrimaryKeyType
Global bool
SplitOpt *SplitOption `json:"-"` // SplitOption contains expr nodes, which cannot marshal for DDL job arguments.
EngineAttr string
SecondaryEngineAttr string
AddColumnarReplicaOnDemand int
Condition ExprNode `json:"-"` // Condition contains expr nodes, which cannot marshal for DDL job arguments. It's used for partial index.
Expand All @@ -770,6 +792,7 @@ func (n *IndexOption) IsEmpty() bool {
n.Global ||
n.Visibility != IndexVisibilityDefault ||
n.SplitOpt != nil ||
len(n.EngineAttr) > 0 ||
len(n.SecondaryEngineAttr) > 0 ||
n.Condition != nil {
return false
Expand Down Expand Up @@ -877,6 +900,16 @@ func (n *IndexOption) Restore(ctx *format.RestoreCtx) error {
hasPrevOption = true
}

if n.EngineAttr != "" {
if hasPrevOption {
ctx.WritePlain(" ")
}
ctx.WriteKeyWord("ENGINE_ATTRIBUTE")
ctx.WritePlain(" = ")
ctx.WriteString(n.EngineAttr)
hasPrevOption = true
}

if n.SecondaryEngineAttr != "" {
if hasPrevOption {
ctx.WritePlain(" ")
Expand Down Expand Up @@ -1465,6 +1498,7 @@ type DropResourceGroupStmt struct {

IfExists bool
ResourceGroupName CIStr
Force bool
}

// Restore implements Restore interface.
Expand All @@ -1478,6 +1512,9 @@ func (n *DropResourceGroupStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("IF EXISTS ")
}
ctx.WriteName(n.ResourceGroupName.O)
if n.Force {
ctx.WriteKeyWord(" FORCE")
}
return nil
}

Expand Down Expand Up @@ -2301,6 +2338,7 @@ type LockTablesStmt struct {
type TableLock struct {
Table *TableName
Type TableLockType
Alias CIStr // empty when absent; restored with AS
}

// Accept implements Node Accept interface.
Expand Down Expand Up @@ -2330,6 +2368,10 @@ func (n *LockTablesStmt) Restore(ctx *format.RestoreCtx) error {
if err := tl.Table.Restore(ctx); err != nil {
return annotate(err, "An error occurred while add index")
}
if tl.Alias.O != "" {
ctx.WriteKeyWord(" AS ")
ctx.WriteName(tl.Alias.O)
}
ctx.WriteKeyWord(" " + tl.Type.String())
}
return nil
Expand Down Expand Up @@ -2883,6 +2925,7 @@ const (
TableOptionIetfQuotes
TableOptionSequence
TableOptionAffinity
TableOptionStartTransaction
TableOptionPlacementPolicy = TableOptionType(PlacementOptionPolicy)
TableOptionStatsBuckets = TableOptionType(StatsOptionBuckets)
TableOptionStatsTopN = TableOptionType(StatsOptionTopN)
Expand Down Expand Up @@ -3270,6 +3313,8 @@ func (n *TableOption) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("AUTOEXTEND_SIZE ")
ctx.WritePlain("= ")
ctx.WritePlain(n.StrValue) // e.g. '4M'
case TableOptionStartTransaction:
ctx.WriteKeyWord("START TRANSACTION")

// MariaDB specific options
case TableOptionPageChecksum:
Expand Down Expand Up @@ -3535,6 +3580,11 @@ const (
AlterTableDropMaskingPolicy
AlterTableModifyMaskingPolicyExpression
AlterTableModifyMaskingPolicyRestrictOn
// AlterTableAlterColumnVisibility is
// ALTER TABLE ... ALTER COLUMN col SET {VISIBLE | INVISIBLE};
// the column is NewColumns[0] (name only) and the Visibility field
// carries the choice.
AlterTableAlterColumnVisibility
)

// LockType is the type for AlterTableSpec.
Expand Down Expand Up @@ -3935,6 +3985,16 @@ func (n *AlterTableSpec) Restore(ctx *format.RestoreCtx) error {
} else {
ctx.WriteKeyWord(" DROP DEFAULT")
}
case AlterTableAlterColumnVisibility:
ctx.WriteKeyWord("ALTER COLUMN ")
if err := n.NewColumns[0].Restore(ctx); err != nil {
return annotate(err, "An error occurred while restore AlterTableSpec.NewColumns[0]")
}
if n.Visibility == IndexVisibilityInvisible {
ctx.WriteKeyWord(" SET INVISIBLE")
} else {
ctx.WriteKeyWord(" SET VISIBLE")
}
case AlterTableLock:
ctx.WriteKeyWord("LOCK ")
ctx.WritePlain("= ")
Expand Down
142 changes: 98 additions & 44 deletions ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,9 @@ type SelectStmt struct {
Limit *Limit
// LockInfo is the lock type
LockInfo *SelectLockInfo
// MoreLockInfos are the second and later locking clauses of a
// statement with several (FOR SHARE OF t1 ... FOR UPDATE OF t2 ...).
MoreLockInfos []*SelectLockInfo
// TableHints represents the table level Optimizer Hint for join type
TableHints []*TableOptimizerHint
// IsInBraces indicates whether it's a stmt in brace.
Expand Down Expand Up @@ -1477,50 +1480,10 @@ func (n *SelectStmt) Restore(ctx *format.RestoreCtx) error {

if n.LockInfo != nil {
ctx.WritePlain(" ")
switch n.LockInfo.LockType {
case SelectLockNone:
case SelectLockForUpdateNoWait:
ctx.WriteKeyWord("for update")
if len(n.LockInfo.Tables) != 0 {
ctx.WriteKeyWord(" OF ")
restoreTables(ctx, n.LockInfo.Tables)
}
ctx.WriteKeyWord(" nowait")
case SelectLockForUpdateWaitN:
ctx.WriteKeyWord("for update")
if len(n.LockInfo.Tables) != 0 {
ctx.WriteKeyWord(" OF ")
restoreTables(ctx, n.LockInfo.Tables)
}
ctx.WriteKeyWord(" wait")
ctx.WritePlainf(" %d", n.LockInfo.WaitSec)
case SelectLockForShareNoWait:
ctx.WriteKeyWord("for share")
if len(n.LockInfo.Tables) != 0 {
ctx.WriteKeyWord(" OF ")
restoreTables(ctx, n.LockInfo.Tables)
}
ctx.WriteKeyWord(" nowait")
case SelectLockForUpdateSkipLocked:
ctx.WriteKeyWord("for update")
if len(n.LockInfo.Tables) != 0 {
ctx.WriteKeyWord(" OF ")
restoreTables(ctx, n.LockInfo.Tables)
}
ctx.WriteKeyWord(" skip locked")
case SelectLockForShareSkipLocked:
ctx.WriteKeyWord("for share")
if len(n.LockInfo.Tables) != 0 {
ctx.WriteKeyWord(" OF ")
restoreTables(ctx, n.LockInfo.Tables)
}
ctx.WriteKeyWord(" skip locked")
default:
ctx.WriteKeyWord(n.LockInfo.LockType.String())
if len(n.LockInfo.Tables) != 0 {
ctx.WriteKeyWord(" OF ")
restoreTables(ctx, n.LockInfo.Tables)
}
restoreSelectLockInfo(ctx, n.LockInfo)
for _, li := range n.MoreLockInfos {
ctx.WritePlain(" ")
restoreSelectLockInfo(ctx, li)
}
}

Expand All @@ -1533,6 +1496,55 @@ func (n *SelectStmt) Restore(ctx *format.RestoreCtx) error {
return nil
}

// restoreSelectLockInfo writes one locking clause.
func restoreSelectLockInfo(ctx *format.RestoreCtx, li *SelectLockInfo) {
switch li.LockType {
case SelectLockNone:
case SelectLockForUpdateNoWait:
ctx.WriteKeyWord("for update")
if len(li.Tables) != 0 {
ctx.WriteKeyWord(" OF ")
restoreTables(ctx, li.Tables)
}
ctx.WriteKeyWord(" nowait")
case SelectLockForUpdateWaitN:
ctx.WriteKeyWord("for update")
if len(li.Tables) != 0 {
ctx.WriteKeyWord(" OF ")
restoreTables(ctx, li.Tables)
}
ctx.WriteKeyWord(" wait")
ctx.WritePlainf(" %d", li.WaitSec)
case SelectLockForShareNoWait:
ctx.WriteKeyWord("for share")
if len(li.Tables) != 0 {
ctx.WriteKeyWord(" OF ")
restoreTables(ctx, li.Tables)
}
ctx.WriteKeyWord(" nowait")
case SelectLockForUpdateSkipLocked:
ctx.WriteKeyWord("for update")
if len(li.Tables) != 0 {
ctx.WriteKeyWord(" OF ")
restoreTables(ctx, li.Tables)
}
ctx.WriteKeyWord(" skip locked")
case SelectLockForShareSkipLocked:
ctx.WriteKeyWord("for share")
if len(li.Tables) != 0 {
ctx.WriteKeyWord(" OF ")
restoreTables(ctx, li.Tables)
}
ctx.WriteKeyWord(" skip locked")
default:
ctx.WriteKeyWord(li.LockType.String())
if len(li.Tables) != 0 {
ctx.WriteKeyWord(" OF ")
restoreTables(ctx, li.Tables)
}
}
}

func restoreTables(ctx *format.RestoreCtx, ts []*TableName) error {
for i, v := range ts {
if err := v.Restore(ctx); err != nil {
Expand Down Expand Up @@ -1975,11 +1987,13 @@ type LoadDataStmt struct {
dmlNode

LowPriority bool
Concurrent bool
FileLocRef FileLocRefTp
Path string
Format *string
OnDuplicate OnDuplicateKeyHandlingType
Table *TableName
Partitions []CIStr // PARTITION (p, ...); empty when absent
Charset *string
Columns []*ColumnName
FieldsInfo *FieldsClause
Expand All @@ -1997,6 +2011,9 @@ func (n *LoadDataStmt) Restore(ctx *format.RestoreCtx) error {
if n.LowPriority {
ctx.WriteKeyWord("LOW_PRIORITY ")
}
if n.Concurrent {
ctx.WriteKeyWord("CONCURRENT ")
}
switch n.FileLocRef {
case FileLocServerOrRemote:
case FileLocClient:
Expand All @@ -2017,6 +2034,17 @@ func (n *LoadDataStmt) Restore(ctx *format.RestoreCtx) error {
if err := n.Table.Restore(ctx); err != nil {
return annotate(err, "An error occurred while restore LoadDataStmt.Table")
}
if len(n.Partitions) > 0 {
ctx.WriteKeyWord(" PARTITION ")
ctx.WritePlain("(")
for i, p := range n.Partitions {
if i != 0 {
ctx.WritePlain(", ")
}
ctx.WriteName(p.O)
}
ctx.WritePlain(")")
}
if n.Charset != nil {
ctx.WriteKeyWord(" CHARACTER SET ")
ctx.WritePlain(*n.Charset)
Expand Down Expand Up @@ -3599,6 +3627,9 @@ func (n *ShowStmt) Restore(ctx *format.RestoreCtx) error {
case ShowIndex:
// here can be INDEX INDEXES KEYS
// FROM or IN
if n.Extended {
ctx.WriteKeyWord("EXTENDED ")
}
ctx.WriteKeyWord("INDEX IN ")
if err := n.Table.Restore(ctx); err != nil {
return annotate(err, "An error occurred while restore ShowStmt.Table")
Expand All @@ -3619,8 +3650,20 @@ func (n *ShowStmt) Restore(ctx *format.RestoreCtx) error {
restoreShowDatabaseNameOpt()
case ShowWarnings:
ctx.WriteKeyWord("WARNINGS")
if n.Limit != nil {
ctx.WritePlain(" ")
if err := n.Limit.Restore(ctx); err != nil {
return annotate(err, "An error occurred while restore ShowStmt.Limit")
}
}
case ShowErrors:
ctx.WriteKeyWord("ERRORS")
if n.Limit != nil {
ctx.WritePlain(" ")
if err := n.Limit.Restore(ctx); err != nil {
return annotate(err, "An error occurred while restore ShowStmt.Limit")
}
}
case ShowVariables:
restoreGlobalScope()
ctx.WriteKeyWord("VARIABLES")
Expand Down Expand Up @@ -3699,6 +3742,10 @@ func (n *ShowStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("SESSION_STATES")
case ShowReplicaStatus:
ctx.WriteKeyWord("REPLICA STATUS")
if n.ChannelName != "" {
ctx.WriteKeyWord(" FOR CHANNEL ")
ctx.WriteString(n.ChannelName)
}
default:
return errors.New("Unknown ShowStmt type")
}
Expand Down Expand Up @@ -3896,6 +3943,9 @@ type SelectIntoOption struct {
FileName string
FieldsInfo *FieldsClause
LinesInfo *LinesClause
// Charset is the CHARACTER SET of the OUTFILE form; empty when
// absent.
Charset string
// Vars is the variable list of the SelectIntoVars form: user
// variables and, in stored programs, program variables (restored as
// plain names).
Expand Down Expand Up @@ -3927,6 +3977,10 @@ func (n *SelectIntoOption) Restore(ctx *format.RestoreCtx) error {

ctx.WriteKeyWord("INTO OUTFILE ")
ctx.WriteString(n.FileName)
if n.Charset != "" {
ctx.WriteKeyWord(" CHARACTER SET ")
ctx.WritePlain(n.Charset)
}
if n.FieldsInfo != nil {
if err := n.FieldsInfo.Restore(ctx); err != nil {
return annotate(err, "An error occurred while restore SelectInto.FieldsInfo")
Expand Down
Loading
Loading