Skip to content

Commit

Permalink
Adding support to table comment
Browse files Browse the repository at this point in the history
Signed-off-by: Sharad Gaur <sharadgaur@gmail.com>
  • Loading branch information
sharadgaur committed Oct 8, 2024
1 parent edc40cc commit 3dbbc2d
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 12 deletions.
5 changes: 5 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,7 @@ type CreateTable struct {
Engine *EngineExpr
SubQuery *SubQuery
HasTemporary bool
Comment *StringLiteral
}

func (c *CreateTable) Pos() Pos {
Expand Down Expand Up @@ -1534,6 +1535,10 @@ func (c *CreateTable) String() string {
builder.WriteString(" AS ")
builder.WriteString(c.SubQuery.String())
}
if c.Comment != nil {
builder.WriteString(" COMMENT ")
builder.WriteString(c.Comment.String())
}
return builder.String()
}

Expand Down
7 changes: 7 additions & 0 deletions parser/parser_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ func (p *Parser) tryParseUUID() (*UUID, error) {
return p.parseUUID()
}

func (p *Parser) tryParseComment() (*StringLiteral, error) {
if p.tryConsumeKeyword(KeywordComment) == nil {
return nil, nil
}
return p.parseString(p.Pos())
}

func (p *Parser) tryParseIfExists() (bool, error) {
if p.tryConsumeKeyword(KeywordIf) == nil {
return false, nil
Expand Down
6 changes: 6 additions & 0 deletions parser/parser_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ func (p *Parser) parseCreateTable(pos Pos) (*CreateTable, error) {
createTable.SubQuery = subQuery
createTable.StatementEnd = subQuery.End()
}

comment, err := p.tryParseComment()
if err != nil {
return nil, err
}
createTable.Comment = comment
return createTable, nil
}

Expand Down
3 changes: 2 additions & 1 deletion parser/testdata/ddl/create_table_basic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ CREATE TABLE IF NOT EXISTS test.events_local (
PRIMARY KEY (f0, f1, f2)
PARTITION BY toYYYYMMDD(f3)
TTL f3 + INTERVAL 6 MONTH
ORDER BY (f1,f2,f3)
ORDER BY (f1,f2,f3)
COMMENT 'Comment for table';
3 changes: 2 additions & 1 deletion parser/testdata/ddl/format/create_table_basic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ PRIMARY KEY (f0, f1, f2)
PARTITION BY toYYYYMMDD(f3)
TTL f3 + INTERVAL 6 MONTH
ORDER BY (f1,f2,f3)
COMMENT 'Comment for table';

-- Format SQL:
CREATE TABLE IF NOT EXISTS test.events_local (f0 String, f1 String CODEC(ZSTD(1)), f2 VARCHAR(255), f3 Datetime, f4 Datetime, f5 Map(String, String), f6 String, f7 Nested(f70 UInt32, f71 UInt32, f72 DateTime, f73 Int64, f74 Int64, f75 String), f8 Datetime DEFAULT now(), f9 String MATERIALIZED toString(f7['f70']), f10 String ALIAS f11) ENGINE = MergeTree PRIMARY KEY (f0, f1, f2) PARTITION BY toYYYYMMDD(f3) TTL f3 + INTERVAL 6 MONTH ORDER BY (f1, f2, f3);
CREATE TABLE IF NOT EXISTS test.events_local (f0 String, f1 String CODEC(ZSTD(1)), f2 VARCHAR(255), f3 Datetime, f4 Datetime, f5 Map(String, String), f6 String, f7 Nested(f70 UInt32, f71 UInt32, f72 DateTime, f73 Int64, f74 Int64, f75 String), f8 Datetime DEFAULT now(), f9 String MATERIALIZED toString(f7['f70']), f10 String ALIAS f11) ENGINE = MergeTree PRIMARY KEY (f0, f1, f2) PARTITION BY toYYYYMMDD(f3) TTL f3 + INTERVAL 6 MONTH ORDER BY (f1, f2, f3) COMMENT 'Comment for table';
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"OrderBy": null
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,11 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": {
"LiteralPos": 687,
"LiteralEnd": 704,
"Literal": "Comment for table"
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]

0 comments on commit 3dbbc2d

Please sign in to comment.