diff --git a/parser/ast.go b/parser/ast.go index 24ebbe4..ba254c4 100644 --- a/parser/ast.go +++ b/parser/ast.go @@ -1490,6 +1490,7 @@ type CreateTable struct { Engine *EngineExpr SubQuery *SubQuery HasTemporary bool + Comment *StringLiteral } func (c *CreateTable) Pos() Pos { @@ -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() } diff --git a/parser/parser_common.go b/parser/parser_common.go index cf486d7..5f21e30 100644 --- a/parser/parser_common.go +++ b/parser/parser_common.go @@ -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 diff --git a/parser/parser_table.go b/parser/parser_table.go index c00937e..37bc521 100644 --- a/parser/parser_table.go +++ b/parser/parser_table.go @@ -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 } diff --git a/parser/testdata/ddl/create_table_basic.sql b/parser/testdata/ddl/create_table_basic.sql index 611083b..5969526 100644 --- a/parser/testdata/ddl/create_table_basic.sql +++ b/parser/testdata/ddl/create_table_basic.sql @@ -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) \ No newline at end of file +ORDER BY (f1,f2,f3) +COMMENT 'Comment for table'; \ No newline at end of file diff --git a/parser/testdata/ddl/format/create_table_basic.sql b/parser/testdata/ddl/format/create_table_basic.sql index 319ab9a..3263d6c 100644 --- a/parser/testdata/ddl/format/create_table_basic.sql +++ b/parser/testdata/ddl/format/create_table_basic.sql @@ -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'; diff --git a/parser/testdata/ddl/output/attach_table_basic.sql.golden.json b/parser/testdata/ddl/output/attach_table_basic.sql.golden.json index 0936734..b5ae026 100644 --- a/parser/testdata/ddl/output/attach_table_basic.sql.golden.json +++ b/parser/testdata/ddl/output/attach_table_basic.sql.golden.json @@ -476,6 +476,7 @@ } }, "SubQuery": null, - "HasTemporary": false + "HasTemporary": false, + "Comment": null } ] \ No newline at end of file diff --git a/parser/testdata/ddl/output/create_distributed_table.sql.golden.json b/parser/testdata/ddl/output/create_distributed_table.sql.golden.json index 37c4d3a..0230585 100644 --- a/parser/testdata/ddl/output/create_distributed_table.sql.golden.json +++ b/parser/testdata/ddl/output/create_distributed_table.sql.golden.json @@ -139,6 +139,7 @@ "OrderBy": null }, "SubQuery": null, - "HasTemporary": false + "HasTemporary": false, + "Comment": null } ] \ No newline at end of file diff --git a/parser/testdata/ddl/output/create_table_basic.sql.golden.json b/parser/testdata/ddl/output/create_table_basic.sql.golden.json index 4718328..6cbb3b5 100644 --- a/parser/testdata/ddl/output/create_table_basic.sql.golden.json +++ b/parser/testdata/ddl/output/create_table_basic.sql.golden.json @@ -838,6 +838,11 @@ } }, "SubQuery": null, - "HasTemporary": false + "HasTemporary": false, + "Comment": { + "LiteralPos": 687, + "LiteralEnd": 704, + "Literal": "Comment for table" + } } ] \ No newline at end of file diff --git a/parser/testdata/ddl/output/create_table_with_codec_delta.sql.golden.json b/parser/testdata/ddl/output/create_table_with_codec_delta.sql.golden.json index df3adfc..8ba0468 100644 --- a/parser/testdata/ddl/output/create_table_with_codec_delta.sql.golden.json +++ b/parser/testdata/ddl/output/create_table_with_codec_delta.sql.golden.json @@ -763,6 +763,7 @@ } }, "SubQuery": null, - "HasTemporary": false + "HasTemporary": false, + "Comment": null } ] \ No newline at end of file diff --git a/parser/testdata/ddl/output/create_table_with_index.sql.golden.json b/parser/testdata/ddl/output/create_table_with_index.sql.golden.json index 0a2f0aa..816f63a 100644 --- a/parser/testdata/ddl/output/create_table_with_index.sql.golden.json +++ b/parser/testdata/ddl/output/create_table_with_index.sql.golden.json @@ -817,6 +817,7 @@ } }, "SubQuery": null, - "HasTemporary": false + "HasTemporary": false, + "Comment": null } ] \ No newline at end of file diff --git a/parser/testdata/ddl/output/create_table_with_keyword_partition_by.sql.golden.json b/parser/testdata/ddl/output/create_table_with_keyword_partition_by.sql.golden.json index 6ae223f..5201d5e 100644 --- a/parser/testdata/ddl/output/create_table_with_keyword_partition_by.sql.golden.json +++ b/parser/testdata/ddl/output/create_table_with_keyword_partition_by.sql.golden.json @@ -244,6 +244,7 @@ } }, "SubQuery": null, - "HasTemporary": false + "HasTemporary": false, + "Comment": null } ] \ No newline at end of file diff --git a/parser/testdata/ddl/output/create_table_with_nullable.sql.golden.json b/parser/testdata/ddl/output/create_table_with_nullable.sql.golden.json index ab339d0..5ca1b1a 100644 --- a/parser/testdata/ddl/output/create_table_with_nullable.sql.golden.json +++ b/parser/testdata/ddl/output/create_table_with_nullable.sql.golden.json @@ -422,6 +422,7 @@ } }, "SubQuery": null, - "HasTemporary": false + "HasTemporary": false, + "Comment": null } ] \ No newline at end of file diff --git a/parser/testdata/ddl/output/create_table_with_on_clsuter.sql.golden.json b/parser/testdata/ddl/output/create_table_with_on_clsuter.sql.golden.json index 0936734..b5ae026 100644 --- a/parser/testdata/ddl/output/create_table_with_on_clsuter.sql.golden.json +++ b/parser/testdata/ddl/output/create_table_with_on_clsuter.sql.golden.json @@ -476,6 +476,7 @@ } }, "SubQuery": null, - "HasTemporary": false + "HasTemporary": false, + "Comment": null } ] \ No newline at end of file diff --git a/parser/testdata/ddl/output/create_table_with_sample_by.sql.golden.json b/parser/testdata/ddl/output/create_table_with_sample_by.sql.golden.json index c46b9a1..2df3fb1 100644 --- a/parser/testdata/ddl/output/create_table_with_sample_by.sql.golden.json +++ b/parser/testdata/ddl/output/create_table_with_sample_by.sql.golden.json @@ -304,6 +304,7 @@ } }, "SubQuery": null, - "HasTemporary": false + "HasTemporary": false, + "Comment": null } ] \ No newline at end of file diff --git a/parser/testdata/ddl/output/create_table_with_uuid.sql.golden.json b/parser/testdata/ddl/output/create_table_with_uuid.sql.golden.json index c67ed98..ae96c7c 100644 --- a/parser/testdata/ddl/output/create_table_with_uuid.sql.golden.json +++ b/parser/testdata/ddl/output/create_table_with_uuid.sql.golden.json @@ -482,6 +482,7 @@ } }, "SubQuery": null, - "HasTemporary": false + "HasTemporary": false, + "Comment": null } ] \ No newline at end of file