Skip to content

Commit

Permalink
Implement CREATE PLACEMENT statement (#194)
Browse files Browse the repository at this point in the history
* Implement CREATE PLACEMENT statement

* Update testdata

* Update generated ast/pos.go
  • Loading branch information
apstndb authored Nov 13, 2024
1 parent 2b2b4b2 commit 2bd7987
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (CreateSchema) isStatement() {}
func (DropSchema) isStatement() {}
func (CreateDatabase) isStatement() {}
func (AlterDatabase) isStatement() {}
func (CreatePlacement) isStatement() {}
func (CreateProtoBundle) isStatement() {}
func (AlterProtoBundle) isStatement() {}
func (DropProtoBundle) isStatement() {}
Expand Down Expand Up @@ -307,6 +308,7 @@ func (CreateSchema) isDDL() {}
func (DropSchema) isDDL() {}
func (CreateDatabase) isDDL() {}
func (AlterDatabase) isDDL() {}
func (CreatePlacement) isDDL() {}
func (CreateProtoBundle) isDDL() {}
func (AlterProtoBundle) isDDL() {}
func (DropProtoBundle) isDDL() {}
Expand Down Expand Up @@ -1827,6 +1829,19 @@ type AlterDatabase struct {
Options *Options
}

// CreatePlacement is CREATE PLACEMENT statement node.
//
// CREATE PLACEMENT {{.Name | sql}} {{.Options | sqlOpt}}
type CreatePlacement struct {
// pos = Create
// end = (Options ?? Name).end

Create token.Pos // position of "CREATE" keyword

Name *Ident
Options *Options // optional
}

// ================================================================================
//
// PROTO BUNDLE statements
Expand Down
8 changes: 8 additions & 0 deletions ast/pos.go

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

4 changes: 4 additions & 0 deletions ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,10 @@ func (d *AlterDatabase) SQL() string {
return "ALTER DATABASE " + d.Name.SQL() + " SET " + d.Options.SQL()
}

func (c *CreatePlacement) SQL() string {
return "CREATE PLACEMENT " + c.Name.SQL() + sqlOpt(" ", c.Options, " ")
}

func (p *ProtoBundleTypes) SQL() string { return "(" + sqlJoin(p.Types, ", ") + ")" }

func (b *CreateProtoBundle) SQL() string { return "CREATE PROTO BUNDLE " + b.Types.SQL() }
Expand Down
15 changes: 15 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2304,6 +2304,8 @@ func (p *Parser) parseDDL() ast.DDL {
return p.parseCreateSchema(pos)
case p.Token.IsKeywordLike("DATABASE"):
return p.parseCreateDatabase(pos)
case p.Token.IsKeywordLike("PLACEMENT"):
return p.parseCreatePlacement(pos)
case p.Token.Kind == "PROTO":
return p.parseCreateProtoBundle(pos)
case p.Token.IsKeywordLike("TABLE"):
Expand Down Expand Up @@ -2411,6 +2413,7 @@ func (p *Parser) parseDropSchema(pos token.Pos) *ast.DropSchema {
func (p *Parser) parseCreateDatabase(pos token.Pos) *ast.CreateDatabase {
p.expectKeywordLike("DATABASE")
name := p.parseIdent()

return &ast.CreateDatabase{
Create: pos,
Name: name,
Expand All @@ -2430,6 +2433,18 @@ func (p *Parser) parseAlterDatabase(pos token.Pos) *ast.AlterDatabase {
}
}

func (p *Parser) parseCreatePlacement(pos token.Pos) *ast.CreatePlacement {
p.expectKeywordLike("PLACEMENT")
name := p.parseIdent()
options := p.parseOptions()

return &ast.CreatePlacement{
Create: pos,
Name: name,
Options: options,
}
}

func (p *Parser) parseProtoBundleTypes() *ast.ProtoBundleTypes {
lparen := p.expect("(").Pos
types := parseCommaSeparatedList(p, p.parseNamedType)
Expand Down
1 change: 1 addition & 0 deletions testdata/input/ddl/create_placement.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE PLACEMENT `europeplacement` OPTIONS (instance_partition="europe-partition")
32 changes: 32 additions & 0 deletions testdata/result/ddl/create_placement.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--- create_placement.sql
CREATE PLACEMENT `europeplacement` OPTIONS (instance_partition="europe-partition")
--- AST
&ast.CreatePlacement{
Create: 0,
Name: &ast.Ident{
NamePos: 17,
NameEnd: 34,
Name: "europeplacement",
},
Options: &ast.Options{
Options: 35,
Rparen: 81,
Records: []*ast.OptionsDef{
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 44,
NameEnd: 62,
Name: "instance_partition",
},
Value: &ast.StringLiteral{
ValuePos: 63,
ValueEnd: 81,
Value: "europe-partition",
},
},
},
},
}

--- SQL
CREATE PLACEMENT europeplacement OPTIONS (instance_partition = "europe-partition")
32 changes: 32 additions & 0 deletions testdata/result/statement/create_placement.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--- create_placement.sql
CREATE PLACEMENT `europeplacement` OPTIONS (instance_partition="europe-partition")
--- AST
&ast.CreatePlacement{
Create: 0,
Name: &ast.Ident{
NamePos: 17,
NameEnd: 34,
Name: "europeplacement",
},
Options: &ast.Options{
Options: 35,
Rparen: 81,
Records: []*ast.OptionsDef{
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 44,
NameEnd: 62,
Name: "instance_partition",
},
Value: &ast.StringLiteral{
ValuePos: 63,
ValueEnd: 81,
Value: "europe-partition",
},
},
},
},
}

--- SQL
CREATE PLACEMENT europeplacement OPTIONS (instance_partition = "europe-partition")

0 comments on commit 2bd7987

Please sign in to comment.