Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: support the 'create global temporary table' syntax #1211

Merged
merged 1 commit into from
Apr 27, 2021
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
64 changes: 45 additions & 19 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,29 +933,44 @@ func (n *ColumnDef) Validate() bool {
return !(generatedCol && illegalOpt4gc)
}

type TemporaryKeyword int

const (
TemporaryNone TemporaryKeyword = iota
TemporaryGlobal
TemporaryLocal
)

// CreateTableStmt is a statement to create a table.
// See https://dev.mysql.com/doc/refman/5.7/en/create-table.html
type CreateTableStmt struct {
ddlNode

IfNotExists bool
IsTemporary bool
Table *TableName
ReferTable *TableName
Cols []*ColumnDef
Constraints []*Constraint
Options []*TableOption
Partition *PartitionOptions
OnDuplicate OnDuplicateKeyHandlingType
Select ResultSetNode
TemporaryKeyword
// Meanless when TemporaryKeyword is not TemporaryGlobal.
// ON COMMIT DELETE ROWS => true
// ON COMMIT PRESERVE ROW => false
OnCommitDelete bool
Table *TableName
ReferTable *TableName
Cols []*ColumnDef
Constraints []*Constraint
Options []*TableOption
Partition *PartitionOptions
OnDuplicate OnDuplicateKeyHandlingType
Select ResultSetNode
}

// Restore implements Node interface.
func (n *CreateTableStmt) Restore(ctx *format.RestoreCtx) error {
if n.IsTemporary {
ctx.WriteKeyWord("CREATE TEMPORARY TABLE ")
} else {
switch n.TemporaryKeyword {
case TemporaryNone:
ctx.WriteKeyWord("CREATE TABLE ")
case TemporaryGlobal:
ctx.WriteKeyWord("CREATE GLOBAL TEMPORARY TABLE ")
case TemporaryLocal:
ctx.WriteKeyWord("CREATE TEMPORARY TABLE ")
}
if n.IfNotExists {
ctx.WriteKeyWord("IF NOT EXISTS ")
Expand Down Expand Up @@ -1023,6 +1038,14 @@ func (n *CreateTableStmt) Restore(ctx *format.RestoreCtx) error {
}
}

if n.TemporaryKeyword == TemporaryGlobal {
if n.OnCommitDelete {
ctx.WriteKeyWord(" ON COMMIT DELETE ROWS")
} else {
ctx.WriteKeyWord(" ON COMMIT PRESERVE ROWS")
}
}

return nil
}

Expand Down Expand Up @@ -1082,21 +1105,24 @@ func (n *CreateTableStmt) Accept(v Visitor) (Node, bool) {
type DropTableStmt struct {
ddlNode

IfExists bool
Tables []*TableName
IsView bool
IsTemporary bool // make sense ONLY if/when IsView == false
IfExists bool
Tables []*TableName
IsView bool
TemporaryKeyword // make sense ONLY if/when IsView == false
}

// Restore implements Node interface.
func (n *DropTableStmt) Restore(ctx *format.RestoreCtx) error {
if n.IsView {
ctx.WriteKeyWord("DROP VIEW ")
} else {
if n.IsTemporary {
ctx.WriteKeyWord("DROP TEMPORARY TABLE ")
} else {
switch n.TemporaryKeyword {
case TemporaryNone:
ctx.WriteKeyWord("DROP TABLE ")
case TemporaryGlobal:
ctx.WriteKeyWord("DROP GLOBAL TEMPORARY TABLE ")
case TemporaryLocal:
ctx.WriteKeyWord("DROP TEMPORARY TABLE ")
}
}
if n.IfExists {
Expand Down
1 change: 1 addition & 0 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ var tokenMap = map[string]int{
"PRECEDING": preceding,
"PRECISION": precisionType,
"PREPARE": prepare,
"PRESERVE": preserve,
"PRIMARY": primary,
"PRIVILEGES": privileges,
"PROCEDURE": procedure,
Expand Down
21 changes: 21 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,27 @@ type TableInfo struct {
// IsColumnar means the table is column-oriented.
// It's true when the engine of the table is TiFlash only.
IsColumnar bool `json:"is_columnar"`

TempTableType `json:"temp_table_type"`
}

type TempTableType byte

const (
TempTableNone TempTableType = iota
TempTableGlobal
TempTableLocal
)

func (t TempTableType) String() string {
switch t {
case TempTableGlobal:
return "global"
case TempTableLocal:
return "local"
default:
return ""
}
}

// TableLockInfo provides meta data describing a table lock.
Expand Down
Loading