Skip to content

Commit

Permalink
Implement inline PRIMARY KEY (#248)
Browse files Browse the repository at this point in the history
* Implement column inline PRIMARY KEY

* Update testdata
  • Loading branch information
apstndb authored Jan 1, 2025
1 parent 9ed6971 commit 480c4e8
Show file tree
Hide file tree
Showing 53 changed files with 291 additions and 118 deletions.
18 changes: 11 additions & 7 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2350,7 +2350,7 @@ type DropProtoBundle struct {
// {{.TableConstraints | sqlJoin ","}}{{if and .TableConstraints .Synonym}},{{end}}
// {{.Synonym | sqlJoin ","}}
// )
// PRIMARY KEY ({{.PrimaryKeys | sqlJoin ","}})
// {{if .PrimaryKeys}}PRIMARY KEY ({{.PrimaryKeys | sqlJoin ","}}){{end}}
// {{.Cluster | sqlOpt}}
// {{.CreateRowDeletionPolicy | sqlOpt}}
//
Expand All @@ -2368,7 +2368,7 @@ type CreateTable struct {
Name *Path
Columns []*ColumnDef
TableConstraints []*TableConstraint
PrimaryKeys []*IndexKey
PrimaryKeys []*IndexKey // when omitted, len(PrimaryKeys) = 0
Synonyms []*Synonym
Cluster *Cluster // optional
RowDeletionPolicy *CreateRowDeletionPolicy // optional
Expand Down Expand Up @@ -2438,22 +2438,26 @@ type BitReversedPositive struct {
BitReversedPositive token.Pos // position of "BIT_REVERSED_POSITIVE" keyword
}

// ColumnDef is column definition in CREATE TABLE.
// ColumnDef is column definition in CREATE TABLE and ALTER TABLE ADD COLUMN.
// Note: Some fields are not valid in ADD COLUMN.
//
// {{.Name | sql}}
// {{.Type | sql}} {{if .NotNull}}NOT NULL{{end}}
// {{.DefaultSemantics | sqlOpt}}
// {{if .Hidden.Invalid | not)}}HIDDEN{{end}}
// {{if .PrimaryKey}}PRIMARY KEY{{end}}
// {{.Options | sqlOpt}}
type ColumnDef struct {
// pos = Name.pos
// end = Options.end || Hidden + 6 || DefaultSemantics.end || Null + 4 || Type.end
// end = Options.end || Key + 3 || Hidden + 6 || DefaultSemantics.end || Null + 4 || Type.end

Null token.Pos // position of "NULL"
Key token.Pos // position of "KEY" of PRIMARY KEY

Name *Ident
Type SchemaType
NotNull bool
Name *Ident
Type SchemaType
NotNull bool
PrimaryKey bool

DefaultSemantics ColumnDefaultSemantics // optional

Expand Down
2 changes: 1 addition & 1 deletion ast/pos.go

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

4 changes: 3 additions & 1 deletion ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,8 @@ func (c *CreateTable) SQL() string {
indent + sqlJoin(c.Columns, ",\n"+indent) + strOpt(len(c.Columns) > 0 && (len(c.TableConstraints) > 0 || len(c.Synonyms) > 0), ",\n") +
strOpt(len(c.TableConstraints) > 0, indent) + sqlJoin(c.TableConstraints, ",\n"+indent) + strOpt(len(c.TableConstraints) > 0 && len(c.Synonyms) > 0, ",\n") +
strOpt(len(c.Synonyms) > 0, indent) + sqlJoin(c.Synonyms, ",\n") +
"\n) PRIMARY KEY (" + sqlJoin(c.PrimaryKeys, ", ") + ")" +
"\n)" +
strOpt(len(c.PrimaryKeys) > 0, " PRIMARY KEY ("+sqlJoin(c.PrimaryKeys, ", ")+")") +
sqlOpt("", c.Cluster, "") +
sqlOpt("", c.RowDeletionPolicy, "")
}
Expand Down Expand Up @@ -836,6 +837,7 @@ func (c *ColumnDef) SQL() string {
strOpt(c.NotNull, " NOT NULL") +
sqlOpt(" ", c.DefaultSemantics, "") +
strOpt(!c.Hidden.Invalid(), " HIDDEN") +
strOpt(c.PrimaryKey, " PRIMARY KEY") +
sqlOpt(" ", c.Options, "")
}

Expand Down
38 changes: 25 additions & 13 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3072,22 +3072,26 @@ func (p *Parser) parseCreateTable(pos token.Pos) *ast.CreateTable {
}
p.expect(")")

p.expectKeywordLike("PRIMARY")
p.expectKeywordLike("KEY")

p.expect("(")
// PRIMARY KEY clause is now optional
var keys []*ast.IndexKey
for p.Token.Kind != token.TokenEOF {
if p.Token.Kind == ")" {
break
}
keys = append(keys, p.parseIndexKey())
if p.Token.Kind != "," {
break
}
rparen := token.InvalidPos
if p.Token.IsKeywordLike("PRIMARY") {
p.nextToken()
p.expectKeywordLike("KEY")

p.expect("(")
for p.Token.Kind != token.TokenEOF {
if p.Token.Kind == ")" {
break
}
keys = append(keys, p.parseIndexKey())
if p.Token.Kind != "," {
break
}
p.nextToken()
}
rparen = p.expect(")").Pos
}
rparen := p.expect(")").Pos

cluster := p.tryParseCluster()
rdp := p.tryParseCreateRowDeletionPolicy()
Expand Down Expand Up @@ -3223,13 +3227,21 @@ func (p *Parser) parseColumnDef() *ast.ColumnDef {
hiddenPos = p.expectKeywordLike("HIDDEN").Pos
}

key := token.InvalidPos
if p.Token.IsKeywordLike("PRIMARY") {
p.nextToken()
key = p.expectKeywordLike("KEY").Pos
}

options := p.tryParseOptions()

return &ast.ColumnDef{
Null: null,
Key: key,
Name: name,
Type: t,
NotNull: notNull,
PrimaryKey: !key.Invalid(),
DefaultSemantics: defaultSemantics,
Hidden: hiddenPos,
Options: options,
Expand Down
5 changes: 3 additions & 2 deletions testdata/input/ddl/create_table_with_identity_columns.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
create table foo (
id int64 generated by default as identity (
bit_reversed_positive start counter with 1000 skip range 1, 12345),
bit_reversed_positive start counter with 1000 skip range 1, 12345)
primary key,
startCount int64 generated by default as identity (
bit_reversed_positive start counter with 1000),
skipRange int64 generated by default as identity (
bit_reversed_positive skip range 1000, 2000),
simple int64 generated by default as identity ( bit_reversed_positive),
) primary key (id)
)
1 change: 1 addition & 0 deletions testdata/result/ddl/!bad_alter_table_add_column.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ syntax error: testdata/input/ddl/!bad_alter_table_add_column.sql:1:44: expected
Add: 16,
Column: &ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 27,
NameEnd: 30,
Expand Down
1 change: 1 addition & 0 deletions testdata/result/ddl/alter_table_add_column.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ alter table foo add column baz string(max) not null
Add: 16,
Column: &ast.ColumnDef{
Null: 47,
Key: -1,
Name: &ast.Ident{
NamePos: 27,
NameEnd: 30,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ alter table foo add column baz int64 not null generated by default as identity (
Add: 16,
Column: &ast.ColumnDef{
Null: 41,
Key: -1,
Name: &ast.Ident{
NamePos: 27,
NameEnd: 30,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ alter table foo add column if not exists baz string(max) not null
IfNotExists: true,
Column: &ast.ColumnDef{
Null: 61,
Key: -1,
Name: &ast.Ident{
NamePos: 41,
NameEnd: 44,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ALTER TABLE foo ADD COLUMN expired_at TIMESTAMP AS (IF (status != "OPEN" AND sta
Add: 16,
Column: &ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 27,
NameEnd: 37,
Expand Down
6 changes: 6 additions & 0 deletions testdata/result/ddl/create_table.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ create table foo (
Columns: []*ast.ColumnDef{
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 21,
NameEnd: 24,
Expand All @@ -42,6 +43,7 @@ create table foo (
},
&ast.ColumnDef{
Null: 50,
Key: -1,
Name: &ast.Ident{
NamePos: 34,
NameEnd: 37,
Expand All @@ -56,6 +58,7 @@ create table foo (
},
&ast.ColumnDef{
Null: 78,
Key: -1,
Name: &ast.Ident{
NamePos: 58,
NameEnd: 61,
Expand Down Expand Up @@ -93,6 +96,7 @@ create table foo (
},
&ast.ColumnDef{
Null: 145,
Key: -1,
Name: &ast.Ident{
NamePos: 125,
NameEnd: 128,
Expand Down Expand Up @@ -147,6 +151,7 @@ create table foo (
},
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 482,
NameEnd: 486,
Expand All @@ -160,6 +165,7 @@ create table foo (
},
&ast.ColumnDef{
Null: 515,
Key: -1,
Name: &ast.Ident{
NamePos: 495,
NameEnd: 500,
Expand Down
4 changes: 3 additions & 1 deletion testdata/result/ddl/create_table_cluster.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ create table foo (
Columns: []*ast.ColumnDef{
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 21,
NameEnd: 24,
Expand All @@ -33,6 +34,7 @@ create table foo (
},
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 34,
NameEnd: 37,
Expand Down Expand Up @@ -64,5 +66,5 @@ create table foo (
CREATE TABLE foo (
foo INT64,
bar INT64
) PRIMARY KEY (),
),
INTERLEAVE IN PARENT foobar
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ create table foo (
Columns: []*ast.ColumnDef{
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 21,
NameEnd: 24,
Expand All @@ -35,6 +36,7 @@ create table foo (
},
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 34,
NameEnd: 37,
Expand All @@ -48,6 +50,7 @@ create table foo (
},
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 47,
NameEnd: 50,
Expand Down Expand Up @@ -98,5 +101,5 @@ CREATE TABLE foo (
foo INT64,
bar INT64,
baz TIMESTAMP
) PRIMARY KEY (),
),
INTERLEAVE IN PARENT foobar, ROW DELETION POLICY ( OLDER_THAN ( baz, INTERVAL 30 DAY ))
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ create table foo (
Columns: []*ast.ColumnDef{
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 21,
NameEnd: 24,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ create table foo (
Columns: []*ast.ColumnDef{
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 21,
NameEnd: 24,
Expand Down
5 changes: 5 additions & 0 deletions testdata/result/ddl/create_table_for_format_test.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ create table if not exists foo (
Columns: []*ast.ColumnDef{
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 37,
NameEnd: 40,
Expand All @@ -42,6 +43,7 @@ create table if not exists foo (
},
&ast.ColumnDef{
Null: 68,
Key: -1,
Name: &ast.Ident{
NamePos: 52,
NameEnd: 55,
Expand All @@ -56,6 +58,7 @@ create table if not exists foo (
},
&ast.ColumnDef{
Null: 98,
Key: -1,
Name: &ast.Ident{
NamePos: 78,
NameEnd: 81,
Expand Down Expand Up @@ -93,6 +96,7 @@ create table if not exists foo (
},
&ast.ColumnDef{
Null: 167,
Key: -1,
Name: &ast.Ident{
NamePos: 147,
NameEnd: 150,
Expand Down Expand Up @@ -147,6 +151,7 @@ create table if not exists foo (
},
&ast.ColumnDef{
Null: 408,
Key: -1,
Name: &ast.Ident{
NamePos: 388,
NameEnd: 393,
Expand Down
Loading

0 comments on commit 480c4e8

Please sign in to comment.