Skip to content

Implement TABLE OPTIONS #291

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

Merged
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
16 changes: 15 additions & 1 deletion ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ func (DropRowDeletionPolicy) isTableAlteration() {}
func (ReplaceRowDeletionPolicy) isTableAlteration() {}
func (SetOnDelete) isTableAlteration() {}
func (AlterColumn) isTableAlteration() {}
func (AlterTableSetOptions) isTableAlteration() {}

// ColumnDefaultSemantics is interface of DefaultExpr, GeneratedColumnExpr, IdentityColumn, AutoIncrement.
// They are change default value of column and mutually exclusive.
Expand Down Expand Up @@ -2390,13 +2391,14 @@ type DropProtoBundle struct {
// {{if .PrimaryKeys}}PRIMARY KEY ({{.PrimaryKeys | sqlJoin ","}}){{end}}
// {{.Cluster | sqlOpt}}
// {{.CreateRowDeletionPolicy | sqlOpt}}
// {{if .Options}}, {{.Options | sqlOpt}}{{end}}
//
// Spanner SQL allows to mix `Columns` and `TableConstraints` and `Synonyms`,
// however they are separated in AST definition for historical reasons. If you want to get
// the original order of them, please sort them by their `Pos()`.
type CreateTable struct {
// pos = Create
// end = RowDeletionPolicy.end || Cluster.end || PrimaryKeyRparen + 1 || Rparen + 1
// end = Options.end || RowDeletionPolicy.end || Cluster.end || PrimaryKeyRparen + 1 || Rparen + 1

Create token.Pos // position of "CREATE" keyword
Rparen token.Pos // position of ")" of end of column definitions
Expand All @@ -2410,6 +2412,7 @@ type CreateTable struct {
Synonyms []*Synonym
Cluster *Cluster // optional
RowDeletionPolicy *CreateRowDeletionPolicy // optional
Options *Options // optional
}

// Synonym is SYNONYM node in CREATE TABLE
Expand Down Expand Up @@ -2874,6 +2877,17 @@ type SetOnDelete struct {
OnDelete OnDeleteAction
}

// AlterTableSetOptions is SET OPTIONS node in ALTER TABLE.
//
// SET {{.Options | sql}}
type AlterTableSetOptions struct {
// pos = Set
// end = Options.end

Set token.Pos
Options *Options
}

// AlterColumn is ALTER COLUMN clause in ALTER TABLE.
//
// ALTER COLUMN {{.Name | sql}} {{.Alteration | sql}}
Expand Down
10 changes: 9 additions & 1 deletion ast/pos.go

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

5 changes: 4 additions & 1 deletion ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,8 @@ func (c *CreateTable) SQL() string {
"\n)" +
strOpt(len(c.PrimaryKeys) > 0, " PRIMARY KEY ("+sqlJoin(c.PrimaryKeys, ", ")+")") +
sqlOpt("", c.Cluster, "") +
sqlOpt("", c.RowDeletionPolicy, "")
sqlOpt("", c.RowDeletionPolicy, "") +
sqlOpt(", ", c.Options, "")
}

func (s *Synonym) SQL() string { return "SYNONYM (" + s.Name.SQL() + ")" }
Expand Down Expand Up @@ -935,6 +936,8 @@ func (s *SetOnDelete) SQL() string {
return "SET " + string(s.OnDelete)
}

func (a *AlterTableSetOptions) SQL() string { return "SET " + a.Options.SQL() }

func (a *AlterColumn) SQL() string {
return "ALTER COLUMN " + a.Name.SQL() + " " + a.Alteration.SQL()
}
Expand Down
4 changes: 4 additions & 0 deletions ast/walk_internal.go

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

39 changes: 32 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3153,6 +3153,12 @@ func (p *Parser) parseCreateTable(pos token.Pos) *ast.CreateTable {
cluster := p.tryParseCluster()
rdp := p.tryParseCreateRowDeletionPolicy()

var options *ast.Options
if p.Token.Kind == "," {
p.nextToken()
options = p.parseOptions()
}

return &ast.CreateTable{
Create: pos,
Rparen: rparen,
Expand All @@ -3165,6 +3171,7 @@ func (p *Parser) parseCreateTable(pos token.Pos) *ast.CreateTable {
PrimaryKeys: keys,
Cluster: cluster,
RowDeletionPolicy: rdp,
Options: options,
}
}

Expand Down Expand Up @@ -3488,7 +3495,14 @@ func (p *Parser) tryParseCreateRowDeletionPolicy() *ast.CreateRowDeletionPolicy
if p.Token.Kind != "," {
return nil
}

lexer := p.Lexer.Clone()
pos := p.expect(",").Pos
if !p.Token.IsKeywordLike("ROW") {
p.Lexer = lexer
return nil
}

rdp := p.parseRowDeletionPolicy()
return &ast.CreateRowDeletionPolicy{
Comma: pos,
Expand Down Expand Up @@ -3886,7 +3900,7 @@ func (p *Parser) parseAlterTable(pos token.Pos) *ast.AlterTable {
case p.Token.IsKeywordLike("REPLACE"):
alteration = p.parseAlterTableReplace()
case p.Token.Kind == "SET":
alteration = p.parseSetOnDelete()
alteration = p.parseAlterTableSet()
case p.Token.IsKeywordLike("ALTER"):
alteration = p.parseAlterColumn()
default:
Expand Down Expand Up @@ -4039,14 +4053,25 @@ func (p *Parser) parseAlterTableReplace() ast.TableAlteration {
}
}

func (p *Parser) parseSetOnDelete() *ast.SetOnDelete {
func (p *Parser) parseAlterTableSet() ast.TableAlteration {
pos := p.expect("SET").Pos
onDelete, onDeleteEnd := p.parseOnDeleteAction()
switch {
case p.Token.Kind == "ON":
onDelete, onDeleteEnd := p.parseOnDeleteAction()

return &ast.SetOnDelete{
Set: pos,
OnDeleteEnd: onDeleteEnd,
OnDelete: onDelete,
return &ast.SetOnDelete{
Set: pos,
OnDeleteEnd: onDeleteEnd,
OnDelete: onDelete,
}
case p.Token.IsKeywordLike("OPTIONS"):
options := p.parseOptions()
return &ast.AlterTableSetOptions{
Set: pos,
Options: options,
}
default:
panic(p.errorfAtToken(&p.Token, "expected token: ON, OPTIONS, but: %s", p.Token.AsString))
}
}

Expand Down
1 change: 1 addition & 0 deletions testdata/input/ddl/alter_table_set_options.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE Singers SET OPTIONS (locality_group = 'spill_to_hdd')
6 changes: 6 additions & 0 deletions testdata/input/ddl/create_table_options.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
Awards ARRAY<STRING(MAX)> OPTIONS (locality_group = 'spill_to_hdd')
) PRIMARY KEY (SingerId), OPTIONS (locality_group = 'ssd_only')
38 changes: 38 additions & 0 deletions testdata/result/ddl/alter_table_set_options.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--- alter_table_set_options.sql
ALTER TABLE Singers SET OPTIONS (locality_group = 'spill_to_hdd')
--- AST
&ast.AlterTable{
Name: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 12,
NameEnd: 19,
Name: "Singers",
},
},
},
TableAlteration: &ast.AlterTableSetOptions{
Set: 20,
Options: &ast.Options{
Options: 24,
Rparen: 64,
Records: []*ast.OptionsDef{
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 33,
NameEnd: 47,
Name: "locality_group",
},
Value: &ast.StringLiteral{
ValuePos: 50,
ValueEnd: 64,
Value: "spill_to_hdd",
},
},
},
},
},
}

--- SQL
ALTER TABLE Singers SET OPTIONS (locality_group = "spill_to_hdd")
155 changes: 155 additions & 0 deletions testdata/result/ddl/create_table_options.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
--- create_table_options.sql
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
Awards ARRAY<STRING(MAX)> OPTIONS (locality_group = 'spill_to_hdd')
) PRIMARY KEY (SingerId), OPTIONS (locality_group = 'ssd_only')
--- AST
&ast.CreateTable{
Rparen: 180,
PrimaryKeyRparen: 203,
Name: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 13,
NameEnd: 20,
Name: "Singers",
},
},
},
Columns: []*ast.ColumnDef{
&ast.ColumnDef{
Null: 46,
Key: -1,
Name: &ast.Ident{
NamePos: 25,
NameEnd: 33,
Name: "SingerId",
},
Type: &ast.ScalarSchemaType{
NamePos: 36,
Name: "INT64",
},
NotNull: true,
Hidden: -1,
},
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 54,
NameEnd: 63,
Name: "FirstName",
},
Type: &ast.SizedSchemaType{
NamePos: 65,
Rparen: 76,
Name: "STRING",
Size: &ast.IntLiteral{
ValuePos: 72,
ValueEnd: 76,
Base: 10,
Value: "1024",
},
},
Hidden: -1,
},
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 81,
NameEnd: 89,
Name: "LastName",
},
Type: &ast.SizedSchemaType{
NamePos: 92,
Rparen: 103,
Name: "STRING",
Size: &ast.IntLiteral{
ValuePos: 99,
ValueEnd: 103,
Base: 10,
Value: "1024",
},
},
Hidden: -1,
},
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 108,
NameEnd: 114,
Name: "Awards",
},
Type: &ast.ArraySchemaType{
Array: 119,
Gt: 136,
Rparen: -1,
Item: &ast.SizedSchemaType{
NamePos: 125,
Rparen: 135,
Name: "STRING",
Max: true,
},
},
Hidden: -1,
Options: &ast.Options{
Options: 138,
Rparen: 178,
Records: []*ast.OptionsDef{
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 147,
NameEnd: 161,
Name: "locality_group",
},
Value: &ast.StringLiteral{
ValuePos: 164,
ValueEnd: 178,
Value: "spill_to_hdd",
},
},
},
},
},
},
PrimaryKeys: []*ast.IndexKey{
&ast.IndexKey{
DirPos: -1,
Name: &ast.Ident{
NamePos: 195,
NameEnd: 203,
Name: "SingerId",
},
},
},
Options: &ast.Options{
Options: 206,
Rparen: 242,
Records: []*ast.OptionsDef{
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 215,
NameEnd: 229,
Name: "locality_group",
},
Value: &ast.StringLiteral{
ValuePos: 232,
ValueEnd: 242,
Value: "ssd_only",
},
},
},
},
}

--- SQL
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
Awards ARRAY<STRING(MAX)> OPTIONS (locality_group = "spill_to_hdd")
) PRIMARY KEY (SingerId), OPTIONS (locality_group = "ssd_only")
Loading
Loading